1. /*
  2. * Copyright 1999-2004 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.sun.org.apache.xalan.internal.xslt;
  17. import java.io.FileOutputStream;
  18. import java.io.FileWriter;
  19. import java.io.PrintWriter;
  20. import java.io.StringReader;
  21. import java.util.Properties;
  22. import java.util.ResourceBundle;
  23. import java.util.Vector;
  24. import javax.xml.parsers.DocumentBuilder;
  25. import javax.xml.parsers.DocumentBuilderFactory;
  26. import javax.xml.transform.OutputKeys;
  27. import javax.xml.transform.Source;
  28. import javax.xml.transform.Templates;
  29. import javax.xml.transform.Transformer;
  30. import javax.xml.transform.TransformerException;
  31. import javax.xml.transform.TransformerFactory;
  32. import javax.xml.transform.TransformerFactoryConfigurationError;
  33. import javax.xml.transform.URIResolver;
  34. import javax.xml.transform.dom.DOMResult;
  35. import javax.xml.transform.dom.DOMSource;
  36. import javax.xml.transform.sax.SAXResult;
  37. import javax.xml.transform.sax.SAXSource;
  38. import javax.xml.transform.sax.SAXTransformerFactory;
  39. import javax.xml.transform.sax.TransformerHandler;
  40. import javax.xml.transform.stream.StreamResult;
  41. import javax.xml.transform.stream.StreamSource;
  42. import com.sun.org.apache.xalan.internal.Version;
  43. import com.sun.org.apache.xalan.internal.res.XSLMessages;
  44. import com.sun.org.apache.xalan.internal.res.XSLTErrorResources;
  45. import com.sun.org.apache.xml.internal.utils.DefaultErrorHandler;
  46. import org.w3c.dom.Document;
  47. import org.w3c.dom.Node;
  48. import org.xml.sax.ContentHandler;
  49. import org.xml.sax.EntityResolver;
  50. import org.xml.sax.InputSource;
  51. import org.xml.sax.XMLReader;
  52. import org.xml.sax.helpers.XMLReaderFactory;
  53. /**
  54. * The main() method handles the Xalan command-line interface.
  55. * @xsl.usage general
  56. */
  57. public class Process
  58. {
  59. /**
  60. * Prints argument options.
  61. *
  62. * @param resbundle Resource bundle
  63. */
  64. protected static void printArgOptions(ResourceBundle resbundle)
  65. {
  66. System.out.println(resbundle.getString("xslProc_option")); //"xslproc options: ");
  67. System.out.println("\n\t\t\t" + resbundle.getString("xslProc_common_options") + "\n");
  68. System.out.println(resbundle.getString("optionXSLTC")); //" [-XSLTC (use XSLTC for transformation)]
  69. System.out.println(resbundle.getString("optionIN")); //" [-IN inputXMLURL]");
  70. System.out.println(resbundle.getString("optionXSL")); //" [-XSL XSLTransformationURL]");
  71. System.out.println(resbundle.getString("optionOUT")); //" [-OUT outputFileName]");
  72. // System.out.println(resbundle.getString("optionE")); //" [-E (Do not expand entity refs)]");
  73. System.out.println(resbundle.getString("optionV")); //" [-V (Version info)]");
  74. // System.out.println(resbundle.getString("optionVALIDATE")); //" [-VALIDATE (Set whether validation occurs. Validation is off by default.)]");
  75. System.out.println(resbundle.getString("optionEDUMP")); //" [-EDUMP {optional filename} (Do stackdump on error.)]");
  76. System.out.println(resbundle.getString("optionXML")); //" [-XML (Use XML formatter and add XML header.)]");
  77. System.out.println(resbundle.getString("optionTEXT")); //" [-TEXT (Use simple Text formatter.)]");
  78. System.out.println(resbundle.getString("optionHTML")); //" [-HTML (Use HTML formatter.)]");
  79. System.out.println(resbundle.getString("optionPARAM")); //" [-PARAM name expression (Set a stylesheet parameter)]");
  80. System.out.println(resbundle.getString("optionMEDIA"));
  81. System.out.println(resbundle.getString("optionFLAVOR"));
  82. System.out.println(resbundle.getString("optionDIAG"));
  83. System.out.println(resbundle.getString("optionURIRESOLVER")); //" [-URIRESOLVER full class name (URIResolver to be used to resolve URIs)]");
  84. System.out.println(resbundle.getString("optionENTITYRESOLVER")); //" [-ENTITYRESOLVER full class name (EntityResolver to be used to resolve entities)]");
  85. waitForReturnKey(resbundle);
  86. System.out.println(resbundle.getString("optionCONTENTHANDLER")); //" [-CONTENTHANDLER full class name (ContentHandler to be used to serialize output)]");
  87. System.out.println("\n\t\t\t" + resbundle.getString("xslProc_xsltc_options") + "\n");
  88. System.out.println(resbundle.getString("optionXO"));
  89. System.out.println(resbundle.getString("optionXD"));
  90. waitForReturnKey(resbundle);
  91. System.out.println(resbundle.getString("optionXJ"));
  92. System.out.println(resbundle.getString("optionXP"));
  93. System.out.println(resbundle.getString("optionXN"));
  94. System.out.println(resbundle.getString("optionXX"));
  95. System.out.println(resbundle.getString("optionXT"));
  96. }
  97. /**
  98. * Command line interface to transform an XML document according to
  99. * the instructions found in an XSL stylesheet.
  100. * <p>The Process class provides basic functionality for
  101. * performing transformations from the command line. To see a
  102. * list of arguments supported, call with zero arguments.</p>
  103. * <p>To set stylesheet parameters from the command line, use
  104. * <code>-PARAM name expression</code>. If you want to set the
  105. * parameter to a string value, simply pass the string value
  106. * as-is, and it will be interpreted as a string. (Note: if
  107. * the value has spaces in it, you may need to quote it depending
  108. * on your shell environment).</p>
  109. *
  110. * @param argv Input parameters from command line
  111. */
  112. public static void _main(String argv[])
  113. {
  114. boolean doStackDumpOnError = false;
  115. boolean setQuietMode = false;
  116. boolean doDiag = false;
  117. /**
  118. * The default diagnostic writer...
  119. */
  120. java.io.PrintWriter diagnosticsWriter = new PrintWriter(System.err, true);
  121. java.io.PrintWriter dumpWriter = diagnosticsWriter;
  122. ResourceBundle resbundle =
  123. (XSLMessages.loadResourceBundle(
  124. com.sun.org.apache.xml.internal.utils.res.XResourceBundle.ERROR_RESOURCES));
  125. String flavor = "s2s";
  126. if (argv.length < 1)
  127. {
  128. printArgOptions(resbundle);
  129. }
  130. else
  131. {
  132. // since xsltc is the default transformer
  133. boolean useXSLTC = true;
  134. for (int i = 0; i < argv.length; i++)
  135. {
  136. if ("-XSLTC".equalsIgnoreCase(argv[i]))
  137. {
  138. useXSLTC = true;
  139. }
  140. }
  141. TransformerFactory tfactory;
  142. // Since xsltc is the default transformer
  143. String key = "javax.xml.transform.TransformerFactory";
  144. String value = "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl";
  145. Properties props = System.getProperties();
  146. props.put(key, value);
  147. System.setProperties(props);
  148. try
  149. {
  150. tfactory = TransformerFactory.newInstance();
  151. }
  152. catch (TransformerFactoryConfigurationError pfe)
  153. {
  154. pfe.printStackTrace(dumpWriter);
  155. diagnosticsWriter.println(
  156. XSLMessages.createMessage(
  157. XSLTErrorResources.ER_NOT_SUCCESSFUL, null)); //"XSL Process was not successful.");
  158. tfactory = null; // shut up compiler
  159. doExit(-1);
  160. }
  161. boolean formatOutput = false;
  162. boolean useSourceLocation = false;
  163. String inFileName = null;
  164. String outFileName = null;
  165. String dumpFileName = null;
  166. String xslFileName = null;
  167. String treedumpFileName = null;
  168. String outputType = null;
  169. String media = null;
  170. Vector params = new Vector();
  171. boolean quietConflictWarnings = false;
  172. URIResolver uriResolver = null;
  173. EntityResolver entityResolver = null;
  174. ContentHandler contentHandler = null;
  175. int recursionLimit=-1;
  176. for (int i = 0; i < argv.length; i++)
  177. {
  178. if ("-XSLTC".equalsIgnoreCase(argv[i]))
  179. {
  180. // The -XSLTC option has been processed.
  181. }
  182. else if ("-INDENT".equalsIgnoreCase(argv[i]))
  183. {
  184. int indentAmount;
  185. if (((i + 1) < argv.length) && (argv[i + 1].charAt(0) != '-'))
  186. {
  187. indentAmount = Integer.parseInt(argv[++i]);
  188. }
  189. else
  190. {
  191. indentAmount = 0;
  192. }
  193. // TBD:
  194. // xmlProcessorLiaison.setIndent(indentAmount);
  195. }
  196. else if ("-IN".equalsIgnoreCase(argv[i]))
  197. {
  198. if (i + 1 < argv.length && argv[i + 1].charAt(0) != '-')
  199. inFileName = argv[++i];
  200. else
  201. System.err.println(
  202. XSLMessages.createMessage(
  203. XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,
  204. new Object[]{ "-IN" })); //"Missing argument for);
  205. }
  206. else if ("-MEDIA".equalsIgnoreCase(argv[i]))
  207. {
  208. if (i + 1 < argv.length)
  209. media = argv[++i];
  210. else
  211. System.err.println(
  212. XSLMessages.createMessage(
  213. XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,
  214. new Object[]{ "-MEDIA" })); //"Missing argument for);
  215. }
  216. else if ("-OUT".equalsIgnoreCase(argv[i]))
  217. {
  218. if (i + 1 < argv.length && argv[i + 1].charAt(0) != '-')
  219. outFileName = argv[++i];
  220. else
  221. System.err.println(
  222. XSLMessages.createMessage(
  223. XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,
  224. new Object[]{ "-OUT" })); //"Missing argument for);
  225. }
  226. else if ("-XSL".equalsIgnoreCase(argv[i]))
  227. {
  228. if (i + 1 < argv.length && argv[i + 1].charAt(0) != '-')
  229. xslFileName = argv[++i];
  230. else
  231. System.err.println(
  232. XSLMessages.createMessage(
  233. XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,
  234. new Object[]{ "-XSL" })); //"Missing argument for);
  235. }
  236. else if ("-FLAVOR".equalsIgnoreCase(argv[i]))
  237. {
  238. if (i + 1 < argv.length)
  239. {
  240. flavor = argv[++i];
  241. }
  242. else
  243. System.err.println(
  244. XSLMessages.createMessage(
  245. XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,
  246. new Object[]{ "-FLAVOR" })); //"Missing argument for);
  247. }
  248. else if ("-PARAM".equalsIgnoreCase(argv[i]))
  249. {
  250. if (i + 2 < argv.length)
  251. {
  252. String name = argv[++i];
  253. params.addElement(name);
  254. String expression = argv[++i];
  255. params.addElement(expression);
  256. }
  257. else
  258. System.err.println(
  259. XSLMessages.createMessage(
  260. XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,
  261. new Object[]{ "-PARAM" })); //"Missing argument for);
  262. }
  263. else if ("-E".equalsIgnoreCase(argv[i]))
  264. {
  265. // TBD:
  266. // xmlProcessorLiaison.setShouldExpandEntityRefs(false);
  267. }
  268. else if ("-V".equalsIgnoreCase(argv[i]))
  269. {
  270. diagnosticsWriter.println(resbundle.getString("version") //">>>>>>> Xalan Version "
  271. + Version.getVersion() + ", " +
  272. resbundle.getString("version2")); // "<<<<<<<");
  273. }
  274. else if ("-Q".equalsIgnoreCase(argv[i]))
  275. {
  276. setQuietMode = true;
  277. }
  278. else if ("-DIAG".equalsIgnoreCase(argv[i]))
  279. {
  280. doDiag = true;
  281. }
  282. else if ("-XML".equalsIgnoreCase(argv[i]))
  283. {
  284. outputType = "xml";
  285. }
  286. else if ("-TEXT".equalsIgnoreCase(argv[i]))
  287. {
  288. outputType = "text";
  289. }
  290. else if ("-HTML".equalsIgnoreCase(argv[i]))
  291. {
  292. outputType = "html";
  293. }
  294. else if ("-EDUMP".equalsIgnoreCase(argv[i]))
  295. {
  296. doStackDumpOnError = true;
  297. if (((i + 1) < argv.length) && (argv[i + 1].charAt(0) != '-'))
  298. {
  299. dumpFileName = argv[++i];
  300. }
  301. }
  302. else if ("-URIRESOLVER".equalsIgnoreCase(argv[i]))
  303. {
  304. if (i + 1 < argv.length)
  305. {
  306. try
  307. {
  308. uriResolver = (URIResolver) ObjectFactory.newInstance(
  309. argv[++i], ObjectFactory.findClassLoader(), true);
  310. tfactory.setURIResolver(uriResolver);
  311. }
  312. catch (ObjectFactory.ConfigurationError cnfe)
  313. {
  314. System.err.println(
  315. XSLMessages.createMessage(
  316. XSLTErrorResources.ER_CLASS_NOT_FOUND_FOR_OPTION,
  317. new Object[]{ "-URIResolver" }));
  318. doExit(-1);
  319. }
  320. }
  321. else
  322. {
  323. System.err.println(
  324. XSLMessages.createMessage(
  325. XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,
  326. new Object[]{ "-URIResolver" })); //"Missing argument for);
  327. doExit(-1);
  328. }
  329. }
  330. else if ("-ENTITYRESOLVER".equalsIgnoreCase(argv[i]))
  331. {
  332. if (i + 1 < argv.length)
  333. {
  334. try
  335. {
  336. entityResolver = (EntityResolver) ObjectFactory.newInstance(
  337. argv[++i], ObjectFactory.findClassLoader(), true);
  338. }
  339. catch (ObjectFactory.ConfigurationError cnfe)
  340. {
  341. System.err.println(
  342. XSLMessages.createMessage(
  343. XSLTErrorResources.ER_CLASS_NOT_FOUND_FOR_OPTION,
  344. new Object[]{ "-EntityResolver" }));
  345. doExit(-1);
  346. }
  347. }
  348. else
  349. {
  350. System.err.println(
  351. XSLMessages.createMessage(
  352. XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,
  353. new Object[]{ "-EntityResolver" })); //"Missing argument for);
  354. doExit(-1);
  355. }
  356. }
  357. else if ("-CONTENTHANDLER".equalsIgnoreCase(argv[i]))
  358. {
  359. if (i + 1 < argv.length)
  360. {
  361. try
  362. {
  363. contentHandler = (ContentHandler) ObjectFactory.newInstance(
  364. argv[++i], ObjectFactory.findClassLoader(), true);
  365. }
  366. catch (ObjectFactory.ConfigurationError cnfe)
  367. {
  368. System.err.println(
  369. XSLMessages.createMessage(
  370. XSLTErrorResources.ER_CLASS_NOT_FOUND_FOR_OPTION,
  371. new Object[]{ "-ContentHandler" }));
  372. doExit(-1);
  373. }
  374. }
  375. else
  376. {
  377. System.err.println(
  378. XSLMessages.createMessage(
  379. XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,
  380. new Object[]{ "-ContentHandler" })); //"Missing argument for);
  381. doExit(-1);
  382. }
  383. }
  384. // Generate the translet class and optionally specify the name
  385. // of the translet class.
  386. else if ("-XO".equalsIgnoreCase(argv[i]))
  387. {
  388. if (useXSLTC)
  389. {
  390. if (i + 1 < argv.length && argv[i+1].charAt(0) != '-')
  391. {
  392. tfactory.setAttribute("generate-translet", "true");
  393. tfactory.setAttribute("translet-name", argv[++i]);
  394. }
  395. else
  396. tfactory.setAttribute("generate-translet", "true");
  397. }
  398. else
  399. {
  400. if (i + 1 < argv.length && argv[i + 1].charAt(0) != '-')
  401. i++;
  402. printInvalidXalanOption("-XO");
  403. }
  404. }
  405. // Specify the destination directory for the translet classes.
  406. else if ("-XD".equalsIgnoreCase(argv[i]))
  407. {
  408. if (useXSLTC)
  409. {
  410. if (i + 1 < argv.length && argv[i+1].charAt(0) != '-')
  411. tfactory.setAttribute("destination-directory", argv[++i]);
  412. else
  413. System.err.println(
  414. XSLMessages.createMessage(
  415. XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,
  416. new Object[]{ "-XD" })); //"Missing argument for);
  417. }
  418. else
  419. {
  420. if (i + 1 < argv.length && argv[i + 1].charAt(0) != '-')
  421. i++;
  422. printInvalidXalanOption("-XD");
  423. }
  424. }
  425. // Specify the jar file name which the translet classes are packaged into.
  426. else if ("-XJ".equalsIgnoreCase(argv[i]))
  427. {
  428. if (useXSLTC)
  429. {
  430. if (i + 1 < argv.length && argv[i+1].charAt(0) != '-')
  431. {
  432. tfactory.setAttribute("generate-translet", "true");
  433. tfactory.setAttribute("jar-name", argv[++i]);
  434. }
  435. else
  436. System.err.println(
  437. XSLMessages.createMessage(
  438. XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,
  439. new Object[]{ "-XJ" })); //"Missing argument for);
  440. }
  441. else
  442. {
  443. if (i + 1 < argv.length && argv[i + 1].charAt(0) != '-')
  444. i++;
  445. printInvalidXalanOption("-XJ");
  446. }
  447. }
  448. // Specify the package name prefix for the generated translet classes.
  449. else if ("-XP".equalsIgnoreCase(argv[i]))
  450. {
  451. if (useXSLTC)
  452. {
  453. if (i + 1 < argv.length && argv[i+1].charAt(0) != '-')
  454. tfactory.setAttribute("package-name", argv[++i]);
  455. else
  456. System.err.println(
  457. XSLMessages.createMessage(
  458. XSLTErrorResources.ER_MISSING_ARG_FOR_OPTION,
  459. new Object[]{ "-XP" })); //"Missing argument for);
  460. }
  461. else
  462. {
  463. if (i + 1 < argv.length && argv[i + 1].charAt(0) != '-')
  464. i++;
  465. printInvalidXalanOption("-XP");
  466. }
  467. }
  468. // Enable template inlining.
  469. else if ("-XN".equalsIgnoreCase(argv[i]))
  470. {
  471. if (useXSLTC)
  472. {
  473. tfactory.setAttribute("enable-inlining", "true");
  474. }
  475. else
  476. printInvalidXalanOption("-XN");
  477. }
  478. // Turns on additional debugging message output
  479. else if ("-XX".equalsIgnoreCase(argv[i]))
  480. {
  481. if (useXSLTC)
  482. {
  483. tfactory.setAttribute("debug", "true");
  484. }
  485. else
  486. printInvalidXalanOption("-XX");
  487. }
  488. // Create the Transformer from the translet if the translet class is newer
  489. // than the stylesheet.
  490. else if ("-XT".equalsIgnoreCase(argv[i]))
  491. {
  492. if (useXSLTC)
  493. {
  494. tfactory.setAttribute("auto-translet", "true");
  495. }
  496. else
  497. printInvalidXalanOption("-XT");
  498. }
  499. else
  500. System.err.println(
  501. XSLMessages.createMessage(
  502. XSLTErrorResources.ER_INVALID_OPTION, new Object[]{ argv[i] })); //"Invalid argument:);
  503. }
  504. // Print usage instructions if no xml and xsl file is specified in the command line
  505. if (inFileName == null && xslFileName == null)
  506. {
  507. System.err.println(resbundle.getString("xslProc_no_input"));
  508. doExit(-1);
  509. }
  510. // Note that there are usage cases for calling us without a -IN arg
  511. // The main XSL transformation occurs here!
  512. try
  513. {
  514. long start = System.currentTimeMillis();
  515. if (null != dumpFileName)
  516. {
  517. dumpWriter = new PrintWriter(new FileWriter(dumpFileName));
  518. }
  519. Templates stylesheet = null;
  520. if (null != xslFileName)
  521. {
  522. if (flavor.equals("d2d"))
  523. {
  524. // Parse in the xml data into a DOM
  525. DocumentBuilderFactory dfactory =
  526. DocumentBuilderFactory.newInstance();
  527. dfactory.setNamespaceAware(true);
  528. DocumentBuilder docBuilder = dfactory.newDocumentBuilder();
  529. Node xslDOM = docBuilder.parse(new InputSource(xslFileName));
  530. stylesheet = tfactory.newTemplates(new DOMSource(xslDOM,
  531. xslFileName));
  532. }
  533. else
  534. {
  535. stylesheet = tfactory.newTemplates(new StreamSource(xslFileName));
  536. }
  537. }
  538. PrintWriter resultWriter;
  539. StreamResult strResult;
  540. if (null != outFileName)
  541. {
  542. strResult = new StreamResult(new FileOutputStream(outFileName));
  543. // One possible improvement might be to ensure this is
  544. // a valid URI before setting the systemId, but that
  545. // might have subtle changes that pre-existing users
  546. // might notice; we can think about that later -sc r1.46
  547. strResult.setSystemId(outFileName);
  548. }
  549. else
  550. {
  551. strResult = new StreamResult(System.out);
  552. // We used to default to incremental mode in this case.
  553. // We've since decided that since the -INCREMENTAL switch is
  554. // available, that default is probably not necessary nor
  555. // necessarily a good idea.
  556. }
  557. SAXTransformerFactory stf = (SAXTransformerFactory) tfactory;
  558. // Did they pass in a stylesheet, or should we get it from the
  559. // document?
  560. if (null == stylesheet)
  561. {
  562. Source source =
  563. stf.getAssociatedStylesheet(new StreamSource(inFileName), media,
  564. null, null);
  565. if (null != source)
  566. stylesheet = tfactory.newTemplates(source);
  567. else
  568. {
  569. if (null != media)
  570. throw new TransformerException(XSLMessages.createMessage(XSLTErrorResources.ER_NO_STYLESHEET_IN_MEDIA, new Object[]{inFileName, media})); //"No stylesheet found in: "
  571. // + inFileName + ", media="
  572. // + media);
  573. else
  574. throw new TransformerException(XSLMessages.createMessage(XSLTErrorResources.ER_NO_STYLESHEET_PI, new Object[]{inFileName})); //"No xml-stylesheet PI found in: "
  575. //+ inFileName);
  576. }
  577. }
  578. if (null != stylesheet)
  579. {
  580. Transformer transformer = flavor.equals("th") ? null : stylesheet.newTransformer();
  581. // Override the output format?
  582. if (null != outputType)
  583. {
  584. transformer.setOutputProperty(OutputKeys.METHOD, outputType);
  585. }
  586. int nParams = params.size();
  587. for (int i = 0; i < nParams; i += 2)
  588. {
  589. transformer.setParameter((String) params.elementAt(i),
  590. (String) params.elementAt(i + 1));
  591. }
  592. if (uriResolver != null)
  593. transformer.setURIResolver(uriResolver);
  594. if (null != inFileName)
  595. {
  596. if (flavor.equals("d2d"))
  597. {
  598. // Parse in the xml data into a DOM
  599. DocumentBuilderFactory dfactory =
  600. DocumentBuilderFactory.newInstance();
  601. dfactory.setCoalescing(true);
  602. dfactory.setNamespaceAware(true);
  603. DocumentBuilder docBuilder = dfactory.newDocumentBuilder();
  604. if (entityResolver != null)
  605. docBuilder.setEntityResolver(entityResolver);
  606. Node xmlDoc = docBuilder.parse(new InputSource(inFileName));
  607. Document doc = docBuilder.newDocument();
  608. org.w3c.dom.DocumentFragment outNode =
  609. doc.createDocumentFragment();
  610. transformer.transform(new DOMSource(xmlDoc, inFileName),
  611. new DOMResult(outNode));
  612. // Now serialize output to disk with identity transformer
  613. Transformer serializer = stf.newTransformer();
  614. Properties serializationProps =
  615. stylesheet.getOutputProperties();
  616. serializer.setOutputProperties(serializationProps);
  617. if (contentHandler != null)
  618. {
  619. SAXResult result = new SAXResult(contentHandler);
  620. serializer.transform(new DOMSource(outNode), result);
  621. }
  622. else
  623. serializer.transform(new DOMSource(outNode), strResult);
  624. }
  625. else if (flavor.equals("th"))
  626. {
  627. for (int i = 0; i < 1; i++) // Loop for diagnosing bugs with inconsistent behavior
  628. {
  629. // System.out.println("Testing the TransformerHandler...");
  630. // ===============
  631. XMLReader reader = null;
  632. // Use JAXP1.1 ( if possible )
  633. try
  634. {
  635. javax.xml.parsers.SAXParserFactory factory =
  636. javax.xml.parsers.SAXParserFactory.newInstance();
  637. factory.setNamespaceAware(true);
  638. javax.xml.parsers.SAXParser jaxpParser =
  639. factory.newSAXParser();
  640. reader = jaxpParser.getXMLReader();
  641. }
  642. catch (javax.xml.parsers.ParserConfigurationException ex)
  643. {
  644. throw new org.xml.sax.SAXException(ex);
  645. }
  646. catch (javax.xml.parsers.FactoryConfigurationError ex1)
  647. {
  648. throw new org.xml.sax.SAXException(ex1.toString());
  649. }
  650. catch (NoSuchMethodError ex2){}
  651. catch (AbstractMethodError ame){}
  652. if (null == reader)
  653. {
  654. reader = XMLReaderFactory.createXMLReader();
  655. }
  656. TransformerHandler th = stf.newTransformerHandler(stylesheet);
  657. reader.setContentHandler(th);
  658. reader.setDTDHandler(th);
  659. if(th instanceof org.xml.sax.ErrorHandler)
  660. reader.setErrorHandler((org.xml.sax.ErrorHandler)th);
  661. try
  662. {
  663. reader.setProperty(
  664. "http://xml.org/sax/properties/lexical-handler", th);
  665. }
  666. catch (org.xml.sax.SAXNotRecognizedException e){}
  667. catch (org.xml.sax.SAXNotSupportedException e){}
  668. try
  669. {
  670. reader.setFeature("http://xml.org/sax/features/namespace-prefixes",
  671. true);
  672. } catch (org.xml.sax.SAXException se) {}
  673. th.setResult(strResult);
  674. reader.parse(new InputSource(inFileName));
  675. }
  676. }
  677. else
  678. {
  679. if (entityResolver != null)
  680. {
  681. XMLReader reader = null;
  682. // Use JAXP1.1 ( if possible )
  683. try
  684. {
  685. javax.xml.parsers.SAXParserFactory factory =
  686. javax.xml.parsers.SAXParserFactory.newInstance();
  687. factory.setNamespaceAware(true);
  688. javax.xml.parsers.SAXParser jaxpParser =
  689. factory.newSAXParser();
  690. reader = jaxpParser.getXMLReader();
  691. }
  692. catch (javax.xml.parsers.ParserConfigurationException ex)
  693. {
  694. throw new org.xml.sax.SAXException(ex);
  695. }
  696. catch (javax.xml.parsers.FactoryConfigurationError ex1)
  697. {
  698. throw new org.xml.sax.SAXException(ex1.toString());
  699. }
  700. catch (NoSuchMethodError ex2){}
  701. catch (AbstractMethodError ame){}
  702. if (null == reader)
  703. {
  704. reader = XMLReaderFactory.createXMLReader();
  705. }
  706. reader.setEntityResolver(entityResolver);
  707. if (contentHandler != null)
  708. {
  709. SAXResult result = new SAXResult(contentHandler);
  710. transformer.transform(
  711. new SAXSource(reader, new InputSource(inFileName)),
  712. result);
  713. }
  714. else
  715. {
  716. transformer.transform(
  717. new SAXSource(reader, new InputSource(inFileName)),
  718. strResult);
  719. }
  720. }
  721. else if (contentHandler != null)
  722. {
  723. SAXResult result = new SAXResult(contentHandler);
  724. transformer.transform(new StreamSource(inFileName), result);
  725. }
  726. else
  727. {
  728. transformer.transform(new StreamSource(inFileName),
  729. strResult);
  730. }
  731. }
  732. }
  733. else
  734. {
  735. StringReader reader =
  736. new StringReader("<?xml version=\"1.0\"?> <doc/>");
  737. transformer.transform(new StreamSource(reader), strResult);
  738. }
  739. }
  740. else
  741. {
  742. diagnosticsWriter.println(
  743. XSLMessages.createMessage(
  744. XSLTErrorResources.ER_NOT_SUCCESSFUL, null)); //"XSL Process was not successful.");
  745. doExit(-1);
  746. }
  747. // close output streams
  748. if (null != outFileName && strResult!=null)
  749. {
  750. java.io.OutputStream out = strResult.getOutputStream();
  751. java.io.Writer writer = strResult.getWriter();
  752. try
  753. {
  754. if (out != null) out.close();
  755. if (writer != null) writer.close();
  756. }
  757. catch(java.io.IOException ie) {}
  758. }
  759. long stop = System.currentTimeMillis();
  760. long millisecondsDuration = stop - start;
  761. if (doDiag)
  762. {
  763. Object[] msgArgs = new Object[]{ inFileName, xslFileName, new Long(millisecondsDuration) };
  764. String msg = XSLMessages.createMessage("diagTiming", msgArgs);
  765. diagnosticsWriter.println('\n');
  766. diagnosticsWriter.println(msg);
  767. }
  768. }
  769. catch (Throwable throwable)
  770. {
  771. while (throwable
  772. instanceof com.sun.org.apache.xml.internal.utils.WrappedRuntimeException)
  773. {
  774. throwable =
  775. ((com.sun.org.apache.xml.internal.utils.WrappedRuntimeException) throwable).getException();
  776. }
  777. if ((throwable instanceof NullPointerException)
  778. || (throwable instanceof ClassCastException))
  779. doStackDumpOnError = true;
  780. diagnosticsWriter.println();
  781. if (doStackDumpOnError)
  782. throwable.printStackTrace(dumpWriter);
  783. else
  784. {
  785. DefaultErrorHandler.printLocation(diagnosticsWriter, throwable);
  786. diagnosticsWriter.println(
  787. XSLMessages.createMessage(XSLTErrorResources.ER_XSLT_ERROR, null)
  788. + " (" + throwable.getClass().getName() + "): "
  789. + throwable.getMessage());
  790. }
  791. // diagnosticsWriter.println(XSLMessages.createMessage(XSLTErrorResources.ER_NOT_SUCCESSFUL, null)); //"XSL Process was not successful.");
  792. if (null != dumpFileName)
  793. {
  794. dumpWriter.close();
  795. }
  796. doExit(-1);
  797. }
  798. if (null != dumpFileName)
  799. {
  800. dumpWriter.close();
  801. }
  802. if (null != diagnosticsWriter)
  803. {
  804. // diagnosticsWriter.close();
  805. }
  806. }
  807. }
  808. /** It is _much_ easier to debug under VJ++ if I can set a single breakpoint
  809. * before this blows itself out of the water...
  810. * (I keep checking this in, it keeps vanishing. Grr!)
  811. * */
  812. static void doExit(int i)
  813. {
  814. System.exit(i);
  815. }
  816. /**
  817. * Wait for a return key to continue
  818. *
  819. * @param resbundle The resource bundle
  820. */
  821. private static void waitForReturnKey(ResourceBundle resbundle)
  822. {
  823. System.out.println(resbundle.getString("xslProc_return_to_continue"));
  824. try
  825. {
  826. while (System.in.read() != '\n');
  827. }
  828. catch (java.io.IOException e) { }
  829. }
  830. /**
  831. * Print a message if an option cannot be used with -XSLTC.
  832. *
  833. * @param option The option String
  834. */
  835. private static void printInvalidXSLTCOption(String option)
  836. {
  837. System.err.println(XSLMessages.createMessage("xslProc_invalid_xsltc_option", new Object[]{option}));
  838. }
  839. /**
  840. * Print a message if an option can only be used with -XSLTC.
  841. *
  842. * @param option The option String
  843. */
  844. private static void printInvalidXalanOption(String option)
  845. {
  846. System.err.println(XSLMessages.createMessage("xslProc_invalid_xalan_option", new Object[]{option}));
  847. }
  848. }