0

In my Ant build script I set my xslt processor with the following lines:

    <xslt>
        <factory>
            <attribute name="http://saxon.sf.net/feature/xinclude-aware" value="true" />
            <attribute name="http://saxon.sf.net/feature/version-warning" value="false" />
        </factory>
        <classpath>
            <fileset dir="${param}/lib">
                <include name="saxon9.jar" />
            </fileset>
        </classpath>
    </xslt>

When ANT runs from the command line, like this

"C:\Programs\jre\bin\java.exe" -classpath "C:\ant\lib\ant-launcher.jar" "-Dant.home=C:\ant" org.apache.tools.ant.launch.Launcher Install -Dparam "C:\dir" basedir "C:\dir2\bin" -emacs -f "C:\dir2\build.xml"

The build completes successful. However when I call the script from within a java program like this:

File buildFile = new File(dir2 + "/build.xml");
Project p = new Project();

p.setUserProperty("ant.file", buildFile.getAbsolutePath());
p.setUserProperty("ant.library.dir", "C:/dir2/lib");
p.setUserProperty("param", "C:/dir");
p.setBasedir("C:/dir2/bin");               

DefaultLogger consoleLogger = new DefaultLogger();

consoleLogger.setEmacsMode(true); 
consoleLogger.setErrorPrintStream(System.err);
consoleLogger.setOutputPrintStream(System.out);
consoleLogger.setMessageOutputLevel(Project.MSG_INFO);
p.addBuildListener(consoleLogger);

try {
 p.fireBuildStarted();
 p.initProperties();
 p.init();
 ProjectHelper helper = ProjectHelper.getProjectHelper();
 p.addReference("ant.projectHelper", helper);
 helper.parse(p, buildFile);

p.executeTarget("Install");
p.fireBuildFinished(null);
} catch (BuildException e) {
   p.fireBuildFinished(e);
}

The program returns the following xslt error: TransformerFactory does not recognise attribute 'http://saxon.sf.net/feature/xinclude-aware'. at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.setAttribute(Unknown Source). This error is similar as in How to execute XSLT 2.0 with ant?.

I tried several options to set the lib directory containing saxon (like with ant.lib.directory), but this doesn't have any effect. What is the correct way to set the lib directory from java (or force noclasspath)?

Community
  • 1
  • 1
user2209562
  • 244
  • 2
  • 16

1 Answers1

0

Have you tried setting the "name" attribute of the "factory" element to "net.sf.saxon.TransformerFactoryImpl"?

Michael Kay
  • 156,231
  • 11
  • 92
  • 164
  • Yes, I did. It worked when using an ant version later then 1.8.1. However, because I had no control over the build files I was calling, I had to use ant 1.7. There the TransformerFactory on the name attribute of the factory element was ignored. Setting the classpath for Saxon and adding System variable in my Java code kept my processor on Saxon: System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl"); TransformerFactory tfactory = TransformerFactory.newInstance(); – user2209562 Mar 24 '14 at 08:13
  • Unfortunately there's a long history of Ant bugs in this area. I suspect they don't have a good set of test cases, but that's just conjecture. – Michael Kay Mar 24 '14 at 13:24