1 18 19 package com.Ostermiller.util; 20 import java.io.*; 21 import java.util.*; 22 23 72 public class ExcelCSVParser implements CSVParse { 73 74 79 private InputStream inStream; 80 81 86 private Reader inReader; 87 88 95 private ExcelCSVLexer lexer; 96 97 104 private String tokenCache; 105 106 113 private int lineCache; 114 115 121 private int lastLine = -1; 122 123 134 public ExcelCSVParser(InputStream in, char delimiter) throws BadDelimiterException { 135 inStream = in; 136 lexer = new ExcelCSVLexer(in); 137 changeDelimiter(delimiter); 138 } 139 140 148 public ExcelCSVParser(InputStream in){ 149 inStream = in; 150 lexer = new ExcelCSVLexer(in); 151 } 152 153 164 public ExcelCSVParser(Reader in, char delimiter) throws BadDelimiterException { 165 inReader = in; 166 lexer = new ExcelCSVLexer(in); 167 changeDelimiter(delimiter); 168 } 169 170 178 public ExcelCSVParser(Reader in){ 179 inReader = in; 180 lexer = new ExcelCSVLexer(in); 181 } 182 183 189 public void close() throws IOException { 190 if (inStream != null) inStream.close(); 191 if (inReader != null) inReader.close(); 192 } 193 194 202 public String nextValue() throws IOException { 203 if (tokenCache == null){ 204 tokenCache = lexer.getNextToken(); 205 lineCache = lexer.getLineNumber(); 206 } 207 lastLine = lineCache; 208 String result = tokenCache; 209 tokenCache = null; 210 return result; 211 } 212 213 223 public int lastLineNumber(){ 224 return lastLine; 225 } 226 227 238 public String [] getLine() throws IOException{ 239 int lineNumber = -1; 240 Vector<String > v = new Vector<String >(); 241 if (tokenCache != null){ 242 v.add(tokenCache); 243 lineNumber = lineCache; 244 } 245 while ((tokenCache = lexer.getNextToken()) != null 246 && (lineNumber == -1 || lexer.getLineNumber() == lineNumber)){ 247 v.add(tokenCache); 248 lineNumber = lexer.getLineNumber(); 249 } 250 if (v.size() == 0){ 251 return null; 252 } 253 lastLine = lineNumber; 254 lineCache = lexer.getLineNumber(); 255 String [] result = new String [v.size()]; 256 return ((String [])v.toArray(result)); 257 } 258 259 275 public String [][] getAllValues() throws IOException { 276 Vector<String []> v = new Vector<String []>(); 277 String [] line; 278 while((line = getLine()) != null){ 279 v.add(line); 280 } 281 if (v.size() == 0){ 282 return null; 283 } 284 String [][] result = new String [v.size()][]; 285 return ((String [][])v.toArray(result)); 286 } 287 288 299 public void changeDelimiter(char newDelim) throws BadDelimiterException { 300 lexer.changeDelimiter(newDelim); 301 } 302 303 314 public void changeQuote(char newQuote) throws BadQuoteException { 315 lexer.changeQuote(newQuote); 316 } 317 318 332 public void setCommentStart(String commentDelims){ 333 lexer.setCommentStart(commentDelims); 334 } 335 336 343 public int getLastLineNumber(){ 344 return lastLine; 345 } 346 347 355 private static void main(String [] args){ 356 InputStream in; 357 try { 358 if (args.length > 0){ 359 File f = new File(args[0]); 360 if (f.exists()){ 361 if (f.canRead()){ 362 in = new FileInputStream(f); 363 } else { 364 throw new IOException("Could not open " + args[0]); 365 } 366 } else { 367 throw new IOException("Could not find " + args[0]); 368 } 369 } else { 370 in = System.in; 371 } 372 ExcelCSVParser p = new ExcelCSVParser(in); 373 String [] t; 374 while ((t = p.getLine()) != null){ 375 for (int i=0; i<t.length; i++){ 376 System.out.print('"' + t[i] + '"'); 377 if (i<t.length-1){ 378 System.out.print(", "); 379 } 380 } 381 System.out.println(); 382 } 383 } catch (IOException e){ 384 System.out.println(e.getMessage()); 385 } 386 } 387 388 396 public static String [][] parse(String s){ 397 try { 398 return (new ExcelCSVParser(new StringReader(s))).getAllValues(); 399 } catch (IOException x){ 400 return null; 401 } 402 } 403 404 414 public static String [][] parse(String s, char delimiter) throws BadDelimiterException { 415 try { 416 return (new ExcelCSVParser(new StringReader(s), delimiter)).getAllValues(); 417 } catch (IOException x){ 418 return null; 419 } 420 } 421 422 431 public static String [][] parse(Reader in) throws IOException { 432 return (new ExcelCSVParser(in)).getAllValues(); 433 } 434 435 436 447 public static String [][] parse(Reader in, char delimiter) throws IOException, BadDelimiterException { 448 return (new ExcelCSVParser(in, delimiter)).getAllValues(); 449 } 450 } 451 | Popular Tags |