PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/saxonB/net/sf/saxon/Compile.java

https://bitbucket.org/dmwelch/phdxnat_pipeline
Java | 319 lines | 176 code | 50 blank | 93 comment | 36 complexity | 048dea39d5e30576b568365d378dab85 MD5 | raw file
  1. package net.sf.saxon;
  2. import net.sf.saxon.instruct.TerminationException;
  3. import net.sf.saxon.trans.XPathException;
  4. import org.xml.sax.InputSource;
  5. import javax.xml.transform.Source;
  6. import javax.xml.transform.Templates;
  7. import javax.xml.transform.TransformerConfigurationException;
  8. import javax.xml.transform.TransformerFactoryConfigurationError;
  9. import javax.xml.transform.sax.SAXSource;
  10. import java.io.*;
  11. import java.util.Date;
  12. /**
  13. * This <B>Compile</B> class provides a command-line interface allowing a
  14. * stylesheet to be compiled.<p>
  15. *
  16. * @author M.H.Kay
  17. */
  18. public class Compile {
  19. private TransformerFactoryImpl factory = new TransformerFactoryImpl();
  20. private boolean showTime = false;
  21. private boolean debug = false;
  22. /**
  23. * Main program, can be used directly from the command line.
  24. * <p>The format is:</P>
  25. * <p>java net.sf.saxon.Compile [options] <I>style-file</I> <I>output-file</I></P>
  26. * <p>This program compiles the XSL style sheet in style-file to the output-file.</p>
  27. *
  28. * @param args Arguments supplied on the command line
  29. * @exception java.lang.Exception Any compilation error occurs
  30. */
  31. public static void main (String args[])
  32. throws java.lang.Exception
  33. {
  34. // the real work is delegated to another routine so that it can be used in a subclass
  35. (new Compile()).doMain(args);
  36. }
  37. /**
  38. * Support method for main program. This support method can also be invoked from subclasses
  39. * that support the same command line interface
  40. *
  41. * @param args the command-line arguments
  42. */
  43. protected void doMain(String args[]) {
  44. String styleFileName;
  45. boolean useURLs = false;
  46. String outputFileName;
  47. // Check the command-line arguments.
  48. try {
  49. int i = 0;
  50. while (true) {
  51. if (i>=args.length) badUsage("No stylesheet file name");
  52. if (args[i].charAt(0)=='-') {
  53. if (args[i].equals("-u")) {
  54. useURLs = true;
  55. i++;
  56. }
  57. else if (args[i].equals("-t")) {
  58. System.err.println(factory.getConfiguration().getProductTitle());
  59. //System.err.println("Java version " + System.getProperty("java.version"));
  60. System.err.println(Configuration.getPlatform().getPlatformVersion());
  61. factory.setAttribute(
  62. FeatureKeys.TIMING,
  63. Boolean.TRUE);
  64. //Loader.setTracing(true);
  65. showTime = true;
  66. i++;
  67. }
  68. else if (args[i].equals("-y")) {
  69. i++;
  70. if (args.length < i+2) badUsage("No style parser class");
  71. String styleParserName = args[i++];
  72. factory.setAttribute(
  73. FeatureKeys.STYLE_PARSER_CLASS,
  74. styleParserName);
  75. }
  76. else if (args[i].equals("-r")) {
  77. i++;
  78. if (args.length < i+2) badUsage("No URIResolver class");
  79. String r = args[i++];
  80. factory.setURIResolver(factory.getConfiguration().makeURIResolver(r));
  81. }
  82. else if (args[i].equals("-debug")) {
  83. i++;
  84. debug = true;
  85. }
  86. else if (args[i].equals("-1.1")) { // XML 1.1
  87. i++;
  88. factory.setAttribute(FeatureKeys.XML_VERSION, "1.1");
  89. }
  90. else badUsage("Unknown option " + args[i]);
  91. }
  92. else break;
  93. }
  94. if (args.length < i+1 ) badUsage("No stylesheet file name");
  95. styleFileName = args[i++];
  96. if (args.length < i+1 ) badUsage("No output file name");
  97. outputFileName = args[i++];
  98. long startTime = (new Date()).getTime();
  99. Source styleSource;
  100. if (useURLs || styleFileName.startsWith("http:")
  101. || styleFileName.startsWith("file:")) {
  102. styleSource = factory.getURIResolver().resolve(styleFileName, null);
  103. if (styleSource == null) {
  104. styleSource = factory.getConfiguration().getSystemURIResolver().resolve(styleFileName, null);
  105. }
  106. } else {
  107. File sheetFile = new File(styleFileName);
  108. if (!sheetFile.exists()) {
  109. quit("Stylesheet file " + sheetFile + " does not exist", 2);
  110. }
  111. InputSource eis = new InputSource(sheetFile.toURI().toString());
  112. styleSource = new SAXSource(factory.getConfiguration().getStyleParser(), eis);
  113. }
  114. if (styleSource==null) {
  115. quit("URIResolver for stylesheet file must return a Source", 2);
  116. }
  117. Templates sheet = factory.newTemplates(styleSource);
  118. if (showTime) {
  119. long endTime = (new Date()).getTime();
  120. System.err.println("Stylesheet compilation time: " + (endTime-startTime) + " milliseconds");
  121. }
  122. try {
  123. String msg = ((PreparedStylesheet)sheet).getExecutable().getReasonUnableToCompile();
  124. if (msg != null) {
  125. System.err.println(msg);
  126. quit("Unable to compile stylesheet", 2);
  127. }
  128. System.err.println("Serializing compiled stylesheet");
  129. ((PreparedStylesheet)sheet).setTargetNamePool(
  130. ((PreparedStylesheet)sheet).getConfiguration().getNamePool());
  131. OutputStream fos = new FileOutputStream(outputFileName);
  132. if (debug) {
  133. fos = new TracingObjectOutputStream(fos);
  134. }
  135. ObjectOutputStream oos = new ObjectOutputStream(fos);
  136. //noinspection RedundantCast
  137. oos.writeObject((PreparedStylesheet)sheet);
  138. oos.close();
  139. System.err.println("Finished serializing stylesheet");
  140. } catch (Exception err) {
  141. err.printStackTrace();
  142. }
  143. } catch (TerminationException err) {
  144. quit(err.getMessage(), 1);
  145. } catch (XPathException err) {
  146. quit("Stylesheet compilation failed: " + err.getMessage(), 2);
  147. } catch (TransformerConfigurationException err) {
  148. quit("Stylesheet compilation failed: " + err.getMessage(), 2);
  149. } catch (TransformerFactoryConfigurationError err) {
  150. quit("Stylesheet compilation failed: " + err.getMessage(), 2);
  151. } catch (Exception err2) {
  152. err2.printStackTrace();
  153. }
  154. }
  155. /**
  156. * Exit with a message
  157. *
  158. * @param message Message to be output
  159. * @param code Result code to be returned to the operating system
  160. */
  161. protected static void quit(String message, int code) {
  162. System.err.println(message);
  163. System.exit(code);
  164. }
  165. /** Output error message when incorrect command line options/arguments are used
  166. *
  167. * @param message Error message to be displayed
  168. */
  169. protected void badUsage(String message) {
  170. System.err.println(message);
  171. System.err.println(factory.getConfiguration().getProductTitle());
  172. System.err.println("Usage: java net.sf.saxon.Compile [options] stylesheet-file output-file");
  173. System.err.println("Options: ");
  174. System.err.println(" -r classname Use specified URIResolver class");
  175. System.err.println(" -t Display version and timing information");
  176. System.err.println(" -u Names are URLs not filenames");
  177. System.err.println(" -y classname Use specified SAX parser for stylesheet");
  178. System.err.println(" -debug Produce trace output to diagnose failures");
  179. System.err.println(" -1.1 Allow XML 1.1 documents");
  180. System.err.println(" -? Display this message ");
  181. System.exit(2);
  182. }
  183. /**
  184. * Tracing version of ObjectOutputStream for diagnostics
  185. */
  186. private static class TracingObjectOutputStream extends FilterOutputStream {
  187. OutputStream oos;
  188. public TracingObjectOutputStream(OutputStream oos) {
  189. super(oos);
  190. this.oos = oos;
  191. }
  192. public void write(byte b[]) throws IOException {
  193. char[] chars = new char[b.length];
  194. for (int i=0; i<b.length; i++) {
  195. chars[i] = (char)b[i];
  196. }
  197. String s = new String(chars);
  198. if (s.indexOf("saxon") >= 0) {
  199. System.err.println("write byte[]: " + s);
  200. }
  201. super.write(b);
  202. }
  203. /**
  204. * Writes <code>len</code> bytes from the specified
  205. * <code>byte</code> array starting at offset <code>off</code> to
  206. * this output stream.
  207. * <p/>
  208. * The <code>write</code> method of <code>FilterOutputStream</code>
  209. * calls the <code>write</code> method of one argument on each
  210. * <code>byte</code> to output.
  211. * <p/>
  212. * Note that this method does not call the <code>write</code> method
  213. * of its underlying input stream with the same arguments. Subclasses
  214. * of <code>FilterOutputStream</code> should provide a more efficient
  215. * implementation of this method.
  216. *
  217. * @param b the data.
  218. * @param off the start offset in the data.
  219. * @param len the number of bytes to write.
  220. * @throws java.io.IOException if an I/O error occurs.
  221. * @see java.io.FilterOutputStream#write(int)
  222. */
  223. public void write(byte b[], int off, int len) throws IOException {
  224. char[] chars = new char[len];
  225. for (int i=0; i<len; i++) {
  226. chars[i] = (char)b[i+off];
  227. }
  228. String s = new String(chars);
  229. if (s.indexOf("saxon") >= 0) {
  230. System.err.println("write byte[]: " + s);
  231. }
  232. super.write(b, off, len);
  233. }
  234. /**
  235. * Writes the specified <code>byte</code> to this output stream.
  236. * <p/>
  237. * The <code>write</code> method of <code>FilterOutputStream</code>
  238. * calls the <code>write</code> method of its underlying output stream,
  239. * that is, it performs <tt>out.write(b)</tt>.
  240. * <p/>
  241. * Implements the abstract <tt>write</tt> method of <tt>OutputStream</tt>.
  242. *
  243. * @param b the <code>byte</code>.
  244. * @throws java.io.IOException if an I/O error occurs.
  245. */
  246. // public void write(int b) throws IOException {
  247. // char c = (char)b;
  248. // System.err.println("write byte: " + c);
  249. // super.write(b);
  250. // }
  251. }
  252. }
  253. //
  254. // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
  255. // you may not use this file except in compliance with the License. You may obtain a copy of the
  256. // License at http://www.mozilla.org/MPL/
  257. //
  258. // Software distributed under the License is distributed on an "AS IS" basis,
  259. // WITHOUT WARRANTY OF ANY KIND, either express or implied.
  260. // See the License for the specific language governing rights and limitations under the License.
  261. //
  262. // The Original Code is: all this file.
  263. //
  264. // The Initial Developer of the Original Code is Michael H. Kay.
  265. //
  266. // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
  267. //
  268. // Contributor(s): none.
  269. //