java - JENA : Create a single Statement based on a String object -
i have parse n-triple content , apply modification on every literal of given type.
for example, have modify every wktliteral make them using referential. triples such :
"point (0.0 0.0)"^^<http://www.opengis.net/ont/geosparql#wktliteral>
must become :
"<http://www.opengis.net/def/crs/epsg/0/4326> point (0.0 0.0)"^^<http://www.opengis.net/ont/geosparql#wktliteral>
i got each triple line line string object , create jena statement string. aim use jena parsers avoid dirty string manipulation such split error prone.
for way find :
string line = "%a triple here%"; //create empty model final model model = modelfactory.createdefaultmodel(); //parse , store rdf triple in model rdfdatamgr.read(model, new bytearrayinputstream(line.getbytes(standardcharsets.utf_8)), lang.ntriples); //get statements - 1 if final stmtiterator liststatements = model.liststatements(); //got statement final statement statement = liststatements.next();
i tried use rdfreader don't know use rdfoutputstream... statement object automatically created string have create model,use reader , iterator. seems overkill in opinion (i cutted off of test such testing there next statement...) .
do know kicker/simpler way achieve ?
arthur.
you'll find using streams more efficient. streamrdf
instances sent triples encountered. can rewrite appropriate.
streams use spi level of jena -- nodes, triples , quads rather statements, resources, etc -- lack niceities tasks ideal.
given you've written suspect writing out fixed n-triples want? here's example that. 1) create stream output triples, 2) create stream waits triples, corrects object (if needed), , writes results , 3) starts whole parse going:
final string wkt = "http://www.opengis.net/ont/geosparql#wktliteral"; // stream result stdout final streamrdf outputhandler = streamrdflib.writer(system.out); streamrdf inputhandler = new streamrdfbase() { @override public void triple(triple triple) { // got triple node object = triple.getobject(); node transformed; // if object literal , has wkt type if (object.isliteral() && wkt.equals(object.getliteraldatatypeuri())) { // make new node, suitably modified transformed = nodefactory.createliteral( "<http://www.opengis.net/def/crs/epsg/0/4326> " + object.getliterallexicalform(), object.getliteraldatatype()); } else { // nothing transformed = object; } // write out corrected object outputhandler.triple( triple.create( triple.getsubject(), triple.getpredicate(), transformed )); } }; // parse rdfdatamgr.parse(inputhandler, rdfdatamgr.open("file"));
Comments
Post a Comment