1 16 19 20 package com.sun.org.apache.xalan.internal.xsltc.trax; 21 22 import javax.xml.XMLConstants ; 23 import javax.xml.transform.Source ; 24 import javax.xml.transform.Templates ; 25 import javax.xml.transform.TransformerException ; 26 import javax.xml.transform.URIResolver ; 27 import javax.xml.transform.sax.TemplatesHandler ; 28 29 import com.sun.org.apache.xalan.internal.xsltc.compiler.CompilerException; 30 import com.sun.org.apache.xalan.internal.xsltc.compiler.Parser; 31 import com.sun.org.apache.xalan.internal.xsltc.compiler.SourceLoader; 32 import com.sun.org.apache.xalan.internal.xsltc.compiler.Stylesheet; 33 import com.sun.org.apache.xalan.internal.xsltc.compiler.SyntaxTreeNode; 34 import com.sun.org.apache.xalan.internal.xsltc.compiler.XSLTC; 35 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg; 36 37 import org.xml.sax.ContentHandler ; 38 import org.xml.sax.InputSource ; 39 import org.xml.sax.Locator ; 40 import org.xml.sax.SAXException ; 41 import org.xml.sax.Attributes ; 42 43 import java.util.Vector ; 44 45 50 public class TemplatesHandlerImpl 51 implements ContentHandler , TemplatesHandler , SourceLoader 52 { 53 56 private String _systemId; 57 58 61 private int _indentNumber; 62 63 66 private URIResolver _uriResolver = null; 67 68 72 private TransformerFactoryImpl _tfactory = null; 73 74 77 private Parser _parser = null; 78 79 82 private TemplatesImpl _templates = null; 83 84 87 protected TemplatesHandlerImpl(int indentNumber, 88 TransformerFactoryImpl tfactory) 89 { 90 _indentNumber = indentNumber; 91 _tfactory = tfactory; 92 93 XSLTC xsltc = new XSLTC(); 95 if (tfactory.getFeature(XMLConstants.FEATURE_SECURE_PROCESSING)) 96 xsltc.setSecureProcessing(true); 97 98 _parser = new XSLTC().getParser(); 100 } 101 102 108 public String getSystemId() { 109 return _systemId; 110 } 111 112 118 public void setSystemId(String id) { 119 _systemId = id; 120 } 121 122 125 public void setURIResolver(URIResolver resolver) { 126 _uriResolver = resolver; 127 } 128 129 138 public Templates getTemplates() { 139 return _templates; 140 } 141 142 151 public InputSource loadSource(String href, String context, XSLTC xsltc) { 152 try { 153 final Source source = _uriResolver.resolve(href, context); 155 if (source != null) { 156 return Util.getInputSource(xsltc, source); 157 } 158 } 159 catch (TransformerException e) { 160 } 162 return null; 163 } 164 165 167 170 public void startDocument() { 171 XSLTC xsltc = _parser.getXSLTC(); 172 xsltc.init(); xsltc.setOutputType(XSLTC.BYTEARRAY_OUTPUT); 174 _parser.startDocument(); 175 } 176 177 180 public void endDocument() throws SAXException { 181 _parser.endDocument(); 182 183 try { 185 XSLTC xsltc = _parser.getXSLTC(); 186 187 String transletName = null; 189 if (_systemId != null) { 190 transletName = Util.baseName(_systemId); 191 } 192 else { 193 transletName = (String )_tfactory.getAttribute("translet-name"); 194 } 195 xsltc.setClassName(transletName); 196 197 transletName = xsltc.getClassName(); 199 200 Stylesheet stylesheet = null; 201 SyntaxTreeNode root = _parser.getDocumentRoot(); 202 203 if (!_parser.errorsFound() && root != null) { 205 stylesheet = _parser.makeStylesheet(root); 207 stylesheet.setSystemId(_systemId); 208 stylesheet.setParentStylesheet(null); 209 210 if (_uriResolver != null) { 212 stylesheet.setSourceLoader(this); 213 } 214 215 _parser.setCurrentStylesheet(stylesheet); 216 217 xsltc.setStylesheet(stylesheet); 219 220 _parser.createAST(stylesheet); 222 } 223 224 if (!_parser.errorsFound() && stylesheet != null) { 226 stylesheet.setMultiDocument(xsltc.isMultiDocument()); 227 stylesheet.setHasIdCall(xsltc.hasIdCall()); 228 229 synchronized (xsltc.getClass()) { 231 stylesheet.translate(); 232 } 233 } 234 235 if (!_parser.errorsFound()) { 236 final byte[][] bytecodes = xsltc.getBytecodes(); 238 if (bytecodes != null) { 239 _templates = 240 new TemplatesImpl(xsltc.getBytecodes(), transletName, 241 _parser.getOutputProperties(), _indentNumber, _tfactory); 242 243 if (_uriResolver != null) { 245 _templates.setURIResolver(_uriResolver); 246 } 247 } 248 } 249 else { 250 StringBuffer errorMessage = new StringBuffer (); 251 Vector errors = _parser.getErrors(); 252 final int count = errors.size(); 253 for (int i = 0; i < count; i++) { 254 if (errorMessage.length() > 0) 255 errorMessage.append('\n'); 256 errorMessage.append(errors.elementAt(i).toString()); 257 } 258 throw new SAXException (ErrorMsg.JAXP_COMPILE_ERR, new TransformerException (errorMessage.toString())); 259 } 260 } 261 catch (CompilerException e) { 262 throw new SAXException (ErrorMsg.JAXP_COMPILE_ERR, e); 263 } 264 } 265 266 269 public void startPrefixMapping(String prefix, String uri) { 270 _parser.startPrefixMapping(prefix, uri); 271 } 272 273 276 public void endPrefixMapping(String prefix) { 277 _parser.endPrefixMapping(prefix); 278 } 279 280 283 public void startElement(String uri, String localname, String qname, 284 Attributes attributes) throws SAXException 285 { 286 _parser.startElement(uri, localname, qname, attributes); 287 } 288 289 292 public void endElement(String uri, String localname, String qname) { 293 _parser.endElement(uri, localname, qname); 294 } 295 296 299 public void characters(char[] ch, int start, int length) { 300 _parser.characters(ch, start, length); 301 } 302 303 306 public void processingInstruction(String name, String value) { 307 _parser.processingInstruction(name, value); 308 } 309 310 313 public void ignorableWhitespace(char[] ch, int start, int length) { 314 _parser.ignorableWhitespace(ch, start, length); 315 } 316 317 320 public void skippedEntity(String name) { 321 _parser.skippedEntity(name); 322 } 323 324 327 public void setDocumentLocator(Locator locator) { 328 setSystemId(locator.getSystemId()); 329 _parser.setDocumentLocator(locator); 330 } 331 } 332 333 334 | Popular Tags |