1 18 19 package com.Ostermiller.util; 20 import java.io.*; 21 import java.util.*; 22 23 69 public class CSVParser implements CSVParse { 70 71 76 private InputStream inStream; 77 78 83 private Reader inReader; 84 85 92 private CSVLexer lexer; 93 94 101 private String tokenCache; 102 103 110 private int lineCache; 111 112 118 private int lastLine = -1; 119 120 131 public CSVParser(InputStream in){ 132 inStream = in; 133 lexer = new CSVLexer(in); 134 } 135 136 150 public CSVParser(InputStream in, char delimiter) throws BadDelimiterException { 151 inStream = in; 152 lexer = new CSVLexer(in); 153 changeDelimiter(delimiter); 154 } 155 156 164 public CSVParser(Reader in){ 165 inReader = in; 166 lexer = new CSVLexer(in); 167 } 168 169 180 public CSVParser(Reader in, char delimiter) throws BadDelimiterException { 181 inReader = in; 182 lexer = new CSVLexer(in); 183 changeDelimiter(delimiter); 184 } 185 186 203 public CSVParser(InputStream in, char delimiter, String escapes, String replacements, String commentDelims) throws BadDelimiterException { 204 inStream = in; 205 lexer = new CSVLexer(in); 206 setEscapes(escapes, replacements); 207 setCommentStart(commentDelims); 208 changeDelimiter(delimiter); 209 } 210 211 225 public CSVParser(InputStream in, String escapes, String replacements, String commentDelims){ 226 inStream = in; 227 lexer = new CSVLexer(in); 228 setEscapes(escapes, replacements); 229 setCommentStart(commentDelims); 230 } 231 232 246 public CSVParser(Reader in, char delimiter, String escapes, String replacements, String commentDelims) throws BadDelimiterException { 247 inReader = in; 248 lexer = new CSVLexer(in); 249 setEscapes(escapes, replacements); 250 setCommentStart(commentDelims); 251 changeDelimiter(delimiter); 252 } 253 254 265 public CSVParser(Reader in, String escapes, String replacements, String commentDelims){ 266 inReader = in; 267 lexer = new CSVLexer(in); 268 setEscapes(escapes, replacements); 269 setCommentStart(commentDelims); 270 } 271 272 278 public void close() throws IOException { 279 if (inStream != null) inStream.close(); 280 if (inReader != null) inReader.close(); 281 } 282 283 291 public String nextValue() throws IOException { 292 if (tokenCache == null){ 293 tokenCache = lexer.getNextToken(); 294 lineCache = lexer.getLineNumber(); 295 } 296 lastLine = lineCache; 297 String result = tokenCache; 298 tokenCache = null; 299 return result; 300 } 301 302 312 public int lastLineNumber(){ 313 return lastLine; 314 } 315 316 327 public String [] getLine() throws IOException{ 328 int lineNumber = -1; 329 Vector<String > v = new Vector<String >(); 330 if (tokenCache != null){ 331 v.add(tokenCache); 332 lineNumber = lineCache; 333 } 334 while ((tokenCache = lexer.getNextToken()) != null 335 && (lineNumber == -1 || lexer.getLineNumber() == lineNumber)){ 336 v.add(tokenCache); 337 lineNumber = lexer.getLineNumber(); 338 } 339 if (v.size() == 0){ 340 return null; 341 } 342 lastLine = lineNumber; 343 lineCache = lexer.getLineNumber(); 344 String [] result = new String [v.size()]; 345 return ((String [])v.toArray(result)); 346 } 347 348 364 public String [][] getAllValues() throws IOException { 365 Vector<String []> v = new Vector<String []>(); 366 String [] line; 367 while((line = getLine()) != null){ 368 v.add(line); 369 } 370 if (v.size() == 0){ 371 return null; 372 } 373 String [][] result = new String [v.size()][]; 374 return ((String [][])v.toArray(result)); 375 } 376 377 396 public void setEscapes(String escapes, String replacements){ 397 lexer.setEscapes(escapes, replacements); 398 } 399 400 411 public void changeDelimiter(char newDelim) throws BadDelimiterException { 412 lexer.changeDelimiter(newDelim); 413 } 414 415 426 public void changeQuote(char newQuote) throws BadQuoteException { 427 lexer.changeQuote(newQuote); 428 } 429 430 444 public void setCommentStart(String commentDelims){ 445 lexer.setCommentStart(commentDelims); 446 } 447 448 455 public int getLastLineNumber(){ 456 return lastLine; 457 } 458 459 467 private static void main(String [] args){ 468 InputStream in; 469 try { 470 if (args.length > 0){ 471 File f = new File(args[0]); 472 if (f.exists()){ 473 if (f.canRead()){ 474 in = new FileInputStream(f); 475 } else { 476 throw new IOException("Could not open " + args[0]); 477 } 478 } else { 479 throw new IOException("Could not find " + args[0]); 480 } 481 } else { 482 in = System.in; 483 } 484 CSVParser p = new CSVParser(in); 485 p.setCommentStart("#;!"); 486 p.setEscapes("nrtf", "\n\r\t\f"); 487 String [] t; 488 while ((t = p.getLine()) != null){ 489 for (int i=0; i<t.length; i++){ 490 System.out.print('"' + t[i] + '"'); 491 if (i<t.length-1){ 492 System.out.print(", "); 493 } 494 } 495 System.out.println(); 496 } 497 } catch (IOException e){ 498 System.out.println(e.getMessage()); 499 } 500 } 501 502 513 public static String [][] parse(String s){ 514 try { 515 return (new CSVParser(new StringReader(s))).getAllValues(); 516 } catch (IOException x){ 517 return null; 518 } 519 } 520 521 534 public static String [][] parse(String s, char delimiter) throws BadDelimiterException { 535 try { 536 return (new CSVParser(new StringReader(s), delimiter)).getAllValues(); 537 } catch (IOException x){ 538 return null; 539 } 540 } 541 542 554 public static String [][] parse(String s, String escapes, String replacements, String commentDelims){ 555 try { 556 return (new CSVParser(new StringReader(s), escapes, replacements, commentDelims)).getAllValues(); 557 } catch (IOException x){ 558 return null; 559 } 560 } 561 562 576 public static String [][] parse(String s, char delimiter, String escapes, String replacements, String commentDelims) throws BadDelimiterException{ 577 try { 578 return (new CSVParser(new StringReader(s), delimiter, escapes, replacements, commentDelims)).getAllValues(); 579 } catch (IOException x){ 580 return null; 581 } 582 } 583 584 598 public static String [][] parse(Reader in, char delimiter) throws IOException, BadDelimiterException { 599 return (new CSVParser(in, delimiter)).getAllValues(); 600 } 601 602 614 public static String [][] parse(Reader in) throws IOException { 615 return (new CSVParser(in)).getAllValues(); 616 } 617 618 633 public static String [][] parse(Reader in, char delimiter, String escapes, String replacements, String commentDelims) throws IOException, BadDelimiterException { 634 return (new CSVParser(in, delimiter, escapes, replacements, commentDelims)).getAllValues(); 635 } 636 637 650 public static String [][] parse(Reader in, String escapes, String replacements, String commentDelims) throws IOException { 651 return (new CSVParser(in, escapes, replacements, commentDelims)).getAllValues(); 652 } 653 } 654 | Popular Tags |