1 package com.icl.saxon; 2 import com.icl.saxon.om.*; 3 import com.icl.saxon.tree.TreeBuilder; 4 import com.icl.saxon.tree.DocumentImpl; 5 import com.icl.saxon.tree.NodeFactory; 6 import com.icl.saxon.style.StyleNodeFactory; 7 import com.icl.saxon.style.StyleElement; 8 import com.icl.saxon.style.LiteralResultElement; 9 import com.icl.saxon.style.XSLStyleSheet; 10 import com.icl.saxon.output.Outputter; 11 12 import java.util.Properties ; 13 14 import javax.xml.transform.*; 15 import javax.xml.transform.sax.SAXSource ; 16 import javax.xml.transform.dom.DOMSource ; 17 18 import org.w3c.dom.Document ; 19 import org.xml.sax.SAXParseException ; 20 21 25 26 public class PreparedStyleSheet implements Templates { 27 28 private DocumentImpl styleDoc; 29 private TransformerFactoryImpl factory; 30 private NamePool namePool; 31 private StyleNodeFactory nodeFactory; 32 private int errorCount = 0; 33 34 37 38 protected PreparedStyleSheet(TransformerFactoryImpl factory) { 39 this.factory = factory; 40 } 41 42 45 46 public Transformer newTransformer() { 47 Controller c = new Controller(factory); 48 c.setPreparedStyleSheet(this); 49 return c; 50 } 51 52 55 56 public TransformerFactoryImpl getTransformerFactory() { 57 return factory; 58 } 59 60 63 64 public void setNamePool(NamePool pool) { 65 namePool = pool; 66 } 67 68 71 72 public NamePool getNamePool() { 73 return namePool; 74 } 75 76 79 80 public StyleNodeFactory getStyleNodeFactory() { 81 return nodeFactory; 82 } 83 84 87 88 protected void prepare(SAXSource styleSource) throws TransformerConfigurationException { 89 90 if (namePool==null || namePool.isSealed()) { 91 namePool = NamePool.getDefaultNamePool(); 92 } 93 94 nodeFactory = new StyleNodeFactory(namePool); 95 97 StylesheetStripper styleStripper = new StylesheetStripper(); 98 styleStripper.setStylesheetRules(namePool); 99 100 TreeBuilder styleBuilder = new TreeBuilder(); 101 styleBuilder.setNamePool(namePool); 102 styleBuilder.setErrorListener(factory.getErrorListener()); 103 styleBuilder.setStripper(styleStripper); 104 styleBuilder.setSystemId(styleSource.getSystemId()); 105 styleBuilder.setNodeFactory(nodeFactory); 106 styleBuilder.setDiscardCommentsAndPIs(true); 107 styleBuilder.setLineNumbering(true); 108 109 111 DocumentImpl doc; 112 try { 113 doc = (DocumentImpl)styleBuilder.build(styleSource); 114 } catch (TransformerException err) { 115 Throwable cause = err.getException(); 116 if (cause != null) { 117 if (cause instanceof SAXParseException ) { 118 throw new TransformerConfigurationException("Failed to parse stylesheet"); 120 } else if (cause instanceof TransformerConfigurationException) { 121 throw (TransformerConfigurationException)cause; 122 } else { 123 throw new TransformerConfigurationException(cause); 124 } 125 } 126 throw new TransformerConfigurationException(err); 127 } 128 129 if (doc.getDocumentElement()==null) { 130 throw new TransformerConfigurationException("Stylesheet is empty or absent"); 131 } 132 133 setStyleSheetDocument(doc); 134 135 if (errorCount > 0) { 136 throw new TransformerConfigurationException( 137 "Failed to compile stylesheet. " + 138 errorCount + 139 (errorCount==1 ? " error " : " errors ") + 140 "detected."); 141 } 142 143 } 144 145 146 150 151 protected void setStyleSheetDocument(DocumentImpl doc) 152 throws TransformerConfigurationException { 153 154 styleDoc = doc; 155 namePool = doc.getNamePool(); 156 nodeFactory = new StyleNodeFactory(namePool); 158 159 161 StyleElement topnode = (StyleElement)styleDoc.getDocumentElement(); 162 if (topnode instanceof LiteralResultElement) { 163 styleDoc = ((LiteralResultElement)topnode).makeStyleSheet(this); 164 } 165 166 if (!(styleDoc.getDocumentElement() instanceof XSLStyleSheet)) { 167 throw new TransformerConfigurationException( 168 "Top-level element of stylesheet is not xsl:stylesheet or xsl:transform or literal result element"); 169 } 170 171 XSLStyleSheet top = (XSLStyleSheet)styleDoc.getDocumentElement(); 172 173 175 top.setPreparedStyleSheet(this); 176 top.preprocess(); 177 } 178 179 182 183 public DocumentImpl getStyleSheetDocument() { 184 return styleDoc; 185 } 186 187 197 198 public Properties getOutputProperties() { 199 200 Properties defaults = new Properties (); 201 defaults.put(OutputKeys.ENCODING, "utf-8"); 204 defaults.put(OutputKeys.OMIT_XML_DECLARATION, "no"); 206 defaults.put(OutputKeys.CDATA_SECTION_ELEMENTS, ""); 210 213 Properties details = new Properties (defaults); 214 ((XSLStyleSheet)styleDoc.getDocumentElement()).gatherOutputProperties(details); 215 return details; 216 } 217 218 222 223 public void reportError(TransformerException err) throws TransformerException { 224 errorCount++; 225 factory.getErrorListener().error(err); 226 } 227 228 232 233 public DocumentInfo stripWhitespace(Document doc) throws TransformerException { 234 XSLStyleSheet top = (XSLStyleSheet)styleDoc.getDocumentElement(); 235 if (top.stripsWhitespace() || (!(doc instanceof DocumentInfo))) { 236 Builder b = ((Controller)newTransformer()).makeBuilder(); 237 b.setNamePool(namePool); 238 return b.build(factory.getSAXSource(new DOMSource (doc), false)); 239 } else { 240 return (DocumentInfo)doc; 241 } 242 } 243 } 244 245 | Popular Tags |