如何找出正在使用的JAXP实现以及从哪里加载? [英] How do I find out which JAXP implementation is in use and where it was loaded from?

查看:230
本文介绍了如何找出正在使用的JAXP实现以及从哪里加载?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to provide diagnostic information about what JAXP implementation is in use, and which JAR file it was loaded from.

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

One way to achieve this is to create in instance of, for example, a DocumentBuilderFactory, and then inspect the properties of that class:

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

Is there a better way to achieve this, perhaps without having to create a DocumentBuilderFactory?

推荐答案

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

It is quite difficult to predict what concrete JAXP factory implementation will be loaded without actually creating an instance because the process for selecting an implementation.

来自官方JAXP常见问题解答(问题14):


当应用程序想要创建
时,新的JAXP DocumentBuilderFactory
实例,它调用staic方法
DocumentBuilderFactory.newInstance()
这导致使用
以下顺序搜索
DocumentBuilderFactory
具体子类的名称:

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. 系统属性的值,如 javax.xml.parsers.DocumentBuilderFactory 如果存在且可以访问。

  2. 文件的内容 $ JAVA_HOME / jre / lib / jaxp.properties (如果存在)。

  3. Jar文件规范中指定的Jar服务提供程序发现机制。 jar文件可以有一个资源(即嵌入文件),例如 META-INF / services / javax.xml.parsers.DocumentBuilderFactory ,包含要实例化的具体类的名称。

  4. 后备平台默认实现。

  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实现,但上面选择机制的粒度允许您进行更大程度的混合和匹配。

Adding to this complexity, each individual JAXP factory can have an independent implementation specified. It is common to use one parser implementation and another XSLT implementation, but the granularity of the selection mechanism above allows you to mix and match to an even greater degree.

以下代码将输出有关四个主要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());
}

以下示例输出说明了混合 - 和-match有三种不同的JAXP实现(内置Xerces和Xerces 2.8和Xalan的外部JAR)协同工作:

The following sample output illustrates a mix-and-match of three different JAXP implementations (Built-in Xerces and external JARs for Xerces 2.8 and Xalan) working together:

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

这篇关于如何找出正在使用的JAXP实现以及从哪里加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