phax / ph-schematron

Java Schematron library that supports XSLT and native application
Apache License 2.0
111 stars 36 forks source link

Ant-Task: Specifying classpath internally in the build script leads to Failed to parse error #78

Closed nkutsche closed 5 years ago

nkutsche commented 5 years ago

Hi,

In the documentation about declaring the ant task there is this paragraph:

For this Ant Task to be available you need to include the ph-schematron-ant-task "JAR with dependencies" in your classpath. Alternatively you can use the classpath attribute to reference a classpath that is defined internally in the build script.

At least in my case the "Alternative" does not work.

My ant script:

<?xml version="1.0" encoding="UTF-8"?>
<project basedir="." name="ph-schematron test" default="db.common.validation.schematron">

    <taskdef name="schematron" classname="com.helger.schematron.ant.Schematron" classpath="lib1.0/ph-schematron-ant-task-5.1.0-jar-with-dependencies.jar"/>

    <target name="db.common.validation.schematron">
        <schematron schematronFile="test.sch" expectSuccess="true" failonerror="true">
            <file file="test.xml"/>
        </schematron>
    </target>

</project>

If I just call cls && ant -f build.xml I get this log:

Buildfile: C:\Users\Nico\Desktop\tests\ant\schematron\ph-schematron-usage\build.xml

db.common.validation.schematron:
[schematron] [main] WARN com.helger.xml.transform.LoggingTransformErrorListener - [warn] Transformation warning (javax.xml.transform.TransformerConfigurationException: jar:file:/C:/Users/Nico/Desktop/tests/ant/schematron/ph-schematron-usage/lib1.0/ph-schematron-ant-task-5.1.0-jar-with-dependencies.jar!/schematron/20100710-xslt2/iso_schematron_skeleton_for_saxon.xsl: line 580: Attribut "version" au▀erhalb des Elements.)
[schematron] [main] WARN com.helger.xml.transform.LoggingTransformErrorListener - [warn] Transformation warning (javax.xml.transform.TransformerConfigurationException: jar:file:/C:/Users/Nico/Desktop/tests/ant/schematron/ph-schematron-usage/lib1.0/ph-schematron-ant-task-5.1.0-jar-with-dependencies.jar!/schematron/20100710-xslt2/iso_schematron_skeleton_for_saxon.xsl: line 604: Attribut "version" au▀erhalb des Elements.)
[schematron] [main] WARN com.helger.xml.transform.LoggingTransformErrorListener - [warn] Transformation warning (javax.xml.transform.TransformerConfigurationException: jar:file:/C:/Users/Nico/Desktop/tests/ant/schematron/ph-schematron-usage/lib1.0/ph-schematron-ant-task-5.1.0-jar-with-dependencies.jar!/schematron/20100710-xslt2/iso_schematron_skeleton_for_saxon.xsl: line 623: Attribut "version" au▀erhalb des Elements.)
[schematron] [main] ERROR com.helger.xml.transform.XMLTransformerFactory - Failed to parse javax.xml.transform.dom.DOMSource@47dd778
[schematron] javax.xml.transform.TransformerConfigurationException: Fehler beim Pr³fen des Typs des Ausdrucks "funcall(document-uri, [AbsoluteLocationPath(null)])".
[schematron]    at com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl.newTemplates(TransformerFactoryImpl.java:988)
[schematron]    at com.helger.xml.transform.XMLTransformerFactory.newTemplates(XMLTransformerFactory.java:300)
[schematron]    at com.helger.schematron.xslt.SchematronProviderXSLTFromSCH.<init>(SchematronProviderXSLTFromSCH.java:195)
[schematron]    at com.helger.schematron.xslt.SchematronResourceSCHCache.createSchematronXSLTProvider(SchematronResourceSCHCache.java:69)
[schematron]    at com.helger.schematron.xslt.SchematronResourceSCHCache.getSchematronXSLTProvider(SchematronResourceSCHCache.java:145)
[schematron]    at com.helger.schematron.xslt.SchematronResourceSCH.getXSLTProvider(SchematronResourceSCH.java:92)
[schematron]    at com.helger.schematron.xslt.AbstractSchematronXSLTBasedResource.isValidSchematron(AbstractSchematronXSLTBasedResource.java:188)
[schematron]    at com.helger.schematron.ant.Schematron.execute(Schematron.java:752)
[schematron]    at org.apache.tools.ant.UnknownElement.execute(UnknownElement.java:292)
[schematron]    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[schematron]    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[schematron]    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[schematron]    at java.lang.reflect.Method.invoke(Method.java:498)
[schematron]    at org.apache.tools.ant.dispatch.DispatchUtils.execute(DispatchUtils.java:99)
[schematron]    at org.apache.tools.ant.Task.perform(Task.java:350)
[schematron]    at org.apache.tools.ant.Target.execute(Target.java:449)
[schematron]    at org.apache.tools.ant.Target.performTasks(Target.java:470)
[schematron]    at org.apache.tools.ant.Project.executeSortedTargets(Project.java:1391)
[schematron]    at org.apache.tools.ant.Project.executeTarget(Project.java:1364)
[schematron]    at org.apache.tools.ant.helper.DefaultExecutor.executeTargets(DefaultExecutor.java:41)
[schematron]    at org.apache.tools.ant.Project.executeTargets(Project.java:1254)
[schematron]    at org.apache.tools.ant.Main.runBuild(Main.java:830)
[schematron]    at org.apache.tools.ant.Main.startAnt(Main.java:223)
[schematron]    at org.apache.tools.ant.launch.Launcher.run(Launcher.java:284)
[schematron]    at org.apache.tools.ant.launch.Launcher.main(Launcher.java:101)
[schematron] Caused by: Fehler beim Pr³fen des Typs des Ausdrucks "funcall(document-uri, [AbsoluteLocationPath(null)])".
[...]
[schematron] [main] WARN com.helger.schematron.xslt.SchematronResourceSCHCache - The Schematron resource 'C:\Users\Nico\Desktop\tests\ant\schematron\ph-schematron-usage\test.sch' is invalid!

