1 16 19 package org.apache.xalan.xslt; 20 21 import java.io.FileOutputStream ; 22 import java.io.FileWriter ; 23 import java.io.PrintWriter ; 24 import java.io.StringReader ; 25 import java.util.Properties ; 26 import java.util.ResourceBundle ; 27 import java.util.Vector ; 28 29 import javax.xml.parsers.DocumentBuilder ; 30 import javax.xml.parsers.DocumentBuilderFactory ; 31 import javax.xml.transform.OutputKeys ; 32 import javax.xml.transform.Source ; 33 import javax.xml.transform.Templates ; 34 import javax.xml.transform.Transformer ; 35 import javax.xml.transform.TransformerException ; 36 import javax.xml.transform.TransformerFactory ; 37 import javax.xml.transform.TransformerFactoryConfigurationError ; 38 import javax.xml.transform.URIResolver ; 39 import javax.xml.transform.dom.DOMResult ; 40 import javax.xml.transform.dom.DOMSource ; 41 import javax.xml.transform.sax.SAXResult ; 42 import javax.xml.transform.sax.SAXSource ; 43 import javax.xml.transform.sax.SAXTransformerFactory ; 44 import javax.xml.transform.sax.TransformerHandler ; 45 import javax.xml.transform.stream.StreamResult ; 46 import javax.xml.transform.stream.StreamSource ; 47 48 import org.apache.xalan.Version; 49 import org.apache.xalan.res.XSLMessages; 50 import org.apache.xalan.res.XSLTErrorResources; 51 import org.apache.xalan.trace.PrintTraceListener; 52 import org.apache.xalan.trace.TraceManager; 53 import org.apache.xalan.transformer.XalanProperties; 54 import org.apache.xml.utils.DefaultErrorHandler; 55 56 import org.w3c.dom.Document ; 57 import org.w3c.dom.Node ; 58 59 import org.xml.sax.ContentHandler ; 60 import org.xml.sax.EntityResolver ; 61 import org.xml.sax.InputSource ; 62 import org.xml.sax.XMLReader ; 63 import org.xml.sax.helpers.XMLReaderFactory ; 64 65 69 public class Process 70 { 71 76 protected static void printArgOptions(ResourceBundle resbundle) 77 { 78 System.out.println(resbundle.getString("xslProc_option")); System.out.println("\n\t\t\t" + resbundle.getString("xslProc_common_options") + "\n"); 80 System.out.println(resbundle.getString("optionXSLTC")); System.out.println(resbundle.getString("optionIN")); System.out.println(resbundle.getString("optionXSL")); System.out.println(resbundle.getString("optionOUT")); 85 System.out.println(resbundle.getString("optionV")); 88 System.out.println(resbundle.getString("optionEDUMP")); System.out.println(resbundle.getString("optionXML")); System.out.println(resbundle.getString("optionTEXT")); System.out.println(resbundle.getString("optionHTML")); System.out.println(resbundle.getString("optionPARAM")); 95 System.out.println(resbundle.getString("optionMEDIA")); 96 System.out.println(resbundle.getString("optionFLAVOR")); 97 System.out.println(resbundle.getString("optionDIAG")); 98 System.out.println(resbundle.getString("optionURIRESOLVER")); System.out.println(resbundle.getString("optionENTITYRESOLVER")); waitForReturnKey(resbundle); 101 System.out.println(resbundle.getString("optionCONTENTHANDLER")); 103 System.out.println("\n\t\t\t" + resbundle.getString("xslProc_xalan_options") + "\n"); 104 105 System.out.println(resbundle.getString("optionQC")); 107 System.out.println(resbundle.getString("optionTT")); System.out.println(resbundle.getString("optionTG")); System.out.println(resbundle.getString("optionTS")); System.out.println(resbundle.getString("optionTTC")); System.out.println(resbundle.getString("optionTCLASS")); System.out.println(resbundle.getString("optionLINENUMBERS")); System.out.println(resbundle.getString("optionINCREMENTAL")); 115 System.out.println(resbundle.getString("optionNOOPTIMIMIZE")); 116 System.out.println(resbundle.getString("optionRL")); 117 118 System.out.println("\n\t\t\t" + resbundle.getString("xslProc_xsltc_options") + "\n"); 119 System.out.println(resbundle.getString("optionXO")); 120 System.out.println(resbundle.getString("optionXD")); 121 waitForReturnKey(resbundle); 122 System.out.println(resbundle.getString("optionXJ")); 123 System.out.println(resbundle.getString("optionXP")); 124 System.out.println(resbundle.getString("optionXN")); 125 System.out.println(resbundle.getString("optionXX")); 126 System.out.println(resbundle.getString("optionXT")); 127 } 128 129 144 public static void main(String argv[]) 145 { 146 147 boolean doStackDumpOnError = false; 149 boolean setQuietMode = false; 150 boolean doDiag = false; 151 152 155 158 java.io.PrintWriter diagnosticsWriter = new PrintWriter (System.err, true); 159 java.io.PrintWriter dumpWriter = diagnosticsWriter; 160 ResourceBundle resbundle = 161 (XSLMessages.loadResourceBundle( 162 org.apache.xml.utils.res.XResourceBundle.ERROR_RESOURCES)); 163 String flavor = "s2s"; 164 165 if (argv.length < 1) 166 { 167 printArgOptions(resbundle); 168 } 169 else 170 { 171 boolean useXSLTC = false; 172 for (int i = 0; i < argv.length; i++) 173 { 174 if ("-XSLTC".equalsIgnoreCase(argv[i])) 175 { 176 useXSLTC = true; 177 } 178 } 179 180 TransformerFactory tfactory; 181 if (useXSLTC) 182 { 183 String key = "javax.xml.transform.TransformerFactory"; 184 String value = "org.apache.xalan.xsltc.trax.TransformerFactoryImpl"; 185 Properties props = System.getProperties(); 186 props.put(key, value); 187 System.setProperties(props); 188 } 189 190 try 191 { 192 tfactory = TransformerFactory.newInstance(); 193 } 194 catch (TransformerFactoryConfigurationError pfe) 195 { 196 pfe.printStackTrace(dumpWriter); 197 diagnosticsWriter.println( 198 XSLMessages.createMessage( 199 XSLTErrorResources.ER_NOT_SUCCESSFUL, null)); 201 tfactory = null; 203 doExit(-1); 204 } 205 206 boolean formatOutput = false; 207 boolean useSourceLocation = false; 208 String inFileName = null; 209 String outFileName = null; 210 String dumpFileName = null; 211 String xslFileName = null; 212 String treedumpFileName = null; 213 PrintTraceListener tracer = null; 214 String outputType = null; 215 String media = null; 216 Vector params = new Vector (); 217 boolean quietConflictWarnings = false; 218 URIResolver uriResolver = null; 219 EntityResolver entityResolver = null; 220 ContentHandler contentHandler = null; 221 int recursionLimit=-1; 222 223 for (int i = 0; i < argv.length; i++) 224 { 225 if ("-XSLTC".equalsIgnoreCase(argv[i])) 226 { 227 } 229 else if ("-TT".equalsIgnoreCase(argv[i])) 230 { 231 if (!useXSLTC) 232 { 233 if (null == tracer) 234 tracer = new PrintTraceListener(diagnosticsWriter); 235 236 tracer.m_traceTemplates = true; 237 } 238 else 239 printInvalidXSLTCOption("-TT"); 240 241 } 243 else if ("-TG".equalsIgnoreCase(argv[i])) 244 { 245 if (!useXSLTC) 246 { 247 if (null == tracer) 248 tracer = new PrintTraceListener(diagnosticsWriter); 249 250 tracer.m_traceGeneration = true; 251 } 252 else 253 printInvalidXSLTCOption("-TG"); 254 255 } 257 else if ("-TS".equalsIgnoreCase(argv[i])) 258 { 259 if (!useXSLTC) 260 { 261 if (null == tracer) 262 tracer = new PrintTraceListener(diagnosticsWriter); 263 264 tracer.m_traceSelection = true; 265 } 266 else 267 printInvalidXSLTCOption("-TS"); 268 269 } 271 else if ("-TTC".equalsIgnoreCase(argv[i])) 272 { 273 if (!useXSLTC) 274 { 275 if (null == tracer) 276 tracer = new PrintTraceListener(diagnosticsWriter); 277 278 tracer.m_traceElements = true; 279 } 280 else 281 printInvalidXSLTCOption("-TTC"); 282 283 } 285 else if ("-INDENT".equalsIgnoreCase(argv[i])) 286 { 287 int indentAmount; 288 289 if (((i + 1) < argv.length) && (argv[i + 1].charAt(0) != '-')) 290 { 291 indentAmount = Integer.parseInt(argv[++i]); 292 } 293 else 294 { 295 indentAmount = 0; 296 } 297 298 } 301 else if ("-IN".equalsIgnoreCase(argv[i])) 302 { 303 if (i + 1 < argv.length && argv[i + 1].charAt(0) != '-') 304 inFileName = argv[++i]; 305 else 306 System.err.println( 307 XSLMessages.createMessage( 308 XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION, 309 new Object []{ "-IN" })); } 311 else if ("-MEDIA".equalsIgnoreCase(argv[i])) 312 { 313 if (i + 1 < argv.length) 314 media = argv[++i]; 315 else 316 System.err.println( 317 XSLMessages.createMessage( 318 XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION, 319 new Object []{ "-MEDIA" })); } 321 else if ("-OUT".equalsIgnoreCase(argv[i])) 322 { 323 if (i + 1 < argv.length && argv[i + 1].charAt(0) != '-') 324 outFileName = argv[++i]; 325 else 326 System.err.println( 327 XSLMessages.createMessage( 328 XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION, 329 new Object []{ "-OUT" })); } 331 else if ("-XSL".equalsIgnoreCase(argv[i])) 332 { 333 if (i + 1 < argv.length && argv[i + 1].charAt(0) != '-') 334 xslFileName = argv[++i]; 335 else 336 System.err.println( 337 XSLMessages.createMessage( 338 XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION, 339 new Object []{ "-XSL" })); } 341 else if ("-FLAVOR".equalsIgnoreCase(argv[i])) 342 { 343 if (i + 1 < argv.length) 344 { 345 flavor = argv[++i]; 346 } 347 else 348 System.err.println( 349 XSLMessages.createMessage( 350 XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION, 351 new Object []{ "-FLAVOR" })); } 353 else if ("-PARAM".equalsIgnoreCase(argv[i])) 354 { 355 if (i + 2 < argv.length) 356 { 357 String name = argv[++i]; 358 359 params.addElement(name); 360 361 String expression = argv[++i]; 362 363 params.addElement(expression); 364 } 365 else 366 System.err.println( 367 XSLMessages.createMessage( 368 XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION, 369 new Object []{ "-PARAM" })); } 371 else if ("-E".equalsIgnoreCase(argv[i])) 372 { 373 374 } 377 else if ("-V".equalsIgnoreCase(argv[i])) 378 { 379 diagnosticsWriter.println(resbundle.getString("version") + Version.getVersion() + ", " + 381 382 383 resbundle.getString("version2")); } 385 else if ("-QC".equalsIgnoreCase(argv[i])) 386 { 387 if (!useXSLTC) 388 quietConflictWarnings = true; 389 else 390 printInvalidXSLTCOption("-QC"); 391 } 392 else if ("-Q".equalsIgnoreCase(argv[i])) 393 { 394 setQuietMode = true; 395 } 396 else if ("-DIAG".equalsIgnoreCase(argv[i])) 397 { 398 doDiag = true; 399 } 400 else if ("-XML".equalsIgnoreCase(argv[i])) 401 { 402 outputType = "xml"; 403 } 404 else if ("-TEXT".equalsIgnoreCase(argv[i])) 405 { 406 outputType = "text"; 407 } 408 else if ("-HTML".equalsIgnoreCase(argv[i])) 409 { 410 outputType = "html"; 411 } 412 else if ("-EDUMP".equalsIgnoreCase(argv[i])) 413 { 414 doStackDumpOnError = true; 415 416 if (((i + 1) < argv.length) && (argv[i + 1].charAt(0) != '-')) 417 { 418 dumpFileName = argv[++i]; 419 } 420 } 421 else if ("-URIRESOLVER".equalsIgnoreCase(argv[i])) 422 { 423 if (i + 1 < argv.length) 424 { 425 try 426 { 427 uriResolver = (URIResolver ) ObjectFactory.newInstance( 428 argv[++i], ObjectFactory.findClassLoader(), true); 429 430 tfactory.setURIResolver(uriResolver); 431 } 432 catch (ObjectFactory.ConfigurationError cnfe) 433 { 434 System.err.println( 435 XSLMessages.createMessage( 436 XSLTErrorResources.ER_CLASS_NOT_FOUND_FOR_OPTION, 437 new Object []{ "-URIResolver" })); 438 doExit(-1); 439 } 440 } 441 else 442 { 443 System.err.println( 444 XSLMessages.createMessage( 445 XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION, 446 new Object []{ "-URIResolver" })); doExit(-1); 448 } 449 } 450 else if ("-ENTITYRESOLVER".equalsIgnoreCase(argv[i])) 451 { 452 if (i + 1 < argv.length) 453 { 454 try 455 { 456 entityResolver = (EntityResolver ) ObjectFactory.newInstance( 457 argv[++i], ObjectFactory.findClassLoader(), true); 458 } 459 catch (ObjectFactory.ConfigurationError cnfe) 460 { 461 System.err.println( 462 XSLMessages.createMessage( 463 XSLTErrorResources.ER_CLASS_NOT_FOUND_FOR_OPTION, 464 new Object []{ "-EntityResolver" })); 465 doExit(-1); 466 } 467 } 468 else 469 { 470 System.err.println( 471 XSLMessages.createMessage( 472 XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION, 473 new Object []{ "-EntityResolver" })); doExit(-1); 475 } 476 } 477 else if ("-CONTENTHANDLER".equalsIgnoreCase(argv[i])) 478 { 479 if (i + 1 < argv.length) 480 { 481 try 482 { 483 contentHandler = (ContentHandler ) ObjectFactory.newInstance( 484 argv[++i], ObjectFactory.findClassLoader(), true); 485 } 486 catch (ObjectFactory.ConfigurationError cnfe) 487 { 488 System.err.println( 489 XSLMessages.createMessage( 490 XSLTErrorResources.ER_CLASS_NOT_FOUND_FOR_OPTION, 491 new Object []{ "-ContentHandler" })); 492 doExit(-1); 493 } 494 } 495 else 496 { 497 System.err.println( 498 XSLMessages.createMessage( 499 XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION, 500 new Object []{ "-ContentHandler" })); doExit(-1); 502 } 503 } 504 else if ("-L".equalsIgnoreCase(argv[i])) 505 { 506 if (!useXSLTC) 507 useSourceLocation = true; 508 else 509 printInvalidXSLTCOption("-L"); 510 } 511 else if ("-INCREMENTAL".equalsIgnoreCase(argv[i])) 512 { 513 if (!useXSLTC) 514 tfactory.setAttribute 515 ("http://xml.apache.org/xalan/features/incremental", 516 java.lang.Boolean.TRUE); 517 else 518 printInvalidXSLTCOption("-INCREMENTAL"); 519 } 520 else if ("-NOOPTIMIZE".equalsIgnoreCase(argv[i])) 521 { 522 if (!useXSLTC) 528 tfactory.setAttribute 529 ("http://xml.apache.org/xalan/features/optimize", 530 java.lang.Boolean.FALSE); 531 else 532 printInvalidXSLTCOption("-NOOPTIMIZE"); 533 } 534 else if ("-RL".equalsIgnoreCase(argv[i])) 535 { 536 if (!useXSLTC) 537 { 538 if (i + 1 < argv.length) 539 recursionLimit = Integer.parseInt(argv[++i]); 540 else 541 System.err.println( 542 XSLMessages.createMessage( 543 XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION, 544 new Object []{ "-rl" })); } 546 else 547 { 548 if (i + 1 < argv.length && argv[i + 1].charAt(0) != '-') 549 i++; 550 551 printInvalidXSLTCOption("-RL"); 552 } 553 } 554 else if ("-XO".equalsIgnoreCase(argv[i])) 557 { 558 if (useXSLTC) 559 { 560 if (i + 1 < argv.length && argv[i+1].charAt(0) != '-') 561 { 562 tfactory.setAttribute("generate-translet", "true"); 563 tfactory.setAttribute("translet-name", argv[++i]); 564 } 565 else 566 tfactory.setAttribute("generate-translet", "true"); 567 } 568 else 569 { 570 if (i + 1 < argv.length && argv[i + 1].charAt(0) != '-') 571 i++; 572 printInvalidXalanOption("-XO"); 573 } 574 } 575 else if ("-XD".equalsIgnoreCase(argv[i])) 577 { 578 if (useXSLTC) 579 { 580 if (i + 1 < argv.length && argv[i+1].charAt(0) != '-') 581 tfactory.setAttribute("destination-directory", argv[++i]); 582 else 583 System.err.println( 584 XSLMessages.createMessage( 585 XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION, 586 new Object []{ "-XD" })); 588 } 589 else 590 { 591 if (i + 1 < argv.length && argv[i + 1].charAt(0) != '-') 592 i++; 593 594 printInvalidXalanOption("-XD"); 595 } 596 } 597 else if ("-XJ".equalsIgnoreCase(argv[i])) 599 { 600 if (useXSLTC) 601 { 602 if (i + 1 < argv.length && argv[i+1].charAt(0) != '-') 603 { 604 tfactory.setAttribute("generate-translet", "true"); 605 tfactory.setAttribute("jar-name", argv[++i]); 606 } 607 else 608 System.err.println( 609 XSLMessages.createMessage( 610 XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION, 611 new Object []{ "-XJ" })); } 613 else 614 { 615 if (i + 1 < argv.length && argv[i + 1].charAt(0) != '-') 616 i++; 617 618 printInvalidXalanOption("-XJ"); 619 } 620 621 } 622 else if ("-XP".equalsIgnoreCase(argv[i])) 624 { 625 if (useXSLTC) 626 { 627 if (i + 1 < argv.length && argv[i+1].charAt(0) != '-') 628 tfactory.setAttribute("package-name", argv[++i]); 629 else 630 System.err.println( 631 XSLMessages.createMessage( 632 XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION, 633 new Object []{ "-XP" })); } 635 else 636 { 637 if (i + 1 < argv.length && argv[i + 1].charAt(0) != '-') 638 i++; 639 640 printInvalidXalanOption("-XP"); 641 } 642 643 } 644 else if ("-XN".equalsIgnoreCase(argv[i])) 646 { 647 if (useXSLTC) 648 { 649 tfactory.setAttribute("enable-inlining", "true"); 650 } 651 else 652 printInvalidXalanOption("-XN"); 653 } 654 else if ("-XX".equalsIgnoreCase(argv[i])) 656 { 657 if (useXSLTC) 658 { 659 tfactory.setAttribute("debug", "true"); 660 } 661 else 662 printInvalidXalanOption("-XX"); 663 } 664 else if ("-XT".equalsIgnoreCase(argv[i])) 667 { 668 if (useXSLTC) 669 { 670 tfactory.setAttribute("auto-translet", "true"); 671 } 672 else 673 printInvalidXalanOption("-XT"); 674 } 675 else 676 System.err.println( 677 XSLMessages.createMessage( 678 XSLTErrorResources.ER_INVALID_OPTION, new Object []{ argv[i] })); } 680 681 if (inFileName == null && xslFileName == null) 683 { 684 System.err.println(resbundle.getString("xslProc_no_input")); 685 doExit(-1); 686 } 687 688 try 691 { 692 long start = System.currentTimeMillis(); 693 694 if (null != dumpFileName) 695 { 696 dumpWriter = new PrintWriter (new FileWriter (dumpFileName)); 697 } 698 699 Templates stylesheet = null; 700 701 if (null != xslFileName) 702 { 703 if (flavor.equals("d2d")) 704 { 705 706 DocumentBuilderFactory dfactory = 708 DocumentBuilderFactory.newInstance(); 709 710 dfactory.setNamespaceAware(true); 711 712 DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); 713 Node xslDOM = docBuilder.parse(new InputSource (xslFileName)); 714 715 stylesheet = tfactory.newTemplates(new DOMSource (xslDOM, 716 xslFileName)); 717 } 718 else 719 { 720 stylesheet = tfactory.newTemplates(new StreamSource (xslFileName)); 722 } 724 } 725 726 PrintWriter resultWriter; 727 StreamResult strResult; 728 729 if (null != outFileName) 730 { 731 strResult = new StreamResult (new FileOutputStream (outFileName)); 732 strResult.setSystemId(outFileName); 737 } 738 else 739 { 740 strResult = new StreamResult (System.out); 741 } 746 747 SAXTransformerFactory stf = (SAXTransformerFactory ) tfactory; 748 749 if (!useXSLTC && useSourceLocation) 751 stf.setAttribute(XalanProperties.SOURCE_LOCATION, Boolean.TRUE); 752 753 if (null == stylesheet) 756 { 757 Source source = 758 stf.getAssociatedStylesheet(new StreamSource (inFileName), media, 759 null, null); 760 761 if (null != source) 762 stylesheet = tfactory.newTemplates(source); 763 else 764 { 765 if (null != media) 766 throw new TransformerException (XSLMessages.createMessage(XSLTErrorResources.ER_NO_STYLESHEET_IN_MEDIA, new Object []{inFileName, media})); else 770 throw new TransformerException (XSLMessages.createMessage(XSLTErrorResources.ER_NO_STYLESHEET_PI, new Object []{inFileName})); } 773 } 774 775 if (null != stylesheet) 776 { 777 Transformer transformer = flavor.equals("th") ? null : stylesheet.newTransformer(); 778 779 if (null != outputType) 781 { 782 transformer.setOutputProperty(OutputKeys.METHOD, outputType); 783 } 784 785 if (transformer instanceof org.apache.xalan.transformer.TransformerImpl) 786 { 787 org.apache.xalan.transformer.TransformerImpl impl = (org.apache.xalan.transformer.TransformerImpl)transformer; 788 TraceManager tm = impl.getTraceManager(); 789 790 if (null != tracer) 791 tm.addTraceListener(tracer); 792 793 impl.setQuietConflictWarnings(quietConflictWarnings); 794 795 if (useSourceLocation) 797 impl.setProperty(XalanProperties.SOURCE_LOCATION, Boolean.TRUE); 798 799 if(recursionLimit>0) 800 impl.setRecursionLimit(recursionLimit); 801 802 } 805 806 int nParams = params.size(); 807 808 for (int i = 0; i < nParams; i += 2) 809 { 810 transformer.setParameter((String ) params.elementAt(i), 811 (String ) params.elementAt(i + 1)); 812 } 813 814 if (uriResolver != null) 815 transformer.setURIResolver(uriResolver); 816 817 if (null != inFileName) 818 { 819 if (flavor.equals("d2d")) 820 { 821 822 DocumentBuilderFactory dfactory = 824 DocumentBuilderFactory.newInstance(); 825 826 dfactory.setCoalescing(true); 827 dfactory.setNamespaceAware(true); 828 829 DocumentBuilder docBuilder = dfactory.newDocumentBuilder(); 830 831 if (entityResolver != null) 832 docBuilder.setEntityResolver(entityResolver); 833 834 Node xmlDoc = docBuilder.parse(new InputSource (inFileName)); 835 Document doc = docBuilder.newDocument(); 836 org.w3c.dom.DocumentFragment outNode = 837 doc.createDocumentFragment(); 838 839 transformer.transform(new DOMSource (xmlDoc, inFileName), 840 new DOMResult (outNode)); 841 842 Transformer serializer = stf.newTransformer(); 844 Properties serializationProps = 845 stylesheet.getOutputProperties(); 846 847 serializer.setOutputProperties(serializationProps); 848 849 if (contentHandler != null) 850 { 851 SAXResult result = new SAXResult (contentHandler); 852 853 serializer.transform(new DOMSource (outNode), result); 854 } 855 else 856 serializer.transform(new DOMSource (outNode), strResult); 857 } 858 else if (flavor.equals("th")) 859 { 860 for (int i = 0; i < 1; i++) { 862 864 XMLReader reader = null; 866 867 try 869 { 870 javax.xml.parsers.SAXParserFactory factory = 871 javax.xml.parsers.SAXParserFactory.newInstance(); 872 873 factory.setNamespaceAware(true); 874 875 javax.xml.parsers.SAXParser jaxpParser = 876 factory.newSAXParser(); 877 878 reader = jaxpParser.getXMLReader(); 879 } 880 catch (javax.xml.parsers.ParserConfigurationException ex) 881 { 882 throw new org.xml.sax.SAXException (ex); 883 } 884 catch (javax.xml.parsers.FactoryConfigurationError ex1) 885 { 886 throw new org.xml.sax.SAXException (ex1.toString()); 887 } 888 catch (NoSuchMethodError ex2){} 889 catch (AbstractMethodError ame){} 890 891 if (null == reader) 892 { 893 reader = XMLReaderFactory.createXMLReader(); 894 } 895 896 if (!useXSLTC) 897 stf.setAttribute(org.apache.xalan.processor.TransformerFactoryImpl.FEATURE_INCREMENTAL, 898 Boolean.TRUE); 899 900 TransformerHandler th = stf.newTransformerHandler(stylesheet); 901 902 reader.setContentHandler(th); 903 reader.setDTDHandler(th); 904 905 if(th instanceof org.xml.sax.ErrorHandler ) 906 reader.setErrorHandler((org.xml.sax.ErrorHandler )th); 907 908 try 909 { 910 reader.setProperty( 911 "http://xml.org/sax/properties/lexical-handler", th); 912 } 913 catch (org.xml.sax.SAXNotRecognizedException e){} 914 catch (org.xml.sax.SAXNotSupportedException e){} 915 try 916 { 917 reader.setFeature("http://xml.org/sax/features/namespace-prefixes", 918 true); 919 } catch (org.xml.sax.SAXException se) {} 920 921 th.setResult(strResult); 922 923 reader.parse(new InputSource (inFileName)); 924 } 925 } 926 else 927 { 928 if (entityResolver != null) 929 { 930 XMLReader reader = null; 931 932 try 934 { 935 javax.xml.parsers.SAXParserFactory factory = 936 javax.xml.parsers.SAXParserFactory.newInstance(); 937 938 factory.setNamespaceAware(true); 939 940 javax.xml.parsers.SAXParser jaxpParser = 941 factory.newSAXParser(); 942 943 reader = jaxpParser.getXMLReader(); 944 } 945 catch (javax.xml.parsers.ParserConfigurationException ex) 946 { 947 throw new org.xml.sax.SAXException (ex); 948 } 949 catch (javax.xml.parsers.FactoryConfigurationError ex1) 950 { 951 throw new org.xml.sax.SAXException (ex1.toString()); 952 } 953 catch (NoSuchMethodError ex2){} 954 catch (AbstractMethodError ame){} 955 956 if (null == reader) 957 { 958 reader = XMLReaderFactory.createXMLReader(); 959 } 960 961 reader.setEntityResolver(entityResolver); 962 963 if (contentHandler != null) 964 { 965 SAXResult result = new SAXResult (contentHandler); 966 967 transformer.transform( 968 new SAXSource (reader, new InputSource (inFileName)), 969 result); 970 } 971 else 972 { 973 transformer.transform( 974 new SAXSource (reader, new InputSource (inFileName)), 975 strResult); 976 } 977 } 978 else if (contentHandler != null) 979 { 980 SAXResult result = new SAXResult (contentHandler); 981 982 transformer.transform(new StreamSource (inFileName), result); 983 } 984 else 985 { 986 transformer.transform(new StreamSource (inFileName), 988 strResult); 989 } 991 } 992 } 993 else 994 { 995 StringReader reader = 996 new StringReader ("<?xml version=\"1.0\"?> <doc/>"); 997 998 transformer.transform(new StreamSource (reader), strResult); 999 } 1000 } 1001 else 1002 { 1003 diagnosticsWriter.println( 1004 XSLMessages.createMessage( 1005 XSLTErrorResources.ER_NOT_SUCCESSFUL, null)); doExit(-1); 1007 } 1008 1009 if (null != outFileName && strResult!=null) 1011 { 1012 java.io.OutputStream out = strResult.getOutputStream(); 1013 java.io.Writer writer = strResult.getWriter(); 1014 try 1015 { 1016 if (out != null) out.close(); 1017 if (writer != null) writer.close(); 1018 } 1019 catch(java.io.IOException ie) {} 1020 } 1021 1022 long stop = System.currentTimeMillis(); 1023 long millisecondsDuration = stop - start; 1024 1025 if (doDiag) 1026 { 1027 Object [] msgArgs = new Object []{ inFileName, xslFileName, new Long (millisecondsDuration) }; 1028 String msg = XSLMessages.createMessage("diagTiming", msgArgs); 1029 diagnosticsWriter.println('\n'); 1030 diagnosticsWriter.println(msg); 1031 } 1032 1033 } 1034 catch (Throwable throwable) 1035 { 1036 while (throwable 1037 instanceof org.apache.xml.utils.WrappedRuntimeException) 1038 { 1039 throwable = 1040 ((org.apache.xml.utils.WrappedRuntimeException) throwable).getException(); 1041 } 1042 1043 if ((throwable instanceof NullPointerException ) 1044 || (throwable instanceof ClassCastException )) 1045 doStackDumpOnError = true; 1046 1047 diagnosticsWriter.println(); 1048 1049 if (doStackDumpOnError) 1050 throwable.printStackTrace(dumpWriter); 1051 else 1052 { 1053 DefaultErrorHandler.printLocation(diagnosticsWriter, throwable); 1054 diagnosticsWriter.println( 1055 XSLMessages.createMessage(XSLTErrorResources.ER_XSLT_ERROR, null) 1056 + " (" + throwable.getClass().getName() + "): " 1057 + throwable.getMessage()); 1058 } 1059 1060 if (null != dumpFileName) 1062 { 1063 dumpWriter.close(); 1064 } 1065 1066 doExit(-1); 1067 } 1068 1069 if (null != dumpFileName) 1070 { 1071 dumpWriter.close(); 1072 } 1073 1074 if (null != diagnosticsWriter) 1075 { 1076 1077 } 1079 1080 } 1085 } 1086 1087 1091 static void doExit(int i) 1092 { 1093 System.exit(i); 1094 } 1095 1096 1101 private static void waitForReturnKey(ResourceBundle resbundle) 1102 { 1103 System.out.println(resbundle.getString("xslProc_return_to_continue")); 1104 try 1105 { 1106 while (System.in.read() != '\n'); 1107 } 1108 catch (java.io.IOException e) { } 1109 } 1110 1111 1116 private static void printInvalidXSLTCOption(String option) 1117 { 1118 System.err.println(XSLMessages.createMessage("xslProc_invalid_xsltc_option", new Object []{option})); 1119 } 1120 1121 1126 private static void printInvalidXalanOption(String option) 1127 { 1128 System.err.println(XSLMessages.createMessage("xslProc_invalid_xalan_option", new Object []{option})); 1129 } 1130} 1131 | Popular Tags |