1 16 19 20 package org.apache.xalan.xsltc.trax; 21 22 import javax.xml.transform.Source ; 23 import javax.xml.transform.Templates ; 24 import javax.xml.transform.TransformerException ; 25 import javax.xml.transform.URIResolver ; 26 import javax.xml.transform.sax.TemplatesHandler ; 27 28 import org.apache.xalan.xsltc.compiler.CompilerException; 29 import org.apache.xalan.xsltc.compiler.Parser; 30 import org.apache.xalan.xsltc.compiler.SourceLoader; 31 import org.apache.xalan.xsltc.compiler.Stylesheet; 32 import org.apache.xalan.xsltc.compiler.SyntaxTreeNode; 33 import org.apache.xalan.xsltc.compiler.XSLTC; 34 import org.apache.xalan.xsltc.compiler.util.ErrorMsg; 35 36 import org.xml.sax.ContentHandler ; 37 import org.xml.sax.InputSource ; 38 import org.xml.sax.Locator ; 39 import org.xml.sax.SAXException ; 40 import org.xml.sax.Attributes ; 41 42 import java.util.Vector ; 43 44 49 public class TemplatesHandlerImpl 50 implements ContentHandler , TemplatesHandler , SourceLoader 51 { 52 55 private String _systemId; 56 57 60 private int _indentNumber; 61 62 65 private URIResolver _uriResolver = null; 66 67 71 private TransformerFactoryImpl _tfactory = null; 72 73 76 private Parser _parser = null; 77 78 81 private TemplatesImpl _templates = null; 82 83 86 protected TemplatesHandlerImpl(int indentNumber, 87 TransformerFactoryImpl tfactory) 88 { 89 _indentNumber = indentNumber; 90 _tfactory = tfactory; 91 92 _parser = new XSLTC().getParser(); 94 } 95 96 102 public String getSystemId() { 103 return _systemId; 104 } 105 106 112 public void setSystemId(String id) { 113 _systemId = id; 114 } 115 116 119 public void setURIResolver(URIResolver resolver) { 120 _uriResolver = resolver; 121 } 122 123 132 public Templates getTemplates() { 133 return _templates; 134 } 135 136 145 public InputSource loadSource(String href, String context, XSLTC xsltc) { 146 try { 147 final Source source = _uriResolver.resolve(href, context); 149 if (source != null) { 150 return Util.getInputSource(xsltc, source); 151 } 152 } 153 catch (TransformerException e) { 154 } 156 return null; 157 } 158 159 161 164 public void startDocument() { 165 XSLTC xsltc = _parser.getXSLTC(); 166 xsltc.init(); xsltc.setOutputType(XSLTC.BYTEARRAY_OUTPUT); 168 _parser.startDocument(); 169 } 170 171 174 public void endDocument() throws SAXException { 175 _parser.endDocument(); 176 177 try { 179 XSLTC xsltc = _parser.getXSLTC(); 180 181 String transletName = null; 183 if (_systemId != null) { 184 transletName = Util.baseName(_systemId); 185 } 186 else { 187 transletName = (String )_tfactory.getAttribute("translet-name"); 188 } 189 xsltc.setClassName(transletName); 190 191 transletName = xsltc.getClassName(); 193 194 Stylesheet stylesheet = null; 195 SyntaxTreeNode root = _parser.getDocumentRoot(); 196 197 if (!_parser.errorsFound() && root != null) { 199 stylesheet = _parser.makeStylesheet(root); 201 stylesheet.setSystemId(_systemId); 202 stylesheet.setParentStylesheet(null); 203 204 if (_uriResolver != null) { 206 stylesheet.setSourceLoader(this); 207 } 208 209 _parser.setCurrentStylesheet(stylesheet); 210 211 xsltc.setStylesheet(stylesheet); 213 214 _parser.createAST(stylesheet); 216 } 217 218 if (!_parser.errorsFound() && stylesheet != null) { 220 stylesheet.setMultiDocument(xsltc.isMultiDocument()); 221 stylesheet.setHasIdCall(xsltc.hasIdCall()); 222 223 synchronized (xsltc.getClass()) { 225 stylesheet.translate(); 226 } 227 } 228 229 if (!_parser.errorsFound()) { 230 final byte[][] bytecodes = xsltc.getBytecodes(); 232 if (bytecodes != null) { 233 _templates = 234 new TemplatesImpl(xsltc.getBytecodes(), transletName, 235 _parser.getOutputProperties(), _indentNumber, _tfactory); 236 237 if (_uriResolver != null) { 239 _templates.setURIResolver(_uriResolver); 240 } 241 } 242 } 243 else { 244 StringBuffer errorMessage = new StringBuffer (); 245 Vector errors = _parser.getErrors(); 246 final int count = errors.size(); 247 for (int i = 0; i < count; i++) { 248 if (errorMessage.length() > 0) 249 errorMessage.append('\n'); 250 errorMessage.append(errors.elementAt(i).toString()); 251 } 252 throw new SAXException (ErrorMsg.JAXP_COMPILE_ERR, new TransformerException (errorMessage.toString())); 253 } 254 } 255 catch (CompilerException e) { 256 throw new SAXException (ErrorMsg.JAXP_COMPILE_ERR, e); 257 } 258 } 259 260 263 public void startPrefixMapping(String prefix, String uri) { 264 _parser.startPrefixMapping(prefix, uri); 265 } 266 267 270 public void endPrefixMapping(String prefix) { 271 _parser.endPrefixMapping(prefix); 272 } 273 274 277 public void startElement(String uri, String localname, String qname, 278 Attributes attributes) throws SAXException 279 { 280 _parser.startElement(uri, localname, qname, attributes); 281 } 282 283 286 public void endElement(String uri, String localname, String qname) { 287 _parser.endElement(uri, localname, qname); 288 } 289 290 293 public void characters(char[] ch, int start, int length) { 294 _parser.characters(ch, start, length); 295 } 296 297 300 public void processingInstruction(String name, String value) { 301 _parser.processingInstruction(name, value); 302 } 303 304 307 public void ignorableWhitespace(char[] ch, int start, int length) { 308 _parser.ignorableWhitespace(ch, start, length); 309 } 310 311 314 public void skippedEntity(String name) { 315 _parser.skippedEntity(name); 316 } 317 318 321 public void setDocumentLocator(Locator locator) { 322 setSystemId(locator.getSystemId()); 323 _parser.setDocumentLocator(locator); 324 } 325 } 326 327 328 | Popular Tags |