I would like to provide diagnostic information about what JAXP implementation is in use, and which JAR file it was loaded from.
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());
}
Is there a better way to achieve this, perhaps without having to create a DocumentBuilderFactory?
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.
From the Official JAXP FAQ (Question 14):
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.
The following code will output information about the four main JAXP factories:
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: