private static URIRef coherceStringURIRef(boolean nullable, String str) { URIRef tmp=null; try { tmp=uriRef(new URI(str)); } catch (URISyntaxException e) { if(!nullable) { throw new IllegalArgumentException("Cannot crete URIRef from string '"+str+"': invalid URI",e); } } return tmp; }
private void append(org.ldp4j.rdf.Literal<?> object) { TripleSetBuilder.this.triples.add( triple( TripleGenerator.this.subject, TripleGenerator.this.predicate, object ) ); } @Override
private static URIRef coherceURIRef(Object object, boolean nullable) { URIRef result=null; if(object instanceof URIRef) { result=(URIRef)object; } else if(object instanceof QName) { result=uriRef((QName)object); } else if(object instanceof URI) { result=uriRef((URI)object); } else if(object instanceof URL) { result=uriRef((URL)object); } else if(object instanceof String) { result=coherceStringURIRef(nullable, (String)object); } return result; }
public static Triple triple(Object subject, Object predicate, Object object) { Objects.requireNonNull(subject, "Subject cannot be null"); Objects.requireNonNull(predicate, "Predicate cannot be null"); Objects.requireNonNull(object, "Object cannot be null"); return FACTORY. newTriple( asResource(subject), asURIRef(predicate), asNode(object)); }
@Test public void testJSON() throws Exception { Iterable<Triple> content= tripleSet( triple( uriRef("example"), uriRef("http://www.example.org/vocab#size"), literal(3)), triple( uriRef("example"), uriRef("http://www.example.org/vocab#property"), uriRef("example/test")), triple( uriRef("example/test"), uriRef("http://www.example.org/vocab#propertyInverse"), uriRef("example")), triple( uriRef("example"), uriRef("http://www.example.org/vocab#otherProperty"), literal("27")) ); RDFContext rdfContext = RDFContext.createContext(URI.create("http://www.example.org/")); StringWriter writer = new StringWriter(); rdfContext.serialize(content, Format.JSON_LD, writer); System.out.println(writer.toString()); }
public Literal<?> parseLiteral(org.openrdf.model.Literal literal) { org.openrdf.model.URI datatype = literal.getDatatype(); Literal<?> result; String label = literal.getLabel(); if(datatype==null) { String language = literal.getLanguage(); if(language!=null) { result=RDFModelDSL.literal(label,language); } else { result=RDFModelDSL.literal(label); } } else { String dataType = datatype.stringValue(); Datatype type=Datatype.fromString(dataType); if(type!=null) { result=RDFModelDSL.typedLiteral(boxTypedLiteral(type, label), dataType); } else { result=RDFModelDSL.typedLiteral(label,URI.create(dataType)); } } return result; }
@Override public void visitString(String id) { this.resource=blankNode(id); }
@Override public void visitLiteral(Literal<?> literal) { append(literal(literal.get())); } @Override
public static <T> Literal<T> typedLiteral(T value,String type) { Objects.requireNonNull(value, VALUE_PARAM); Objects.requireNonNull(type, TYPE_PARAM); return typedLiteral(value,URI.create(type)); }
@Override public void visitObject(Object id) { this.resource=blankNode(id.toString()); }
private void marshallProperty(Resource<?> subject, URIRef predicate, Object value) { Node object=null; PropertyType propertyType=null; if(TypeSupport.isLiteral(value.getClass())) { propertyType=PropertyType.LITERAL; object=RDFModelDSL.literal(value); } else { propertyType=PropertyType.OBJECT; object=resolve(value); } log("Adding %s property '%s' to object '%s'...",propertyType,predicate,source); logTriple(source,predicate,object); session.addTriple(subject,predicate,object); }
public static <T> Literal<T> typedLiteral(T value, URL type) { Objects.requireNonNull(value, VALUE_PARAM); Objects.requireNonNull(type, TYPE_PARAM); return typedLiteral(value,type.toString()); }
public static URIRef uriRef(URL identity) { Objects.requireNonNull(identity, IDENTITY_PARAM); return uriRef(identity.toString()); }
@Override public void visitNumber(Number id) { this.resource=blankNode(id.toString()); }
@Override public void visitLanguageLiteral(LanguageLiteral literal) { append(literal(literal.get(),literal.language())); } }
public void add(Resource<?> subject, URIRef predicate, Node object) { Objects.requireNonNull(subject, "Subject cannot be null"); Objects.requireNonNull(predicate, "Predicate cannot be null"); Objects.requireNonNull(object, "Object cannot be null"); add(RDFModelDSL.triple(subject,predicate,object)); }
public static <T> Literal<T> typedLiteral(T value, QName type) { Objects.requireNonNull(value, VALUE_PARAM); Objects.requireNonNull(type, TYPE_PARAM); return typedLiteral(value,type.getNamespaceURI() + type.getLocalPart()); }
public static URIRef uriRef(String identity) { Objects.requireNonNull(identity, IDENTITY_PARAM); try { return uriRef(new URI(identity)); } catch (URISyntaxException e) { throw new IllegalArgumentException("Cannot create URIRef from '"+identity+"'",e); } }