Package org.apache.commons.csv

Examples of org.apache.commons.csv.CSVParser


        for (int i=0; i<skipLines; i++) {
          r.readLine();
        }
      }

      CSVParser parser = new CSVParser(reader, strategy);

      // parse the fieldnames from the header of the file
      if (fieldnames==null) {
        fieldnames = parser.getLine();
        if (fieldnames==null) {
          throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,"Expected fieldnames in CSV input");
        }
        prepareFields();
      }

      // read the rest of the CSV file
      for(;;) {
        int line = parser.getLineNumber()// for error reporting in MT mode
        String[] vals = null;
        try {
          vals = parser.getLine();
        } catch (IOException e) {
          //Catch the exception and rethrow it with more line information
         input_err("can't read line: " + line, null, line, e);
        }
        if (vals==null) break;
View Full Code Here


      this.base = base;
    }

    @Override
    void add(SolrInputDocument doc, int line, int column, String val) {
      CSVParser parser = new CSVParser(new StringReader(val), strategy);
      try {
        String[] vals = parser.getLine();
        if (vals!=null) {
          for (String v: vals) base.add(doc,line,column,v);
        } else {
          base.add(doc,line,column,val);
        }
View Full Code Here

        Reader reader = null;
        boolean error = false;
        try {
            reader = IOHelper.buffered(new InputStreamReader(inputStream, IOHelper.getCharsetName(exchange)));
            CSVParser parser = new CSVParser(reader, strategy);

            if (skipFirstLine) {
                // read one line ahead and skip it
                parser.getLine();
            }

            CsvIterator csvIterator = new CsvIterator(parser, reader);
            return lazyLoad ? csvIterator : loadAllAsList(csvIterator);
        } catch (Exception e) {
View Full Code Here

        Reader reader = null;
        boolean error = false;
        try {
            reader = IOHelper.buffered(new InputStreamReader(inputStream, IOHelper.getCharsetName(exchange)));
            CSVParser parser = new CSVParser(reader, strategy);

            CsvLineConverter<?> lineConverter;
            if (useMaps) {
                lineConverter = CsvLineConverters.getMapLineConverter(parser.getLine());
            } else {
                lineConverter = CsvLineConverters.getListConverter();
                if (skipFirstLine) {
                    // read one line ahead and skip it
                    parser.getLine();
                }
            }

            @SuppressWarnings({"unchecked", "rawtypes"}) CsvIterator<?> csvIterator = new CsvIterator(parser, reader, lineConverter);
            return lazyLoad ? csvIterator : loadAllAsList(csvIterator);
View Full Code Here

    }

    public Object unmarshal(Exchange exchange, InputStream inputStream) throws Exception {
        InputStreamReader in = new InputStreamReader(inputStream);
        try {
            CSVParser parser = new CSVParser(in, getStrategy());
            List<List<String>> list = new ArrayList<List<String>>();
            while (true) {
                String[] strings = parser.getLine();
                if (strings == null) {
                    break;
                }
                List<String> line = Arrays.asList(strings);
                list.add(line);
View Full Code Here

        if (delimiter != null) {
            strategy.setDelimiter(delimiter.charAt(0));
        }
       
        try {
            CSVParser parser = new CSVParser(in, strategy);
            List<List<String>> list = new ArrayList<List<String>>();
            while (true) {
                String[] strings = parser.getLine();
                if (strings == null) {
                    break;
                }
                List<String> line = Arrays.asList(strings);
                list.add(line);
View Full Code Here

     * @throws java.io.IOException
     */
    public static CSVParser build(InputStream is) throws IOException {
        CSVStrategy bestStrategy = getBestStrategy(is);
        if(bestStrategy == null) bestStrategy = getCSVStrategyFromConfiguration();
        return new CSVParser( new InputStreamReader(is), bestStrategy );
    }
View Full Code Here

    private static boolean testStrategy(InputStream is, CSVStrategy strategy) throws IOException {
        final int MIN_COLUMNS = 2;

        is.mark(Integer.MAX_VALUE);
        try {
            final CSVParser parser = new CSVParser(new InputStreamReader(is), strategy);
            int linesToCheck = 5;
            int headerColumnCount = -1;
            while (linesToCheck > 0) {
                String[] row;
                row = parser.getLine();
                if (row == null) {
                    break;
                }
                if (row.length < MIN_COLUMNS) {
                    return false;
View Full Code Here

        for (int i=0; i<skipLines; i++) {
          r.readLine();
        }
      }

      CSVParser parser = new CSVParser(reader, strategy);

      // parse the fieldnames from the header of the file
      if (fieldnames==null) {
        fieldnames = parser.getLine();
        if (fieldnames==null) {
          throw new SolrException( SolrException.ErrorCode.BAD_REQUEST,"Expected fieldnames in CSV input");
        }
        prepareFields();
      }

      // read the rest of the CSV file
      for(;;) {
        int line = parser.getLineNumber()// for error reporting in MT mode
        String[] vals = null;
        try {
          vals = parser.getLine();
        } catch (IOException e) {
          //Catch the exception and rethrow it with more line information
         input_err("can't read line: " + line, null, line, e);
        }
        if (vals==null) break;
View Full Code Here

      this.base = base;
    }

    @Override
    void add(SolrInputDocument doc, int line, int column, String val) {
      CSVParser parser = new CSVParser(new StringReader(val), strategy);
      try {
        String[] vals = parser.getLine();
        if (vals!=null) {
          for (String v: vals) base.add(doc,line,column,v);
        } else {
          base.add(doc,line,column,val);
        }
View Full Code Here

TOP

Related Classes of org.apache.commons.csv.CSVParser

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.