1 package net.sf.saxon; 2 3 import net.sf.saxon.event.PIGrabber; 4 import net.sf.saxon.event.Sender; 5 import net.sf.saxon.om.NamePool; 6 import net.sf.saxon.om.NamespaceConstant; 7 import net.sf.saxon.trace.TraceListener; 8 import net.sf.saxon.trans.XPathException; 9 import net.sf.saxon.sort.CollationURIResolver; 10 import org.xml.sax.InputSource ; 11 import org.xml.sax.XMLFilter ; 12 13 import javax.xml.transform.*; 14 import javax.xml.transform.dom.DOMResult ; 15 import javax.xml.transform.dom.DOMSource ; 16 import javax.xml.transform.sax.*; 17 import javax.xml.transform.stream.StreamResult ; 18 import javax.xml.transform.stream.StreamSource ; 19 import java.io.StringReader ; 20 import java.util.List ; 21 22 23 36 37 public class TransformerFactoryImpl extends SAXTransformerFactory { 38 39 private Configuration config; 40 41 44 public TransformerFactoryImpl() { 45 config = new Configuration(); 46 } 47 48 51 public TransformerFactoryImpl(Configuration config) { 52 this.config = config; 53 } 54 55 58 59 public void setConfiguration(Configuration config) { 60 this.config = config; 61 } 62 63 66 67 public Configuration getConfiguration() { 68 return config; 69 } 70 71 85 86 public Transformer newTransformer(Source source) 87 throws TransformerConfigurationException { 88 Templates templates = newTemplates(source); 89 Transformer trans = templates.newTransformer(); 90 return trans; 91 } 92 93 104 105 public Transformer newTransformer() 106 throws TransformerConfigurationException { 107 108 return new IdentityTransformer(config); 109 } 110 111 112 128 129 public Templates newTemplates(Source source) 130 throws TransformerConfigurationException { 131 132 PreparedStylesheet pss = new PreparedStylesheet(config); 133 pss.prepare(source); 134 return pss; 135 } 136 137 156 157 public Source getAssociatedStylesheet( 158 Source source, String media, String title, String charset) 159 throws TransformerConfigurationException { 160 161 162 PIGrabber grabber = new PIGrabber(); 163 grabber.setFactory(config); 164 grabber.setCriteria(media, title, charset); 165 grabber.setBaseURI(source.getSystemId()); 166 grabber.setURIResolver(config.getURIResolver()); 167 168 169 try { 170 new Sender(config.makePipelineConfiguration()).send(source, grabber); 171 } catch (XPathException err) { 173 if (grabber.isTerminated()) { 174 } else { 176 throw new TransformerConfigurationException( 177 "Failed while looking for xml-stylesheet PI", err); 178 } 179 } 180 181 try { 182 Source [] sources = grabber.getAssociatedStylesheets(); 183 if (sources==null) { 184 throw new TransformerConfigurationException( 185 "No matching <?xml-stylesheet?> processing instruction found"); 186 } 187 return compositeStylesheet(source.getSystemId(), sources); 188 } catch (TransformerException err) { 189 if (err instanceof TransformerConfigurationException) { 190 throw (TransformerConfigurationException)err; 191 } else { 192 throw new TransformerConfigurationException(err); 193 } 194 } 195 } 196 197 205 206 private Source compositeStylesheet(String baseURI, Source [] sources) 207 throws TransformerConfigurationException { 208 209 if (sources.length == 1) { 210 return sources[0]; 211 } else if (sources.length == 0) { 212 throw new TransformerConfigurationException( 213 "No stylesheets were supplied"); 214 } 215 216 218 StringBuffer sb = new StringBuffer (250); 219 sb.append("<xsl:stylesheet version='1.0' "); 220 sb.append(" xmlns:xsl='" + NamespaceConstant.XSLT + "'>"); 221 for (int i=0; i<sources.length; i++) { 222 sb.append("<xsl:import HREF='" + sources[i].getSystemId() + "'/>"); 223 } 224 sb.append("</xsl:stylesheet>"); 225 InputSource composite = new InputSource (); 226 composite.setSystemId(baseURI); 227 composite.setCharacterStream(new StringReader (sb.toString())); 228 return new SAXSource(config.getSourceParser(), composite); 229 } 230 231 238 239 public void setURIResolver(URIResolver resolver) { 240 config.setURIResolver(resolver); 241 } 242 243 249 250 public URIResolver getURIResolver() { 251 return config.getURIResolver(); 252 } 253 254 256 private static final String FEATURE_SECURE_PROCESSING = 257 "http://javax.xml.XMLConstants/feature/secure-processing"; 259 261 262 269 270 public boolean getFeature(String name) { 271 if (name.equals(SAXSource.FEATURE)) return true; 272 if (name.equals(SAXResult.FEATURE)) return true; 273 if (name.equals(DOMSource.FEATURE)) return isDOMAvailable(); 274 if (name.equals(DOMResult.FEATURE)) return isDOMAvailable(); 275 if (name.equals(StreamSource.FEATURE)) return true; 276 if (name.equals(StreamResult.FEATURE)) return true; 277 if (name.equals(SAXTransformerFactory.FEATURE)) return true; 278 if (name.equals(SAXTransformerFactory.FEATURE_XMLFILTER)) return true; 279 if (name.equals(FEATURE_SECURE_PROCESSING)) { 280 return !config.isAllowExternalFunctions(); 281 } 282 throw new IllegalArgumentException ("Unknown feature " + name); 283 } 284 285 288 289 private boolean isDOMAvailable() { 290 List models = config.getExternalObjectModels(); 291 for (int i=0; i<models.size(); i++) { 292 if (models.get(i).getClass().getName().equals("net.sf.saxon.dom.DOMObjectModel")) { 293 return true; 294 } 295 } 296 return false; 297 } 298 299 311 312 public void setAttribute(String name, Object value) 313 throws IllegalArgumentException { 314 if (name.equals(FeatureKeys.TREE_MODEL)) { 315 if (!(value instanceof Integer )) { 316 throw new IllegalArgumentException ("Tree model must be an Integer"); 317 } 318 config.setTreeModel(((Integer )value).intValue()); 319 320 } else if (name.equals(FeatureKeys.ALLOW_EXTERNAL_FUNCTIONS)) { 321 if (!(value instanceof Boolean )) { 322 throw new IllegalArgumentException ("allow-external-functions must be a boolean"); 323 } 324 config.setAllowExternalFunctions(((Boolean )value).booleanValue()); 325 326 } else if (name.equals(FeatureKeys.RECOGNIZE_URI_QUERY_PARAMETERS)) { 327 if (!(value instanceof Boolean )) { 328 throw new IllegalArgumentException ("recognize-uri-query-parameters must be a boolean"); 329 } 330 config.getSystemURIResolver().setRecognizeQueryParameters(((Boolean )value).booleanValue()); 331 332 } else if (name.equals(FeatureKeys.TRACE_EXTERNAL_FUNCTIONS)) { 333 if (!(value instanceof Boolean )) { 334 throw new IllegalArgumentException ("trace-external-functions must be a boolean"); 335 } 336 config.setTraceExternalFunctions(((Boolean )value).booleanValue()); 337 338 } else if (name.equals(FeatureKeys.TIMING)) { 339 if (!(value instanceof Boolean )) { 340 throw new IllegalArgumentException ("Timing must be a boolean"); 341 } 342 config.setTiming(((Boolean )value).booleanValue()); 343 344 } else if (name.equals(FeatureKeys.DTD_VALIDATION)) { 345 if (!(value instanceof Boolean )) { 346 throw new IllegalArgumentException ("Validation must be a boolean"); 347 } 348 config.setValidation(((Boolean )value).booleanValue()); 349 350 } else if (name.equals(FeatureKeys.SCHEMA_VALIDATION)) { 351 if (!(value instanceof Integer )) { 352 throw new IllegalArgumentException ("Schema validation must be an integer"); 353 } 354 config.setSchemaValidationMode(((Integer )value).intValue()); 355 356 } else if (name.equals(FeatureKeys.VALIDATION_WARNINGS)) { 357 if (!(value instanceof Boolean )) { 358 throw new IllegalArgumentException ("validation-warnings must be a boolean"); 359 } 360 config.setValidationWarnings(((Boolean )value).booleanValue()); 361 362 } else if (name.equals(FeatureKeys.VERSION_WARNING)) { 363 if (!(value instanceof Boolean )) { 364 throw new IllegalArgumentException ("version-warning must be a boolean"); 365 } 366 config.setVersionWarning(((Boolean )value).booleanValue()); 367 368 } else if (name.equals(FeatureKeys.TRACE_LISTENER)) { 369 if (!(value instanceof TraceListener)) { 370 throw new IllegalArgumentException ("Trace listener is of wrong class"); 371 } 372 config.setTraceListener((TraceListener)value); 373 374 } else if (name.equals(FeatureKeys.LINE_NUMBERING)) { 375 if (!(value instanceof Boolean )) { 376 throw new IllegalArgumentException ("Line Numbering value must be Boolean"); 377 } 378 config.setLineNumbering(((Boolean )value).booleanValue()); 379 380 } else if (name.equals(FeatureKeys.RECOVERY_POLICY)) { 381 if (!(value instanceof Integer )) { 382 throw new IllegalArgumentException ("Recovery Policy value must be Integer"); 383 } 384 config.setRecoveryPolicy(((Integer )value).intValue()); 385 386 } else if (name.equals(FeatureKeys.MESSAGE_EMITTER_CLASS)) { 387 if (!(value instanceof String )) { 388 throw new IllegalArgumentException ("Message Emitter class must be a String"); 389 } 390 config.setMessageEmitterClass((String )value); 391 392 } else if (name.equals(FeatureKeys.SOURCE_PARSER_CLASS)) { 393 if (!(value instanceof String )) { 394 throw new IllegalArgumentException ("Source Parser class must be a String"); 395 } 396 config.setSourceParserClass((String )value); 397 398 } else if (name.equals(FeatureKeys.STYLE_PARSER_CLASS)) { 399 if (!(value instanceof String )) { 400 throw new IllegalArgumentException ("Style Parser class must be a String"); 401 } 402 config.setStyleParserClass((String )value); 403 404 } else if (name.equals(FeatureKeys.OUTPUT_URI_RESOLVER)) { 405 if (!(value instanceof OutputURIResolver)) { 406 throw new IllegalArgumentException ("Output URI resolver value must be an instance of net.sf.saxon.OutputURIResolver"); 407 } 408 config.setOutputURIResolver((OutputURIResolver)value); 409 410 } else if (name.equals(FeatureKeys.NAME_POOL)) { 411 if (!(value instanceof NamePool)) { 412 throw new IllegalArgumentException ("NAME_POOL value must be an instance of net.sf.saxon.om.NamePool"); 413 } 414 config.setNamePool((NamePool)value); 415 416 } else if (name.equals(FeatureKeys.COLLATION_URI_RESOLVER)) { 417 if (!(value instanceof CollationURIResolver)) { 418 throw new IllegalArgumentException ( 419 "COLLATION_URI_RESOLVER value must be an instance of net.sf.saxon.sort.CollationURIResolver"); 420 } 421 config.setCollationURIResolver((CollationURIResolver)value); 422 423 } else if (name.equals(FeatureKeys.COLLECTION_URI_RESOLVER)) { 424 if (!(value instanceof CollectionURIResolver)) { 425 throw new IllegalArgumentException ( 426 "COLLECTION_URI_RESOLVER value must be an instance of net.sf.saxon.CollectionURIResolver"); 427 } 428 config.setCollectionURIResolver((CollectionURIResolver)value); 429 430 } else { 431 throw new IllegalArgumentException ("Unknown attribute " + name); 432 } 433 } 434 435 443 public Object getAttribute(String name) 444 throws IllegalArgumentException { 445 if (name.equals(FeatureKeys.TREE_MODEL)) { 446 return new Integer (config.getTreeModel()); 447 448 } else if (name.equals(FeatureKeys.TIMING)) { 449 return Boolean.valueOf(config.isTiming()); 450 451 } else if (name.equals(FeatureKeys.DTD_VALIDATION)) { 452 return Boolean.valueOf(config.isValidation()); 453 454 } else if (name.equals(FeatureKeys.SCHEMA_VALIDATION)) { 455 return new Integer (config.getSchemaValidationMode()); 456 457 } else if (name.equals(FeatureKeys.VALIDATION_WARNINGS)) { 458 return Boolean.valueOf(config.isValidationWarnings()); 459 460 } else if (name.equals(FeatureKeys.ALLOW_EXTERNAL_FUNCTIONS)) { 461 return Boolean.valueOf(config.isAllowExternalFunctions()); 462 463 } else if (name.equals(FeatureKeys.RECOGNIZE_URI_QUERY_PARAMETERS)) { 464 return Boolean.valueOf(config.getSystemURIResolver().queryParametersAreRecognized()); 465 466 } else if (name.equals(FeatureKeys.TRACE_EXTERNAL_FUNCTIONS)) { 467 return Boolean.valueOf(config.isTraceExternalFunctions()); 468 469 } else if (name.equals(FeatureKeys.VERSION_WARNING)) { 470 return Boolean.valueOf(config.isVersionWarning()); 471 472 } else if (name.equals(FeatureKeys.TRACE_LISTENER)) { 473 return config.getTraceListener(); 474 475 } else if (name.equals(FeatureKeys.LINE_NUMBERING)) { 476 return Boolean.valueOf(config.isLineNumbering()); 477 478 } else if (name.equals(FeatureKeys.RECOVERY_POLICY)) { 479 return new Integer (config.getRecoveryPolicy()); 480 481 } else if (name.equals(FeatureKeys.MESSAGE_EMITTER_CLASS)) { 482 return config.getMessageEmitterClass(); 483 484 } else if (name.equals(FeatureKeys.SOURCE_PARSER_CLASS)) { 485 return config.getSourceParserClass(); 486 487 } else if (name.equals(FeatureKeys.STYLE_PARSER_CLASS)) { 488 return config.getStyleParserClass(); 489 490 } else if (name.equals(FeatureKeys.OUTPUT_URI_RESOLVER)) { 491 return config.getOutputURIResolver(); 492 493 } else if (name.equals(FeatureKeys.NAME_POOL)) { 494 return config.getNamePool(); 495 496 } else if (name.equals(FeatureKeys.COLLATION_URI_RESOLVER)) { 497 return config.getCollationURIResolver(); 498 499 } else if (name.equals(FeatureKeys.COLLECTION_URI_RESOLVER)) { 500 return config.getCollectionURIResolver(); 501 502 } else { 503 throw new IllegalArgumentException ("Unknown attribute " + name); 504 } 505 } 506 507 515 516 public void setErrorListener(ErrorListener listener) 517 throws IllegalArgumentException { 518 config.setErrorListener(listener); 519 } 520 521 525 public ErrorListener getErrorListener() { 526 return config.getErrorListener(); 527 } 528 529 530 531 532 536 548 549 public TransformerHandler newTransformerHandler(Source src) 550 throws TransformerConfigurationException { 551 Templates tmpl = newTemplates(src); 552 return newTransformerHandler(tmpl); 553 } 554 555 566 567 public TransformerHandler newTransformerHandler(Templates templates) 568 throws TransformerConfigurationException { 569 if (!(templates instanceof PreparedStylesheet)) { 570 throw new TransformerConfigurationException("Templates object was not created by Saxon"); 571 } 572 Controller controller = (Controller)templates.newTransformer(); 573 TransformerHandlerImpl handler = new TransformerHandlerImpl(controller); 574 return handler; 575 } 576 577 589 590 public TransformerHandler newTransformerHandler() 591 throws TransformerConfigurationException { 592 Controller controller = new IdentityTransformer(config); 593 return new IdentityTransformerHandler(controller); 594 } 595 596 606 607 public TemplatesHandler newTemplatesHandler() 608 throws TransformerConfigurationException { 609 return new TemplatesHandlerImpl(config); 610 } 611 612 623 624 public XMLFilter newXMLFilter(Source src) 625 throws TransformerConfigurationException { 626 Templates tmpl = newTemplates(src); 627 return newXMLFilter(tmpl); 628 } 629 630 640 641 public XMLFilter newXMLFilter(Templates templates) 642 throws TransformerConfigurationException { 643 if (!(templates instanceof PreparedStylesheet)) { 644 throw new TransformerConfigurationException("Supplied Templates object was not created using Saxon"); 645 } 646 Controller controller = (Controller)templates.newTransformer(); 647 return new Filter(controller); 648 } 649 650 688 689 public void setFeature(String name, boolean value) throws TransformerConfigurationException { 690 if (name.equals(FEATURE_SECURE_PROCESSING)) { 691 config.setAllowExternalFunctions(!value); 692 } else { 693 throw new TransformerConfigurationException("Unsupported TransformerFactory feature: " + name); 694 } 695 } 696 697 } 698 699 | Popular Tags |