diff --git a/.travis.yml b/.travis.yml index 2ee0939..f39b742 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,9 @@ language: java +sudo: false +cache: + directories: + - $HOME/.m2 jdk: - - openjdk7 - - oraclejdk7 - oraclejdk8 notifications: email: diff --git a/README.md b/README.md index 9ddc09c..09b6803 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,8 @@ JSONLD-Java Clerezza Integration module [![Build Status](https://travis-ci.org/jsonld-java/jsonld-java-clerezza.svg?branch=master)](https://travis-ci.org/jsonld-java/jsonld-java-clerezza) [![Coverage Status](https://coveralls.io/repos/jsonld-java/jsonld-java-clerezza/badge.svg?branch=master)](https://coveralls.io/r/jsonld-java/jsonld-java-clerezza?branch=master) +This module provide a `ParsingProvider`and `SerializingProvider` for Apache Clerezza. Those Providers plug into the Clerezza `Parser` and `Serializer` service infrastructure. Meaning that adding this bundle will allow Clerezza to parse and serialize JSON-LD. + USAGE ===== @@ -13,7 +15,7 @@ From Maven com.github.jsonld-java jsonld-java-clerezza - 0.7.0-SNAPSHOT + 0.12.0 (Adjust for most recent , as found in ``pom.xml``). @@ -21,6 +23,35 @@ From Maven ClerezzaTripleCallback ------------------ -The ClerezzaTripleCallback returns an instance of `org.apache.clerezza.rdf.core.MGraph` +The ClerezzaTripleCallback returns an instance of `org.apache.clerezza.commons.rdf.Graph` See [ClerezzaTripleCallbackTest.java](./src/test/java/com/github/jsonldjava/clerezza/ClerezzaTripleCallbackTest.java) for example Usage. + + +From OSGI +--------- + +Assuming the above Bundle is active in the OSGI Environment one can simple inject the `Serializer` and/or `Parser` service. + + @Reference + private Serializer serializer; + + @Reference + private Parser parser; + + +Normal Java +----------- + +Both the `Parser` and `Serializer` also support `java.util.ServiceLoader`. So when running outside an OSGI environment one can use the `getInstance()` to obtain an instance. + + Serializer serializer = Serializer.getInstance(); + + Parser parser = Parser.getInstance(); + +Supported Formats +----------------- + +The JSON-LD parser implementation supports `application/ld+json`. The serializer supports both `application/ld+json` and `application/json`. + +The rational behind this is that the parser can not parse any JSON however the Serializer does generate valid JSON. diff --git a/pom.xml b/pom.xml index 7de9e08..ddd25f0 100644 --- a/pom.xml +++ b/pom.xml @@ -4,10 +4,11 @@ jsonld-java-parent com.github.jsonld-java - 0.7.0-SNAPSHOT + 0.12.0 4.0.0 jsonld-java-clerezza + 0.12.1-SNAPSHOT JSONLD Java :: Clerezza Integration JSON-LD Java integration module for Clerezza bundle @@ -22,20 +23,36 @@ Peter Ansell + + Rupert Westenthaler + - 0.14 + 1.0.1 ${project.groupId} jsonld-java - ${project.version} + 0.12.0 jar compile + + org.apache.clerezza + rdf.core + ${clerezza.version} + + + + org.apache.felix + org.apache.felix.scr.annotations + 1.12.0 + + + ${project.groupId} jsonld-java @@ -45,8 +62,9 @@ org.apache.clerezza - rdf.core - ${clerezza.version} + rdf.ontologies + 1.0.0 + test junit @@ -74,6 +92,48 @@ org.jacoco jacoco-maven-plugin + + org.apache.felix + maven-bundle-plugin + true + + + https://github.com/jsonld-java/jsonld-java-clerezza + JSONLD-JAVA Clerezza Integration + ${project.artifactId} + <_versionpolicy>$${version;===;${@}} + <_provider-policy>[$(version;===;$(@)),$(version;=+;$(@))) + <_consumer-policy>[$(version;===;$(@)),$(version;+;$(@))) + + + + + org.apache.felix + maven-scr-plugin + 1.26.0 + + + + org.slf4j + slf4j-simple + ${slf4j.version} + + + + + generate-scr-scrdescriptor + + scr + + + + JSONLD-JAVA + + + + + diff --git a/src/main/java/com/github/jsonldjava/clerezza/ClerezzaJsonLdParsingProvider.java b/src/main/java/com/github/jsonldjava/clerezza/ClerezzaJsonLdParsingProvider.java new file mode 100644 index 0000000..13b003d --- /dev/null +++ b/src/main/java/com/github/jsonldjava/clerezza/ClerezzaJsonLdParsingProvider.java @@ -0,0 +1,66 @@ +package com.github.jsonldjava.clerezza; + +import java.io.IOException; +import java.io.InputStream; + +import org.apache.clerezza.commons.rdf.Graph; +import org.apache.clerezza.commons.rdf.IRI; +import org.apache.clerezza.rdf.core.serializedform.ParsingProvider; +import org.apache.clerezza.rdf.core.serializedform.SupportedFormat; +import org.apache.clerezza.utils.Uri; +import org.apache.felix.scr.annotations.Component; +import org.apache.felix.scr.annotations.ConfigurationPolicy; +import org.apache.felix.scr.annotations.Service; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.github.jsonldjava.core.JsonLdError; +import com.github.jsonldjava.core.JsonLdProcessor; +import com.github.jsonldjava.utils.JsonUtils; + +/** + * A {@link org.apache.clerezza.rdf.core.serializedform.ParsingProvider} for + * JSON-LD (application/ld+json) based on the java-jsonld library + * + * @author Rupert Westenthaler + * + */ +@Component(immediate = true, policy = ConfigurationPolicy.OPTIONAL) +@Service +@SupportedFormat("application/ld+json") +public class ClerezzaJsonLdParsingProvider implements ParsingProvider { + + private final Logger logger = LoggerFactory.getLogger(getClass()); + + @Override + public void parse(Graph target, InputStream serializedGraph, String formatIdentifier, + IRI baseUri) { + // The callback will add parsed triples to the target MGraph + final ClerezzaTripleCallback ctc = new ClerezzaTripleCallback(); + ctc.setMGraph(target); + Object input; + int startSize = 0; + if (logger.isDebugEnabled()) { + startSize = target.size(); + } + final long start = System.currentTimeMillis(); + try { + input = JsonUtils.fromInputStream(serializedGraph, "UTF-8"); + } catch (final IOException e) { + logger.error("Unable to read from the parsed input stream", e); + throw new RuntimeException(e.getMessage(), e); + } + try { + JsonLdProcessor.toRDF(input, ctc); + } catch (final JsonLdError e) { + logger.error("Unable to parse JSON-LD from the parsed input stream", e); + throw new RuntimeException(e.getMessage(), e); + } + if (logger.isDebugEnabled()) { + logger.debug(" - parsed {} triples in {}ms", target.size() - startSize, + System.currentTimeMillis() - start); + } + } + +} + diff --git a/src/main/java/com/github/jsonldjava/clerezza/ClerezzaJsonLdSerializingProvider.java b/src/main/java/com/github/jsonldjava/clerezza/ClerezzaJsonLdSerializingProvider.java new file mode 100644 index 0000000..7761fcf --- /dev/null +++ b/src/main/java/com/github/jsonldjava/clerezza/ClerezzaJsonLdSerializingProvider.java @@ -0,0 +1,183 @@ +package com.github.jsonldjava.clerezza; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.OutputStreamWriter; +import java.io.Writer; +import java.nio.charset.Charset; +import java.util.Collections; +import java.util.Dictionary; +import java.util.HashMap; +import java.util.LinkedHashMap; +import java.util.Map; + +import org.apache.clerezza.commons.rdf.Graph; +import org.apache.clerezza.rdf.core.serializedform.SerializingProvider; +import org.apache.clerezza.rdf.core.serializedform.SupportedFormat; +import org.apache.felix.scr.annotations.Activate; +import org.apache.felix.scr.annotations.Component; +import org.apache.felix.scr.annotations.ConfigurationPolicy; +import org.apache.felix.scr.annotations.Deactivate; +import org.apache.felix.scr.annotations.Property; +import org.apache.felix.scr.annotations.PropertyOption; +import org.apache.felix.scr.annotations.Service; +import org.osgi.service.component.ComponentContext; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.github.jsonldjava.core.JsonLdError; +import com.github.jsonldjava.core.JsonLdOptions; +import com.github.jsonldjava.core.JsonLdProcessor; +import com.github.jsonldjava.utils.JsonUtils; + +/** + * A {@link org.apache.clerezza.rdf.core.serializedform.SerializingProvider} for + * JSON-LD (application/ld+json) based on the java-jsonld library + * + * @author Rupert Westenthaler + */ +@Component(immediate = true, policy = ConfigurationPolicy.OPTIONAL) +@Service +@SupportedFormat(value = { "application/ld+json", "application/json" }) +public class ClerezzaJsonLdSerializingProvider implements SerializingProvider { + + private final Logger logger = LoggerFactory.getLogger(getClass()); + + private static final Charset UTF8 = Charset.forName("UTF-8"); + + private static final String MODE_EXPAND = "expand"; + private static final String MODE_FLATTEN = "flatten"; + private static final String MODE_COMPACT = "compact"; + + @Property(value = "", options = { + @PropertyOption(value = "%mode.option.none", name = ""), + @PropertyOption(value = "%mode.option.flatten", name = "flatten"), + @PropertyOption(value = "%mode.option.compact", name = "compact"), + @PropertyOption(value = "%mode.option.expand", name = MODE_EXPAND) }) + private static final String PROP_MODE = "mode"; + + @Property(boolValue = false) + private static final String PROP_USE_RDF_TYPE = "useRdfTye"; + + @Property(boolValue = false) + private static final String PROP_USE_NATIVE_TYPES = "useNativeTypes"; + + @Property(boolValue = true) + private static final String PROP_PRETTY_PRINT = "prettyPrint"; + + // TODO: make configurable or read the whole prefix.cc list from a file and + // search for really used namespaces while parsing the triples in the + // ClerezzaRDFParser + private static Map DEFAULT_NAMESPACES; + static { + // core ontologies, top from prefixcc and some stanbol specific + final Map ns = new LinkedHashMap(); + // core schemas + ns.put("xsd", "http://www.w3.org/2001/XMLSchema#"); + ns.put("owl", "http://www.w3.org/2002/07/owl#"); + ns.put("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"); + ns.put("rdfs", "http://www.w3.org/2000/01/rdf-schema#"); + // well known ontologies + ns.put("skos", "http://www.w3.org/2004/02/skos/core#"); + ns.put("geo", "http://www.w3.org/2003/01/geo/wgs84_pos#"); + ns.put("dc", "http://purl.org/dc/elements/1.1/"); + ns.put("foaf", "http://xmlns.com/foaf/0.1/"); + ns.put("ma", "http://www.w3.org/ns/ma-ont#"); + // big datasets + ns.put("dbo", "http://dbpedia.org/ontology/"); + ns.put("dbp", "http://dbpedia.org/property/"); + ns.put("yago", "http://yago-knowledge.org/resource/"); + ns.put("fb", "http://rdf.freebase.com/ns/"); + ns.put("geonames", "http://www.geonames.org/ontology#"); + // stanbol specific + ns.put("fise", "http://fise.iks-project.eu/ontology/"); + ns.put("enhancer", "http://stanbol.apache.org/ontology/enhancer/enhancer#"); + ns.put("entityhub", "http://stanbol.apache.org/ontology/entityhub/entityhub#"); + + DEFAULT_NAMESPACES = Collections.unmodifiableMap(ns); + } + + private JsonLdOptions opts = null; + private String mode; + + private boolean prettyPrint; + + @Override + public void serialize(OutputStream serializedGraph, Graph tc, String formatIdentifier) { + final ClerezzaRDFParser serializer = new ClerezzaRDFParser(); + try { + final long start = System.currentTimeMillis(); + Object output = JsonLdProcessor.fromRDF(tc, serializer); + + if (MODE_EXPAND.equalsIgnoreCase(mode)) { + logger.debug(" - mode: {}", MODE_EXPAND); + output = JsonLdProcessor.expand(output, opts); + } + if (MODE_FLATTEN.equalsIgnoreCase(mode)) { + logger.debug(" - mode: {}", MODE_FLATTEN); + // TODO: Allow inframe config + final Object inframe = null; + output = JsonLdProcessor.flatten(output, inframe, opts); + } + if (MODE_COMPACT.equalsIgnoreCase(mode)) { + logger.debug(" - mode: {}", MODE_COMPACT); + // TODO: collect namespaces used in the triples in the + // ClerezzaRDFParser + final Map localCtx = new HashMap(); + localCtx.put("@context", DEFAULT_NAMESPACES); + output = JsonLdProcessor.compact(output, localCtx, opts); + } + final Writer writer = new OutputStreamWriter(serializedGraph, UTF8); + logger.debug(" - prettyPrint: {}", prettyPrint); + if (prettyPrint) { + JsonUtils.writePrettyPrint(writer, output); + } else { + JsonUtils.write(writer, output); + } + if (logger.isDebugEnabled()) { + logger.debug(" - serialized {} triples in {}ms", serializer.getCount(), + System.currentTimeMillis() - start); + } + } catch (final JsonLdError e) { + throw new RuntimeException(e.getMessage(), e); + } catch (final IOException e) { + throw new RuntimeException(e.getMessage(), e); + } + } + + @Activate + protected void activate(ComponentContext ctx) { + opts = new JsonLdOptions(); + @SuppressWarnings("unchecked") + final Dictionary config = ctx.getProperties(); + // boolean properties + opts.setUseRdfType(getState(config.get(PROP_USE_RDF_TYPE), false)); + opts.setUseNativeTypes(getState(config.get(PROP_USE_NATIVE_TYPES), false)); + prettyPrint = getState(config.get(PROP_PRETTY_PRINT), true); + // parse the string mode + final Object value = config.get(PROP_MODE); + mode = value == null ? null : value.toString(); + } + + @Deactivate + protected void deactivate(ComponentContext ctx) { + opts = null; + mode = null; + prettyPrint = false; + } + + /** + * @param value + */ + private boolean getState(Object value, boolean defaultState) { + if (value instanceof Boolean) { + return ((Boolean) value).booleanValue(); + } else if (value != null) { + return Boolean.parseBoolean(value.toString()); + } else { + return defaultState; + } + } + +} + diff --git a/src/main/java/com/github/jsonldjava/clerezza/ClerezzaRDFParser.java b/src/main/java/com/github/jsonldjava/clerezza/ClerezzaRDFParser.java new file mode 100644 index 0000000..0ffb062 --- /dev/null +++ b/src/main/java/com/github/jsonldjava/clerezza/ClerezzaRDFParser.java @@ -0,0 +1,100 @@ +package com.github.jsonldjava.clerezza; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.clerezza.commons.rdf.BlankNode; +import org.apache.clerezza.commons.rdf.BlankNodeOrIRI; +import org.apache.clerezza.commons.rdf.Graph; +import org.apache.clerezza.commons.rdf.IRI; +import org.apache.clerezza.commons.rdf.Literal; +import org.apache.clerezza.commons.rdf.RDFTerm; +import org.apache.clerezza.commons.rdf.Triple; + +import com.github.jsonldjava.core.JsonLdError; +import com.github.jsonldjava.core.JsonLdProcessor; +import com.github.jsonldjava.core.RDFDataset; +import com.github.jsonldjava.core.RDFParser; + +/** + * Converts a Clerezza {@link Graph} to the {@link RDFDataset} used + * by the {@link JsonLdProcessor} + * + * @author Rupert Westenthaler + * + */ +public class ClerezzaRDFParser implements RDFParser { + + private static String RDF_LANG_STRING = "http://www.w3.org/1999/02/22-rdf-syntax-ns#langString"; + + private long count = 0; + + @Override + public RDFDataset parse(Object input) throws JsonLdError { + count = 0; + final Map blankNodeMap = new HashMap(1024); + final RDFDataset result = new RDFDataset(); + if (input instanceof Graph) { + for (final Triple t : ((Graph) input)) { + handleStatement(result, t, blankNodeMap); + } + } + blankNodeMap.clear(); // help gc + return result; + } + + private void handleStatement(RDFDataset result, Triple t, Map blankNodeMap) { + final String subject = getResourceValue(t.getSubject(), blankNodeMap); + final String predicate = getResourceValue(t.getPredicate(), blankNodeMap); + final RDFTerm object = t.getObject(); + + if (object instanceof Literal) { + final Literal literalObject = (Literal) object; + final String value = literalObject.getLexicalForm(); + final String language; + final String datatype; + if (literalObject.getLanguage() != null) { + language = literalObject.getLanguage().toString(); + datatype = RDF_LANG_STRING; + } else { + datatype = getResourceValue(literalObject.getDataType(), blankNodeMap); + language = null; + } + result.addTriple(subject, predicate, value, datatype, language); + count++; + } else { + result.addTriple(subject, predicate, getResourceValue((BlankNodeOrIRI) object, blankNodeMap)); + count++; + } + + } + + /** + * The count of processed triples (not thread save) + * + * @return the count of triples processed by the last {@link #parse(Object)} + * call + */ + public long getCount() { + return count; + } + + private String getResourceValue(BlankNodeOrIRI nl, Map BlankNodeMap) { + if (nl == null) { + return null; + } else if (nl instanceof IRI) { + return ((IRI) nl).getUnicodeString(); + } else if (nl instanceof BlankNode) { + String BlankNodeId = BlankNodeMap.get(nl); + if (BlankNodeId == null) { + BlankNodeId = Integer.toString(BlankNodeMap.size()); + BlankNodeMap.put((BlankNode) nl, BlankNodeId); + } + return new StringBuilder("_:b").append(BlankNodeId).toString(); + } else { + throw new IllegalStateException("Unknwon BlankNodeOrIRI type " + nl.getClass().getName() + + "!"); + } + } +} + diff --git a/src/main/java/com/github/jsonldjava/clerezza/ClerezzaTripleCallback.java b/src/main/java/com/github/jsonldjava/clerezza/ClerezzaTripleCallback.java index cc91d36..01bf188 100644 --- a/src/main/java/com/github/jsonldjava/clerezza/ClerezzaTripleCallback.java +++ b/src/main/java/com/github/jsonldjava/clerezza/ClerezzaTripleCallback.java @@ -4,31 +4,33 @@ import java.util.List; import java.util.Map; -import org.apache.clerezza.rdf.core.BNode; -import org.apache.clerezza.rdf.core.Language; -import org.apache.clerezza.rdf.core.MGraph; -import org.apache.clerezza.rdf.core.NonLiteral; -import org.apache.clerezza.rdf.core.Resource; -import org.apache.clerezza.rdf.core.UriRef; -import org.apache.clerezza.rdf.core.impl.PlainLiteralImpl; -import org.apache.clerezza.rdf.core.impl.SimpleMGraph; -import org.apache.clerezza.rdf.core.impl.TripleImpl; -import org.apache.clerezza.rdf.core.impl.TypedLiteralImpl; +import org.apache.clerezza.commons.rdf.BlankNode; +import org.apache.clerezza.commons.rdf.BlankNodeOrIRI; +import org.apache.clerezza.commons.rdf.Graph; +import org.apache.clerezza.commons.rdf.IRI; +import org.apache.clerezza.commons.rdf.Language; +import org.apache.clerezza.commons.rdf.RDFTerm; +import org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl; +import org.apache.clerezza.commons.rdf.impl.utils.TripleImpl; +import org.apache.clerezza.commons.rdf.impl.utils.TypedLiteralImpl; +import org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph; import com.github.jsonldjava.core.JsonLdTripleCallback; import com.github.jsonldjava.core.RDFDataset; public class ClerezzaTripleCallback implements JsonLdTripleCallback { - private MGraph mGraph = new SimpleMGraph(); - private Map bNodeMap = new HashMap(); + private static final String RDF_LANG_STRING = "http://www.w3.org/1999/02/22-rdf-syntax-ns#langString"; - public void setMGraph(MGraph mGraph) { - this.mGraph = mGraph; - bNodeMap = new HashMap(); + private Graph mGraph = new SimpleGraph(); + private Map bNodeMap = new HashMap<>(); + + public void setMGraph(Graph graph) { + this.mGraph = graph; + bNodeMap = new HashMap<>(); } - public MGraph getMGraph() { + public Graph getMGraph() { return mGraph; } @@ -38,43 +40,41 @@ private void triple(String s, String p, String o, String graph) { return; } - final NonLiteral subject = getNonLiteral(s); - final UriRef predicate = new UriRef(p); - final NonLiteral object = getNonLiteral(o); + final BlankNodeOrIRI subject = getNonLiteral(s); + final IRI predicate = new IRI(p); + final BlankNodeOrIRI object = getNonLiteral(o); mGraph.add(new TripleImpl(subject, predicate, object)); } private void triple(String s, String p, String value, String datatype, String language, String graph) { - final NonLiteral subject = getNonLiteral(s); - final UriRef predicate = new UriRef(p); - Resource object; + final BlankNodeOrIRI subject = getNonLiteral(s); + final IRI predicate = new IRI(p); + RDFTerm object; if (language != null) { object = new PlainLiteralImpl(value, new Language(language)); + } else if (datatype == null || RDF_LANG_STRING.equals(datatype)) { + object = new PlainLiteralImpl(value); } else { - if (datatype != null) { - object = new TypedLiteralImpl(value, new UriRef(datatype)); - } else { - object = new PlainLiteralImpl(value); - } + object = new TypedLiteralImpl(value, new IRI(datatype)); } mGraph.add(new TripleImpl(subject, predicate, object)); } - private NonLiteral getNonLiteral(String s) { + private BlankNodeOrIRI getNonLiteral(String s) { if (s.startsWith("_:")) { return getBNode(s); } else { - return new UriRef(s); + return new IRI(s); } } - private BNode getBNode(String s) { + private BlankNode getBNode(String s) { if (bNodeMap.containsKey(s)) { return bNodeMap.get(s); } else { - final BNode result = new BNode(); + final BlankNode result = new BlankNode(); bNodeMap.put(s, result); return result; } diff --git a/src/main/resources/META-INF/services/org.apache.clerezza.rdf.core.serializedform.ParsingProvider b/src/main/resources/META-INF/services/org.apache.clerezza.rdf.core.serializedform.ParsingProvider new file mode 100644 index 0000000..2473510 --- /dev/null +++ b/src/main/resources/META-INF/services/org.apache.clerezza.rdf.core.serializedform.ParsingProvider @@ -0,0 +1 @@ +com.github.jsonldjava.clerezza.ClerezzaJsonLdParsingProvider \ No newline at end of file diff --git a/src/main/resources/META-INF/services/org.apache.clerezza.rdf.core.serializedform.SerializingProvider b/src/main/resources/META-INF/services/org.apache.clerezza.rdf.core.serializedform.SerializingProvider new file mode 100644 index 0000000..e65291c --- /dev/null +++ b/src/main/resources/META-INF/services/org.apache.clerezza.rdf.core.serializedform.SerializingProvider @@ -0,0 +1 @@ +com.github.jsonldjava.clerezza.ClerezzaJsonLdSerializingProvider \ No newline at end of file diff --git a/src/test/java/com/github/jsonldjava/clerezza/ClerezzaJsonLdParserSerializerTest.java b/src/test/java/com/github/jsonldjava/clerezza/ClerezzaJsonLdParserSerializerTest.java new file mode 100644 index 0000000..a34d596 --- /dev/null +++ b/src/test/java/com/github/jsonldjava/clerezza/ClerezzaJsonLdParserSerializerTest.java @@ -0,0 +1,90 @@ +package com.github.jsonldjava.clerezza; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.InputStream; +import java.nio.charset.Charset; +import java.util.ServiceLoader; + +import org.apache.clerezza.commons.rdf.Graph; +import org.apache.clerezza.commons.rdf.IRI; +import org.apache.clerezza.commons.rdf.Language; +import org.apache.clerezza.commons.rdf.Triple; +import org.apache.clerezza.commons.rdf.impl.utils.PlainLiteralImpl; +import org.apache.clerezza.commons.rdf.impl.utils.TripleImpl; +import org.apache.clerezza.commons.rdf.impl.utils.simple.SimpleGraph; +import org.apache.clerezza.rdf.core.LiteralFactory; +import org.apache.clerezza.rdf.core.serializedform.Parser; +import org.apache.clerezza.rdf.core.serializedform.Serializer; +import org.apache.clerezza.rdf.ontologies.FOAF; +import org.apache.clerezza.rdf.ontologies.RDF; +import org.junit.Assert; +import org.junit.BeforeClass; +import org.junit.Test; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class ClerezzaJsonLdParserSerializerTest { + + private Logger log = LoggerFactory.getLogger(getClass()); + + private static final Charset UTF8 = Charset.forName("UTF8"); + + private static Graph rdfData; + + /** + * Typical Clerezza Parser initialization. The JSON-LD serializing provider + * will be found by using the java {@link ServiceLoader} + */ + private Parser parser = Parser.getInstance(); + /** + * Typical Clerezza Serializer initialization. The JSON-LD serializing provider + * will be found by using the java {@link ServiceLoader} + */ + private Serializer serializer = Serializer.getInstance(); + + @BeforeClass + public static void init(){ + LiteralFactory lf = LiteralFactory.getInstance(); + IRI pers1 = new IRI("http://www.example.org/test#pers1"); + IRI pers2 = new IRI("http://www.example.org/test#pers2"); + Graph data = new SimpleGraph(); + //NOTE: This test a language literal with and without language as + // well as a xsd:string typed literal. To test correct handling of + // RDF1.1 + data.add(new TripleImpl(pers1, RDF.type, FOAF.Person)); + data.add(new TripleImpl(pers1, FOAF.name, new PlainLiteralImpl("Rupert Westenthaler", + new Language("de")))); + data.add(new TripleImpl(pers1, FOAF.nick, new PlainLiteralImpl("westei"))); + data.add(new TripleImpl(pers1, FOAF.mbox, lf.createTypedLiteral("rwesten@apache.org"))); + data.add(new TripleImpl(pers1, FOAF.age, lf.createTypedLiteral(38))); + data.add(new TripleImpl(pers1, FOAF.knows, pers2)); + data.add(new TripleImpl(pers2, FOAF.name, new PlainLiteralImpl("Reto Bachmann-Gmür"))); + rdfData = data; + } + + @Test + public void parserTest() { + final InputStream in = getClass().getClassLoader().getResourceAsStream( + "testfiles/product.jsonld"); + SimpleGraph graph = new SimpleGraph(); + parser.parse(graph, in, "application/ld+json"); + Assert.assertEquals(13, graph.size()); + } + @Test + public void serializerTest(){ + ByteArrayOutputStream out = new ByteArrayOutputStream(); + serializer.serialize(out, rdfData, "application/ld+json"); + byte[] data = out.toByteArray(); + log.info("Serialized Graph: \n {}",new String(data,UTF8)); + + //Now we reparse the graph to validate it was serialized correctly + SimpleGraph reparsed = new SimpleGraph(); + parser.parse(reparsed, new ByteArrayInputStream(data), "application/ld+json"); + Assert.assertEquals(7, reparsed.size()); + for(Triple t : rdfData){ + Assert.assertTrue(reparsed.contains(t)); + } + + } +} diff --git a/src/test/java/com/github/jsonldjava/clerezza/ClerezzaTripleCallbackTest.java b/src/test/java/com/github/jsonldjava/clerezza/ClerezzaTripleCallbackTest.java index dac25e7..199a59e 100644 --- a/src/test/java/com/github/jsonldjava/clerezza/ClerezzaTripleCallbackTest.java +++ b/src/test/java/com/github/jsonldjava/clerezza/ClerezzaTripleCallbackTest.java @@ -5,9 +5,10 @@ import java.io.IOException; import java.io.InputStream; +import java.nio.charset.StandardCharsets; -import org.apache.clerezza.rdf.core.MGraph; -import org.apache.clerezza.rdf.core.Triple; +import org.apache.clerezza.commons.rdf.Graph; +import org.apache.clerezza.commons.rdf.Triple; import org.junit.Test; import com.github.jsonldjava.core.JsonLdError; @@ -20,11 +21,11 @@ public class ClerezzaTripleCallbackTest { public void triplesTest() throws IOException, JsonLdError { final InputStream in = getClass().getClassLoader().getResourceAsStream( "testfiles/product.jsonld"); - final Object input = JsonUtils.fromInputStream(in); + final Object input = JsonUtils.fromInputStream(in, StandardCharsets.UTF_8); final ClerezzaTripleCallback callback = new ClerezzaTripleCallback(); - final MGraph graph = (MGraph) JsonLdProcessor.toRDF(input, callback); + final Graph graph = (Graph) JsonLdProcessor.toRDF(input, callback); for (final Triple t : graph) { System.out.println(t); @@ -37,11 +38,11 @@ public void triplesTest() throws IOException, JsonLdError { public void curiesInContextTest() throws IOException, JsonLdError { final InputStream in = getClass().getClassLoader().getResourceAsStream( "testfiles/curies-in-context.jsonld"); - final Object input = JsonUtils.fromInputStream(in); + final Object input = JsonUtils.fromInputStream(in, StandardCharsets.UTF_8); final ClerezzaTripleCallback callback = new ClerezzaTripleCallback(); - final MGraph graph = (MGraph) JsonLdProcessor.toRDF(input, callback); + final Graph graph = (Graph) JsonLdProcessor.toRDF(input, callback); for (final Triple t : graph) { System.out.println(t); diff --git a/src/test/resources/testfiles/curies-in-context.jsonld b/src/test/resources/testfiles/curies-in-context.jsonld index 0490d79..e066620 100644 --- a/src/test/resources/testfiles/curies-in-context.jsonld +++ b/src/test/resources/testfiles/curies-in-context.jsonld @@ -6,5 +6,5 @@ }, "@type": "Person", "name": "Santa Claus", - "foaf:nick": "Sämi" + "foaf:nick": "Sami" } \ No newline at end of file