How to select saxon TransformerFactory in Java

前端 未结 4 1696
南笙
南笙 2020-12-23 22:22

In my web application I need to use Saxon TransformerFactory in order to use XSLT 2.0 but I can\'t use setProperty method because I don\'t have this right on the web server

相关标签:
4条回答
  • 2020-12-23 22:26

    Create file META-INF/services/javax.xml.transform.TransformerFactory with content: net.sf.saxon.TransformerFactoryImpl. That's it.

    0 讨论(0)
  • 2020-12-23 22:32

    The proper way to do this is by specifying the factory class when getting a new TransformerFactory.

    I dont think calling a specific factory implementation will work - I believe the default system transformer might still be returned (at least thats what happened when I had xalan and saxon in the classpath).

    example:

    TransformerFactory tFactory = TransformerFactory.newInstance("org.apache.xalan.processor.TransformerFactoryImpl",null);
    

    or for saxon

    TransformerFactory tFactory = TransformerFactory.newInstance("net.sf.saxon.TransformerFactoryImpl",null);
    

    Javadocs:

    Obtain a new instance of a TransformerFactory from factory class name. This function is useful when there are multiple providers in the classpath. It gives more control to the application as it can specify which provider should be loaded. Once an application has obtained a reference to a TransformerFactory it can use the factory to configure and obtain transformer instances.

    0 讨论(0)
  • 2020-12-23 22:33

    If your application really requires saxon and won't work with another processor then it would probably be fine to instantiate the saxon TransformerFactory directly using new net.sf.saxon.TransformerFactoryImpl()

    0 讨论(0)
  • 2020-12-23 22:45

    Can you try by setting the system property in your code like

    System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl");
    

    do this before getting an instance of TransformerFactory.

    Note: this will force all the webapps on your Tomcat to use saxon - so you have to make sure any other webapps which were using the default are okay.

    0 讨论(0)
提交回复
热议问题