1 package jimm.datavision.source.charsep; 2 import java.io.Reader ; 3 import java.io.IOException ; 4 import java.util.List ; 5 import java.util.ArrayList ; 6 7 12 public class DelimParser { 13 14 public static final int EOF = -1; 15 16 protected char delimiter; 17 protected Reader in; 18 protected int pushbackChar; 19 20 26 public DelimParser(Reader in) { 27 this(in, ','); 28 } 29 30 36 public DelimParser(Reader in, char delimiter) { 37 this.delimiter = delimiter; 38 this.in = in; 39 pushbackChar = EOF; 40 } 41 42 50 public List parse() throws IOException { 51 ArrayList columns = null; 52 boolean insideQuotes = false; 53 int numQuotesSeen = 0; 54 StringBuffer buf = new StringBuffer (); 55 56 int charAsInt; 57 char c; 58 char prevChar = '\0'; 59 while ((charAsInt = nextChar()) != EOF) { 60 c = (char)charAsInt; 61 62 switch (c) { 63 case '"': if (!insideQuotes) { insideQuotes = true; 66 numQuotesSeen = 0; 67 } 68 else if (insideQuotes) { if (numQuotesSeen == 1) { buf.append(c); 71 numQuotesSeen = 0; 72 } 73 else 74 numQuotesSeen = 1; 75 } 76 break; 77 case '\n': case '\r': 79 if (insideQuotes) { 80 if (numQuotesSeen == 1) { if (columns == null) columns = new ArrayList (); 82 columns.add(buf.toString()); 83 return columns; 84 } 85 else 86 buf.append(c); 87 } 88 else { if (c == '\r') { charAsInt = nextChar(); 92 c = (char)charAsInt; 93 if (c != '\n') pushback(charAsInt); } 96 97 charAsInt = nextChar(); 98 c = (char)charAsInt; 99 if (columns == null && buf.length() == 0 && charAsInt == EOF) 100 return null; 102 pushback(charAsInt); 103 if (columns == null) columns = new ArrayList (); 104 columns.add(buf.toString()); 105 return columns; 106 } 107 break; 108 default: 109 if (c == delimiter) { if (!insideQuotes) { 111 if (columns == null) columns = new ArrayList (); 112 columns.add(buf.toString()); 113 buf = new StringBuffer (); 114 } 115 else { if (numQuotesSeen == 1) { 118 insideQuotes = false; 119 if (columns == null) columns = new ArrayList (); 120 columns.add(buf.toString()); 121 buf = new StringBuffer (); 122 } 123 else 125 buf.append(delimiter); 126 } 127 } 128 else { numQuotesSeen = 0; 130 buf.append(c); 131 } 132 break; 133 } 134 135 prevChar = c; 136 } 137 138 if (columns == null && buf.length() == 0) return null; 141 142 if (buf.length() > 0 || prevChar == delimiter) { 143 if (columns == null) columns = new ArrayList (); 144 columns.add(buf.toString()); 145 } 146 return columns; 147 } 148 149 protected int nextChar() throws IOException { 150 if (pushbackChar == EOF) 151 return in.read(); 152 else { 153 int c = pushbackChar; 154 pushbackChar = EOF; 155 return c; 156 } 157 } 158 159 protected void pushback(int charAsInt) { 160 pushbackChar = charAsInt; 161 } 162 163 } 164 | Popular Tags |