javax.xml xpath.XPath Factoryy provider-configuration file of Saxon-HE 9.3 中的语法错误
我在Mac OS X和Saxon-HE 9.3.0.5上使用Java SE 6。ServiceLoader 无法找到 的 Saxon 实现。javax.xml.xpath.XPathFactory
mac:test2 ludo$ java -version
java version "1.6.0_26"
Java(TM) SE Runtime Environment (build 1.6.0_26-b03-383-11A511)
Java HotSpot(TM) 64-Bit Server VM (build 20.1-b02-383, mixed mode)
查找过程第 3 点中的状态方法的 javadoc,用于本地化以下实现:newInstance
javax.xml.xpath.XPathFactory
类装入器被要求提供与资源目录 META-INF/services 中的 javax.xml xpath.XPathFactory 匹配的服务提供者配置文件。有关文件格式和解析规则,请参阅 JAR 文件规范。
JAR 文件规范的“服务提供程序”部分指出:
该文件应包含唯一具体提供程序类名称的换行符分隔列表。
但是,如果我提取saxon9he.jar文件并查看META-INF目录,我会看到:
mac:Java ludo$ mkdir test
mac:Java ludo$ cd test
mac:test ludo$ jar fx ../saxon9he.jar
mac:test ludo$ cat META-INF/services/javax.xml.xpath.XPathFactory
net.sf.saxon.xpath.XPathFactoryImpl
http\://java.sun.com/jaxp/xpath/dom: net.sf.saxon.xpath.XPathFactoryImpl
http\://saxon.sf.net/jaxp/xpath/om: net.sf.saxon.xpath.XPathFactoryImpl
第一行是正确的,但我不明白为什么有两行额外的行,看起来这些行给ServiceLoader带来了麻烦。我看到了一个测试示例的问题,我写了理解用于查找提供者的mecanism。我们可以看到 saxon9he.jar 在 CLASSPATH 中。
mac:services ludo$ java ServicesTest
CLASSPATH = ..., /Users/ludo/Library/Java/saxon9he.jar, ...
Service XPathFactory: java.util.ServiceLoader[javax.xml.xpath.XPathFactory]
ServiceConfigurationError: javax.xml.xpath.XPathFactory: jar:file:/Users/ludo/Library/Java/saxon9he.jar!/META-INF/services/javax.xml.xpath.XPathFactory:2: Illegal configuration-file syntax
感兴趣的是:
jar:file:/Users/ludo/Library/Java/saxon9he.jar!/META-INF/services/javax.xml.xpath.XPathFactory:2: Illegal configuration-file syntax
是Saxon的错误还是我的系统不支持的扩展语法?我能做些什么来解决这个问题?
请注意,如果我显式选择实现的类,我可以得到一个工厂。但我想使用服务机制。以下代码有效:
XPathFactory xpf = XPathFactory.newInstance(
XPathFactory.DEFAULT_OBJECT_MODEL_URI,
"net.sf.saxon.xpath.XPathFactoryImpl",
ClassLoader.getSystemClassLoader());
我在下面添加了整个Java测试程序。
import java.net.URL;
import java.net.URLClassLoader;
import java.util.Iterator;
import java.util.ServiceConfigurationError;
import java.util.ServiceLoader;
import javax.xml.xpath.XPathFactory;
public class ServicesTest {
public static String getClasspathString() {
StringBuilder classpath = new StringBuilder();
ClassLoader classLoader = ClassLoader.getSystemClassLoader();
URL[] urls = ((URLClassLoader) classLoader).getURLs();
for (int i = 0; i < urls.length - 1; i++) {
classpath.append(urls[i].getFile()).append(", ");
}
if (urls.length > 0) {
classpath.append(urls[urls.length - 1].getFile());
}
return classpath.toString();
}
public static void availableProviders(ServiceLoader sl) {
Iterator it = sl.iterator();
int index = 0;
for (;;) {
try {
if (!it.hasNext()) {
break;
}
index++;
Object o = it.next();
System.out.printf("%03d Concrete class name: %s\n", index, o.getClass().getName());
} catch (ServiceConfigurationError e) {
System.err.printf("ServiceConfigurationError: %s\n", e.getMessage());
}
}
}
public static void main(String[] args) {
System.out.printf("CLASSPATH = %s\n", getClasspathString());
System.out.println();
ServiceLoader<XPathFactory> slXPathFactory = ServiceLoader.load(XPathFactory.class);
System.out.printf("Service XPathFactory: %s\n", slXPathFactory.toString());
availableProviders(slXPathFactory);
}
}