1 16 19 20 21 package org.apache.xalan.xsltc.trax; 22 23 import javax.xml.transform.ErrorListener ; 24 import javax.xml.transform.Source ; 25 import javax.xml.transform.Templates ; 26 import javax.xml.transform.Transformer ; 27 import javax.xml.transform.TransformerConfigurationException ; 28 import javax.xml.transform.TransformerException ; 29 import javax.xml.transform.URIResolver ; 30 import javax.xml.transform.dom.DOMResult ; 31 import javax.xml.transform.dom.DOMSource ; 32 import javax.xml.transform.sax.SAXResult ; 33 import javax.xml.transform.sax.SAXSource ; 34 import javax.xml.transform.sax.SAXTransformerFactory ; 35 import javax.xml.transform.sax.TemplatesHandler ; 36 import javax.xml.transform.sax.TransformerHandler ; 37 import javax.xml.transform.stream.StreamResult ; 38 import javax.xml.transform.stream.StreamSource ; 39 40 import org.xml.sax.XMLFilter ; 41 42 49 public class SmartTransformerFactoryImpl extends SAXTransformerFactory 50 { 51 52 private SAXTransformerFactory _xsltcFactory = null; 53 private SAXTransformerFactory _xalanFactory = null; 54 private SAXTransformerFactory _currFactory = null; 55 private ErrorListener _errorlistener = null; 56 private URIResolver _uriresolver = null; 57 58 65 public SmartTransformerFactoryImpl() { } 66 67 private void createXSLTCTransformerFactory() { 68 _xsltcFactory = new TransformerFactoryImpl(); 69 _currFactory = _xsltcFactory; 70 } 71 72 private void createXalanTransformerFactory() { 73 final String xalanMessage = 74 "org.apache.xalan.xsltc.trax.SmartTransformerFactoryImpl "+ 75 "could not create an "+ 76 "org.apache.xalan.processor.TransformerFactoryImpl."; 77 try { 79 Class xalanFactClass = ObjectFactory.findProviderClass( 80 "org.apache.xalan.processor.TransformerFactoryImpl", 81 ObjectFactory.findClassLoader(), true); 82 _xalanFactory = (SAXTransformerFactory ) 83 xalanFactClass.newInstance(); 84 } 85 catch (ClassNotFoundException e) { 86 System.err.println(xalanMessage); 87 } 88 catch (InstantiationException e) { 89 System.err.println(xalanMessage); 90 } 91 catch (IllegalAccessException e) { 92 System.err.println(xalanMessage); 93 } 94 _currFactory = _xalanFactory; 95 } 96 97 public void setErrorListener(ErrorListener listener) 98 throws IllegalArgumentException 99 { 100 _errorlistener = listener; 101 } 102 103 public ErrorListener getErrorListener() { 104 return _errorlistener; 105 } 106 107 public Object getAttribute(String name) 108 throws IllegalArgumentException 109 { 110 if ((name.equals("translet-name")) || (name.equals("debug"))) { 112 if (_xsltcFactory == null) { 113 createXSLTCTransformerFactory(); 114 } 115 return _xsltcFactory.getAttribute(name); 116 } 117 else { 118 if (_xalanFactory == null) { 119 createXalanTransformerFactory(); 120 } 121 return _xalanFactory.getAttribute(name); 122 } 123 } 124 125 public void setAttribute(String name, Object value) 126 throws IllegalArgumentException { 127 if ((name.equals("translet-name")) || (name.equals("debug"))) { 129 if (_xsltcFactory == null) { 130 createXSLTCTransformerFactory(); 131 } 132 _xsltcFactory.setAttribute(name, value); 133 } 134 else { 135 if (_xalanFactory == null) { 136 createXalanTransformerFactory(); 137 } 138 _xalanFactory.setAttribute(name, value); 139 } 140 } 141 142 151 public boolean getFeature(String name) { 152 String [] features = { 154 DOMSource.FEATURE, 155 DOMResult.FEATURE, 156 SAXSource.FEATURE, 157 SAXResult.FEATURE, 158 StreamSource.FEATURE, 159 StreamResult.FEATURE 160 }; 161 162 for (int i=0; i<features.length; i++) { 164 if (name.equals(features[i])) return true; 165 } 166 167 return false; 169 } 170 171 public URIResolver getURIResolver() { 172 return _uriresolver; 173 } 174 175 public void setURIResolver(URIResolver resolver) { 176 _uriresolver = resolver; 177 } 178 179 public Source getAssociatedStylesheet(Source source, String media, 180 String title, String charset) 181 throws TransformerConfigurationException 182 { 183 if (_currFactory == null) { 184 createXSLTCTransformerFactory(); 185 } 186 return _currFactory.getAssociatedStylesheet(source, media, 187 title, charset); 188 } 189 190 195 public Transformer newTransformer() 196 throws TransformerConfigurationException 197 { 198 if (_xalanFactory == null) { 199 createXalanTransformerFactory(); 200 } 201 if (_errorlistener != null) { 202 _xalanFactory.setErrorListener(_errorlistener); 203 } 204 if (_uriresolver != null) { 205 _xalanFactory.setURIResolver(_uriresolver); 206 } 207 _currFactory = _xalanFactory; 208 return _currFactory.newTransformer(); 209 } 210 211 217 public Transformer newTransformer(Source source) throws 218 TransformerConfigurationException 219 { 220 if (_xalanFactory == null) { 221 createXalanTransformerFactory(); 222 } 223 if (_errorlistener != null) { 224 _xalanFactory.setErrorListener(_errorlistener); 225 } 226 if (_uriresolver != null) { 227 _xalanFactory.setURIResolver(_uriresolver); 228 } 229 _currFactory = _xalanFactory; 230 return _currFactory.newTransformer(source); 231 } 232 233 239 public Templates newTemplates(Source source) 240 throws TransformerConfigurationException 241 { 242 if (_xsltcFactory == null) { 243 createXSLTCTransformerFactory(); 244 } 245 if (_errorlistener != null) { 246 _xsltcFactory.setErrorListener(_errorlistener); 247 } 248 if (_uriresolver != null) { 249 _xsltcFactory.setURIResolver(_uriresolver); 250 } 251 _currFactory = _xsltcFactory; 252 return _currFactory.newTemplates(source); 253 } 254 255 260 public TemplatesHandler newTemplatesHandler() 261 throws TransformerConfigurationException 262 { 263 if (_xsltcFactory == null) { 264 createXSLTCTransformerFactory(); 265 } 266 if (_errorlistener != null) { 267 _xsltcFactory.setErrorListener(_errorlistener); 268 } 269 if (_uriresolver != null) { 270 _xsltcFactory.setURIResolver(_uriresolver); 271 } 272 return _xsltcFactory.newTemplatesHandler(); 273 } 274 275 280 public TransformerHandler newTransformerHandler() 281 throws TransformerConfigurationException 282 { 283 if (_xalanFactory == null) { 284 createXalanTransformerFactory(); 285 } 286 if (_errorlistener != null) { 287 _xalanFactory.setErrorListener(_errorlistener); 288 } 289 if (_uriresolver != null) { 290 _xalanFactory.setURIResolver(_uriresolver); 291 } 292 return _xalanFactory.newTransformerHandler(); 293 } 294 295 300 public TransformerHandler newTransformerHandler(Source src) 301 throws TransformerConfigurationException 302 { 303 if (_xalanFactory == null) { 304 createXalanTransformerFactory(); 305 } 306 if (_errorlistener != null) { 307 _xalanFactory.setErrorListener(_errorlistener); 308 } 309 if (_uriresolver != null) { 310 _xalanFactory.setURIResolver(_uriresolver); 311 } 312 return _xalanFactory.newTransformerHandler(src); 313 } 314 315 316 321 public TransformerHandler newTransformerHandler(Templates templates) 322 throws TransformerConfigurationException 323 { 324 if (_xsltcFactory == null) { 325 createXSLTCTransformerFactory(); 326 } 327 if (_errorlistener != null) { 328 _xsltcFactory.setErrorListener(_errorlistener); 329 } 330 if (_uriresolver != null) { 331 _xsltcFactory.setURIResolver(_uriresolver); 332 } 333 return _xsltcFactory.newTransformerHandler(templates); 334 } 335 336 337 342 public XMLFilter newXMLFilter(Source src) 343 throws TransformerConfigurationException { 344 if (_xsltcFactory == null) { 345 createXSLTCTransformerFactory(); 346 } 347 if (_errorlistener != null) { 348 _xsltcFactory.setErrorListener(_errorlistener); 349 } 350 if (_uriresolver != null) { 351 _xsltcFactory.setURIResolver(_uriresolver); 352 } 353 Templates templates = _xsltcFactory.newTemplates(src); 354 if (templates == null ) return null; 355 return newXMLFilter(templates); 356 } 357 358 363 public XMLFilter newXMLFilter(Templates templates) 364 throws TransformerConfigurationException { 365 try { 366 return new org.apache.xalan.xsltc.trax.TrAXFilter(templates); 367 } 368 catch(TransformerConfigurationException e1) { 369 if (_xsltcFactory == null) { 370 createXSLTCTransformerFactory(); 371 } 372 ErrorListener errorListener = _xsltcFactory.getErrorListener(); 373 if(errorListener != null) { 374 try { 375 errorListener.fatalError(e1); 376 return null; 377 } 378 catch( TransformerException e2) { 379 new TransformerConfigurationException (e2); 380 } 381 } 382 throw e1; 383 } 384 } 385 } 386 | Popular Tags |