java - 是否可以避免使用 xalan TransformerFactory?

标签 java xml xalan

我有以下代码:

final TransformerFactory factory = TransformerFactory.newInstance();

factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");

第二行在具有默认 TransformerFactory 的现代 JDK(我试过 1.8)中运行良好。但是当我将 xalan(版本 2.7.2,最新版本)添加到类路径时,我在第二行得到以下内容:

Exception in thread "main" java.lang.IllegalArgumentException: Not supported: http://javax.xml.XMLConstants/property/accessExternalDTD
    at org.apache.xalan.processor.TransformerFactoryImpl.setAttribute(TransformerFactoryImpl.java:571)
    at Main.main(Main.java:11)

我猜这是因为xalan的TransformerFactory不支持这个属性。 Xalan 的实现通过 ServiceLoader 机制获取:它在 xalan jar 的 services/javax.xml.transform.TransfomerFactory 中指定。

可以使用 javax.xml.transform.TransformerFactory 系统属性或 $JRE/lib/jaxp.properties 覆盖 TransformerFactory 实现code> 文件,或者直接在代码中传递类名。但要做到这一点,我必须提供一个具体的类名。现在,它是 com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl,但是在系统属性中对其进行硬编码有点可怕,因为在 JDK 升级时它们可以轻松地更改类名,我们只会得到一个运行时错误。

有什么方法可以指示 TransformerFactory.newInstance() 忽略 xalan 提供的实现吗?或者告诉它“只使用系统默认值”。

附言我不能只从类路径中删除 xalan,因为我们使用的许多其他库都依赖于它。

最佳答案

我在这里唯一可以实现的是硬编码 JDK 默认工厂并使用正常的发现过程作为后备:

TransformerFactory factory;
try {
   //the open jdk implementation allows the disabling of the feature used for XXE
    factory = TransformerFactory.newInstance("com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl", SecureXmlFactories.class.getClassLoader());
} catch (Exception | TransformerFactoryConfigurationError e) {
    //this part uses the default implementation of in xalan 2.7.2
   LOGGER.error("Cannot load default TransformerFactory, le's try the usual way", e);
   //not advisable if you dont want your application to be vulnerable. If needed you can put null here.
   factory = TransformerFactory.newInstance();

}

然后在try/catch下配置

// this works everywhere, but it does not disable accessing
// external DTDs... still enabling it just in case
try {
    factory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
} catch (TransformerConfigurationException e) {
    LOGGER.error("Cannot enable secure processing", e);
}

// this does not work in Xalan 2.7.2
try {
    factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
} catch (Exception e) {
    LOGGER.error("Cannot disable external DTD access", e);
}
// this does not work in Xalan 2.7.2
try {
    factory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
} catch (Exception e) {
    LOGGER.error("Cannot disable external stylesheet access", e);
}

并监控日志以查看默认 JDK 工厂类名称是否/何时更改。

关于java - 是否可以避免使用 xalan TransformerFactory?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50000756/

相关文章:

java - 使用@JsonProperty 时是否需要使用@JsonCreator?

java - 如何将一个节点链插入另一个节点链

python - 无法使用默认命名空间写入 XML 文件

java - 使用 Xalan-J 的 xsl cdata-section-elements 输出属性

java - ant - 尝试复制到/lib/endorsed,库在 Windows 7 中无法用于下一个任务

Java 脚本 XSLT 错误 : For extension function, 找不到方法 java.lang.String。 ([ExpressionContext,] #STRING)

java - 无法在 ArrayList 中添加对象?

java - 错误: "non static variable this cannot be referenced from a static context [duplicate]

sql-server - MsSQL xml 解析为十进制

Java:如何将 XML 流拆分为具有父节点的小型 XML 文档。 VTD-XML