1 19 20 package com.Ostermiller.util; 21 import java.io.*; 22 23 32 public class CSVPrinter implements CSVPrint { 33 34 39 protected boolean autoFlush = true; 40 41 46 protected boolean alwaysQuote = false; 47 48 53 protected boolean error = false; 54 55 60 protected char delimiterChar = ','; 61 62 67 protected char quoteChar = '"'; 68 69 74 protected Writer out; 75 76 81 protected boolean newLine = true; 82 83 88 protected char commentStart = '#'; 89 90 99 public void changeDelimiter(char newDelimiter) throws BadDelimiterException { 100 if (delimiterChar == newDelimiter) return; if (newDelimiter == '\n' || newDelimiter == '\r' || 102 newDelimiter == delimiterChar || newDelimiter == quoteChar){ 103 throw new BadDelimiterException(); 104 } 105 delimiterChar = newDelimiter; 106 } 107 108 117 public void changeQuote(char newQuote) throws BadQuoteException { 118 if (newQuote == '\n' || newQuote == '\r' || 119 newQuote == delimiterChar || newQuote == quoteChar){ 120 throw new BadQuoteException(); 121 } 122 quoteChar = newQuote; 123 } 124 125 138 public CSVPrinter(OutputStream out){ 139 this.out = new OutputStreamWriter(out); 140 } 141 142 154 public CSVPrinter(Writer out){ 155 this.out = out; 156 } 157 158 171 public CSVPrinter(OutputStream out, char commentStart){ 172 this(out); 173 this.commentStart = commentStart; 174 } 175 176 188 public CSVPrinter(Writer out, char commentStart){ 189 this(out); 190 this.commentStart = commentStart; 191 } 192 193 204 public CSVPrinter(Writer out, boolean alwaysQuote, boolean autoFlush){ 205 this.out = out; 206 setAlwaysQuote(alwaysQuote); 207 setAutoFlush(autoFlush); 208 } 209 210 224 public CSVPrinter(Writer out, char commentStart, char quote, char delimiter) throws BadDelimiterException, BadQuoteException { 225 this.out = out; 226 this.commentStart = commentStart; 227 changeQuote(quote); 228 changeDelimiter(delimiter); 229 } 230 231 246 public CSVPrinter(Writer out, char commentStart, char quote, char delimiter, boolean alwaysQuote, boolean autoFlush) throws BadDelimiterException, BadQuoteException { 247 this.out = out; 248 this.commentStart = commentStart; 249 changeQuote(quote); 250 changeDelimiter(delimiter); 251 setAlwaysQuote(alwaysQuote); 252 setAutoFlush(autoFlush); 253 } 254 255 267 public void println(String value){ 268 try { 269 writeln(value); 270 } catch (IOException iox){ 271 error = true; 272 } 273 } 274 275 284 public void writeln(String value) throws IOException { 285 try { 286 write(value); 287 writeln(); 288 } catch (IOException iox){ 289 error = true; 290 throw iox; 291 } 292 } 293 294 303 public void println(){ 304 try { 305 writeln(); 306 } catch (IOException iox){ 307 error = true; 308 } 309 } 310 311 318 public void writeln() throws IOException { 319 try { 320 out.write("\n"); 321 if (autoFlush) flush(); 322 newLine = true; 323 } catch (IOException iox){ 324 error = true; 325 throw iox; 326 } 327 } 328 329 342 public void println(String [] values){ 343 try { 344 writeln(values); 345 } catch (IOException iox){ 346 error = true; 347 } 348 } 349 350 360 public void writeln(String [] values) throws IOException { 361 try { 362 print(values); 363 writeln(); 364 } catch (IOException iox){ 365 error = true; 366 throw iox; 367 } 368 } 369 370 383 public void print(String [] values){ 384 try { 385 write(values); 386 } catch (IOException iox){ 387 error = true; 388 } 389 } 390 391 401 public void write(String [] values) throws IOException { 402 try { 403 for (int i=0; i<values.length; i++){ 404 write(values[i]); 405 } 406 } catch (IOException iox){ 407 error = true; 408 throw iox; 409 } 410 } 411 412 425 public void println(String [][] values){ 426 try { 427 writeln(values); 428 } catch (IOException iox){ 429 error = true; 430 } 431 } 432 433 443 public void writeln(String [][] values) throws IOException { 444 try { 445 for (int i=0; i<values.length; i++){ 446 writeln(values[i]); 447 } 448 if (values.length == 0){ 449 writeln(); 450 } 451 } catch (IOException iox){ 452 error = true; 453 throw iox; 454 } 455 } 456 457 473 public void printlnComment(String comment){ 474 try { 475 writelnComment(comment); 476 } catch (IOException iox){ 477 error = true; 478 } 479 } 480 481 494 public void writelnComment(String comment) throws IOException { 495 try { 496 if (comment==null) comment = ""; 497 if (!newLine){ 498 writeln(); 499 } 500 out.write(commentStart); 501 out.write(' '); 502 for (int i=0; i<comment.length(); i++){ 503 char c = comment.charAt(i); 504 switch (c){ 505 case '\r': { 506 if (i+1 < comment.length() && comment.charAt(i+1) == '\n'){ 507 i++; 508 } 509 } case '\n': { 511 writeln(); 512 out.write(commentStart); 513 out.write(' '); 514 } break; 515 default: { 516 out.write(c); 517 } break; 518 } 519 } 520 writeln(); 521 } catch (IOException iox){ 522 error = true; 523 throw iox; 524 } 525 } 526 527 539 public void print(String value){ 540 try { 541 write(value); 542 } catch (IOException iox){ 543 error = true; 544 } 545 } 546 547 556 public void write(String value) throws IOException { 557 try { 558 if (value == null) value = ""; 559 boolean quote = false; 560 if (alwaysQuote){ 561 quote = true; 562 } else if (value.length() > 0){ 563 char c = value.charAt(0); 564 if (newLine && (c<'0' || (c>'9' && c<'A') || (c>'Z' && c<'a') || (c>'z'))){ 565 quote = true; 566 } 567 if (c==' ' || c=='\f' || c=='\t'){ 568 quote = true; 569 } 570 for (int i=0; i<value.length(); i++){ 571 c = value.charAt(i); 572 if (c==quoteChar || c==delimiterChar || c=='\n' || c=='\r'){ 573 quote = true; 574 } 575 } 576 if (c==' ' || c=='\f' || c=='\t'){ 577 quote = true; 578 } 579 } else if (newLine) { 580 quote = true; 585 } 586 if (newLine){ 587 newLine = false; 588 } else { 589 out.write(delimiterChar); 590 } 591 if (quote){ 592 out.write(escapeAndQuote(value)); 593 } else { 594 out.write(value); 595 } 596 if (autoFlush) flush(); 597 } catch (IOException iox){ 598 error = true; 599 throw iox; 600 } 601 } 602 603 612 private String escapeAndQuote(String value){ 613 int count = 2; 614 for (int i=0; i<value.length(); i++){ 615 char c = value.charAt(i); 616 switch(c){ 617 case '\n': case '\r': case '\\': { 618 count ++; 619 } break; 620 default: { 621 if (c == quoteChar){ 622 count++; 623 } 624 } break; 625 } 626 } 627 StringBuffer sb = new StringBuffer (value.length() + count); 628 sb.append(quoteChar); 629 for (int i=0; i<value.length(); i++){ 630 char c = value.charAt(i); 631 switch(c){ 632 case '\n': { 633 sb.append("\\n"); 634 } break; 635 case '\r': { 636 sb.append("\\r"); 637 } break; 638 case '\\': { 639 sb.append("\\\\"); 640 } break; 641 default: { 642 if (c == quoteChar){ 643 sb.append("\\" + quoteChar); 644 } else { 645 sb.append(c); 646 } 647 } 648 } 649 } 650 sb.append(quoteChar); 651 return (sb.toString()); 652 } 653 654 659 public void flush() throws IOException { 660 out.flush(); 661 } 662 663 668 public void close() throws IOException { 669 out.close(); 670 } 671 672 682 public boolean checkError(){ 683 try { 684 if (error) return true; 685 flush(); 686 if (error) return true; 687 if (out instanceof PrintWriter){ 688 error = ((PrintWriter)out).checkError(); 689 } 690 } catch (IOException iox){ 691 error = true; 692 } 693 return error; 694 } 695 696 705 public void setAutoFlush(boolean autoFlush){ 706 this.autoFlush = autoFlush; 707 } 708 709 718 public void setAlwaysQuote(boolean alwaysQuote){ 719 this.alwaysQuote = alwaysQuote; 720 } 721 722 729 private static void main(String [] args) { 730 OutputStream out; 731 try { 732 if (args.length > 0){ 733 File f = new File(args[0]); 734 if (!f.exists()){ 735 f.createNewFile(); 736 if (f.canWrite()){ 737 out = new FileOutputStream(f); 738 } else { 739 throw new IOException("Could not open " + args[0]); 740 } 741 } else { 742 throw new IOException("File already exists: " + args[0]); 743 } 744 } else { 745 out = System.out; 746 } 747 CSVPrinter p = new CSVPrinter(out); 748 p.print("unquoted"); 749 p.print("un\\quoted"); 750 p.print("escaped\"quote"); 751 p.print("escaped\"quote\\"); 752 p.println("comma,comma"); 753 p.print("!quoted"); 754 p.print("!unquoted"); 755 p.print(" quoted"); 756 p.print("quoted "); 757 p.printlnComment("A comment."); 758 p.print("one"); 759 p.print(""); 760 p.print(""); 761 p.print(""); 762 p.print(""); 763 p.printlnComment("Multi\nLine\rComment\r\nto test line breaks\r"); 764 p.println("two"); 765 p.printlnComment("Comment after explicit new line."); 766 p.print("\nthree\nline\n"); 767 p.println("\ttab"); 768 } catch (IOException e){ 769 System.out.println(e.getMessage()); 770 } 771 } 772 } 773 | Popular Tags |