@Override public String marshallContent(Context context, Iterable<Triple> content, MediaType type) throws ContentTransformationException { try { RDFContext rdfContext = RDFContext.createContext(context.getBase()); rdfContext.setNamespaces(context.getNamespaces()); StringWriter writer = new StringWriter(); rdfContext.serialize(content, Format.TURTLE, writer); return writer.toString(); } catch (IOException e) { throw new ContentTransformationException("Could not marshall contents",e); } }
@Override public Iterable<Triple> unmarshallContent(Context context, String content, MediaType type) throws ContentTransformationException { try { RDFContext rdfContext = RDFContext.createContext(context.getBase()); rdfContext.setOption(UnmarshallOptions.TRIPLE_ORDERING, UnmarshallOptions.Ordering.KEEP_TRIPLE_ORDER); return rdfContext.deserialize(content, Format.JSON_LD); } catch (IOException e) { throw new ContentTransformationException("Could not unmarshall contents",e); } }
public static RDFContext createContext(URI base) { Objects.requireNonNull(base, "Base cannot be null"); return new RDFContext(base); }
private InmutableTripleSet deserialize(String serialization, URI baseURI, Format format) throws IOException { System.out.println( String.format( "Deserializing {\n%s,\n%s,\n%s,\n}", indent("Serialization...: {"+LINE_SEPARATOR+indent(serialization)+LINE_SEPARATOR+"}"), indent("BaseURI...: "+baseURI), indent("Format....: "+format) ) ); InmutableTripleSet result = new InmutableTripleSet(RDFContext.createContext(baseURI).deserialize(serialization, format)); return result; }
@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 <T> void serialize(Iterable<Triple> triples, Format format, T output) throws IOException { Objects.requireNonNull(triples, "Triples cannot be null"); Objects.requireNonNull(format, FORMAT_PARAM); Objects.requireNonNull(output, "Output cannot be null"); Marshaller<T> marshaller=RuntimeInstance.getInstance().newMarshaller(format,output); marshaller.setConfiguration(getConfiguration(format)); marshaller.marshall(triples, output); }
public <T> Iterable<Triple> deserialize(T source, Format format) throws IOException { Objects.requireNonNull(source, "Source cannot be null"); Objects.requireNonNull(format, FORMAT_PARAM); Unmarshaller<T> unmarshaller=RuntimeInstance.getInstance().newUnmarshaller(format,source); unmarshaller.setConfiguration(getConfiguration(format)); return unmarshaller.unmarshall(source); }
@Override public String marshallContent(Context context, Iterable<Triple> content, MediaType type) throws ContentTransformationException { try { RDFContext rdfContext = RDFContext.createContext(context.getBase()); rdfContext.setNamespaces(context.getNamespaces()); StringWriter writer = new StringWriter(); rdfContext.serialize(content, Format.JSON_LD, writer); return writer.toString(); } catch (IOException e) { throw new ContentTransformationException("Could not marshall contents",e); } }
@Override public Iterable<Triple> unmarshallContent(Context context, String content, MediaType type) throws ContentTransformationException { try { RDFContext rdfContext = RDFContext.createContext(context.getBase()); rdfContext.setOption(UnmarshallOptions.TRIPLE_ORDERING, UnmarshallOptions.Ordering.KEEP_TRIPLE_ORDER); return rdfContext.deserialize(content, Format.RDF_XML); } catch (IOException e) { throw new ContentTransformationException("Could not unmarshall contents",e); } }
@Override public String marshallContent(Context context, Iterable<Triple> content, MediaType type) throws ContentTransformationException { try { RDFContext rdfContext = RDFContext.createContext(context.getBase()); rdfContext.setNamespaces(context.getNamespaces()); StringWriter writer = new StringWriter(); rdfContext.serialize(content, Format.RDF_XML, writer); return writer.toString(); } catch (IOException e) { throw new ContentTransformationException("Could not marshall contents",e); } }
@Override public Iterable<Triple> unmarshallContent(Context context, String content, MediaType type) throws ContentTransformationException { try { RDFContext rdfContext = RDFContext.createContext(context.getBase()); rdfContext.setOption(UnmarshallOptions.TRIPLE_ORDERING, UnmarshallOptions.Ordering.KEEP_TRIPLE_ORDER); return rdfContext.deserialize(content, Format.TURTLE); } catch (IOException e) { throw new ContentTransformationException("Could not unmarshall contents",e); } }
private String serialize(InmutableTripleSet triples, URI baseURI, Namespaces namespaces, Format format) throws IOException { System.out.println( String.format( "Serializing {\n%s,\n%s,\n%s,\n%s\n}", indent("Triples...: {"+LINE_SEPARATOR+indent(triples)+LINE_SEPARATOR+"}"), indent("BaseURI...: "+baseURI), indent("Format....: "+format), indent("Namespaces: "+namespaces) ) ); if(triples==null) { throw new IllegalArgumentException("Object 'triples' cannot be null"); } if(baseURI==null) { throw new IllegalArgumentException("Object 'baseURI' cannot be null"); } if(namespaces==null) { throw new IllegalArgumentException("Object 'namespaces' cannot be null"); } if(format==null) { throw new IllegalArgumentException("Object 'format' cannot be null"); } RDFContext context = RDFContext.createContext(baseURI); context.setNamespaces(namespaces); StringWriter writer = new StringWriter(); context.serialize(triples, format, writer); String serialization = writer.toString(); return serialization; }