javax.xml.validation.Schema未正确读取JBoss EAP 5.1中的服务器文件



我在RESTEasy web服务上运行了以下Java代码,以获得一个模式文件来验证xml(注意:项目文件夹名为"MyWebService"):

String classDir = this.getClass().getProtectionDomain().getCodeSource().getLocation().getPath();    
String myWebServiceDir = classDir.substring(0, classDir.lastIndexOf("MyWebService"));       
String instanceXSDPath = myWebServiceDir + SCHEMAS_FOLDER + xsdFile;
System.out.println("Streamsource file location: " + instanceXSDPath);
File file = new File(instanceXSDPath);
//StreamSource streamSource = new StreamSource(file);
// Note: Using StreamSource or File seems to make no difference to the issue
Schema schema = factory.newSchema(file);

当我在本地机器上运行上述代码时,一切都很好。然而,当我在开发服务器上运行上述代码时,我会得到以下错误:

2013-11-20 12:47:48275 INFO〔STDOUT〕(ajp-27.0.0.1-8009-4)流源文件位置:文件:/Y:/jboss/jboss as/server/default/deploy/DEVELOVER_deploy/Schemas/Extensions/1/instance.xsd2013-11-20 12:47:48275错误[com.mywebservice.dao.validate](ajp-27.0.0.1-8009-4)无法分析给定对象的架构验证:org.xml.sax.SAXParseException:schema_rereference.4:失败读取架构文档'file:/Y:/jboss/jboss as/bin/file:/Y:/jboss/jboss as/server/default/deploy/DEVELOPER_deploy/Schemas/Extensions/1/instance.xsd',因为1)找不到文档;2) 该文档不能阅读3) 文档的根元素不是。在org.apache.xerces.util.ErrorHandlerWrapper.createSAXParseException(未知来源)

我不明白为什么在服务器EAP日志上,上面的代码试图从如下路径读取instance.xsd:

'file:/Y:/jboss/jboss-as/bin/file:/Y:/jboss/jboss-as/server/default/deploy/DEVELOPER_DEPLOY/Schemas/Extensions/1/instance.xsd'

因为在我的localhostEAP日志上,它正在从如下位置读取instance.xsd:

'位置:/C:/Users/chen/workspace/.metadata/.plugins/org.jboss.ide.eclipse.as.core/Joss_5.1_Runtime_Server1382499548190/deploy/Schemas/Extensions/1/instance.xsd'

有人知道为什么吗?为什么打印消息在服务器上说"文件",而在每个服务器的服务器日志中我的本地主机上说"位置"?也许这与这个问题有关。

答案是使用StreamSource并将StreamSource的系统Id设置为其原始路径,如下所示:

StreamSource streamSource = new StreamSource(file);
System.out.println("Stream source system id: " + streamSource.getSystemId());
streamSource.setSystemId(xsltPath);
Schema schema = factory.newSchema(streamSource);  // change for each artefact type

这起到了防止两个"file:"位置混合的作用,因为它将系统Id硬连接为一个。我不知道为什么服务器将两个"file:"位置混合到系统Id中,但谁在乎呢,问题解决了!