1

I have a simple program which have to make xslt transformation of given xml(>1GB).

public static void main(String[] args) throws Exception {
    if (args.length != 2) {
        System.err.println(
            "Usage: java Transform [xmlfile] [xsltfile]");
        System.exit(1);
    }

    File xmlFile = new File(args[0]);
    File xsltFile = new File(args[1]);

    // JAXP reads data using the Source interface
    Source xmlSource = new StreamSource(xmlFile);
    Source xsltSource = new StreamSource(xsltFile);

    // the factory pattern supports different XSLT processors
    TransformerFactory transFact =
            TransformerFactory.newInstance();
    Transformer trans = transFact.newTransformer(xsltSource);

    trans.transform(xmlSource, new StreamResult(System.out));
}

I want to use Xalan 2.7.1 so from the documentation I read:

Finally, if JAXP cannot find an implementation class from any of the three locations, it uses its default implementation of TransformerFactory. To summarize, here are the steps that JAXP performs when attempting to locate a factory:

  1. Use the value of the javax.xml.transform.TransformerFactory system property if it exists.
  2. If JRE/lib/jaxp.properties exists, then look for a javax.xml.transform.TransformerFactory=ImplementationClass entry in that file.
  3. Use a JAR file service provider to look for a file called META-INF/services/javax.xml.transform.TransformerFactory in any JAR file on the CLASSPATH.
  4. Use the default TransformerFactory instance. (Implementation of a JAXP1.1 TransformerFactory for Translets.) от com.sun.org.apache.xalan.internal.xsltc.trax

So if I put Xalan-2.7.1.jar on the classpath (3) will be hit and everything will be OK. But I am wondering which is the best way to define TransformerFactoryImpl?

I think as a variant to create jaxp.properties file and to put in the resource dir and in it to specify

factoryImpl=org.apache.xalan.processor.TransformerFactoryImpl

and then to set the javax.xml.transform.TransformerFactoryproperty in the code. In that way I when the implantation of the processor is changed only the properties file will be changed.

But this complicate the logic and I am wondering if I change the implementation JVM will find the new jar on the classpath and will other implementation be used. So is there a good reason to choose the properties decision or just adding the proper jar will fix all the problems? What will happen if other jar comes with transitive dependency to Saxon for example and I do not use the properties way?

Xelian
  • 16,680
  • 25
  • 99
  • 152
  • Why can't you just set the system property on run that guarantees that you'll use that particular transformer in the classpath? – Neil Feb 11 '16 at 09:08
  • @Neil I can but wondering which is the best decision from design point of view. – Xelian Feb 11 '16 at 09:23
  • To do that, set the system property and simply add the jar to the classpath. There is no way that could be misinterpreted incorrectly. – Neil Feb 11 '16 at 09:25
  • 1
    Personally, I think it's a good idea to write your application so it is in control of the decision which XSLT processor to use. Picking up a random XSLT processor from the classpath invites errors. The best way to do that is to instantiate the XSLT engine explicitly and ignore the JAXP factory mechanism. The only time to use the JAXP mechanism is if you explicitly want to delegate the choice of XSLT processor to the user. – Michael Kay Feb 11 '16 at 14:29
  • @Michael Kay so you say the best way is explicitly to specify the processor during factory creation? TransformerFactory transFact = new org.apache.xalan.processor.TransformerFactoryImpl()? If we do not user to choose the processor itself? – Xelian Feb 11 '16 at 14:50
  • 1
    Well, I don't know your project requirements but if you've decided that you definitely want to use Apache Xalan then that is what I would do. Setting the system property is too global: it affects other components running in the same JVM. – Michael Kay Feb 11 '16 at 21:20
  • Possible duplicate of [Using Xalan alongside Saxon](http://stackoverflow.com/questions/11224850/using-xalan-alongside-saxon) – Paul Sweatte Oct 14 '16 at 14:28

0 Answers0