1 19 20 package com.Ostermiller.util; 21 import java.io.*; 22 23 33 public class ExcelCSVPrinter implements CSVPrint { 34 35 40 protected boolean autoFlush = true; 41 42 47 protected boolean alwaysQuote = false; 48 49 54 protected boolean error = false; 55 56 61 protected char delimiterChar = ','; 62 63 68 protected char quoteChar = '"'; 69 70 75 protected Writer out; 76 77 82 protected boolean newLine = true; 83 84 96 public ExcelCSVPrinter(OutputStream out){ 97 this.out = new OutputStreamWriter(out); 98 } 99 100 111 public ExcelCSVPrinter(Writer out){ 112 this.out = out; 113 } 114 115 126 public ExcelCSVPrinter(Writer out, boolean alwaysQuote, boolean autoFlush){ 127 this.out = out; 128 setAlwaysQuote(alwaysQuote); 129 setAutoFlush(autoFlush); 130 } 131 132 145 public ExcelCSVPrinter(Writer out, char quote, char delimiter) throws BadDelimiterException, BadQuoteException { 146 this.out = out; 147 changeQuote(quote); 148 changeDelimiter(delimiter); 149 } 150 151 165 public ExcelCSVPrinter(Writer out, char quote, char delimiter, boolean alwaysQuote, boolean autoFlush) throws BadDelimiterException, BadQuoteException { 166 this.out = out; 167 changeQuote(quote); 168 changeDelimiter(delimiter); 169 setAlwaysQuote(alwaysQuote); 170 setAutoFlush(autoFlush); 171 } 172 173 182 public void changeDelimiter(char newDelimiter) throws BadDelimiterException { 183 if (delimiterChar == newDelimiter) return; if (newDelimiter == '\n' || newDelimiter == '\r' || 185 newDelimiter == delimiterChar || newDelimiter == quoteChar){ 186 throw new BadDelimiterException(); 187 } 188 delimiterChar = newDelimiter; 189 } 190 191 200 public void changeQuote(char newQuote) throws BadQuoteException { 201 if (quoteChar == newQuote) return; if (newQuote == '\n' || newQuote == '\r' || 203 newQuote == delimiterChar || newQuote == quoteChar){ 204 throw new BadQuoteException(); 205 } 206 quoteChar = newQuote; 207 } 208 209 221 public void println(String value){ 222 try { 223 writeln(value); 224 } catch (IOException iox){ 225 error = true; 226 } 227 } 228 229 238 public void writeln(String value) throws IOException { 239 try { 240 write(value); 241 writeln(); 242 } catch (IOException iox){ 243 error = true; 244 throw iox; 245 } 246 } 247 248 257 public void println(){ 258 try { 259 writeln(); 260 } catch (IOException iox){ 261 error = true; 262 } 263 } 264 265 272 public void writeln() throws IOException { 273 try { 274 out.write("\n"); 275 if (autoFlush) flush(); 276 newLine = true; 277 } catch (IOException iox){ 278 error = true; 279 throw iox; 280 } 281 } 282 283 296 public void println(String [] values){ 297 try { 298 writeln(values); 299 } catch (IOException iox){ 300 error = true; 301 } 302 } 303 304 314 public void writeln(String [] values) throws IOException { 315 try { 316 print(values); 317 writeln(); 318 } catch (IOException iox){ 319 error = true; 320 throw iox; 321 } 322 } 323 324 337 public void print(String [] values){ 338 try { 339 write(values); 340 } catch (IOException iox){ 341 error = true; 342 } 343 } 344 345 355 public void write(String [] values) throws IOException { 356 try { 357 for (int i=0; i<values.length; i++){ 358 write(values[i]); 359 } 360 } catch (IOException iox){ 361 error = true; 362 throw iox; 363 } 364 } 365 366 379 public void println(String [][] values){ 380 try { 381 writeln(values); 382 } catch (IOException iox){ 383 error = true; 384 } 385 } 386 387 397 public void writeln(String [][] values) throws IOException { 398 try { 399 for (int i=0; i<values.length; i++){ 400 writeln(values[i]); 401 } 402 if (values.length == 0){ 403 writeln(); 404 } 405 } catch (IOException iox){ 406 error = true; 407 throw iox; 408 } 409 } 410 411 424 public void printlnComment(String comment){ 425 println(); 426 } 427 428 438 public void writelnComment(String comment) throws IOException { 439 writeln(); 440 } 441 442 454 public void print(String value){ 455 try { 456 write(value); 457 } catch (IOException iox){ 458 error = true; 459 } 460 } 461 462 471 public void write(String value) throws IOException { 472 try { 473 if (value == null) value = ""; 474 boolean quote = false; 475 if (alwaysQuote){ 476 quote = true; 477 } else if (value.length() > 0){ 478 for (int i=0; i<value.length(); i++){ 479 char c = value.charAt(i); 480 if (c==quoteChar || c==delimiterChar || c=='\n' || c=='\r'){ 481 quote = true; 482 } 483 } 484 } else if (newLine) { 485 quote = true; 490 } 491 if (newLine){ 492 newLine = false; 493 } else { 494 out.write(delimiterChar); 495 } 496 if (quote){ 497 out.write(escapeAndQuote(value)); 498 } else { 499 out.write(value); 500 } 501 if (autoFlush) flush(); 502 } catch (IOException iox){ 503 error = true; 504 throw iox; 505 } 506 } 507 508 517 private String escapeAndQuote(String value){ 518 String s = StringHelper.replace(value, String.valueOf(quoteChar), String.valueOf(quoteChar) + String.valueOf(quoteChar)); 519 return (new StringBuffer (2 + s.length())).append(quoteChar).append(s).append(quoteChar).toString(); 520 } 521 522 527 public void flush() throws IOException { 528 out.flush(); 529 } 530 531 536 public void close() throws IOException { 537 out.close(); 538 } 539 540 550 public boolean checkError(){ 551 try { 552 if (error) return true; 553 flush(); 554 if (error) return true; 555 if (out instanceof PrintWriter){ 556 error = ((PrintWriter)out).checkError(); 557 } 558 } catch (IOException iox){ 559 error = true; 560 } 561 return error; 562 } 563 564 573 public void setAlwaysQuote(boolean alwaysQuote){ 574 this.alwaysQuote = alwaysQuote; 575 } 576 577 586 public void setAutoFlush(boolean autoFlush){ 587 this.autoFlush = autoFlush; 588 } 589 590 597 private static void main(String [] args) { 598 OutputStream out; 599 try { 600 if (args.length > 0){ 601 File f = new File(args[0]); 602 if (!f.exists()){ 603 f.createNewFile(); 604 if (f.canWrite()){ 605 out = new FileOutputStream(f); 606 } else { 607 throw new IOException("Could not open " + args[0]); 608 } 609 } else { 610 throw new IOException("File already exists: " + args[0]); 611 } 612 } else { 613 out = System.out; 614 } 615 ExcelCSVPrinter p = new ExcelCSVPrinter(out); 616 p.print("unquoted"); 617 p.print("escaped\"quote"); 618 p.println("comma,comma"); 619 p.print("!quoted"); 620 p.print("!unquoted"); 621 p.print(" quoted"); 622 p.println("quoted "); 623 p.print("one"); 624 p.print(""); 625 p.print(""); 626 p.print(""); 627 p.println(""); 628 p.println("two"); 629 p.print("\nthree\nline\n"); 630 p.println("\ttab"); 631 } catch (IOException e){ 632 System.out.println(e.getMessage()); 633 } 634 } 635 } 636 | Popular Tags |