使用Jena 2.12.1加载模型时,RiotException [英] RiotException when loading a Model using Jena 2.12.1
问题描述
我创建了一个名为 RDFReader 的简单类,用于从DBpedia的URI加载模型:
I've created this simple class named RDFReader for loading a model from a URI from DBpedia:
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.util.FileManager;
public class RDFReader {
public static Model readFromURL(String URL){
try{
return (new FileManager()).loadModel(URL);
}catch(Exception e){
e.printStackTrace();
}
return null;
}
public static void main(String[] args) {
RDFReader.readFromURL("http://dbpedia.org/resource/Pacific_Rim_(film)");
}
}
我已使用 Jena v2.12.1,如以下pom.xml片段所示
I've used Jena v2.12.1 as shown in the following snippet of my pom.xml
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-core</artifactId>
<version>2.12.1</version>
</dependency>
<dependency>
<groupId>org.apache.jena</groupId>
<artifactId>jena-arq</artifactId>
<version>2.12.1</version>
</dependency>
<dependency>
通过使用 Jena v2.12.1运行此代码,我有了下一个异常:
By running this code with Jena v2.12.1 I've got the next exception:
org.apache.jena.riot.RiotException: [line: 21, col: 17] Unknown char: –(8211;0x2013)
at org.apache.jena.riot.system.ErrorHandlerFactory$ErrorHandlerStd.fatal(ErrorHandlerFactory.java:136)
at org.apache.jena.riot.lang.LangEngine.raiseException(LangEngine.java:163)
at org.apache.jena.riot.lang.LangEngine.nextToken(LangEngine.java:106)
at org.apache.jena.riot.lang.LangTurtleBase.triples(LangTurtleBase.java:249)
at org.apache.jena.riot.lang.LangTurtleBase.triplesSameSubject(LangTurtleBase.java:191)
at org.apache.jena.riot.lang.LangTurtle.oneTopLevelElement(LangTurtle.java:44)
at org.apache.jena.riot.lang.LangTurtleBase.runParser(LangTurtleBase.java:90)
at org.apache.jena.riot.lang.LangBase.parse(LangBase.java:42)
at org.apache.jena.riot.RDFParserRegistry$ReaderRIOTLang.read(RDFParserRegistry.java:182)
at org.apache.jena.riot.RDFDataMgr.process(RDFDataMgr.java:906)
at org.apache.jena.riot.RDFDataMgr.parse(RDFDataMgr.java:687)
at org.apache.jena.riot.RDFDataMgr.read(RDFDataMgr.java:210)
at org.apache.jena.riot.RDFDataMgr.read(RDFDataMgr.java:183)
at org.apache.jena.riot.RDFDataMgr.read(RDFDataMgr.java:121)
at org.apache.jena.riot.RDFDataMgr.read(RDFDataMgr.java:112)
at org.apache.jena.riot.adapters.RDFReaderRIOT.read(RDFReaderRIOT.java:77)
at com.hp.hpl.jena.rdf.model.impl.ModelCom.read(ModelCom.java:253)
at com.hp.hpl.jena.util.FileManager.readModelWorker(FileManager.java:377)
at com.hp.hpl.jena.util.FileManager.loadModelWorker(FileManager.java:308)
at com.hp.hpl.jena.util.FileManager.loadModel(FileManager.java:260)
at edu.polito.rdf.utils.RDFReader.readFromURL(RDFReader.java:12)
at edu.polito.rdf.utils.RDFReader.main(RDFReader.java:20)
但是使用 Jena v2.11.0可以运行代码,没有任何问题.所以我想知道:
However using Jena v2.11.0 the code runs without any problem. So I would like to know:
- 为什么 Jena 的2.12.1版本会产生此错误?
- 是否可以使用 Jena 的2.12.1版本而不是2.11.0来解决此问题?
- Why does the 2.12.1 version of Jena produce this error?
- Is it possible to solve this problem to use the 2.12.1 version of Jena instead of the 2.11.0?.
顺便说一句,我使用的是 eclipse Luna 4.4.1 和Java版本"1.8.0_11".
By the way, I'm using eclipse Luna 4.4.1 and java version "1.8.0_11".
推荐答案
问题是我试图访问资源 http://dbpedia.org/resource/Pacific_Rim_(film),其中包含字符0x2013(Em-dash),该字符在Turtle中当时不合法.
The problem is that I was trying to access to the resource http://dbpedia.org/resource/Pacific_Rim_(film) which contains a character 0x2013 (Em-dash) that is not legal at that point in Turtle.
这是两个问题的答案:
-
我使用的是Jena 2.12.1,该应用了内容协商,并在RDF/XML之前列出了Turtle,因此发现DBpedia(使用Turtle)中的资源是错误的(如前所述).在2.11.0版中,它可以毫无问题地运行,因为该版本存在一个错误,使解析器不那么宽容.然后在2.12.1版中修复了该错误,因此读取具有非法字符的资源将启动RiotException.
I was using Jena 2.12.1 which applies content negotiation and lists Turtle before RDF/XML so it found that the resource in DBpedia (with Turtle) was wrong (as explained before). With version 2.11.0 it run without problems because that version had a bug that allowed the parser to be lenient. This bug was then fixed in version 2.12.1, so reading a resource with an illegal character launches the RiotException.
The solution was simply to ask DBpedia for the RDF/XML (alternatively N-triples) of the resource which can be accessed through http://dbpedia.org/data/Pacific_Rim_(film).rdf. The final code to get the RDF/XML from DBpedia is:
注意:按照@AndyS的建议,我使用了RDFDataMgr.loadModel(String URI)
:
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.util.FileManager;
public class RDFReader {
public static Model readFromURL(String URL){
return RDFDataMgr.loadModel(URL);
}
public static void main(String[] args) {
Model model = RDFReader.readFromURL("http://dbpedia.org/data/Pacific_Rim_(film).rdf");
}
}
这篇关于使用Jena 2.12.1加载模型时,RiotException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!