import org.openrdf.repository.object.ObjectConnection; //导入方法依赖的package包/类
/**
* Used to parse a given text content, supported in a given serialization
* format.
*
* @param content The String representation of the textcontent.
* @param documentURL The basic URL used for namespaces.
* @param format The format of the given serialization. Needs to be
* supported of an instance of RDFFormat.
* @return A list of annotations
*/
private List parse(String content, URL url, RDFFormat format) {
List annos = new LinkedList<>();
List orig;
try {
// Use this connection for parsing the jsonld and create the annotation
RepositoryConnection con = _parseAnno4j.getRepository().getConnection();
RDFParser parser = Rio.createParser(format);
try {
StatementSailHandler handler = new StatementSailHandler(con);
parser.setRDFHandler(handler);
byte[] bytes = content.getBytes("UTF-8");
try (InputStream stream = new ByteArrayInputStream(bytes)) {
parser.parse(stream, url.toString());
}
} catch (RDFHandlerException | RDFParseException | IOException e) {
e.printStackTrace();
}
// Get objects now
ObjectConnection conObject = _parseAnno4j.getObjectRepository().getConnection();
orig = conObject.getObjects(Annotation.class).asList();
long t1 = System.currentTimeMillis();
for (Annotation old : orig) {
// Create a new annotation for our main repository
// Be careful. Make sure that the returned annotation is an
// updated object from our repository
Annotation anno = persistAnnotation(old, conObject, url);
annos.add(anno);
}
long t2 = System.currentTimeMillis();
System.out.println("Create new annotation " + (t2 - t1));
// Clear and close the alibaba object repository used for parsing
con.clear();
conObject.clear();
con.close();
conObject.close();
return annos;
} catch (RepositoryException | QueryEvaluationException ex) {
Logger.getLogger(Anno4jRepository.class.getName()).log(Level.SEVERE, null, ex);
}
return annos;
}