TransformerFactory and Xalan Dependency Conflict

后端 未结 6 1687
我寻月下人不归
我寻月下人不归 2021-01-12 21:54

I have the following code:

javax.xml.transform.TransformerFactory factory = TransformerFactory.newInstance();
factory.setAttribute(XMLConstants.ACCESS_EXTERN         


        
相关标签:
6条回答
  • 2021-01-12 22:41

    If you are multiple XSL processors and or different versions, you have to handle the case that not every implementation will be able to handle every attribute. The only way to do so is to catch the IllegalArgumentException that is thrown if the attribute is not supported. Take a look at this modified example from the JAXP documentation:

    javax.xml.transform.TransformerFactory factory = TransformerFactory.newInstance();
    
    try {
        factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
    } catch (IllegalArgumentException e) {
        //jaxp 1.5 feature not supported
    }
    

    The documentation says:

    When code change is possible, and for new development, it is recommended that the new properties be set as demonstrated above. By setting the properties this way, applications can be sure to maintain the desired behavior whether they are deployed to older or newer version of the JDK, or whether the properties are set through System Properties or jaxp.properties.

    0 讨论(0)
  • 2021-01-12 22:46

    It might be coming from other xalan version of your project.

    Check Dependent Hierarchy in your POM for the xalan and exclude the xercesImpl in all the version of xalan.

    0 讨论(0)
  • 2021-01-12 22:50

    Excluding Xerces from Xalan fixes this issue:

    <dependency>
        <groupId>xalan</groupId>
        <artifactId>xalan</artifactId>
        <version>2.7.2</version>
        <exclusions>
            <exclusion>
                <groupId>xerces</groupId>
                <artifactId>xercesImpl</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    
    0 讨论(0)
  • 2021-01-12 22:55

    I was facing a similar issue where an implementation from SaxonJ was being created in TransformerFactory::newInstance, giving me errors when trying to set attributes not supported by it.

    Taking a look at the method documentation, I found out that TransformerFactory has a priority list where it tries to find an implementation to return.

    The first place it looks for is in the system properties, so in my Ant file, inside my run target, I added the following(other libraries will have the same):

    <jvmarg value="-Djavax.xml.transform.TransformerFactory=com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl"/>
    

    This will make TransformerFactory::newInstance load the correct factory implementation.

    Keep in mind that I was using OpenJDK8, you will have to find the correct package for other versions.

    0 讨论(0)
  • 2021-01-12 22:56

    Need to set the system-level property as below

    System.setProperty("javax.xml.transform.TransformerFactory","com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl");
    
    0 讨论(0)
  • 2021-01-12 22:59

    The solution that worked for me was by doing this:

    compile('org.opensaml:opensaml:2.6.1') {
        exclude group: 'xerces', module: 'xercesImpl'
        exclude module: 'xalan'
    }
    
    0 讨论(0)
提交回复
热议问题