Parse a line of text in CSV format and returns array of Strings Implementation of parsing is extracted from open-csv. : CSV File « Development Class « Java






Parse a line of text in CSV format and returns array of Strings Implementation of parsing is extracted from open-csv.

  
/*
   Licensed to the Apache Software Foundation (ASF) under one
   or more contributor license agreements.  See the NOTICE file
   distributed with this work for additional information
   regarding copyright ownership.  The ASF licenses this file
   to you under the Apache License, Version 2.0 (the
   "License"); you may not use this file except in compliance
   with the License.  You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing,
   software distributed under the License is distributed on an
   "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
   KIND, either express or implied.  See the License for the
   specific language governing permissions and limitations
   under the License.    
 */

//package org.opentides.util;

import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;


import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

public class StringUtil {

    
  /**
   * Parse a line of text in CSV format and returns array of Strings
   * Implementation of parsing is extracted from open-csv.
   * http://opencsv.sourceforge.net/
   * 
   * @param csvLine
   * @param separator
   * @param quotechar
   * @param escape
   * @param strictQuotes
   * @return
   * @throws IOException
   */
    public static List<String> parseCsvLine(String csvLine, 
                      char separator, char quotechar, 
                      char escape, boolean strictQuotes) {
      
        List<String>tokensOnThisLine = new ArrayList<String>();
        StringBuilder sb = new StringBuilder(50);
        boolean inQuotes = false;
        for (int i = 0; i < csvLine.length(); i++) {
          char c = csvLine.charAt(i);
          if (c == escape) {
            boolean isNextCharEscapable = inQuotes  // we are in quotes, therefore there can be escaped quotes in here.
                            && csvLine.length() > (i+1)  // there is indeed another character to check.
                            && ( csvLine.charAt(i+1) == quotechar || csvLine.charAt(i+1) == escape);

            if( isNextCharEscapable ){
              sb.append(csvLine.charAt(i+1));
              i++;
            } 
          } else if (c == quotechar) {
            boolean isNextCharEscapedQuote = inQuotes  // we are in quotes, therefore there can be escaped quotes in here.
                && csvLine.length() > (i+1)  // there is indeed another character to check.
                && csvLine.charAt(i+1) == quotechar;
            if( isNextCharEscapedQuote ){
              sb.append(csvLine.charAt(i+1));
              i++;
            }else{
              inQuotes = !inQuotes;
              // the tricky case of an embedded quote in the middle: a,bc"d"ef,g
                    if (!strictQuotes) {
                        if(i>2 //not on the beginning of the line
                                && csvLine.charAt(i-1) != separator //not at the beginning of an escape sequence
                                && csvLine.length()>(i+1) &&
                                csvLine.charAt(i+1) != separator //not at the  end of an escape sequence
                        ){
                            sb.append(c);
                        }
                    }
            }
          } else if (c == separator && !inQuotes) {
            tokensOnThisLine.add(sb.toString());
            sb = new StringBuilder(50); // start work on next token
          } else {
                if (!strictQuotes || inQuotes)
                    sb.append(c);
          }
        }
        // line is done - check status
        if (inQuotes) {
        //  _log.warn("Un-terminated quoted field at end of CSV line. \n ["+csvLine+"]");
        }
        if (sb != null) {
          tokensOnThisLine.add(sb.toString());
        }
        return tokensOnThisLine;
    }
    

}

   
    
  








Related examples in the same category

1.A utility class that parses a Comma Separated Values (CSV) file
2.Simple demo of CSV parser classSimple demo of CSV parser class
3.CSV in action: lines from a file and printCSV in action: lines from a file and print
4.Simple demo of CSV matching using Regular Expressions
5.Helper class to write table data to a csv-file (comma separated values).
6.Builds a bracketed CSV list from the array
7.Builds a CSV list from the specified String[], separator string and quote string
8.Builds a CSV list from the specified two dimensional String[][], separator string and quote string.
9.The csv tokenizer class allows an application to break a Comma Separated Value format into tokens.
10.The CSVQuoter is a helper class to encode a string for the CSV file format.
11.A stream based parser for parsing delimited text data from a file or a stream
12.Reads CSV (Comma Separated Value) files
13.Writes CSV (Comma Separated Value) files
14.Csv Converter
15.CVS reader
16.CSV Writer
17.CSV parser
18.Csv Reader
19.A very simple CSV parser released under a commercial-friendly license.
20.A very simple CSV reader released under a commercial-friendly license.
21.A very simple CSV writer released under a commercial-friendly license.
22.CSV file reader
23.CSV file writer
24.CSV Tokenizer Util
25.CSV Writer
26.Parse comma-separated list of ints and return as array
27.Parse comma-separated list of longs and return as array