Last active
August 29, 2015 14:16
-
-
Save stain/be58129e5ac382668f53 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.apache.jena.riot.Lang; | |
import org.apache.jena.riot.lang.PipedTriplesStream; | |
import org.apache.jena.riot.system.StreamOps; | |
import org.apache.jena.riot.system.StreamRDF; | |
import org.apache.jena.riot.system.StreamRDFWriter; | |
import org.junit.Test; | |
import com.hp.hpl.jena.datatypes.xsd.XSDDatatype; | |
import com.hp.hpl.jena.graph.Node; | |
import com.hp.hpl.jena.graph.Triple; | |
import com.hp.hpl.jena.rdf.model.Literal; | |
import com.hp.hpl.jena.rdf.model.Model; | |
import com.hp.hpl.jena.rdf.model.ModelFactory; | |
import com.hp.hpl.jena.rdf.model.ResourceFactory; | |
import com.hp.hpl.jena.sparql.core.Quad; | |
public class TestSomething { | |
public static class StreamRDFProxy implements StreamRDF { | |
private final StreamRDF writer; | |
public StreamRDFProxy(StreamRDF writer) { | |
this.writer = writer; | |
} | |
@Override | |
public void start() { | |
writer.start(); | |
} | |
@Override | |
public void triple(Triple triple) { | |
writer.triple(triple); | |
} | |
@Override | |
public void quad(Quad quad) { | |
writer.quad(quad); | |
} | |
@Override | |
public void base(String base) { | |
writer.base(base); | |
} | |
@Override | |
public void prefix(String prefix, String iri) { | |
writer.prefix(prefix, iri); | |
} | |
@Override | |
public void finish() { | |
writer.finish(); | |
} | |
} | |
public static class SimpleLiteralStreamRDF extends StreamRDFProxy { | |
public SimpleLiteralStreamRDF(StreamRDF writer) { | |
super(writer); | |
} | |
@Override | |
public void triple(Triple triple) { | |
Node object = triple.getObject(); | |
if (object.isLiteral()) { | |
Literal literal = ResourceFactory.createPlainLiteral(object.getLiteralLexicalForm()); | |
triple = new Triple(triple.getSubject(), triple.getPredicate(), literal.asNode()); | |
} | |
super.triple(triple); | |
} | |
} | |
@Test | |
public void test() { | |
Model model = ModelFactory.createDefaultModel(); | |
model.add(ResourceFactory.createResource(), | |
ResourceFactory.createProperty("http://example.com/hasDouble"), | |
ResourceFactory.createTypedLiteral("13.37", XSDDatatype.XSDdouble)); | |
model.add(ResourceFactory.createResource(), | |
ResourceFactory.createProperty("http://example.com/hasString"), | |
ResourceFactory.createTypedLiteral("uhu", XSDDatatype.XSDstring)); | |
model.add(ResourceFactory.createResource(), | |
ResourceFactory.createProperty("http://example.com/hasLang"), | |
ResourceFactory.createLangLiteral("Hello", "en-GB")); | |
final StreamRDF writer = StreamRDFWriter.getWriterStream(System.out, Lang.TURTLE); | |
StreamRDF literalWriter = new SimpleLiteralStreamRDF(writer); | |
StreamOps.graphToStream(model.getGraph(), literalWriter); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment