gpt4 book ai didi

java - 如何找出正在使用的 JAXP 实现以及它是从哪里加载的?

转载 作者:IT老高 更新时间:2023-10-28 21:05:10 33 4
gpt4 key购买 nike

我想提供有关正在使用的 JAXP 实现以及从哪个 JAR 文件加载它的诊断信息。

实现此目的的一种方法是在例如 DocumentBuilderFactory 的实例中创建,然后检查该类的属性:

private static String GetJaxpImplementation() {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
Class<? extends DocumentBuilderFactory> c = documentBuilderFactory.getClass();
Package p = c.getPackage();
CodeSource source = c.getProtectionDomain().getCodeSource();
return MessageFormat.format(
"Using JAXP implementation ''{0}'' ({1}) version {2} ({3}){4}",
p.getName(),
p.getImplementationVendor(),
p.getSpecificationVersion(),
p.getImplementationVersion(),
source == null ? "." : " loaded from: " + source.getLocation());
}

有没有更好的方法来实现这一点,也许不必创建 DocumentBuilderFactory

最佳答案

在不实际创建实例的情况下,很难预测将加载哪些具体的 JAXP 工厂实现,因为选择实现的过程。

来自 Official JAXP FAQ (问题 14):

When an application wants to create a new JAXP DocumentBuilderFactory instance, it calls the staic method DocumentBuilderFactory.newInstance(). This causes a search for the name of a concrete subclass of DocumentBuilderFactory using the following order:

  1. The value of a system property like javax.xml.parsers.DocumentBuilderFactory if it exists and is accessible.
  2. The contents of the file $JAVA_HOME/jre/lib/jaxp.properties if it exists.
  3. The Jar Service Provider discovery mechanism specified in the Jar File Specification. A jar file can have a resource (i.e. an embedded file) such as META-INF/services/javax.xml.parsers.DocumentBuilderFactory containing the name of the concrete class to instantiate.
  4. The fallback platform default implementation.

除了这种复杂性之外,每个单独的 JAXP 工厂都可以指定一个独立的实现。使用一个解析器实现和另一个 XSLT 实现是很常见的,但是上面选择机制的粒度允许您在更大程度上混合和匹配。

以下代码将输出有关四个主要 JAXP 工厂的信息:

private static void OutputJaxpImplementationInfo() {
System.out.println(getJaxpImplementationInfo("DocumentBuilderFactory", DocumentBuilderFactory.newInstance().getClass()));
System.out.println(getJaxpImplementationInfo("XPathFactory", XPathFactory.newInstance().getClass()));
System.out.println(getJaxpImplementationInfo("TransformerFactory", TransformerFactory.newInstance().getClass()));
System.out.println(getJaxpImplementationInfo("SAXParserFactory", SAXParserFactory.newInstance().getClass()));
}

private static String getJaxpImplementationInfo(String componentName, Class componentClass) {
CodeSource source = componentClass.getProtectionDomain().getCodeSource();
return MessageFormat.format(
"{0} implementation: {1} loaded from: {2}",
componentName,
componentClass.getName(),
source == null ? "Java Runtime" : source.getLocation());
}

以下示例输出说明了三种不同 JAXP 实现(内置 Xerces 和 Xerces 2.8 和 Xalan 的外部 JAR)的混合搭配:

DocumentBuilderFactory implementation: org.apache.xerces.jaxp.DocumentBuilderFactoryImpl loaded from: file:/C:/Projects/Scratch/lib/xerces-2.8.0.jar
XPathFactory implementation: com.sun.org.apache.xpath.internal.jaxp.XPathFactoryImpl loaded from: Java Runtime
TransformerFactory implementation: org.apache.xalan.processor.TransformerFactoryImpl loaded from: file:/C:/Projects/Scratch/lib/xalan.jar
SAXParserFactory implementation: org.apache.xerces.jaxp.SAXParserFactoryImpl loaded from: file:/C:/Projects/Scratch/lib/xerces-2.8.0.jar

关于java - 如何找出正在使用的 JAXP 实现以及它是从哪里加载的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1798366/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com