目前,我参与了一个项目,其中包括验证xml文件。这些文件将由用户通过restapi上传,restapi是用java和spring框架编写的。架构文件位于xml文件[2]中的url[1]中,属性为“schemalocation”。xsd文件也可能包含多个其他模式文件。用户上载的文件是io链接设备描述(iodd)文件。
因此,我当前的问题是在主模式中加载包含的验证方案。我的目标不是下载模式并使用它们进行验证。整个过程必须是动态的。我也不想使用ioddchecker,它是由io-link本身提供的。
我了解到这可以通过resourcesolver接口完成,但是我找不到任何实现可以通过url或类似的方式从主模式加载包含的模式。
那么,你能帮我找到解决这个问题的办法吗?
提前谢谢!
这是验证文件的方法:
public boolean isValid(String file) {
if (file == null || file.isEmpty() || !Files.exists(Path.of(file)) || !Files.isReadable(Path.of(file)))
return false;
else if (this.getStamp() == null || this.getStamp().getChecker() == null)
return false;
else if (this.getStamp().getCrc().isEmpty())
return false;
try {
SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
factory.setErrorHandler(new LineNumberErrorHandler());
Schema schema = factory.newSchema(XsdReceiver.receive(this.schemaLocation));
Validator validator = schema.newValidator();
//validator.validate(new StreamSource(new ByteArrayInputStream(data)));
validator.setResourceResolver(factory.getResourceResolver());
validator.validate(new StreamSource(new File(file)));
} catch (Exception e) {
return false;
}
return true;
}
这是schema receiver方法。它可以工作,但是当模式包含时,验证过程就失败了(此代码下面的错误消息。
public static Source receive(String url) {
url = url.contains(" ") ? url.replace(" ", "/") : url;
try {
URL u = new URL(url);
HttpURLConnection c = (HttpURLConnection)u.openConnection();
int status = c.getResponseCode();
if (status == HttpURLConnection.HTTP_MOVED_TEMP
|| status == HttpURLConnection.HTTP_MOVED_PERM
|| status == HttpURLConnection.HTTP_SEE_OTHER
)
c = (HttpURLConnection) new URL(c.getHeaderField("Location")).openConnection();
return new StreamSource(c.getInputStream());
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
我从验证器收到的错误消息。
Line: 3) schema_reference.4: Failed to read schema document 'IODD-Primitives1.1.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
Line: 4) schema_reference.4: Failed to read schema document 'IODD-Datatypes1.1.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
Line: 5) schema_reference.4: Failed to read schema document 'IODD-Variables1.1.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
Line: 6) schema_reference.4: Failed to read schema document 'IODD-Events1.1.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
Line: 7) schema_reference.4: Failed to read schema document 'IODD-UserInterface1.1.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
Line: 8) schema_reference.4: Failed to read schema document 'IODD-Communication1.1.xsd', because 1) could not find the document; 2) the document could not be read; 3) the root element of the document is not <xsd:schema>.
Line: 180) src-resolve: Cannot resolve the name 'DeviceIdT' to a(n) 'type definition' component.
Line: 180) src-resolve: Cannot resolve the name 'DeviceIdT' to a(n) 'simpleType definition' component.
Line: 191) src-resolve: Cannot resolve the name 'DeviceIdT' to a(n) 'type definition' component.
Line: 228) src-resolve: Cannot resolve the name 'CollectionT' to a(n) 'type definition' component.
Line: 292) src-resolve: Cannot resolve the name 'ObjectT' to a(n) 'type definition' component.
Line: 303) src-resolve: Cannot resolve the name 'CollectionT' to a(n) 'type definition' component.
Line: 312) src-resolve: Cannot resolve the name 'DataItemT' to a(n) 'type definition' component.
Line: 12) src-resolve: Cannot resolve the name 'DocumentInfoT' to a(n) 'type definition' component.
Line: 15) src-resolve: Cannot resolve the name 'CommNetworkProfileT' to a(n) 'type definition' component.
Line: 16) src-resolve: Cannot resolve the name 'ExternalTextCollectionT' to a(n) 'type definition' component.
Line: 22) src-resolve: Cannot resolve the name 'StampT' to a(n) 'type definition' component.
Line: 152) src-resolve: Cannot resolve the name 'TextRefT' to a(n) 'type definition' component.
Line: 153) src-resolve: Cannot resolve the name 'TextRefT' to a(n) 'type definition' component.
Line: 168) src-resolve: Cannot resolve the name 'TextRefT' to a(n) 'type definition' component.
Line: 169) src-resolve: Cannot resolve the name 'TextRefT' to a(n) 'type definition' component.
Line: 195) src-resolve: Cannot resolve the name 'TextRefT' to a(n) 'type definition' component.
Line: 196) src-resolve: Cannot resolve the name 'TextRefT' to a(n) 'type definition' component.
Line: 238) src-resolve: Cannot resolve the name 'DatatypeCollectionT' to a(n) 'type definition' component.
Line: 239) src-resolve: Cannot resolve the name 'VariableCollectionT' to a(n) 'type definition' component.
Line: 250) src-resolve: Cannot resolve the name 'ErrorTypeCollectionT' to a(n) 'type definition' component.
Line: 257) src-resolve: Cannot resolve the name 'EventCollectionT' to a(n) 'type definition' component.
Line: 263) src-resolve: Cannot resolve the name 'UserInterfaceT' to a(n) 'type definition' component.
[1] https://www.io-link.com/iodd/2010/10/iodd1.1.xsd
[2] https://ioddfinder.io-link.com/productvariants/search/11765 (io-link产品tv7105示例)
1条答案
按热度按时间btqmn9zl1#
好吧,我自己解决了这个问题。简而言之,我必须创建一个实现接口的类
LSResourceResolver
并重写该方法resolveResource
.答案很长:
首先我创建了类
XsdReceiver
并让它实现接口LSResourceResolver
. 还有方法resolveResource
现在必须被覆盖。这个方法最终负责查询丢失的xsd文件并将它们作为资源返回LSInput
. (就像LSResourceResolver
,LSInput
是一个接口,因此必须作为一个单独的类实现。我只是叫它Input
). 最后我用了这个方法setResourceResolver
并示例化为创建的类的参数XsdReceiver
.功能:
方法
isValid
应该检查xml模式的有效性。必要的模式文件可以在根标记中找到。它还必须手动传递给SchemaFactory
班级。调用该方法并读取主模式文件后,每个引用的xml或xsd文件都会自动传递给该方法resolveResource
. 这个SchemaFactory
全班为我们做这件事。玩得开心:d