Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Copyright 1&1 Internet AG, http://www.1and1.org
   *
   * This program is free software; you can redistribute it and/or modify
   * it under the terms of the GNU Lesser General Public License as published by
   * the Free Software Foundation; either version 2 of the License,
   * or (at your option) any later version.
   *
   * This program is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  * See the GNU Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  */
 
 package net.sf.beezle.sushi.csv;
 
 import java.io.Writer;
 import java.util.List;

Parser and Serializer for csv file lines. Note: o whitespace is always significant because it's never stripped by OOCalc (except for digit-only values). o comments are not supported because OOCalc would consider them as strings. o a line has at least one cell You have to implement some pre- or post-processing if you want comments of whitespace handling. Grammar line = cell (SEP cell)* newline ; cell = values | QUOTE values QUOTE ; values = value (VSEP value)* | EMPTY | NULL ; value = char* ; char = QUOTE QUOTE | ESC CH | SEP <-- aber nur wenn drumrum quotes stehen | CH \ QUOTE \ ESC \ SEP \ VSEP ; newline = CR | LF | CR LF ; Terminals as configured for the format: QUOTE, SEP, VSEP, NEWLINE, COMMENT, NULL, EMPTY, ESC Other Terminals: CH: arbitrary unicode character
 
 public class Format {
     public final boolean merged;

    
between cells
 
     private final char separator;
    
    
between values
 
     private final char valueSeparator;
 
     private final String empty;
     
     private final String nul;
    
    
escape special characters
 
     private final char escape;
    
    
to quote cells
 
     private final char quote;
 
     private final String unquoted;
     
     public Format() {
         this(false);
     }
     
     public Format(boolean merged) {
         this(merged'\\''"');
     }
     public Format(boolean mergedchar escapechar quote) {
         this(merged"\n"';''|'"EMPTY""NULL"escapequote"01234567890");
     }
     
     public Format(boolean mergedString newlinechar separatorchar valueSeparator
             String emptyString nulchar escapechar quoteString unquoted) {
         this. = merged;
         this. = separator;
         this. = valueSeparator;
         this. = "EMPTY";
         this. = "NULL";
         this. = escape;
         this. = quote;
         this. = unquoted;
    }
    
    public Line read(String linethrows CsvLineException {
        Line result;
        Source src;
        int c;
        
        result = new Line();
        src = new Source(line);
        while (true) {
            cell(srcresult);
            c = src.peek();
            if (c == .) {
                return result;
            }
            if (c != ) {
                throw new CsvLineException("separator expected");
            }
            src.eat();
        }
    }
    private void cell(Source srcLine resultthrows CsvLineException {
        boolean quoted;
        
        quoted = src.peek() == ;
        if (quoted) {
            src.eat();
        }
        values(srcquotedresult);
        if (quoted) {
            if (src.peek() != ) {
                throw new CsvLineException("quote not closed");
            }
            src.eat();
        }
    }
    private void values(Source srcboolean quotedLine resultthrows CsvLineException {
        List<Stringvalues;
        if (src.eat(quoted ?  : )) {
            result.addNull();
            return;
        }
        values = result.add();
        if (src.eat(quoted ?  : )) {
            return;
        }
        while (true) {
            value(srcquotedvalues);
            if (src.peek() != ) {
                return;
            }
            src.eat();
        }
    }
    private void value(Source srcboolean quotedList<Stringvaluesthrows CsvLineException { 
        StringBuilder builder;
        int c;
        
        builder = new StringBuilder();
        while (true) {
            c = src.peek();
            if (c == -1) {
                break;
            } else if (c == ) {
                if (src.peekNext() == ) {
                    src.eat();
                    builder.append();
                } else {
                    break;
                }
            } else if (c == ) {
                if (quoted) {
                    builder.append(c);
                } else {
                    break;
                }
            } else if (c == ) {
                break;
            } else if (c == ) {
                src.eat();
                c = src.peek();
                if (c == .) {
                    throw new CsvLineException("tailing escape character " + );
                }
                builder.append((charc);
            } else {
                builder.append((charc);
            }
            src.eat();
        }
        values.add(builder.toString());
    }
    
    //--
    
    public String write(Line line) {
        StringWriter dest;
        
        dest = new StringWriter();
        try {
            write(linedest);
        } catch (IOException e) {
            throw new RuntimeException("unused"e);
        }
        return dest.toString();
    }
    
    public void write(Line lineWriter destthrows IOException {
        for (int i = 0, max = line.size(); i < maxi++) {
            if (i > 0) {
                dest.write();
            }
            writeCell(line.get(i), dest);
        }
        dest.write('\n');
    }
    
    private void writeCell(List<StringvaluesWriter destthrows IOException {
        boolean quoted;
        char c;
        boolean first;
        if (values == null) {
            dest.write();
            return;
        }
        if (values.size() == 0) {
            dest.write();
            return;
        }
        quoted = values.size() != 1 || needsQuotes(values.get(0));
        if (quoted) {
            dest.write();
        }
        first = true;
        for (String value : values) {
            if (first) {
                first = false;
            } else {
                dest.write();
            }
            if (value.equals()) {
                dest.write();
                dest.write();
            } else if (value.equals()) {
                dest.write();
                dest.write();
            } else {
                for (int i = 0, max = value.length(); i < maxi++) {
                    c = value.charAt(i);
                    if (c == ) { 
                        dest.write();
                        dest.write();
                    } else if (c ==  || c ==  || c == ) {
                        dest.write();
                        dest.write(c);
                    } else {
                        dest.write(c);
                    }
                }
            }
        }
        if (quoted) {
            dest.write();
        }
    }
    private boolean needsQuotes(String value) {
        for (int i = 0, max = value.length(); i < maxi++) {
            if (.indexOf(value.charAt(i)) == -1) {
                return true;
            }
        }
        return false;
    }
New to GrepCode? Check out our FAQ X