1 package net.sf.saxon; 2 import net.sf.saxon.event.CommentStripper; 4 import net.sf.saxon.event.PipelineConfiguration; 5 import net.sf.saxon.event.Sender; 6 import net.sf.saxon.event.StartTagBuffer; 7 import net.sf.saxon.instruct.Executable; 8 import net.sf.saxon.om.NamePool; 9 import net.sf.saxon.om.Validation; 10 import net.sf.saxon.style.*; 11 import net.sf.saxon.trans.StaticError; 12 import net.sf.saxon.trans.XPathException; 13 import net.sf.saxon.tree.DocumentImpl; 14 import net.sf.saxon.tree.TreeBuilder; 15 import org.xml.sax.XMLReader ; 16 17 import javax.xml.transform.*; 18 import java.io.FileInputStream ; 19 import java.io.IOException ; 20 import java.io.ObjectInputStream ; 21 import java.io.Serializable ; 22 import java.math.BigDecimal ; 23 import java.util.Properties ; 24 25 29 30 public class PreparedStylesheet implements Templates, Serializable { 31 32 private Executable executable; 33 private transient Configuration config; 34 private NamePool targetNamePool; private transient StyleNodeFactory nodeFactory; 37 private int errorCount = 0; 38 39 44 45 protected PreparedStylesheet(Configuration config) { 46 this.config = config; 47 } 48 49 55 56 public Transformer newTransformer() { 57 Controller c = new Controller(config, executable); 58 c.setPreparedStylesheet(this); 59 return c; 60 } 61 62 public void setConfiguration(Configuration config) { 63 this.config = config; 64 } 65 66 public Configuration getConfiguration() { 67 return config; 68 } 69 70 73 74 public void setTargetNamePool(NamePool pool) { 75 targetNamePool = pool; 76 } 77 78 84 85 public NamePool getTargetNamePool() { 86 if (targetNamePool==null) { 87 return config.getNamePool(); 88 } else { 89 return targetNamePool; 90 } 91 } 92 93 99 100 public StyleNodeFactory getStyleNodeFactory() { 101 return nodeFactory; 102 } 103 104 111 112 protected void prepare(Source styleSource) throws TransformerConfigurationException { 113 nodeFactory = new StyleNodeFactory(config); 114 DocumentImpl doc; 115 try { 116 doc = loadStylesheetModule(styleSource, config, config.getNamePool(), nodeFactory); 117 setStylesheetDocument(doc, nodeFactory); 118 } catch (XPathException e) { 119 if (!e.hasBeenReported()) { 120 try { 121 config.getErrorListener().fatalError(e); 122 } catch (TransformerException e1) { 123 } 125 } 126 if (errorCount==0) { 127 errorCount++; 128 } 129 } 130 131 if (errorCount > 0) { 132 throw new TransformerConfigurationException( 133 "Failed to compile stylesheet. " + 134 errorCount + 135 (errorCount==1 ? " error " : " errors ") + 136 "detected."); 137 } 138 } 139 140 153 public static DocumentImpl loadStylesheetModule( 154 Source styleSource, 155 Configuration config, 156 NamePool localNamePool, 157 StyleNodeFactory nodeFactory) 158 throws XPathException { 159 160 TreeBuilder styleBuilder = new TreeBuilder(); 161 PipelineConfiguration pipe = config.makePipelineConfiguration(); 162 styleBuilder.setPipelineConfiguration(pipe); 163 styleBuilder.setSystemId(styleSource.getSystemId()); 164 styleBuilder.setNodeFactory(nodeFactory); 165 styleBuilder.setLineNumbering(true); 166 167 StartTagBuffer startTagBuffer = new StartTagBuffer(); 168 169 UseWhenFilter useWhenFilter = new UseWhenFilter(startTagBuffer); 170 useWhenFilter.setUnderlyingReceiver(styleBuilder); 171 172 startTagBuffer.setUnderlyingReceiver(useWhenFilter); 173 174 StylesheetStripper styleStripper = new StylesheetStripper(); 175 styleStripper.setStylesheetRules(localNamePool); 176 styleStripper.setUnderlyingReceiver(startTagBuffer); 177 178 CommentStripper commentStripper = new CommentStripper(); 179 commentStripper.setUnderlyingReceiver(styleStripper); 180 181 183 DocumentImpl doc; 184 185 Sender sender = new Sender(pipe); 186 AugmentedSource aug = AugmentedSource.makeAugmentedSource(styleSource); 187 aug.setSchemaValidationMode(Validation.STRIP); 188 if (aug.getXMLReader() == null) { 189 XMLReader styleParser = config.getStyleParser(); 190 aug.setXMLReader(styleParser); 191 sender.send(aug, commentStripper); 192 config.reuseStyleParser(styleParser); 193 } else { 194 sender.send(aug, commentStripper); 195 } 196 doc = (DocumentImpl)styleBuilder.getCurrentRoot(); 197 198 return doc; 199 200 } 201 202 212 213 public static PreparedStylesheet loadCompiledStylesheet(Configuration config, String fileName) 214 throws IOException , ClassNotFoundException { 215 ObjectInputStream ois = new ObjectInputStream (new FileInputStream (fileName)); 216 PreparedStylesheet sheet = (PreparedStylesheet)ois.readObject(); 217 ois.close(); 218 NamePool compiledNamePool = sheet.getTargetNamePool(); 219 sheet.setConfiguration(config); 220 sheet.getExecutable().setConfiguration(config); 221 config.setNamePool(compiledNamePool); 222 NamePool.setDefaultNamePool(compiledNamePool); 223 return sheet; 224 } 225 226 235 236 protected void setStylesheetDocument(DocumentImpl doc, StyleNodeFactory snFactory) 237 throws XPathException { 238 239 DocumentImpl styleDoc = doc; 240 nodeFactory = snFactory; 241 242 244 StyleElement topnode = (StyleElement)styleDoc.getDocumentElement(); 245 if (topnode instanceof LiteralResultElement) { 246 styleDoc = ((LiteralResultElement)topnode).makeStylesheet(this, snFactory); 247 } 248 249 if (!(styleDoc.getDocumentElement() instanceof XSLStylesheet)) { 250 throw new StaticError( 251 "Outermost element of stylesheet is not xsl:stylesheet or xsl:transform or literal result element"); 252 } 253 254 XSLStylesheet top = (XSLStylesheet)styleDoc.getDocumentElement(); 255 if (config.isVersionWarning() && top.getVersion().equals(BigDecimal.valueOf(1))) { 256 try { 257 config.getErrorListener().warning( 258 new TransformerException("Running an XSLT 1.0 stylesheet with an XSLT 2.0 processor") 259 ); 260 } catch (TransformerException e) { 261 throw StaticError.makeStaticError(e); 262 } 263 } 264 265 267 top.setPreparedStylesheet(this); 268 try { 269 top.preprocess(); 270 } catch (XPathException e) { 271 Throwable e2 = e.getException(); 272 if (e2 instanceof XPathException && !((XPathException)e2).hasBeenReported()) { 273 try { 275 config.getErrorListener().fatalError((XPathException)e2); 276 } catch (TransformerException e1) { 277 } 279 } 280 throw e; 281 } 282 283 285 executable = top.compileStylesheet(); 286 } 287 288 293 294 public Executable getExecutable() { 295 return executable; 296 } 297 298 317 318 319 public Properties getOutputProperties() { 320 Properties details = executable.getDefaultOutputProperties(); 321 return new Properties (details); 322 } 323 324 332 333 public void reportError(TransformerException err) throws TransformerException { 334 errorCount++; 335 config.getErrorListener().fatalError(err); 336 } 337 338 343 344 public int getErrorCount() { 345 return errorCount; 346 } 347 348 355 356 public void reportWarning(TransformerException err) { 357 try { 358 config.getErrorListener().warning(err); 359 } catch (TransformerException err2) {} 360 } 361 362 } 363 364 | Popular Tags |