1 17 18 19 20 package org.apache.fop.cli; 21 22 import java.io.File ; 24 import java.io.FileNotFoundException ; 25 import java.io.IOException ; 26 import java.util.Locale ; 27 import java.util.Map ; 28 import java.util.Vector ; 29 30 import javax.swing.UIManager ; 31 32 import org.apache.fop.Version; 33 import org.apache.fop.apps.FOPException; 34 import org.apache.fop.apps.FOUserAgent; 35 import org.apache.fop.apps.FopFactory; 36 import org.apache.fop.apps.MimeConstants; 37 import org.apache.fop.pdf.PDFAMode; 38 import org.apache.fop.pdf.PDFEncryptionManager; 39 import org.apache.fop.pdf.PDFEncryptionParams; 40 import org.apache.fop.pdf.PDFXMode; 41 import org.apache.fop.render.awt.AWTRenderer; 42 import org.apache.fop.render.Renderer; 43 import org.apache.fop.render.pdf.PDFRenderer; 44 import org.apache.fop.render.xml.XMLRenderer; 45 import org.apache.fop.util.CommandLineLogger; 46 47 import org.apache.commons.logging.Log; 49 import org.apache.commons.logging.LogFactory; 50 51 import org.xml.sax.SAXException ; 53 54 57 public class CommandLineOptions { 58 59 60 public static final int RENDER_NONE = -1; 61 62 65 66 67 public static final int NOT_SET = 0; 68 69 public static final int FO_INPUT = 1; 70 71 public static final int XSLT_INPUT = 2; 72 73 public static final int AREATREE_INPUT = 3; 74 75 76 private Boolean showConfiguration = Boolean.FALSE; 77 78 private Boolean suppressLowLevelAreas = Boolean.FALSE; 79 80 private File userConfigFile = null; 81 82 private File fofile = null; 83 84 private File xsltfile = null; 85 86 private File xmlfile = null; 87 88 private File areatreefile = null; 89 90 private File outfile = null; 91 92 private int inputmode = NOT_SET; 93 94 private String outputmode = null; 95 96 private Map renderingOptions = new java.util.HashMap (); 97 98 private int targetResolution = 0; 99 100 private FopFactory factory = FopFactory.newInstance(); 101 private FOUserAgent foUserAgent; 102 103 private InputHandler inputHandler; 104 105 private Log log; 106 107 private Vector xsltParams = null; 108 109 private String mimicRenderer = null; 110 111 114 public CommandLineOptions() { 115 LogFactory logFactory = LogFactory.getFactory(); 116 117 if (System.getProperty("org.apache.commons.logging.Log") == null) { 120 logFactory.setAttribute("org.apache.commons.logging.Log", 121 CommandLineLogger.class.getName()); 122 setLogLevel("info"); 123 } 124 125 log = LogFactory.getLog("FOP"); 126 } 127 128 135 public void parse(String [] args) 136 throws FOPException, IOException { 137 boolean optionsParsed = true; 138 139 try { 140 optionsParsed = parseOptions(args); 141 if (optionsParsed) { 142 if (showConfiguration == Boolean.TRUE) { 143 dumpConfiguration(); 144 } 145 checkSettings(); 146 createUserConfig(); 147 148 foUserAgent = factory.newFOUserAgent(); 150 foUserAgent.getRendererOptions().putAll(renderingOptions); 151 if (targetResolution != 0) { 152 foUserAgent.setTargetResolution(targetResolution); 153 } 154 addXSLTParameter("fop-output-format", getOutputFormat()); 155 addXSLTParameter("fop-version", Version.getVersion()); 156 } 157 } catch (FOPException e) { 158 printUsage(); 159 throw e; 160 } catch (java.io.FileNotFoundException e) { 161 printUsage(); 162 throw e; 163 } 164 165 inputHandler = createInputHandler(); 166 167 if (MimeConstants.MIME_FOP_AWT_PREVIEW.equals(outputmode)) { 168 try { 170 UIManager.setLookAndFeel( 171 UIManager.getSystemLookAndFeelClassName()); 172 } catch (Exception e) { 173 System.err.println("Couldn't set system look & feel!"); 174 } 175 176 AWTRenderer renderer = new AWTRenderer(true); 177 renderer.setRenderable(inputHandler); renderer.setUserAgent(foUserAgent); 179 foUserAgent.setRendererOverride(renderer); 180 } else if (MimeConstants.MIME_FOP_AREA_TREE.equals(outputmode) 181 && mimicRenderer != null) { 182 Renderer targetRenderer = foUserAgent.getRendererFactory().createRenderer( 184 foUserAgent, mimicRenderer); 185 XMLRenderer xmlRenderer = new XMLRenderer(); 186 xmlRenderer.setUserAgent(foUserAgent); 187 188 xmlRenderer.mimicRenderer(targetRenderer); 190 191 foUserAgent.setRendererOverride(xmlRenderer); 193 } 194 } 195 196 199 public InputHandler getInputHandler() { 200 return inputHandler; 201 } 202 203 207 public Log getLogger() { 208 return log; 209 } 210 211 private void addXSLTParameter(String name, String value) { 212 if (xsltParams == null) { 213 xsltParams = new Vector (); 214 } 215 xsltParams.addElement(name); 216 xsltParams.addElement(value); 217 } 218 219 225 private boolean parseOptions(String [] args) throws FOPException { 226 for (int i = 0; i < args.length; i++) { 227 if (args[i].equals("-x") 228 || args[i].equals("--dump-config")) { 229 showConfiguration = Boolean.TRUE; 230 } else if (args[i].equals("-c")) { 231 i = i + parseConfigurationOption(args, i); 232 } else if (args[i].equals("-l")) { 233 i = i + parseLanguageOption(args, i); 234 } else if (args[i].equals("-s")) { 235 suppressLowLevelAreas = Boolean.TRUE; 236 } else if (args[i].equals("-d")) { 237 setLogOption("debug", "debug"); 238 } else if (args[i].equals("-r")) { 239 factory.setStrictValidation(false); 240 } else if (args[i].equals("-dpi")) { 241 i = i + parseResolution(args, i); 242 } else if (args[i].equals("-q") || args[i].equals("--quiet")) { 243 setLogOption("quiet", "error"); 244 } else if (args[i].equals("-fo")) { 245 i = i + parseFOInputOption(args, i); 246 } else if (args[i].equals("-xsl")) { 247 i = i + parseXSLInputOption(args, i); 248 } else if (args[i].equals("-xml")) { 249 i = i + parseXMLInputOption(args, i); 250 } else if (args[i].equals("-atin")) { 251 i = i + parseAreaTreeInputOption(args, i); 252 } else if (args[i].equals("-awt")) { 253 i = i + parseAWTOutputOption(args, i); 254 } else if (args[i].equals("-pdf")) { 255 i = i + parsePDFOutputOption(args, i, null); 256 } else if (args[i].equals("-pdfa1b")) { 257 i = i + parsePDFOutputOption(args, i, "PDF/A-1b"); 258 } else if (args[i].equals("-mif")) { 259 i = i + parseMIFOutputOption(args, i); 260 } else if (args[i].equals("-rtf")) { 261 i = i + parseRTFOutputOption(args, i); 262 } else if (args[i].equals("-tiff")) { 263 i = i + parseTIFFOutputOption(args, i); 264 } else if (args[i].equals("-png")) { 265 i = i + parsePNGOutputOption(args, i); 266 } else if (args[i].equals("-print")) { 267 i = i + parsePrintOutputOption(args, i); 268 if (i + 1 < args.length) { 270 if (args[i + 1].equals("help")) { 271 printUsagePrintOutput(); 272 return false; 273 } 274 } 275 } else if (args[i].equals("-pcl")) { 276 i = i + parsePCLOutputOption(args, i); 277 } else if (args[i].equals("-ps")) { 278 i = i + parsePostscriptOutputOption(args, i); 279 } else if (args[i].equals("-txt")) { 280 i = i + parseTextOutputOption(args, i); 281 } else if (args[i].equals("-svg")) { 282 i = i + parseSVGOutputOption(args, i); 283 } else if (args[i].equals("-afp")) { 284 i = i + parseAFPOutputOption(args, i); 285 } else if (args[i].equals("-foout")) { 286 i = i + parseFOOutputOption(args, i); 287 } else if (args[i].equals("-out")) { 288 i = i + parseCustomOutputOption(args, i); 289 } else if (args[i].charAt(0) != '-') { 290 i = i + parseUnknownOption(args, i); 291 } else if (args[i].equals("-at")) { 292 i = i + parseAreaTreeOption(args, i); 293 } else if (args[i].equals("-v")) { 294 System.out.println("FOP Version " + Version.getVersion()); 295 } else if (args[i].equals("-param")) { 296 if (i + 2 < args.length) { 297 String name = args[++i]; 298 String expression = args[++i]; 299 addXSLTParameter(name, expression); 300 } else { 301 throw new FOPException("invalid param usage: use -param <name> <value>"); 302 } 303 } else if (args[i].equals("-o")) { 304 i = i + parsePDFOwnerPassword(args, i); 305 } else if (args[i].equals("-u")) { 306 i = i + parsePDFUserPassword(args, i); 307 } else if (args[i].equals("-pdfprofile")) { 308 i = i + parsePDFProfile(args, i); 309 } else if (args[i].equals("-noprint")) { 310 getPDFEncryptionParams().setAllowPrint(false); 311 } else if (args[i].equals("-nocopy")) { 312 getPDFEncryptionParams().setAllowCopyContent(false); 313 } else if (args[i].equals("-noedit")) { 314 getPDFEncryptionParams().setAllowEditContent(false); 315 } else if (args[i].equals("-noannotations")) { 316 getPDFEncryptionParams().setAllowEditAnnotations(false); 317 } else { 318 printUsage(); 319 return false; 320 } 321 } 322 return true; 323 } 325 private int parseConfigurationOption(String [] args, int i) throws FOPException { 326 if ((i + 1 == args.length) 327 || (args[i + 1].charAt(0) == '-')) { 328 throw new FOPException("if you use '-c', you must specify " 329 + "the name of the configuration file"); 330 } else { 331 userConfigFile = new File (args[i + 1]); 332 return 1; 333 } 334 } 335 336 private int parseLanguageOption(String [] args, int i) throws FOPException { 337 if ((i + 1 == args.length) 338 || (args[i + 1].charAt(0) == '-')) { 339 throw new FOPException("if you use '-l', you must specify a language"); 340 } else { 341 Locale.setDefault(new Locale (args[i + 1], "")); 342 return 1; 343 } 344 } 345 346 private int parseResolution(String [] args, int i) throws FOPException { 347 if ((i + 1 == args.length) 348 || (args[i + 1].charAt(0) == '-')) { 349 throw new FOPException( 350 "if you use '-dpi', you must specify a resolution (dots per inch)"); 351 } else { 352 this.targetResolution = Integer.parseInt(args[i + 1]); 353 return 1; 354 } 355 } 356 357 private int parseFOInputOption(String [] args, int i) throws FOPException { 358 inputmode = FO_INPUT; 359 if ((i + 1 == args.length) 360 || (args[i + 1].charAt(0) == '-')) { 361 throw new FOPException("you must specify the fo file for the '-fo' option"); 362 } else { 363 fofile = new File (args[i + 1]); 364 return 1; 365 } 366 } 367 368 private int parseXSLInputOption(String [] args, int i) throws FOPException { 369 inputmode = XSLT_INPUT; 370 if ((i + 1 == args.length) 371 || (args[i + 1].charAt(0) == '-')) { 372 throw new FOPException("you must specify the stylesheet " 373 + "file for the '-xsl' option"); 374 } else { 375 xsltfile = new File (args[i + 1]); 376 return 1; 377 } 378 } 379 380 private int parseXMLInputOption(String [] args, int i) throws FOPException { 381 inputmode = XSLT_INPUT; 382 if ((i + 1 == args.length) 383 || (args[i + 1].charAt(0) == '-')) { 384 throw new FOPException("you must specify the input file " 385 + "for the '-xml' option"); 386 } else { 387 xmlfile = new File (args[i + 1]); 388 return 1; 389 } 390 } 391 392 private int parseAWTOutputOption(String [] args, int i) throws FOPException { 393 setOutputMode(MimeConstants.MIME_FOP_AWT_PREVIEW); 394 return 0; 395 } 396 397 private int parsePDFOutputOption(String [] args, int i, String pdfAMode) throws FOPException { 398 setOutputMode(MimeConstants.MIME_PDF); 399 if ((i + 1 == args.length) 400 || (args[i + 1].charAt(0) == '-')) { 401 throw new FOPException("you must specify the PDF output file"); 402 } else { 403 outfile = new File (args[i + 1]); 404 if (pdfAMode != null) { 405 if (renderingOptions.get("pdf-a-mode") != null) { 406 throw new FOPException("PDF/A mode already set"); 407 } 408 renderingOptions.put("pdf-a-mode", pdfAMode); 409 } 410 return 1; 411 } 412 } 413 414 private int parseMIFOutputOption(String [] args, int i) throws FOPException { 415 setOutputMode(MimeConstants.MIME_MIF); 416 if ((i + 1 == args.length) 417 || (args[i + 1].charAt(0) == '-')) { 418 throw new FOPException("you must specify the MIF output file"); 419 } else { 420 outfile = new File (args[i + 1]); 421 return 1; 422 } 423 } 424 425 private int parseRTFOutputOption(String [] args, int i) throws FOPException { 426 setOutputMode(MimeConstants.MIME_RTF); 427 if ((i + 1 == args.length) 428 || (args[i + 1].charAt(0) == '-')) { 429 throw new FOPException("you must specify the RTF output file"); 430 } else { 431 outfile = new File (args[i + 1]); 432 return 1; 433 } 434 } 435 436 private int parseTIFFOutputOption(String [] args, int i) throws FOPException { 437 setOutputMode(MimeConstants.MIME_TIFF); 438 if ((i + 1 == args.length) 439 || (args[i + 1].charAt(0) == '-')) { 440 throw new FOPException("you must specify the TIFF output file"); 441 } else { 442 outfile = new File (args[i + 1]); 443 return 1; 444 } 445 } 446 447 private int parsePNGOutputOption(String [] args, int i) throws FOPException { 448 setOutputMode(MimeConstants.MIME_PNG); 449 if ((i + 1 == args.length) 450 || (args[i + 1].charAt(0) == '-')) { 451 throw new FOPException("you must specify the PNG output file"); 452 } else { 453 outfile = new File (args[i + 1]); 454 return 1; 455 } 456 } 457 458 private int parsePrintOutputOption(String [] args, int i) throws FOPException { 459 setOutputMode(MimeConstants.MIME_FOP_PRINT); 460 return 0; 461 } 462 463 private int parsePCLOutputOption(String [] args, int i) throws FOPException { 464 setOutputMode(MimeConstants.MIME_PCL); 465 if ((i + 1 == args.length) 466 || (args[i + 1].charAt(0) == '-')) { 467 throw new FOPException("you must specify the PDF output file"); 468 } else { 469 outfile = new File (args[i + 1]); 470 return 1; 471 } 472 } 473 474 private int parsePostscriptOutputOption(String [] args, int i) throws FOPException { 475 setOutputMode(MimeConstants.MIME_POSTSCRIPT); 476 if ((i + 1 == args.length) 477 || (args[i + 1].charAt(0) == '-')) { 478 throw new FOPException("you must specify the PostScript output file"); 479 } else { 480 outfile = new File (args[i + 1]); 481 return 1; 482 } 483 } 484 485 private int parseTextOutputOption(String [] args, int i) throws FOPException { 486 setOutputMode(MimeConstants.MIME_PLAIN_TEXT); 487 if ((i + 1 == args.length) 488 || (args[i + 1].charAt(0) == '-')) { 489 throw new FOPException("you must specify the text output file"); 490 } else { 491 outfile = new File (args[i + 1]); 492 return 1; 493 } 494 } 495 496 private int parseSVGOutputOption(String [] args, int i) throws FOPException { 497 setOutputMode(MimeConstants.MIME_SVG); 498 if ((i + 1 == args.length) 499 || (args[i + 1].charAt(0) == '-')) { 500 throw new FOPException("you must specify the SVG output file"); 501 } else { 502 outfile = new File (args[i + 1]); 503 return 1; 504 } 505 } 506 507 private int parseAFPOutputOption(String [] args, int i) throws FOPException { 508 setOutputMode(MimeConstants.MIME_AFP); 509 if ((i + 1 == args.length) 510 || (args[i + 1].charAt(0) == '-')) { 511 throw new FOPException("you must specify the AFP output file"); 512 } else { 513 outfile = new File (args[i + 1]); 514 return 1; 515 } 516 } 517 518 private int parseFOOutputOption(String [] args, int i) throws FOPException { 519 setOutputMode(MimeConstants.MIME_XSL_FO); 520 if ((i + 1 == args.length) 521 || (args[i + 1].charAt(0) == '-')) { 522 throw new FOPException("you must specify the FO output file"); 523 } else { 524 outfile = new File (args[i + 1]); 525 return 1; 526 } 527 } 528 529 private int parseCustomOutputOption(String [] args, int i) throws FOPException { 530 String mime = null; 531 if ((i + 1 < args.length) 532 || (args[i + 1].charAt(0) != '-')) { 533 mime = args[i + 1]; 534 if ("list".equals(mime)) { 535 String [] mimes = factory.getRendererFactory().listSupportedMimeTypes(); 536 System.out.println("Supported MIME types:"); 537 for (int j = 0; j < mimes.length; j++) { 538 System.out.println(" " + mimes[j]); 539 } 540 System.exit(0); 541 } 542 } 543 if ((i + 2 >= args.length) 544 || (args[i + 1].charAt(0) == '-') 545 || (args[i + 2].charAt(0) == '-')) { 546 throw new FOPException("you must specify the output format and the output file"); 547 } else { 548 setOutputMode(mime); 549 outfile = new File (args[i + 2]); 550 return 2; 551 } 552 } 553 554 private int parseUnknownOption(String [] args, int i) throws FOPException { 555 if (inputmode == NOT_SET) { 556 inputmode = FO_INPUT; 557 fofile = new File (args[i]); 558 } else if (outputmode == null) { 559 outputmode = MimeConstants.MIME_PDF; 560 outfile = new File (args[i]); 561 } else { 562 throw new FOPException("Don't know what to do with " 563 + args[i]); 564 } 565 return 0; 566 } 567 568 private int parseAreaTreeOption(String [] args, int i) throws FOPException { 569 setOutputMode(MimeConstants.MIME_FOP_AREA_TREE); 570 if ((i + 1 == args.length) 571 || (args[i + 1].charAt(0) == '-')) { 572 throw new FOPException("you must specify the area-tree output file"); 573 } else if ((i + 2 == args.length) 574 || (args[i + 2].charAt(0) == '-')) { 575 outfile = new File (args[i + 1]); 577 return 1; 578 } else { 579 mimicRenderer = args[i + 1]; 581 outfile = new File (args[i + 2]); 582 return 2; 583 } 584 } 585 586 private int parseAreaTreeInputOption(String [] args, int i) throws FOPException { 587 inputmode = AREATREE_INPUT; 588 if ((i + 1 == args.length) 589 || (args[i + 1].charAt(0) == '-')) { 590 throw new FOPException("you must specify the Area Tree file for the '-atin' option"); 591 } else { 592 areatreefile = new File (args[i + 1]); 593 return 1; 594 } 595 } 596 597 private PDFEncryptionParams getPDFEncryptionParams() throws FOPException { 598 PDFEncryptionParams params = (PDFEncryptionParams)renderingOptions.get( 599 PDFRenderer.ENCRYPTION_PARAMS); 600 if (params == null) { 601 if (!PDFEncryptionManager.checkAvailableAlgorithms()) { 602 throw new FOPException("PDF encryption requested but it is not available." 603 + " Please make sure MD5 and RC4 algorithms are available."); 604 } 605 params = new PDFEncryptionParams(); 606 renderingOptions.put(PDFRenderer.ENCRYPTION_PARAMS, params); 607 } 608 return params; 609 } 610 611 private int parsePDFOwnerPassword(String [] args, int i) throws FOPException { 612 if ((i + 1 == args.length) 613 || (args[i + 1].charAt(0) == '-')) { 614 getPDFEncryptionParams().setOwnerPassword(""); 615 return 0; 616 } else { 617 getPDFEncryptionParams().setOwnerPassword(args[i + 1]); 618 return 1; 619 } 620 } 621 622 private int parsePDFUserPassword(String [] args, int i) throws FOPException { 623 if ((i + 1 == args.length) 624 || (args[i + 1].charAt(0) == '-')) { 625 getPDFEncryptionParams().setUserPassword(""); 626 return 0; 627 } else { 628 getPDFEncryptionParams().setUserPassword(args[i + 1]); 629 return 1; 630 } 631 } 632 633 private int parsePDFProfile(String [] args, int i) throws FOPException { 634 if ((i + 1 == args.length) 635 || (args[i + 1].charAt(0) == '-')) { 636 throw new FOPException("You must specify a PDF profile"); 637 } else { 638 String profile = args[i + 1]; 639 PDFAMode pdfAMode = PDFAMode.valueOf(profile); 640 if (pdfAMode != null && pdfAMode != PDFAMode.DISABLED) { 641 if (renderingOptions.get("pdf-a-mode") != null) { 642 throw new FOPException("PDF/A mode already set"); 643 } 644 renderingOptions.put("pdf-a-mode", pdfAMode.getName()); 645 return 1; 646 } else { 647 PDFXMode pdfXMode = PDFXMode.valueOf(profile); 648 if (pdfXMode != null && pdfXMode != PDFXMode.DISABLED) { 649 if (renderingOptions.get("pdf-x-mode") != null) { 650 throw new FOPException("PDF/X mode already set"); 651 } 652 renderingOptions.put("pdf-x-mode", pdfXMode.getName()); 653 return 1; 654 } 655 } 656 throw new FOPException("Unsupported PDF profile: " + profile); 657 } 658 } 659 660 private void setOutputMode(String mime) throws FOPException { 661 if (outputmode == null) { 662 outputmode = mime; 663 } else { 664 throw new FOPException("you can only set one output method"); 665 } 666 } 667 668 private void setLogOption (String option, String level) { 669 if (log instanceof CommandLineLogger 670 || System.getProperty("org.apache.commons.logging.Log") == null) { 671 setLogLevel(level); 672 } else if (log != null) { 673 log.warn("The option " + option + " can only be used"); 674 log.warn("with FOP's command line logger,"); 675 log.warn("which is the default on the command line."); 676 log.warn("Configure other loggers using Java system properties."); 677 } 678 } 679 680 private void setLogLevel(String level) { 681 LogFactory.getFactory().setAttribute("level", level); 683 if (log instanceof CommandLineLogger) { 684 ((CommandLineLogger) log).setLogLevel(level); 686 } 687 } 688 689 692 private void checkSettings() throws FOPException, FileNotFoundException { 693 if (inputmode == NOT_SET) { 694 throw new FOPException("No input file specified"); 695 } 696 697 if (outputmode == null) { 698 throw new FOPException("No output file specified"); 699 } 700 701 if ((outputmode.equals(MimeConstants.MIME_FOP_AWT_PREVIEW) 702 || outputmode.equals(MimeConstants.MIME_FOP_PRINT)) 703 && outfile != null) { 704 throw new FOPException("Output file may not be specified " 705 + "for AWT or PRINT output"); 706 } 707 708 if (inputmode == XSLT_INPUT) { 709 if (xmlfile == null) { 711 throw new FOPException("XML file must be specified for the transform mode"); 712 } 713 if (xsltfile == null) { 714 throw new FOPException("XSLT file must be specified for the transform mode"); 715 } 716 717 if (fofile != null) { 719 log.warn("Can't use fo file with transform mode! Ignoring.\n" 720 + "Your input is " + "\n xmlfile: " 721 + xmlfile.getAbsolutePath() 722 + "\nxsltfile: " 723 + xsltfile.getAbsolutePath() 724 + "\n fofile: " 725 + fofile.getAbsolutePath()); 726 } 727 if (!xmlfile.exists()) { 728 throw new FileNotFoundException ("Error: xml file " 729 + xmlfile.getAbsolutePath() 730 + " not found "); 731 } 732 if (!xsltfile.exists()) { 733 throw new FileNotFoundException ("Error: xsl file " 734 + xsltfile.getAbsolutePath() 735 + " not found "); 736 } 737 738 } else if (inputmode == FO_INPUT) { 739 if (outputmode.equals(MimeConstants.MIME_XSL_FO)) { 740 throw new FOPException( 741 "FO output mode is only available if you use -xml and -xsl"); 742 } 743 if (xmlfile != null || xsltfile != null) { 744 log.warn("fo input mode, but xmlfile or xslt file are set:"); 745 log.error("xml file: " + xmlfile.toString()); 746 log.error("xslt file: " + xsltfile.toString()); 747 } 748 if (!fofile.exists()) { 749 throw new FileNotFoundException ("Error: fo file " 750 + fofile.getAbsolutePath() 751 + " not found "); 752 } 753 } else if (inputmode == AREATREE_INPUT) { 754 if (outputmode.equals(MimeConstants.MIME_XSL_FO)) { 755 throw new FOPException( 756 "FO output mode is only available if you use -xml and -xsl"); 757 } else if (outputmode.equals(MimeConstants.MIME_FOP_AREA_TREE)) { 758 throw new FOPException( 759 "Area Tree Output is not available if Area Tree is used as input!"); 760 } 761 if (xmlfile != null || xsltfile != null) { 762 log.warn("area tree input mode, but xmlfile or xslt file are set:"); 763 log.error("xml file: " + xmlfile.toString()); 764 log.error("xslt file: " + xsltfile.toString()); 765 } 766 if (!areatreefile.exists()) { 767 throw new FileNotFoundException ("Error: area tree file " 768 + areatreefile.getAbsolutePath() 769 + " not found "); 770 } 771 } 772 } 774 779 private void createUserConfig() throws FOPException, IOException { 780 if (userConfigFile == null) { 781 return; 782 } 783 try { 784 factory.setUserConfig(userConfigFile); 785 } catch (SAXException e) { 786 throw new FOPException(e); 787 } 788 } 789 790 794 protected String getOutputFormat() throws FOPException { 795 if (outputmode == null) { 796 throw new FOPException("Renderer has not been set!"); 797 } 798 if (outputmode.equals(MimeConstants.MIME_FOP_AREA_TREE)) { 799 renderingOptions.put("fineDetail", isCoarseAreaXml()); 800 } 801 return outputmode; 802 } 803 804 809 private InputHandler createInputHandler() throws IllegalArgumentException { 810 switch (inputmode) { 811 case FO_INPUT: 812 return new InputHandler(fofile); 813 case AREATREE_INPUT: 814 return new AreaTreeInputHandler(areatreefile); 815 case XSLT_INPUT: 816 return new InputHandler(xmlfile, xsltfile, xsltParams); 817 default: 818 throw new IllegalArgumentException ("Error creating InputHandler object."); 819 } 820 } 821 822 826 protected FOUserAgent getFOUserAgent() { 827 return foUserAgent; 828 } 829 830 834 public File getFOFile() { 835 return fofile; 836 } 837 838 842 public File getXMLFile() { 843 return xmlfile; 844 } 845 846 850 public File getXSLFile() { 851 return xsltfile; 852 } 853 854 858 public File getOutputFile() { 859 return outfile; 860 } 861 862 866 public File getUserConfigFile() { 867 return userConfigFile; 868 } 869 870 874 public Boolean isCoarseAreaXml() { 875 return suppressLowLevelAreas; 876 } 877 878 882 public File getInputFile() { 883 switch (inputmode) { 884 case FO_INPUT: 885 return fofile; 886 case XSLT_INPUT: 887 return xmlfile; 888 default: 889 return fofile; 890 } 891 } 892 893 896 public static void printUsage() { 897 System.err.println( 898 "\nUSAGE\nFop [options] [-fo|-xml] infile [-xsl file] " 899 + "[-awt|-pdf|-mif|-rtf|-tiff|-png|-pcl|-ps|-txt|-at [mime]|-print] <outfile>\n" 900 + " [OPTIONS] \n" 901 + " -d debug mode \n" 902 + " -x dump configuration settings \n" 903 + " -q quiet mode \n" 904 + " -c cfg.xml use additional configuration file cfg.xml\n" 905 + " -l lang the language to use for user information \n" 906 + " -r relaxed/less strict validation (where available)\n" 907 + " -dpi xxx target resolution in dots per inch (dpi) where xxx is a number\n" 908 + " -s for area tree XML, down to block areas only\n" 909 + " -v to show FOP version being used\n\n" 910 + " -o [password] PDF file will be encrypted with option owner password\n" 911 + " -u [password] PDF file will be encrypted with option user password\n" 912 + " -noprint PDF file will be encrypted without printing permission\n" 913 + " -nocopy PDF file will be encrypted without copy content permission\n" 914 + " -noedit PDF file will be encrypted without edit content permission\n" 915 + " -noannotations PDF file will be encrypted without edit annotation permission\n" 916 + " -pdfprofile prof PDF file will be generated with the specified profile\n" 917 + " (Examples for prof: PDF/A-1b or PDF/X-3:2003)\n\n" 918 + " [INPUT] \n" 919 + " infile xsl:fo input file (the same as the next) \n" 920 + " -fo infile xsl:fo input file \n" 921 + " -xml infile xml input file, must be used together with -xsl \n" 922 + " -atin infile area tree input file \n" 923 + " -xsl stylesheet xslt stylesheet \n \n" 924 + " -param name value <value> to use for parameter <name> in xslt stylesheet\n" 925 + " (repeat '-param name value' for each parameter)\n \n" 926 + " [OUTPUT] \n" 927 + " outfile input will be rendered as PDF into outfile\n" 928 + " -pdf outfile input will be rendered as PDF (outfile req'd)\n" 929 + " -pdfa1b outfile input will be rendered as PDF/A-1b compliant PDF\n" 930 + " (outfile req'd, same as \"-pdf outfile -pdfprofile PDF/A-1b\")\n" 931 + " -awt input will be displayed on screen \n" 932 + " -mif outfile input will be rendered as MIF (FrameMaker) (outfile req'd)\n" 933 + " -rtf outfile input will be rendered as RTF (outfile req'd)\n" 934 + " -tiff outfile input will be rendered as TIFF (outfile req'd)\n" 935 + " -png outfile input will be rendered as PNG (outfile req'd)\n" 936 + " -pcl outfile input will be rendered as PCL (outfile req'd) \n" 937 + " -ps outfile input will be rendered as PostScript (outfile req'd) \n" 938 + " -txt outfile input will be rendered as plain text (outfile req'd) \n" 939 + " -svg outfile input will be rendered as an SVG slides file (outfile req'd) \n" 940 + " -at [mime] out representation of area tree as XML (outfile req'd) \n" 941 + " specify optional mime output to allow AT to be converted\n" 942 + " to final format later\n" 943 + " -print input file will be rendered and sent to the printer \n" 944 + " see options with \"-print help\" \n" 945 + " -out mime outfile input will be rendered using the given MIME type\n" 946 + " (outfile req'd) Example: \"-out application/pdf D:\\out.pdf\"\n" 947 + " (Tip: \"-out list\" prints the list of supported MIME types)\n" 948 + "\n" 949 + " -foout outfile input will only be XSL transformed. The intermediate \n" 950 + " XSL-FO file is saved and no rendering is performed. \n" 951 + " (Only available if you use -xml and -xsl parameters)\n\n" 952 + "\n" 953 + " [Examples]\n" + " Fop foo.fo foo.pdf \n" 954 + " Fop -fo foo.fo -pdf foo.pdf (does the same as the previous line)\n" 955 + " Fop -xml foo.xml -xsl foo.xsl -pdf foo.pdf\n" 956 + " Fop -xml foo.xml -xsl foo.xsl -foout foo.fo\n" 957 + " Fop foo.fo -mif foo.mif\n" 958 + " Fop foo.fo -rtf foo.rtf\n" 959 + " Fop foo.fo -print or Fop -print foo.fo \n" 960 + " Fop foo.fo -awt \n"); 961 } 962 963 966 private void printUsagePrintOutput() { 967 System.err.println("USAGE: -print [-Dstart=i] [-Dend=i] [-Dcopies=i] [-Deven=true|false] " 968 + " org.apache.fop.apps.Fop (..) -print \n" 969 + "Example:\n" 970 + "java -Dstart=1 -Dend=2 org.apache.Fop.apps.Fop infile.fo -print "); 971 } 972 973 976 private void dumpConfiguration() { 977 log.info("Input mode: "); 978 switch (inputmode) { 979 case NOT_SET: 980 log.info("not set"); 981 break; 982 case FO_INPUT: 983 log.info("FO "); 984 log.info("fo input file: " + fofile.toString()); 985 break; 986 case XSLT_INPUT: 987 log.info("xslt transformation"); 988 log.info("xml input file: " + xmlfile.toString()); 989 log.info("xslt stylesheet: " + xsltfile.toString()); 990 break; 991 default: 992 log.info("unknown input type"); 993 } 994 log.info("Output mode: "); 995 if (outputmode == null) { 996 log.info("not set"); 997 } else if (MimeConstants.MIME_FOP_AWT_PREVIEW.equals(outputmode)) { 998 log.info("awt on screen"); 999 if (outfile != null) { 1000 log.error("awt mode, but outfile is set:"); 1001 log.info("out file: " + outfile.toString()); 1002 } 1003 } else if (MimeConstants.MIME_FOP_PRINT.equals(outputmode)) { 1004 log.info("print directly"); 1005 if (outfile != null) { 1006 log.error("print mode, but outfile is set:"); 1007 log.error("out file: " + outfile.toString()); 1008 } 1009 } else if (MimeConstants.MIME_FOP_AREA_TREE.equals(outputmode)) { 1010 log.info("area tree"); 1011 if (mimicRenderer != null) { 1012 log.info("mimic renderer: " + mimicRenderer); 1013 } 1014 log.info("output file: " + outfile.toString()); 1015 } else { 1016 log.info(outputmode); 1017 log.info("output file: " + outfile.toString()); 1018 } 1019 1020 log.info("OPTIONS"); 1021 1022 if (userConfigFile != null) { 1023 log.info("user configuration file: " 1024 + userConfigFile.toString()); 1025 } else { 1026 log.info("no user configuration file is used [default]"); 1027 } 1028 } 1029 1030} 1031 1032 | Popular Tags |