Jena-写入owl文件 - 意外的结果结果

时间:2014-07-16 06:26:36

标签: java rdf jena owl fuse

我创建了一个文件系统,用于在owl文件中存储文件和文件夹的元数据。

对于文件系统,我使用的是FUSE的java绑定,即FUSE-JNA

对于OWL,我正在使用耶拿:

最初我的文件系统运行正常,没有错误。但是过了一段时间我的程序停止读取.owl文件并抛出一些错误。其中一个错误如下:

我在阅读.owl文件时遇到的错误:

SEVERE: Exception thrown: org.apache.jena.riot.RiotException: [line: 476, col: 52] The value of attribute "rdf:about" associated with an element type "File" must not contain the '<' character.
org.apache.jena.riot.system.ErrorHandlerFactory$ErrorHandlerStd.fatal(ErrorHandlerFactory.java:136)
org.apache.jena.riot.lang.LangRDFXML$ErrorHandlerBridge.fatalError(LangRDFXML.java:252)
com.hp.hpl.jena.rdf.arp.impl.ARPSaxErrorHandler.fatalError(ARPSaxErrorHandler.java:48)
com.hp.hpl.jena.rdf.arp.impl.XMLHandler.warning(XMLHandler.java:209)
com.hp.hpl.jena.rdf.arp.impl.XMLHandler.fatalError(XMLHandler.java:239)
org.apache.xerces.util.ErrorHandlerWrapper.fatalError(Unknown Source)
org.apache.xerces.impl.XMLErrorReporter.reportError(Unknown Source)
...

我打开我的.owl文件,我发现Jena写的不​​正确。在下图中,如果您看到第3个突出显示的蓝色错误,其不完整,那里有一些代码丢失。

其次,错误地写入了第2个蓝色突出显示的错误。在我的本体中是File的属性。它应该写成1号蓝色突出显示的代码。

尽管数字1和数字2都是由jena编写的。大多数猫头鹰代码都是由Jena正确编写的,类似于数字1,但有时候jena写错了它与图片中的数字2相似。我不知道为什么。

(要查看完整尺寸的图片,请在新标签中将其打开或将其保存在您的计算机上) errors in owl files

这就是我使用jena api写入.owl文件的方式:

public void setDataTypeProperty(String resourceURI, String propertyName, String propertyValue) //create new data type property. Accept four arguments: URI of resource as string, property name (i.e #hasPath), old value as string and new value as string.
{
    Model model = ModelFactory.createDefaultModel();


//read model from file
InputStream in = FileManager.get().open(inputFileName);

 if (in == null) 
 {
     throw new IllegalArgumentException( "File: " + inputFileName + " not found");
 }       
 model.read(in, "");
 try {
    in.close();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}


     // Add property to Model
     Resource resource = model.createResource(resourceURI);
     resource.addProperty(model.createProperty(baseURI+propertyName), model.createLiteral(propertyValue));


     //Writing model to file
        try {
            FileWriter out = new FileWriter( inputFileName );
            model.write( out, "RDF/XML-ABBREV" );
            out.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
}

请指导我如何修正Jena的2号和3号蓝色高亮错误。

3 个答案:

答案 0 :(得分:1)

您的方法存在输入卫生问题。我无法确定您的输入数据是无效的,但它肯定应该在任何以编程方式构造URI或文字的方法中进行测试。

<强>的URI

例如,以下两行是危险的,因为它们可以允许URI中不允许的字符,或者它们可以允许字符值用于无法序列化为XML的字符值。

Resource resource = model.createResource(resourceURI);
resource.addProperty(model.createProperty(baseURI+propertyName), model.createLiteral(propertyValue));

要解决URI问题,请使用URLEncoder自行清理uris:

final String uri  = URLEncoder.encode(resourceURI, "UTF-8");
final String puri = URLEncoder.encode(baseURI+propertyName);
final Resource resource = model.createResource(uri);
resource.addProperty(model.createProperty(puri), model.createLiteral(propertyValue));

要对问题我们的URI进行测试,您可以使用Jena的IRIFactory类型来验证您构建的URI是否符合某些特定规范。

<强>字面

解决文字问题有点棘手。你没有得到一个异常,表明你的文字值不好,但是为了完整性我要包括这个(所以你可以清理所有输入,而不仅仅是那些可能导致现在的问题)。

Jena的作者在将它们序列化为XML之前不会测试文字的值。他们用于检测无效XML字符的模式仅关注作为RDF XML规范的一部分而需要替换的字符。 Jena将最终验证(和抛出异常)委托给底层XML库。这是有道理的,因为可能存在未来的RDF序列化,允许表达所有字符。我最近比特(例如,包含退格符的字符串),所以我创建了一个更严格的模式,以便在运行时急切地检测到这种情况。

final Pattern elementContentEntities = Pattern.compile( "[\0-\31&&[^\n\t\r]]|\127|[\u0080-\u009F]|[\uD800-\uDFFF]|\uFFFF|\uFFFE" );
final Matcher m = elementContentEntities.matcher( propertyValue );
if( m.find() ) {
    // TODO sanitise your string literal, it contains invalid characters
} 
else {
    // TODO your string is good.
}

答案 1 :(得分:1)

#3 - &#34; admi&#34;截断的性质 - 让我认为这可能是您的基础数据传输和存储的问题,并且与XML,RDF,Jena或此级别的任何其他内容无关。也许是一个被忽略的例外?

答案 2 :(得分:0)

我的主程序有时会将resourceURI参数作为空白/ null传递给setDataTypeProperty方法。这就是它造成问题的原因。

所以我修改了我的代码并在方法的开头添加了两行:

    public void setDataTypeProperty(String resourceURI, String propertyName, String propertyValue) //create new data type property. Accept four arguments: URI of resource as string, property name (i.e #hasPath), old value as string and new value as string.
{
    if (resourceURI==null)
    return;
...
...

现在我正在运行它几天但是还没有遇到上述错误。