BUILD FAILED
C:\Users\Nico\Desktop\tests\ant\schematron\ph-schematron-usage\build.xml:18: Error in Schematron: [error] Transformation error (javax.xml.transform.TransformerException: Stylesheet konnte nicht kompiliert werden)

Total time: 1 second

For some reasons it tries to use the Xalan? (com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl)

If I call the same script with cls && ant -lib=lib1.0 -f build.xml it works:

db.common.validation.schematron:
[schematron] Successfully parsed Schematron file 'C:\Users\Nico\Desktop\tests\ant\schematron\ph-schematron-usage\test.sch'
[schematron] Validating XML file 'C:\Users\Nico\Desktop\tests\ant\schematron\ph-schematron-usage\test.xml' against Schematron rules from 'test.sch' expecting success
[schematron] [main] INFO com.helger.jaxb.JAXBContextCache - Creating JAXB context for package org.oclc.purl.dsdl.svrl using ClassLoader java.net.URLClassLoader@4dd8dc3

BUILD FAILED
C:\Users\Nico\Desktop\tests\ant\schematron\ph-schematron-usage\build.xml:18: 1 Schematron error for XML file 'C:\Users\Nico\Desktop\tests\ant\schematron\ph-schematron-usage\test.xml'

Total time: 3 seconds

This leads in my case to nasty nested ant calls in my ant build scripts, because we need a simple ant call on the commandline. It works with such workarounds, but the "Alternative" with the classpath attribute would make it really cool.

Background infos:

phax commented 5 years ago

Try something like

  <path id="phsch.path">
    <fileset dir="../../../../target">
        <include name="*-jar-with-dependencies.jar"/>
    </fileset>
  </path>
  <taskdef name="schematron" classname="com.helger.schematron.ant.Schematron" classpathref="phsch.path" />

I am not an ANT expert :(

nkutsche commented 5 years ago

I am not an ANT expert :(

Me neither. But anything I tried, didn't worked. With path/fileset, taskdef/@classpath, taskdef/classpath, etc. Every time the same result.

And something seems to work, because if I ommit the any classpath reference I get this (for sure):

taskdef class com.helger.schematron.ant.Schematron cannot be found

There is an open stackoverflow question about a very similar error message. Comment from Micheal Kay was:

Well you're definitely running Xalan rather than Saxon.

phax commented 5 years ago

I can reproduce the error. It seems like a problem with created "JAR with dependencies". Maybe this is because ANT loads Xalan itself as the primary JAXP provider? I am investigating it

phax commented 5 years ago

The reasons seems to be that Thread.currentThead ().getContextClassLoader() is not returning the correct ANT class loader. When passing an explicit classloader to the TransformerFactory the ANTClassLoader is used.

phax commented 5 years ago

This is fixed in the 5.2.0 release

nkutsche commented 5 years ago

It's a pleasure to work with such a well-maintained open source project! It works perfect.

phax commented 5 years ago

A star on the project is always appreciated :)