Saxon-HE 9.3的javax.xml.xpath.XPathFactory提供程序-配置文件中的语法错误 [英] Syntax error in javax.xml.xpath.XPathFactory provider-configuration file of Saxon-HE 9.3

查看:46
本文介绍了Saxon-HE 9.3的javax.xml.xpath.XPathFactory提供程序-配置文件中的语法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Mac OS X和Saxon-HE 9.3.0.5上使用Java SE 6.ServiceLoader无法找到 javax.xml.xpath.XPathFactory 的Saxon实现.

I am using Java SE 6 on Mac OS X and Saxon-HE 9.3.0.5. The ServiceLoader is not able to find the Saxon implementation of 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)

javax.xml.xpath.XPathFactory newInstance 方法的Javadoc在查找过程的第3点中指出以实现以下实现的本地化:

The javadoc of the newInstance method of javax.xml.xpath.XPathFactory states in point 3 of the lookup procedure to localize an implementation that:

要求类装入器在资源目录META-INF/services中提供与javax.xml.xpath.XPathFactory匹配的服务提供者提供程序配置文件.有关文件格式和解析规则,请参见JAR文件规范.

The class loader is asked for service provider provider-configuration files matching javax.xml.xpath.XPathFactory in the resource directory META-INF/services. See the JAR File Specification for file format and parsing rules.

服务提供商部分指出:

文件应包含以换行符分隔的唯一的具体提供程序类名称的列表.

The file should contain a newline-separated list of unique concrete provider-class names.

但是,如果我提取saxon9he.jar文件并查看META-INF目录,则会看到:

But if I extract the saxon9he.jar file and look into the META-INF directory, I see:

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造成了麻烦.我看到了一个测试示例的问题,该示例编写了理解用于查找提供程序的机制的知识.我们可以看到saxon9he.jar在CLASSPATH中.

The first line is correct but I can't see why there are two extra lines and it looks like those lines are causing trouble to ServiceLoader. I saw the problem with a test example that I wrote the understand the mecanism used to find a provider. We can see that saxon9he.jar is in the 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的错误还是系统不支持的扩展语法?我该怎么做才能解决这个问题?

Is it a bug of Saxon or an extended syntax not supported by my system ? What could i do to solve the issue ?

请注意,如果我明确选择实现的类,则可以获得工厂.但是我想使用服务机制.以下代码有效:

Note that if I explicitly choose the class for the implementation, I can get a factory. But I want to use the Services mechanism. The following code works:

XPathFactory xpf = XPathFactory.newInstance(
  XPathFactory.DEFAULT_OBJECT_MODEL_URI,
  "net.sf.saxon.xpath.XPathFactoryImpl",
  ClassLoader.getSystemClassLoader());

我在下面添加了整个Java测试程序.

I have added the whole Java test program below.

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);
    }
}

推荐答案

Michael Kay

Michael Kay answered the question on a SourceForge forum. He said that:

选择文件格式来规避JDK5错误.

The format of the file was chosen to circumvent a JDK5 bug.

还有:

实际上,我还是不建议您使用JAXP搜索机制.它非常慢,并且提供了XPath引擎,该引擎不一定与您的应用程序一起使用.您无法知道是否获得了XPath 1.0或2.0的实现,而且API的定义如此微弱,因此除非您首先使用该提供程序进行了测试,否则您的应用程序几乎无法与特定的提供程序一起使用.因此,即使没有此错误,我也会避免.

Actually, I wouldn't recommend using the JAXP search mechanism anyway. It's very slow, and it delivers an XPath engine that won't necessarily work with your application. You have no way of knowing whether you get an XPath 1.0 or 2.0 implementation back, and the API is so weakly defined that there's very little chance your application will work with a particular provider unless you have tested it with that provider first. So even without this bug, I would steer clear of it.

我认为它可以回答问题,即使它没有为该问题提供明确的解决方法.因此,我们可以通过以下方式选择实现:

I think it answers the question even if it does not provide an explicit fix for the problem. So we could chose the implementation by writing:

XPathFactory xpf = XPathFactory.newInstance(
  XPathFactory.DEFAULT_OBJECT_MODEL_URI,
  "net.sf.saxon.xpath.XPathFactoryImpl",
  ClassLoader.getSystemClassLoader());

这篇关于Saxon-HE 9.3的javax.xml.xpath.XPathFactory提供程序-配置文件中的语法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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