How to select saxon TransformerFactory in Java
Asked Answered
I

4

25

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 and there is a Security Manager.

So I have read that it should be possible to do this:

Use the Services API (as detailed in the JAR specification), if available, to determine the classname. The Services API will look for a classname in the file META-INF/services/javax.xml.transform.TransformerFactory in jars available to the runtime.

I found this file in WEB-INF/lib/saxon9.jar but when I istantiate a TransformerFactory, the default factory is always selected instead of a Saxon factory.

How can I select Saxon Transformer Factory?

Thanks

Intoxicating answered 3/6, 2010 at 17:9 Comment(1)
A summary of all approaches below (and more) can be found here: #11315104Parmesan
E
28

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.

Episcopalian answered 2/8, 2010 at 16:1 Comment(2)
This works as of Java v6 (docs.oracle.com/javase/6/docs/api/javax/xml/transform/… vs docs.oracle.com/javase/1.5.0/docs/api/javax/xml/transform/…)Furrow
This doesn't work with the Oracle Fusion Middleware 11.1.1.5.0 release as it uses older implementations within it's client jar files.Subcartilaginous
P
14

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.

Placate answered 4/6, 2010 at 10:43 Comment(0)
U
10

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

Unpremeditated answered 26/5, 2011 at 16:58 Comment(1)
This did not work for me. I used the main META-INF directory of my webapp. Do I need to use a different one?Damnify
B
5

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()

Bejarano answered 4/6, 2010 at 13:22 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.