1 62 63 64 package com.sun.org.apache.xalan.internal.xsltc.trax; 65 66 import javax.xml.transform.ErrorListener ; 67 import javax.xml.transform.Source ; 68 import javax.xml.transform.Templates ; 69 import javax.xml.transform.Transformer ; 70 import javax.xml.transform.TransformerConfigurationException ; 71 import javax.xml.transform.TransformerException ; 72 import javax.xml.transform.URIResolver ; 73 import javax.xml.transform.dom.DOMResult ; 74 import javax.xml.transform.dom.DOMSource ; 75 import javax.xml.transform.sax.SAXResult ; 76 import javax.xml.transform.sax.SAXSource ; 77 import javax.xml.transform.sax.SAXTransformerFactory ; 78 import javax.xml.transform.sax.TemplatesHandler ; 79 import javax.xml.transform.sax.TransformerHandler ; 80 import javax.xml.transform.stream.StreamResult ; 81 import javax.xml.transform.stream.StreamSource ; 82 83 import com.sun.org.apache.xml.internal.utils.ObjectFactory; 84 import org.xml.sax.XMLFilter ; 85 86 92 public class SmartTransformerFactoryImpl extends SAXTransformerFactory 93 { 94 95 private SAXTransformerFactory _xsltcFactory = null; 96 private SAXTransformerFactory _xalanFactory = null; 97 private SAXTransformerFactory _currFactory = null; 98 private ErrorListener _errorlistener = null; 99 private URIResolver _uriresolver = null; 100 101 104 private boolean featureSecureProcessing = false; 105 106 113 public SmartTransformerFactoryImpl() { } 114 115 private void createXSLTCTransformerFactory() { 116 _xsltcFactory = new TransformerFactoryImpl(); 117 _currFactory = _xsltcFactory; 118 } 119 120 private void createXalanTransformerFactory() { 121 final String xalanMessage = 122 "com.sun.org.apache.xalan.internal.xsltc.trax.SmartTransformerFactoryImpl "+ 123 "could not create an "+ 124 "com.sun.org.apache.xalan.internal.processor.TransformerFactoryImpl."; 125 try { 127 Class xalanFactClass = ObjectFactory.findProviderClass( 128 "com.sun.org.apache.xalan.internal.processor.TransformerFactoryImpl", 129 ObjectFactory.findClassLoader(), true); 130 _xalanFactory = (SAXTransformerFactory ) 131 xalanFactClass.newInstance(); 132 } 133 catch (ClassNotFoundException e) { 134 System.err.println(xalanMessage); 135 } 136 catch (InstantiationException e) { 137 System.err.println(xalanMessage); 138 } 139 catch (IllegalAccessException e) { 140 System.err.println(xalanMessage); 141 } 142 _currFactory = _xalanFactory; 143 } 144 145 public void setErrorListener(ErrorListener listener) 146 throws IllegalArgumentException 147 { 148 _errorlistener = listener; 149 } 150 151 public ErrorListener getErrorListener() { 152 return _errorlistener; 153 } 154 155 public Object getAttribute(String name) 156 throws IllegalArgumentException 157 { 158 if ((name.equals("translet-name")) || (name.equals("debug"))) { 160 if (_xsltcFactory == null) { 161 createXSLTCTransformerFactory(); 162 } 163 return _xsltcFactory.getAttribute(name); 164 } 165 else { 166 if (_xalanFactory == null) { 167 createXalanTransformerFactory(); 168 } 169 return _xalanFactory.getAttribute(name); 170 } 171 } 172 173 public void setAttribute(String name, Object value) 174 throws IllegalArgumentException { 175 if ((name.equals("translet-name")) || (name.equals("debug"))) { 177 if (_xsltcFactory == null) { 178 createXSLTCTransformerFactory(); 179 } 180 _xsltcFactory.setAttribute(name, value); 181 } 182 else { 183 if (_xalanFactory == null) { 184 createXalanTransformerFactory(); 185 } 186 _xalanFactory.setAttribute(name, value); 187 } 188 } 189 190 211 public void setFeature(String name, boolean value) 212 throws TransformerConfigurationException { 213 214 if (name == null) { 216 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_SET_FEATURE_NULL_NAME); 217 throw new NullPointerException (err.toString()); 218 } 219 else if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) { 221 featureSecureProcessing = value; 222 return; 224 } 225 else { 226 ErrorMsg err = new ErrorMsg(ErrorMsg.JAXP_UNSUPPORTED_FEATURE, name); 228 throw new TransformerConfigurationException (err.toString()); 229 } 230 } 231 232 241 public boolean getFeature(String name) { 242 String [] features = { 244 DOMSource.FEATURE, 245 DOMResult.FEATURE, 246 SAXSource.FEATURE, 247 SAXResult.FEATURE, 248 StreamSource.FEATURE, 249 StreamResult.FEATURE 250 }; 251 252 for (int i=0; i<features.length; i++) { 254 if (name.equals(features[i])) return true; 255 } 256 257 if (name.equals(XMLConstants.FEATURE_SECURE_PROCESSING)) { 259 return featureSecureProcessing; 260 } 261 262 return false; 264 } 265 266 public URIResolver getURIResolver() { 267 return _uriresolver; 268 } 269 270 public void setURIResolver(URIResolver resolver) { 271 _uriresolver = resolver; 272 } 273 274 public Source getAssociatedStylesheet(Source source, String media, 275 String title, String charset) 276 throws TransformerConfigurationException 277 { 278 if (_currFactory == null) { 279 createXSLTCTransformerFactory(); 280 } 281 return _currFactory.getAssociatedStylesheet(source, media, 282 title, charset); 283 } 284 285 290 public Transformer newTransformer() 291 throws TransformerConfigurationException 292 { 293 if (_xalanFactory == null) { 294 createXalanTransformerFactory(); 295 } 296 if (_errorlistener != null) { 297 _xalanFactory.setErrorListener(_errorlistener); 298 } 299 if (_uriresolver != null) { 300 _xalanFactory.setURIResolver(_uriresolver); 301 } 302 _currFactory = _xalanFactory; 303 return _currFactory.newTransformer(); 304 } 305 306 312 public Transformer newTransformer(Source source) throws 313 TransformerConfigurationException 314 { 315 if (_xalanFactory == null) { 316 createXalanTransformerFactory(); 317 } 318 if (_errorlistener != null) { 319 _xalanFactory.setErrorListener(_errorlistener); 320 } 321 if (_uriresolver != null) { 322 _xalanFactory.setURIResolver(_uriresolver); 323 } 324 _currFactory = _xalanFactory; 325 return _currFactory.newTransformer(source); 326 } 327 328 334 public Templates newTemplates(Source source) 335 throws TransformerConfigurationException 336 { 337 if (_xsltcFactory == null) { 338 createXSLTCTransformerFactory(); 339 } 340 if (_errorlistener != null) { 341 _xsltcFactory.setErrorListener(_errorlistener); 342 } 343 if (_uriresolver != null) { 344 _xsltcFactory.setURIResolver(_uriresolver); 345 } 346 _currFactory = _xsltcFactory; 347 return _currFactory.newTemplates(source); 348 } 349 350 355 public TemplatesHandler newTemplatesHandler() 356 throws TransformerConfigurationException 357 { 358 if (_xsltcFactory == null) { 359 createXSLTCTransformerFactory(); 360 } 361 if (_errorlistener != null) { 362 _xsltcFactory.setErrorListener(_errorlistener); 363 } 364 if (_uriresolver != null) { 365 _xsltcFactory.setURIResolver(_uriresolver); 366 } 367 return _xsltcFactory.newTemplatesHandler(); 368 } 369 370 375 public TransformerHandler newTransformerHandler() 376 throws TransformerConfigurationException 377 { 378 if (_xalanFactory == null) { 379 createXalanTransformerFactory(); 380 } 381 if (_errorlistener != null) { 382 _xalanFactory.setErrorListener(_errorlistener); 383 } 384 if (_uriresolver != null) { 385 _xalanFactory.setURIResolver(_uriresolver); 386 } 387 return _xalanFactory.newTransformerHandler(); 388 } 389 390 395 public TransformerHandler newTransformerHandler(Source src) 396 throws TransformerConfigurationException 397 { 398 if (_xalanFactory == null) { 399 createXalanTransformerFactory(); 400 } 401 if (_errorlistener != null) { 402 _xalanFactory.setErrorListener(_errorlistener); 403 } 404 if (_uriresolver != null) { 405 _xalanFactory.setURIResolver(_uriresolver); 406 } 407 return _xalanFactory.newTransformerHandler(src); 408 } 409 410 411 416 public TransformerHandler newTransformerHandler(Templates templates) 417 throws TransformerConfigurationException 418 { 419 if (_xsltcFactory == null) { 420 createXSLTCTransformerFactory(); 421 } 422 if (_errorlistener != null) { 423 _xsltcFactory.setErrorListener(_errorlistener); 424 } 425 if (_uriresolver != null) { 426 _xsltcFactory.setURIResolver(_uriresolver); 427 } 428 return _xsltcFactory.newTransformerHandler(templates); 429 } 430 431 432 437 public XMLFilter newXMLFilter(Source src) 438 throws TransformerConfigurationException { 439 if (_xsltcFactory == null) { 440 createXSLTCTransformerFactory(); 441 } 442 if (_errorlistener != null) { 443 _xsltcFactory.setErrorListener(_errorlistener); 444 } 445 if (_uriresolver != null) { 446 _xsltcFactory.setURIResolver(_uriresolver); 447 } 448 Templates templates = _xsltcFactory.newTemplates(src); 449 if (templates == null ) return null; 450 return newXMLFilter(templates); 451 } 452 453 458 public XMLFilter newXMLFilter(Templates templates) 459 throws TransformerConfigurationException { 460 try { 461 return new com.sun.org.apache.xalan.internal.xsltc.trax.TrAXFilter(templates); 462 } 463 catch(TransformerConfigurationException e1) { 464 if (_xsltcFactory == null) { 465 createXSLTCTransformerFactory(); 466 } 467 ErrorListener errorListener = _xsltcFactory.getErrorListener(); 468 if(errorListener != null) { 469 try { 470 errorListener.fatalError(e1); 471 return null; 472 } 473 catch( TransformerException e2) { 474 new TransformerConfigurationException (e2); 475 } 476 } 477 throw e1; 478 } 479 } 480 } 481 | Popular Tags |