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 6 import javax.xml.transform.Source ; 7 import javax.xml.transform.Templates ; 8 import javax.xml.transform.TransformerFactoryConfigurationError ; 9 import javax.xml.transform.sax.SAXSource ; 10 import java.io.*; 11 import java.util.Date ; 12 13 19 20 public class Compile { 21 22 private TransformerFactoryImpl factory = new TransformerFactoryImpl(); 23 24 private boolean showTime = false; 25 private boolean debug = false; 26 27 36 37 public static void main (String args[]) 38 throws java.lang.Exception 39 { 40 (new Compile()).doMain(args); 42 } 43 44 50 51 protected void doMain(String args[]) { 52 53 String styleFileName; 54 boolean useURLs = false; 55 String outputFileName; 56 57 59 try { 60 int i = 0; 61 while (true) { 62 if (i>=args.length) badUsage("No stylesheet file name"); 63 64 if (args[i].charAt(0)=='-') { 65 66 if (args[i].equals("-u")) { 67 useURLs = true; 68 i++; 69 } 70 71 else if (args[i].equals("-t")) { 72 System.err.println(factory.getConfiguration().getProductTitle()); 73 System.err.println("Java version " + System.getProperty("java.version")); 74 factory.setAttribute( 75 FeatureKeys.TIMING, 76 Boolean.TRUE); 77 78 showTime = true; 80 i++; 81 } 82 83 else if (args[i].equals("-y")) { 84 i++; 85 if (args.length < i+2) badUsage("No style parser class"); 86 String styleParserName = args[i++]; 87 factory.setAttribute( 88 FeatureKeys.STYLE_PARSER_CLASS, 89 styleParserName); 90 } 91 92 else if (args[i].equals("-r")) { 93 i++; 94 if (args.length < i+2) badUsage("No URIResolver class"); 95 String r = args[i++]; 96 factory.setURIResolver(factory.getConfiguration().makeURIResolver(r)); 97 } 98 99 else if (args[i].equals("-debug")) { 100 i++; 101 debug = true; 102 } 103 104 else badUsage("Unknown option " + args[i]); 105 } 106 107 else break; 108 } 109 110 if (args.length < i+1 ) badUsage("No stylesheet file name"); 111 styleFileName = args[i++]; 112 113 if (args.length < i+1 ) badUsage("No output file name"); 114 outputFileName = args[i++]; 115 116 117 long startTime = (new Date ()).getTime(); 118 119 Source styleSource; 120 if (useURLs || styleFileName.startsWith("http:") 121 || styleFileName.startsWith("file:")) { 122 styleSource = factory.getURIResolver().resolve(styleFileName, null); 123 if (styleSource == null) { 124 styleSource = factory.getConfiguration().getSystemURIResolver().resolve(styleFileName, null); 125 } 126 127 } else { 128 File sheetFile = new File(styleFileName); 129 if (!sheetFile.exists()) { 130 quit("Stylesheet file " + sheetFile + " does not exist", 2); 131 } 132 InputSource eis = new InputSource (sheetFile.toURL().toString()); 133 styleSource = new SAXSource (factory.getConfiguration().getStyleParser(), eis); 134 } 135 136 if (styleSource==null) { 137 quit("URIResolver for stylesheet file must return a Source", 2); 138 } 139 140 Templates sheet = factory.newTemplates(styleSource); 141 142 if (showTime) { 143 long endTime = (new Date ()).getTime(); 144 System.err.println("Stylesheet compilation time: " + (endTime-startTime) + " milliseconds"); 145 } 146 147 try { 148 String msg = ((PreparedStylesheet)sheet).getExecutable().getReasonUnableToCompile(); 149 if (msg != null) { 150 System.err.println(msg); 151 quit("Unable to compile stylesheet", 2); 152 } 153 System.err.println("Serializing compiled stylesheet"); 154 ((PreparedStylesheet)sheet).setTargetNamePool( 155 ((PreparedStylesheet)sheet).getConfiguration().getNamePool()); 156 OutputStream fos = new FileOutputStream(outputFileName); 157 if (debug) { 158 fos = new TracingObjectOutputStream(fos); 159 } 160 ObjectOutputStream oos = new ObjectOutputStream(fos); 161 oos.writeObject(sheet); 162 oos.close(); 163 System.err.println("Finished serializing stylesheet"); 164 } catch (Exception err) { 165 err.printStackTrace(); 166 } 167 168 } catch (TerminationException err) { 169 quit(err.getMessage(), 1); 170 } catch (XPathException err) { 171 quit("Transformation failed: " + err.getMessage(), 2); 172 } catch (TransformerFactoryConfigurationError err) { 173 quit("Transformation failed: " + err.getMessage(), 2); 174 } catch (Exception err2) { 175 err2.printStackTrace(); 176 } 177 178 } 179 180 186 187 protected static void quit(String message, int code) { 188 System.err.println(message); 189 System.exit(code); 190 } 191 192 193 197 protected void badUsage(String message) { 198 System.err.println(message); 199 System.err.println(factory.getConfiguration().getProductTitle()); 200 System.err.println("Usage: java net.sf.saxon.Compile [options] stylesheet-file output-file"); 201 System.err.println("Options: "); 202 System.err.println(" -r classname Use specified URIResolver class"); 203 System.err.println(" -t Display version and timing information"); 204 System.err.println(" -u Names are URLs not filenames"); 205 System.err.println(" -y classname Use specified SAX parser for stylesheet"); 206 System.err.println(" -debug Produce trace output to diagnose failures"); 207 System.err.println(" -? Display this message "); 208 System.exit(2); 209 } 210 211 214 215 private static class TracingObjectOutputStream extends FilterOutputStream { 216 217 OutputStream oos; 218 public TracingObjectOutputStream(OutputStream oos) { 219 super(oos); 220 this.oos = oos; 221 } 222 223 public void write(byte b[]) throws IOException { 224 char[] chars = new char[b.length]; 225 for (int i=0; i<b.length; i++) { 226 chars[i] = (char)b[i]; 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); 233 } 234 235 255 public void write(byte b[], int off, int len) throws IOException { 256 char[] chars = new char[len]; 257 for (int i=0; i<len; i++) { 258 chars[i] = (char)b[i+off]; 259 } 260 String s = new String (chars); 261 if (s.indexOf("saxon") >= 0) { 262 System.err.println("write byte[]: " + s); 263 } 264 super.write(b, off, len); 265 } 266 267 279 285 } 286 287 } 288 289 | Popular Tags |