1 51 package org.apache.fop.apps; 52 53 import org.apache.fop.apps.FOPException; 55 import org.apache.fop.messaging.MessageHandler; 56 import org.apache.fop.render.txt.TXTRenderer; 57 58 import org.apache.avalon.framework.logger.ConsoleLogger; 60 import org.apache.avalon.framework.logger.Logger; 61 62 import java.io.File ; 64 import java.io.FileNotFoundException ; 65 66 69 public class CommandLineOptions { 70 71 72 private static final int NOT_SET = 0; 73 74 private static final int FO_INPUT = 1; 75 76 private static final int XSLT_INPUT = 2; 77 78 private static final int PDF_OUTPUT = 1; 79 80 private static final int AWT_OUTPUT = 2; 81 82 private static final int MIF_OUTPUT = 3; 83 84 private static final int PRINT_OUTPUT = 4; 85 86 private static final int PCL_OUTPUT = 5; 87 88 private static final int PS_OUTPUT = 6; 89 90 private static final int TXT_OUTPUT = 7; 91 92 private static final int SVG_OUTPUT = 8; 93 94 private static final int AREA_OUTPUT = 9; 95 96 97 Boolean errorDump = Boolean.FALSE; 98 99 Boolean dumpConfiguration = Boolean.FALSE; 100 101 Boolean quiet = Boolean.FALSE; 102 103 Boolean suppressLowLevelAreas = Boolean.FALSE; 104 105 File userConfigFile = null; 106 107 File fofile = null; 108 109 File xsltfile = null; 110 111 File xmlfile = null; 112 113 File outfile = null; 114 115 int inputmode = NOT_SET; 116 117 int outputmode = NOT_SET; 118 119 String language = null; 120 121 private java.util.HashMap rendererOptions; 122 123 private Logger log; 124 125 public CommandLineOptions(String [] args) 126 throws FOPException, FileNotFoundException { 127 128 setLogger(new ConsoleLogger(ConsoleLogger.LEVEL_INFO)); 129 130 boolean optionsParsed = true; 131 rendererOptions = new java.util.HashMap (); 132 try { 133 optionsParsed = parseOptions(args); 134 if (optionsParsed) { 135 checkSettings(); 136 if (errorDump != null && errorDump.booleanValue()) { 137 debug(); 138 } 139 } 140 } catch (FOPException e) { 141 printUsage(); 142 throw e; 143 } catch (java.io.FileNotFoundException e) { 144 printUsage(); 145 throw e; 146 } 147 148 } 149 150 private boolean pdfEncryptionAvailable = false; 151 private boolean pdfEncryptionChecked = false; 152 private boolean encryptionAvailable() { 153 if (!pdfEncryptionChecked) { 154 try { 155 Class c = Class.forName("javax.crypto.Cipher"); 156 pdfEncryptionAvailable 157 = org.apache.fop.pdf.PDFEncryption.encryptionAvailable(); 158 } 159 catch(ClassNotFoundException e) { 160 pdfEncryptionAvailable = false; 161 } 162 pdfEncryptionChecked = true; 163 if (!pdfEncryptionAvailable) { 164 log.warn("PDF encryption not available."); 165 } 166 } 167 return pdfEncryptionAvailable; 168 } 169 170 175 private boolean parseOptions(String args[]) throws FOPException { 176 for (int i = 0; i < args.length; i++) { 177 if (args[i].equals("-d") || args[i].equals("--full-error-dump")) { 178 errorDump = Boolean.TRUE; 179 setLogger(new ConsoleLogger(ConsoleLogger.LEVEL_DEBUG)); 180 } else if (args[i].equals("-x") 181 || args[i].equals("--dump-config")) { 182 dumpConfiguration = Boolean.TRUE; 183 } else if (args[i].equals("-q") || args[i].equals("--quiet")) { 184 quiet = Boolean.TRUE; 185 setLogger(new ConsoleLogger(ConsoleLogger.LEVEL_ERROR)); 186 } else if (args[i].equals("-c")) { 187 if ((i + 1 == args.length) 188 || (args[i + 1].charAt(0) == '-')) { 189 throw new FOPException("if you use '-c', you must specify the name of the configuration file"); 190 } else { 191 userConfigFile = new File (args[i + 1]); 192 i++; 193 } 194 } else if (args[i].equals("-l")) { 195 if ((i + 1 == args.length) 196 || (args[i + 1].charAt(0) == '-')) { 197 throw new FOPException("if you use '-l', you must specify a language"); 198 } else { 199 language = args[i + 1]; 200 i++; 201 } 202 } else if (args[i].equals("-s")) { 203 suppressLowLevelAreas = Boolean.TRUE; 204 } else if (args[i].equals("-fo")) { 205 inputmode = FO_INPUT; 206 if ((i + 1 == args.length) 207 || (args[i + 1].charAt(0) == '-')) { 208 throw new FOPException("you must specify the fo file for the '-fo' option"); 209 } else { 210 fofile = new File (args[i + 1]); 211 i++; 212 } 213 } else if (args[i].equals("-xsl")) { 214 inputmode = XSLT_INPUT; 215 if ((i + 1 == args.length) 216 || (args[i + 1].charAt(0) == '-')) { 217 throw new FOPException("you must specify the stylesheet file for the '-xsl' option"); 218 } else { 219 xsltfile = new File (args[i + 1]); 220 i++; 221 } 222 } else if (args[i].equals("-xml")) { 223 inputmode = XSLT_INPUT; 224 if ((i + 1 == args.length) 225 || (args[i + 1].charAt(0) == '-')) { 226 throw new FOPException("you must specify the input file for the '-xml' option"); 227 } else { 228 xmlfile = new File (args[i + 1]); 229 i++; 230 } 231 } else if (args[i].equals("-awt")) { 232 setOutputMode(AWT_OUTPUT); 233 } else if (args[i].equals("-pdf")) { 234 setOutputMode(PDF_OUTPUT); 235 if ((i + 1 == args.length) 236 || (args[i + 1].charAt(0) == '-')) { 237 throw new FOPException("you must specify the pdf output file"); 238 } else { 239 outfile = new File (args[i + 1]); 240 i++; 241 } 242 } else if (args[i].equals("-o")) { 243 if ((i + 1 == args.length) || (args[i + 1].charAt(0) == '-')) { 244 if (encryptionAvailable()) { 245 rendererOptions.put("ownerPassword", ""); 246 } 247 } else { 248 if (encryptionAvailable()) { 249 rendererOptions.put("ownerPassword", args[i + 1]); 250 } 251 i++; 252 } 253 } else if (args[i].equals("-u")) { 254 if ((i + 1 == args.length) || (args[i + 1].charAt(0) == '-')) { 255 if (encryptionAvailable()) { 256 rendererOptions.put("userPassword", ""); 257 } 258 } else { 259 if (encryptionAvailable()) { 260 rendererOptions.put("userPassword", args[i + 1]); 261 } 262 i++; 263 } 264 } else if (args[i].equals("-noprint")) { 265 if (encryptionAvailable()) { 266 rendererOptions.put("allowPrint", "FALSE"); 267 } 268 } else if (args[i].equals("-nocopy")) { 269 if (encryptionAvailable()) { 270 rendererOptions.put("allowCopyContent", "FALSE"); 271 } 272 } else if (args[i].equals("-noedit")) { 273 if (encryptionAvailable()) { 274 rendererOptions.put("allowEditContent", "FALSE"); 275 } 276 } else if (args[i].equals("-noannotations")) { 277 if (encryptionAvailable()) { 278 rendererOptions.put("allowEditAnnotations", "FALSE"); 279 } 280 } else if (args[i].equals("-mif")) { 281 setOutputMode(MIF_OUTPUT); 282 if ((i + 1 == args.length) 283 || (args[i + 1].charAt(0) == '-')) { 284 throw new FOPException("you must specify the mif output file"); 285 } else { 286 outfile = new File (args[i + 1]); 287 i++; 288 } 289 } else if (args[i].equals("-print")) { 290 setOutputMode(PRINT_OUTPUT); 291 if (i + 1 < args.length) { 293 if (args[i + 1].equals("help")) { 294 printUsagePrintOutput(); 295 return false; 296 } 297 } 298 } else if (args[i].equals("-pcl")) { 299 setOutputMode(PCL_OUTPUT); 300 if ((i + 1 == args.length) 301 || (args[i + 1].charAt(0) == '-')) { 302 throw new FOPException("you must specify the pdf output file"); 303 } else { 304 outfile = new File (args[i + 1]); 305 i++; 306 } 307 } else if (args[i].equals("-ps")) { 308 setOutputMode(PS_OUTPUT); 309 if ((i + 1 == args.length) 310 || (args[i + 1].charAt(0) == '-')) { 311 throw new FOPException("you must specify the PostScript output file"); 312 } else { 313 outfile = new File (args[i + 1]); 314 i++; 315 } 316 } else if (args[i].equals("-txt")) { 317 setOutputMode(TXT_OUTPUT); 318 if ((i + 1 == args.length) 319 || (args[i + 1].charAt(0) == '-')) { 320 throw new FOPException("you must specify the text output file"); 321 } else { 322 outfile = new File (args[i + 1]); 323 i++; 324 } 325 } else if (args[i].equals("-svg")) { 326 setOutputMode(SVG_OUTPUT); 327 if ((i + 1 == args.length) 328 || (args[i + 1].charAt(0) == '-')) { 329 throw new FOPException("you must specify the svg output file"); } else { 330 outfile = new File (args[i + 1]); 331 i++; 332 } 333 } else if (args[i].charAt(0) != '-') { 334 if (inputmode == NOT_SET) { 335 inputmode = FO_INPUT; 336 fofile = new File (args[i]); 337 } else if (outputmode == NOT_SET) { 338 outputmode = PDF_OUTPUT; 339 outfile = new File (args[i]); 340 } else { 341 throw new FOPException("Don't know what to do with " 342 + args[i]); 343 } 344 } else if (args[i].equals("-at")) { 345 setOutputMode(AREA_OUTPUT); 346 if ((i + 1 == args.length) 347 || (args[i + 1].charAt(0) == '-')) { 348 throw new FOPException("you must specify the area-tree output file"); 349 } else { 350 outfile = new File (args[i + 1]); 351 i++; 352 } 353 } else if (args[i].equals("-" + TXTRenderer.encodingOptionName)) { 354 if ((i + 1 == args.length) 355 || (args[i + 1].charAt(0) == '-')) { 356 throw new FOPException("you must specify text renderer encoding"); 357 } else { 358 rendererOptions.put(TXTRenderer.encodingOptionName, args[i + 1]); 359 i++; 360 } 361 } else { 362 printUsage(); 363 return false; 364 } 365 } 366 return true; 367 } 369 private void setOutputMode(int mode) throws FOPException { 370 if (outputmode == NOT_SET) { 371 outputmode = mode; 372 } else { 373 throw new FOPException("you can only set one output method"); 374 } 375 } 376 377 380 private void checkSettings() throws FOPException, FileNotFoundException { 381 if (inputmode == NOT_SET) { 382 throw new FOPException("No input file specified"); 383 } 384 385 if (outputmode == NOT_SET) { 386 throw new FOPException("No output file specified"); 387 } 388 389 if (inputmode == XSLT_INPUT) { 390 if (xmlfile == null) { 392 throw new FOPException("XML file must be specified for the tranform mode"); 393 } 394 if (xsltfile == null) { 395 throw new FOPException("XSLT file must be specified for the tranform mode"); 396 } 397 398 if (fofile != null) { 400 log.warn("Can't use fo file with transform mode! Ignoring.\n" 401 + "Your input is " + "\n xmlfile: " 402 + xmlfile.getAbsolutePath() 403 + "\nxsltfile: " 404 + xsltfile.getAbsolutePath() 405 + "\n fofile: " 406 + fofile.getAbsolutePath()); 407 } 408 if (!xmlfile.exists()) { 409 throw new FileNotFoundException ("xml file " 410 + xmlfile.getAbsolutePath() 411 + " not found "); 412 } 413 if (!xsltfile.exists()) { 414 throw new FileNotFoundException ("xsl file " 415 + xsltfile.getAbsolutePath() 416 + " not found "); 417 } 418 419 } else if (inputmode == FO_INPUT) { 420 if (xmlfile != null || xsltfile != null) { 421 log.warn("fo input mode, but xmlfile or xslt file are set:"); 422 log.error("xml file: " + xmlfile.toString()); 423 log.error("xslt file: " + xsltfile.toString()); 424 } 425 if (!fofile.exists()) { 426 throw new FileNotFoundException ("fo file " 427 + fofile.getAbsolutePath() 428 + " not found "); 429 } 430 431 } 432 } 434 private void setLogger(Logger newLogger) { 435 this.log = newLogger; 436 MessageHandler.setScreenLogger(newLogger); 437 } 438 439 442 public int getRenderer() throws FOPException { 443 switch (outputmode) { 444 case NOT_SET: 445 throw new FOPException("Renderer has not been set!"); 446 case PDF_OUTPUT: 447 return Driver.RENDER_PDF; 448 case AWT_OUTPUT: 449 return Driver.RENDER_AWT; 450 case MIF_OUTPUT: 451 return Driver.RENDER_MIF; 452 case PRINT_OUTPUT: 453 return Driver.RENDER_PRINT; 454 case PCL_OUTPUT: 455 return Driver.RENDER_PCL; 456 case PS_OUTPUT: 457 return Driver.RENDER_PS; 458 case TXT_OUTPUT: 459 return Driver.RENDER_TXT; 460 case SVG_OUTPUT: 461 return Driver.RENDER_SVG; 462 case AREA_OUTPUT: 463 rendererOptions.put("fineDetail", isCoarseAreaXml()); 464 return Driver.RENDER_XML; 465 default: 466 throw new FOPException("Invalid Renderer setting!"); 467 } 468 } 469 470 473 public InputHandler getInputHandler() 474 throws FOPException { 475 switch (inputmode) { 476 case FO_INPUT: 477 return new FOInputHandler(fofile); 478 case XSLT_INPUT: 479 return new XSLTInputHandler(xmlfile, xsltfile); 480 default: 481 return new FOInputHandler(fofile); 482 } 483 } 484 485 public java.util.HashMap getRendererOptions() { 486 return rendererOptions; 487 } 488 489 public Starter getStarter() throws FOPException { 490 Starter starter = null; 491 switch (outputmode) { 492 case AWT_OUTPUT: 493 try { 494 starter = ((Starter)Class.forName("org.apache.fop.apps.AWTStarter").getConstructor(new Class [] { 495 CommandLineOptions.class 496 }).newInstance(new Object [] { 497 this 498 })); 499 } catch (Exception e) { 500 if (e instanceof FOPException) { 501 throw (FOPException)e; 502 } 503 throw new FOPException("AWTStarter could not be loaded.", e); 504 } 505 break; 506 case PRINT_OUTPUT: 507 try { 508 starter = ((Starter)Class.forName("org.apache.fop.apps.PrintStarter").getConstructor(new Class [] { 509 CommandLineOptions.class 510 }).newInstance(new Object [] { 511 this 512 })); 513 } catch (Exception e) { 514 if (e instanceof FOPException) { 515 throw (FOPException)e; 516 } 517 throw new FOPException("PrintStarter could not be loaded.", 518 e); 519 } 520 break; 521 default: 522 starter = new CommandLineStarter(this); 523 } 524 starter.setLogger(log); 525 return starter; 526 } 527 528 public int getInputMode() { 529 return inputmode; 530 } 531 532 public int getOutputMode() { 533 return outputmode; 534 } 535 536 public File getFOFile() { 537 return fofile; 538 } 539 540 public File getXMLFile() { 541 return xmlfile; 542 } 543 544 public File getXSLFile() { 545 return xsltfile; 546 } 547 548 public File getOutputFile() { 549 return outfile; 550 } 551 552 public File getUserConfigFile() { 553 return userConfigFile; 554 } 555 556 public String getLanguage() { 557 return language; 558 } 559 560 public Boolean isQuiet() { 561 return quiet; 562 } 563 564 public Boolean dumpConfiguration() { 565 return dumpConfiguration; 566 } 567 568 public Boolean isDebugMode() { 569 return errorDump; 570 } 571 572 public Boolean isCoarseAreaXml() { 573 return suppressLowLevelAreas; 574 } 575 576 579 public File getInputFile() { 580 switch (inputmode) { 581 case FO_INPUT: 582 return fofile; 583 case XSLT_INPUT: 584 return xmlfile; 585 default: 586 return fofile; 587 } 588 } 589 590 593 public static void printUsage() { 594 System.err.println("\nUSAGE\nFop [options] [-fo|-xml] infile [-xsl file] [-awt|-pdf|-mif|-pcl|-ps|-txt|-at|-print] <outfile>\n" 595 + " [OPTIONS]\n" 596 + " -d debug mode\n" 597 + " -x dump configuration settings\n" 598 + " -q quiet mode\n" 599 + " -c cfg.xml use additional configuration file cfg.xml\n" 600 + " -l lang the language to use for user information\n" 601 + " -s (-at output) omit tree below block areas\n" 602 + " -"+TXTRenderer.encodingOptionName+" (-txt output encoding use the encoding for the output file.\n" 603 + " The encoding must be a valid java encoding.\n" 604 + " -o [password] pdf file will be encrypted with option owner password\n" 605 + " -u [password] pdf file will be encrypted with option user password\n" 606 + " -noprint pdf file will be encrypted without printing permission\n" 607 + " -nocopy pdf file will be encrypted without copy content permission\n" 608 + " -noedit pdf file will be encrypted without edit content permission\n" 609 + " -noannotations pdf file will be encrypted without edit annotation permission\n" 610 + "\n [INPUT]\n" 611 + " infile xsl:fo input file (the same as the next)\n" 612 + " -fo infile xsl:fo input file\n" 613 + " -xml infile xml input file, must be used together with -xsl\n" 614 + " -xsl stylesheet xslt stylesheet\n" 615 + "\n [OUTPUT]\n" 616 + " outfile input will be rendered as pdf file into outfile\n" 617 + " -pdf outfile input will be rendered as pdf file (outfile req'd)\n" 618 + " -awt input will be displayed on screen\n" 619 + " -mif outfile input will be rendered as mif file (outfile req'd)\n" 620 + " -pcl outfile input will be rendered as pcl file (outfile req'd)\n" 621 + " -ps outfile input will be rendered as PostScript file (outfile req'd)\n" 622 + " -txt outfile input will be rendered as text file (outfile req'd)\n" 623 + " -svg outfile input will be rendered as an svg slides file (outfile req'd)\n" 624 + " -at outfile representation of area tree as XML (outfile req'd)\n" 625 + " -print input file will be rendered and sent to the printer\n" 626 + " see print specific options with \"-print help\"\n" 627 + "\n [Examples]\n" 628 + " Fop foo.fo foo.pdf\n" 629 + " Fop -fo foo.fo -pdf foo.pdf (does the same as the previous line)\n" 630 + " Fop -xsl foo.xsl -xml foo.xml -pdf foo.pdf\n" 631 + " Fop foo.fo -mif foo.mif\n" 632 + " Fop foo.fo -print or Fop -print foo.fo\n" 633 + " Fop foo.fo -awt\n"); 634 } 635 636 639 public void printUsagePrintOutput() { 640 System.err.println("USAGE: -print [-Dstart=i] [-Dend=i] [-Dcopies=i] [-Deven=true|false] " 641 + " org.apache.fop.apps.Fop (..) -print\n" 642 + "Example:\n" 643 + "java -Dstart=1 -Dend=2 org.apache.Fop.apps.Fop infile.fo -print "); 644 } 645 646 647 650 private void debug() { 651 log.debug("Input mode: "); 652 switch (inputmode) { 653 case NOT_SET: 654 log.debug("not set"); 655 break; 656 case FO_INPUT: 657 log.debug("FO "); 658 log.debug("fo input file: " + fofile.toString()); 659 break; 660 case XSLT_INPUT: 661 log.debug("xslt transformation"); 662 log.debug("xml input file: " + xmlfile.toString()); 663 log.debug("xslt stylesheet: " + xsltfile.toString()); 664 break; 665 default: 666 log.debug("unknown input type"); 667 } 668 log.debug("Output mode: "); 669 switch (outputmode) { 670 case NOT_SET: 671 log.debug("not set"); 672 break; 673 case PDF_OUTPUT: 674 log.debug("pdf"); 675 log.debug("output file: " + outfile.toString()); 676 break; 677 case AWT_OUTPUT: 678 log.debug("awt on screen"); 679 if (outfile != null) { 680 log.error("awt mode, but outfile is set:"); 681 log.debug("out file: " + outfile.toString()); 682 } 683 break; 684 case MIF_OUTPUT: 685 log.debug("mif"); 686 log.debug("output file: " + outfile.toString()); 687 break; 688 case PRINT_OUTPUT: 689 log.debug("print directly"); 690 if (outfile != null) { 691 log.error("print mode, but outfile is set:"); 692 log.error("out file: " + outfile.toString()); 693 } 694 break; 695 case PCL_OUTPUT: 696 log.debug("pcl"); 697 log.debug("output file: " + outfile.toString()); 698 break; 699 case PS_OUTPUT: 700 log.debug("PostScript"); 701 log.debug("output file: " + outfile.toString()); 702 break; 703 case TXT_OUTPUT: 704 log.debug("txt"); 705 log.debug("output file: " + outfile.toString()); 706 if (rendererOptions.containsKey(TXTRenderer.encodingOptionName)) 707 log.debug("output encoding: " + rendererOptions.get(TXTRenderer.encodingOptionName)); 708 break; 709 case SVG_OUTPUT: 710 log.debug("svg"); 711 log.debug("output file: " + outfile.toString()); 712 break; 713 default: 714 log.debug("unknown input type"); 715 } 716 717 718 log.debug("OPTIONS"); 719 if (userConfigFile != null) { 720 log.debug("user configuration file: " 721 + userConfigFile.toString()); 722 } else { 723 log.debug("no user configuration file is used [default]"); 724 } 725 if (errorDump != null) { 726 log.debug("debug mode on"); 727 } else { 728 log.debug("debug mode off [default]"); 729 } 730 if (dumpConfiguration != null) { 731 log.debug("dump configuration"); 732 } else { 733 log.debug("don't dump configuration [default]"); 734 } 735 if (quiet != null) { 736 log.debug("quiet mode on"); 737 } else { 738 log.debug("quiet mode off [default]"); 739 } 740 741 } 742 743 public static void main(String args[]) { 745 750 try { 751 CommandLineOptions options = new CommandLineOptions(args); 752 } catch (Exception e) { 753 e.printStackTrace(); 754 } 755 756 } 758 759 } 760 761 | Popular Tags |