From f7987a283b88988d02a63b5d6405be46bbf37b40 Mon Sep 17 00:00:00 2001 From: Peter Ansell Date: Sun, 27 Sep 2015 16:25:52 +1000 Subject: [PATCH 01/27] Port jsonld-java/jsonld-java#146 to jsonld-java-clerezza --- README.md | 31 +++ pom.xml | 63 +++++- .../ClerezzaJsonLdParsingProvider.java | 65 +++++++ .../ClerezzaJsonLdSerializingProvider.java | 183 ++++++++++++++++++ .../clerezza/ClerezzaRDFParser.java | 113 +++++++++++ .../clerezza/ClerezzaTripleCallback.java | 10 +- ...za.rdf.core.serializedform.ParsingProvider | 1 + ...df.core.serializedform.SerializingProvider | 1 + .../ClerezzaJsonLdParserSerializerTest.java | 91 +++++++++ 9 files changed, 551 insertions(+), 7 deletions(-) create mode 100644 src/main/java/com/github/jsonldjava/clerezza/ClerezzaJsonLdParsingProvider.java create mode 100644 src/main/java/com/github/jsonldjava/clerezza/ClerezzaJsonLdSerializingProvider.java create mode 100644 src/main/java/com/github/jsonldjava/clerezza/ClerezzaRDFParser.java create mode 100644 src/main/resources/META-INF/services/org.apache.clerezza.rdf.core.serializedform.ParsingProvider create mode 100644 src/main/resources/META-INF/services/org.apache.clerezza.rdf.core.serializedform.SerializingProvider create mode 100644 src/test/java/com/github/jsonldjava/clerezza/ClerezzaJsonLdParserSerializerTest.java diff --git a/README.md b/README.md index 9ddc09c..9a9e006 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 ===== @@ -24,3 +26,32 @@ ClerezzaTripleCallback The ClerezzaTripleCallback returns an instance of `org.apache.clerezza.rdf.core.MGraph` 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..f8d32ac 100644 --- a/pom.xml +++ b/pom.xml @@ -22,6 +22,9 @@ Peter Ansell + + Rupert Westenthaler + @@ -36,6 +39,19 @@ jar compile + + org.apache.clerezza + rdf.core + ${clerezza.version} + + + + org.apache.felix + org.apache.felix.scr.annotations + 1.9.12 + + + ${project.groupId} jsonld-java @@ -45,8 +61,9 @@ org.apache.clerezza - rdf.core - ${clerezza.version} + rdf.ontologies + 0.12 + test junit @@ -74,6 +91,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.21.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..0923399 --- /dev/null +++ b/src/main/java/com/github/jsonldjava/clerezza/ClerezzaJsonLdParsingProvider.java @@ -0,0 +1,65 @@ +package com.github.jsonldjava.clerezza; + +import java.io.IOException; +import java.io.InputStream; + +import org.apache.clerezza.rdf.core.MGraph; +import org.apache.clerezza.rdf.core.UriRef; +import org.apache.clerezza.rdf.core.serializedform.ParsingProvider; +import org.apache.clerezza.rdf.core.serializedform.SupportedFormat; +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(MGraph target, InputStream serializedGraph, String formatIdentifier, + UriRef 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..6a14084 --- /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.rdf.core.TripleCollection; +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, TripleCollection 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..7a27d10 --- /dev/null +++ b/src/main/java/com/github/jsonldjava/clerezza/ClerezzaRDFParser.java @@ -0,0 +1,113 @@ +package com.github.jsonldjava.clerezza; + +import java.util.HashMap; +import java.util.Map; + +import org.apache.clerezza.rdf.core.BNode; +import org.apache.clerezza.rdf.core.Language; +import org.apache.clerezza.rdf.core.Literal; +import org.apache.clerezza.rdf.core.NonLiteral; +import org.apache.clerezza.rdf.core.PlainLiteral; +import org.apache.clerezza.rdf.core.Resource; +import org.apache.clerezza.rdf.core.Triple; +import org.apache.clerezza.rdf.core.TripleCollection; +import org.apache.clerezza.rdf.core.TypedLiteral; +import org.apache.clerezza.rdf.core.UriRef; + +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 TripleCollection} 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 bNodeMap = new HashMap(1024); + final RDFDataset result = new RDFDataset(); + if (input instanceof TripleCollection) { + for (final Triple t : ((TripleCollection) input)) { + handleStatement(result, t, bNodeMap); + } + } + bNodeMap.clear(); // help gc + return result; + } + + private void handleStatement(RDFDataset result, Triple t, Map bNodeMap) { + final String subject = getResourceValue(t.getSubject(), bNodeMap); + final String predicate = getResourceValue(t.getPredicate(), bNodeMap); + final Resource object = t.getObject(); + + if (object instanceof Literal) { + + final String value = ((Literal) object).getLexicalForm(); + final String language; + final String datatype; + if (object instanceof TypedLiteral) { + language = null; + datatype = getResourceValue(((TypedLiteral) object).getDataType(), bNodeMap); + } else if (object instanceof PlainLiteral) { + // we use RDF 1.1 literals so we do set the RDF_LANG_STRING + // datatype + datatype = RDF_LANG_STRING; + final Language l = ((PlainLiteral) object).getLanguage(); + if (l == null) { + language = null; + } else { + language = l.toString(); + } + } else { + throw new IllegalStateException("Unknown Literal class " + + object.getClass().getName()); + } + result.addTriple(subject, predicate, value, datatype, language); + count++; + } else { + result.addTriple(subject, predicate, getResourceValue((NonLiteral) object, bNodeMap)); + 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(NonLiteral nl, Map bNodeMap) { + if (nl == null) { + return null; + } else if (nl instanceof UriRef) { + return ((UriRef) nl).getUnicodeString(); + } else if (nl instanceof BNode) { + String bNodeId = bNodeMap.get(nl); + if (bNodeId == null) { + bNodeId = Integer.toString(bNodeMap.size()); + bNodeMap.put((BNode) nl, bNodeId); + } + return new StringBuilder("_:b").append(bNodeId).toString(); + } else { + throw new IllegalStateException("Unknwon NonLiteral 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..89ac284 100644 --- a/src/main/java/com/github/jsonldjava/clerezza/ClerezzaTripleCallback.java +++ b/src/main/java/com/github/jsonldjava/clerezza/ClerezzaTripleCallback.java @@ -20,6 +20,8 @@ public class ClerezzaTripleCallback implements JsonLdTripleCallback { + private static final String RDF_LANG_STRING = "http://www.w3.org/1999/02/22-rdf-syntax-ns#langString"; + private MGraph mGraph = new SimpleMGraph(); private Map bNodeMap = new HashMap(); @@ -51,12 +53,10 @@ private void triple(String s, String p, String value, String datatype, String la Resource 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 UriRef(datatype)); } mGraph.add(new TripleImpl(subject, predicate, object)); 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..2393819 --- /dev/null +++ b/src/main/resources/META-INF/services/org.apache.clerezza.rdf.core.serializedform.ParsingProvider @@ -0,0 +1 @@ +com.github.jsonldjava.clerezza.ClerezzaJsonLdParsingProvider 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..ed9e3c8 --- /dev/null +++ b/src/main/resources/META-INF/services/org.apache.clerezza.rdf.core.serializedform.SerializingProvider @@ -0,0 +1 @@ +com.github.jsonldjava.clerezza.JsonLdSerializingProvider 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..41bff24 --- /dev/null +++ b/src/test/java/com/github/jsonldjava/clerezza/ClerezzaJsonLdParserSerializerTest.java @@ -0,0 +1,91 @@ +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.rdf.core.Graph; +import org.apache.clerezza.rdf.core.Language; +import org.apache.clerezza.rdf.core.LiteralFactory; +import org.apache.clerezza.rdf.core.MGraph; +import org.apache.clerezza.rdf.core.Triple; +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.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(); + UriRef pers1 = new UriRef("http://www.example.org/test#pers1"); + UriRef pers2 = new UriRef("http://www.example.org/test#pers2"); + MGraph data = new SimpleMGraph(); + //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.getGraph(); + } + + @Test + public void parserTest() { + final InputStream in = getClass().getClassLoader().getResourceAsStream( + "testfiles/product.jsonld"); + SimpleMGraph graph = new SimpleMGraph(); + 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 + SimpleMGraph reparsed = new SimpleMGraph(); + parser.parse(reparsed, new ByteArrayInputStream(data), "application/ld+json"); + Assert.assertEquals(7, reparsed.size()); + for(Triple t : rdfData){ + Assert.assertTrue(reparsed.contains(t)); + } + + } +} From a270bb4be9ed885cacaf3c46123ffc9f3b599092 Mon Sep 17 00:00:00 2001 From: Peter Ansell Date: Wed, 30 Sep 2015 13:16:45 +1000 Subject: [PATCH 02/27] release 0.7.0 --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 7de9e08..6141a2a 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ jsonld-java-parent com.github.jsonld-java - 0.7.0-SNAPSHOT + 0.7.0 4.0.0 jsonld-java-clerezza From 39361b3bfb1aaf06b24a341a3ed93f5fdce063f3 Mon Sep 17 00:00:00 2001 From: Peter Ansell Date: Wed, 30 Sep 2015 13:18:02 +1000 Subject: [PATCH 03/27] bump to next development version --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 6141a2a..708b5f7 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ jsonld-java-parent com.github.jsonld-java - 0.7.0 + 0.7.1-SNAPSHOT 4.0.0 jsonld-java-clerezza From 63eff34c795802c2eb7ef2d4cc6e215592030b12 Mon Sep 17 00:00:00 2001 From: Rupert Westenthaler Date: Mon, 5 Oct 2015 17:18:21 +0200 Subject: [PATCH 04/27] fixed the class name of the serializing provider in the META-INF/services file. Now the java.util.ServiceLoader can (again) find the implementing class. Also removed empty lines at the end of those files --- .../org.apache.clerezza.rdf.core.serializedform.ParsingProvider | 2 +- ....apache.clerezza.rdf.core.serializedform.SerializingProvider | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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 index 2393819..2473510 100644 --- 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 @@ -1 +1 @@ -com.github.jsonldjava.clerezza.ClerezzaJsonLdParsingProvider +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 index ed9e3c8..e65291c 100644 --- 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 @@ -1 +1 @@ -com.github.jsonldjava.clerezza.JsonLdSerializingProvider +com.github.jsonldjava.clerezza.ClerezzaJsonLdSerializingProvider \ No newline at end of file From 92a89d4b150ba385e5da52d815f500062c44b173 Mon Sep 17 00:00:00 2001 From: Peter Ansell Date: Thu, 19 Nov 2015 10:14:03 +1100 Subject: [PATCH 05/27] Bump to 0.8.0-SNAPSHOT --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 133e87c..79a69f4 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ jsonld-java-parent com.github.jsonld-java - 0.7.1-SNAPSHOT + 0.8.0-SNAPSHOT 4.0.0 jsonld-java-clerezza From cd30467165092651d4c8263c756cd1044109fa2d Mon Sep 17 00:00:00 2001 From: Peter Ansell Date: Thu, 19 Nov 2015 10:15:16 +1100 Subject: [PATCH 06/27] leave readme at last stable version to avoid confusion by new maven users --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 9a9e006..00903fd 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ From Maven com.github.jsonld-java jsonld-java-clerezza - 0.7.0-SNAPSHOT + 0.7.0 (Adjust for most recent , as found in ``pom.xml``). From fae77df3c16ac8bd1626c69e9682949aa8738454 Mon Sep 17 00:00:00 2001 From: Peter Ansell Date: Tue, 9 Feb 2016 17:46:07 -0500 Subject: [PATCH 07/27] release 0.8.0 --- README.md | 2 +- pom.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 00903fd..2ef5890 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ From Maven com.github.jsonld-java jsonld-java-clerezza - 0.7.0 + 0.8.0 (Adjust for most recent , as found in ``pom.xml``). diff --git a/pom.xml b/pom.xml index 79a69f4..2e57be6 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ jsonld-java-parent com.github.jsonld-java - 0.8.0-SNAPSHOT + 0.8.0 4.0.0 jsonld-java-clerezza From f8ef952933f770ff32108d2f32e3816b48e9b47a Mon Sep 17 00:00:00 2001 From: Peter Ansell Date: Tue, 9 Feb 2016 18:01:57 -0500 Subject: [PATCH 08/27] bump to next development version --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 2e57be6..3dbca1a 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ jsonld-java-parent com.github.jsonld-java - 0.8.0 + 0.8.1-SNAPSHOT 4.0.0 jsonld-java-clerezza From a921d1cb5d218171f485bf52df08132ef2dacc38 Mon Sep 17 00:00:00 2001 From: Peter Ansell Date: Wed, 17 Feb 2016 17:39:12 -0500 Subject: [PATCH 09/27] Release 0.8.1 --- README.md | 2 +- pom.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2ef5890..b60b7f4 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ From Maven com.github.jsonld-java jsonld-java-clerezza - 0.8.0 + 0.8.1 (Adjust for most recent , as found in ``pom.xml``). diff --git a/pom.xml b/pom.xml index 3dbca1a..53798da 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ jsonld-java-parent com.github.jsonld-java - 0.8.1-SNAPSHOT + 0.8.1 4.0.0 jsonld-java-clerezza From 967dbebfb960f913b8febaff8178838ee989d92d Mon Sep 17 00:00:00 2001 From: Peter Ansell Date: Wed, 17 Feb 2016 17:54:28 -0500 Subject: [PATCH 10/27] Bump to next development version --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 53798da..1e36805 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ jsonld-java-parent com.github.jsonld-java - 0.8.1 + 0.9.0-SNAPSHOT 4.0.0 jsonld-java-clerezza From d2266031a43d4b3608f327b60f2abdd5e110a210 Mon Sep 17 00:00:00 2001 From: Peter Ansell Date: Wed, 17 Feb 2016 18:25:47 -0500 Subject: [PATCH 11/27] Release 0.8.2 --- README.md | 2 +- pom.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index b60b7f4..3f8aac8 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ From Maven com.github.jsonld-java jsonld-java-clerezza - 0.8.1 + 0.8.2 (Adjust for most recent , as found in ``pom.xml``). diff --git a/pom.xml b/pom.xml index 1e36805..64418a1 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ jsonld-java-parent com.github.jsonld-java - 0.9.0-SNAPSHOT + 0.8.2 4.0.0 jsonld-java-clerezza From 94119752b9b561949d909736e04934bf0fbf9a1b Mon Sep 17 00:00:00 2001 From: Peter Ansell Date: Wed, 17 Feb 2016 18:35:41 -0500 Subject: [PATCH 12/27] Bump to next development version --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 64418a1..f20d61d 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ jsonld-java-parent com.github.jsonld-java - 0.8.2 + 0.8.3-SNAPSHOT 4.0.0 jsonld-java-clerezza From 4facd975209c835e481f9104abade2ed949af6c5 Mon Sep 17 00:00:00 2001 From: Peter Ansell Date: Wed, 18 May 2016 18:55:49 -0400 Subject: [PATCH 13/27] Release 0.8.3 --- README.md | 2 +- pom.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 3f8aac8..b4f51dd 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ From Maven com.github.jsonld-java jsonld-java-clerezza - 0.8.2 + 0.8.3 (Adjust for most recent , as found in ``pom.xml``). diff --git a/pom.xml b/pom.xml index f20d61d..75e2fec 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ jsonld-java-parent com.github.jsonld-java - 0.8.3-SNAPSHOT + 0.8.3 4.0.0 jsonld-java-clerezza From 24331e16c7e1e586b2369850842e99893d278f28 Mon Sep 17 00:00:00 2001 From: Peter Ansell Date: Wed, 18 May 2016 19:03:53 -0400 Subject: [PATCH 14/27] Bump to next snapshot --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 75e2fec..4f049ad 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ jsonld-java-parent com.github.jsonld-java - 0.8.3 + 0.8.4-SNAPSHOT 4.0.0 jsonld-java-clerezza From e462d20dcce067db8bf8c07cadbe2c0b16e5086e Mon Sep 17 00:00:00 2001 From: Peter Ansell Date: Wed, 18 May 2016 19:22:54 -0400 Subject: [PATCH 15/27] Only test with Java-8 --- .travis.yml | 2 -- 1 file changed, 2 deletions(-) diff --git a/.travis.yml b/.travis.yml index 2ee0939..312621f 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,5 @@ language: java jdk: - - openjdk7 - - oraclejdk7 - oraclejdk8 notifications: email: From d0ae7be3ebb18e152353fab9468ccdb8bf6ec77e Mon Sep 17 00:00:00 2001 From: Peter Ansell Date: Wed, 18 May 2016 19:26:47 -0400 Subject: [PATCH 16/27] Cache maven files on Travis --- .travis.yml | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/.travis.yml b/.travis.yml index 312621f..f39b742 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,4 +1,8 @@ language: java +sudo: false +cache: + directories: + - $HOME/.m2 jdk: - oraclejdk8 notifications: From f181758d67598e7d51e5584010469101893e0a42 Mon Sep 17 00:00:00 2001 From: Peter Ansell Date: Fri, 23 Dec 2016 06:29:48 +1100 Subject: [PATCH 17/27] Bump to 0.9.0-SNAPSHOT Signed-off-by: Peter Ansell --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 4f049ad..1e36805 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ jsonld-java-parent com.github.jsonld-java - 0.8.4-SNAPSHOT + 0.9.0-SNAPSHOT 4.0.0 jsonld-java-clerezza From 317d45e33b0ecfb539027a84aada092991a4d81b Mon Sep 17 00:00:00 2001 From: Peter Ansell Date: Fri, 23 Dec 2016 06:38:54 +1100 Subject: [PATCH 18/27] Release 0.9.0 Signed-off-by: Peter Ansell --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 1e36805..f49cb9b 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ jsonld-java-parent com.github.jsonld-java - 0.9.0-SNAPSHOT + 0.9.0 4.0.0 jsonld-java-clerezza From a59d48320cee7b82f544a53296ead544d47a19a1 Mon Sep 17 00:00:00 2001 From: Peter Ansell Date: Fri, 23 Dec 2016 06:48:49 +1100 Subject: [PATCH 19/27] Bump to next development version Signed-off-by: Peter Ansell --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index f49cb9b..951c797 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ jsonld-java-parent com.github.jsonld-java - 0.9.0 + 0.9.1-SNAPSHOT 4.0.0 jsonld-java-clerezza From 431dfb5fefc71b868c9f129c19f7cc169dab2b13 Mon Sep 17 00:00:00 2001 From: Peter Ansell Date: Thu, 16 Feb 2017 15:26:04 +1100 Subject: [PATCH 20/27] Bump to next development version Signed-off-by: Peter Ansell --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 951c797..6dfcad3 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ jsonld-java-parent com.github.jsonld-java - 0.9.1-SNAPSHOT + 0.10.0-SNAPSHOT 4.0.0 jsonld-java-clerezza From e847ad69dfdaccd0b82d011edaf1b78b03333828 Mon Sep 17 00:00:00 2001 From: Peter Ansell Date: Thu, 16 Feb 2017 15:28:15 +1100 Subject: [PATCH 21/27] Update version number for next release Signed-off-by: Peter Ansell --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index b4f51dd..2be5df5 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ From Maven com.github.jsonld-java jsonld-java-clerezza - 0.8.3 + 0.10.0 (Adjust for most recent , as found in ``pom.xml``). From 9ef0cb25bf5088bab96ee5a83562cc941878a7e7 Mon Sep 17 00:00:00 2001 From: Peter Ansell Date: Thu, 16 Feb 2017 15:48:54 +1100 Subject: [PATCH 22/27] Release 0.10.0 Signed-off-by: Peter Ansell --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 6dfcad3..7be3360 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ jsonld-java-parent com.github.jsonld-java - 0.10.0-SNAPSHOT + 0.10.0 4.0.0 jsonld-java-clerezza From c94eee2332c87bdb1ec0cb1d8b3040266bff5438 Mon Sep 17 00:00:00 2001 From: Peter Ansell Date: Thu, 16 Feb 2017 15:52:02 +1100 Subject: [PATCH 23/27] Bump to next development version Signed-off-by: Peter Ansell --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 7be3360..a635b82 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ jsonld-java-parent com.github.jsonld-java - 0.10.0 + 0.10.1-SNAPSHOT 4.0.0 jsonld-java-clerezza From 1faeb764f17cd6332bf523faf2f9461bc47d5878 Mon Sep 17 00:00:00 2001 From: Peter Ansell Date: Sun, 8 Apr 2018 15:10:32 +1000 Subject: [PATCH 24/27] Bump some versions to latest for development Signed-off-by: Peter Ansell --- pom.xml | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/pom.xml b/pom.xml index a635b82..c2f7c8b 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ jsonld-java-parent com.github.jsonld-java - 0.10.1-SNAPSHOT + 0.12.0-SNAPSHOT 4.0.0 jsonld-java-clerezza @@ -28,14 +28,14 @@ - 0.14 + 1.0.1 ${project.groupId} jsonld-java - ${project.version} + 0.12.0 jar compile @@ -48,7 +48,7 @@ org.apache.felix org.apache.felix.scr.annotations - 1.9.12 + 1.12.0 @@ -62,7 +62,7 @@ org.apache.clerezza rdf.ontologies - 0.12 + 1.0.0 test @@ -109,7 +109,7 @@ org.apache.felix maven-scr-plugin - 1.21.0 + 1.26.0 From d41d992e14ab621ca5ea2c2961f25ee18b5ccb53 Mon Sep 17 00:00:00 2001 From: Peter Ansell Date: Sun, 8 Apr 2018 15:43:33 +1000 Subject: [PATCH 25/27] Patch class names that changed in release Signed-off-by: Peter Ansell --- pom.xml | 3 +- .../ClerezzaJsonLdParsingProvider.java | 9 +- .../ClerezzaJsonLdSerializingProvider.java | 4 +- .../clerezza/ClerezzaRDFParser.java | 83 ++++++++----------- .../clerezza/ClerezzaTripleCallback.java | 54 ++++++------ .../ClerezzaJsonLdParserSerializerTest.java | 27 +++--- .../clerezza/ClerezzaTripleCallbackTest.java | 13 +-- .../testfiles/curies-in-context.jsonld | 2 +- 8 files changed, 92 insertions(+), 103 deletions(-) diff --git a/pom.xml b/pom.xml index c2f7c8b..4d553ce 100644 --- a/pom.xml +++ b/pom.xml @@ -4,10 +4,11 @@ jsonld-java-parent com.github.jsonld-java - 0.12.0-SNAPSHOT + 0.12.0 4.0.0 jsonld-java-clerezza + 0.12.0-SNAPSHOT JSONLD Java :: Clerezza Integration JSON-LD Java integration module for Clerezza bundle diff --git a/src/main/java/com/github/jsonldjava/clerezza/ClerezzaJsonLdParsingProvider.java b/src/main/java/com/github/jsonldjava/clerezza/ClerezzaJsonLdParsingProvider.java index 0923399..13b003d 100644 --- a/src/main/java/com/github/jsonldjava/clerezza/ClerezzaJsonLdParsingProvider.java +++ b/src/main/java/com/github/jsonldjava/clerezza/ClerezzaJsonLdParsingProvider.java @@ -3,10 +3,11 @@ import java.io.IOException; import java.io.InputStream; -import org.apache.clerezza.rdf.core.MGraph; -import org.apache.clerezza.rdf.core.UriRef; +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; @@ -32,8 +33,8 @@ public class ClerezzaJsonLdParsingProvider implements ParsingProvider { private final Logger logger = LoggerFactory.getLogger(getClass()); @Override - public void parse(MGraph target, InputStream serializedGraph, String formatIdentifier, - UriRef baseUri) { + 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); diff --git a/src/main/java/com/github/jsonldjava/clerezza/ClerezzaJsonLdSerializingProvider.java b/src/main/java/com/github/jsonldjava/clerezza/ClerezzaJsonLdSerializingProvider.java index 6a14084..7761fcf 100644 --- a/src/main/java/com/github/jsonldjava/clerezza/ClerezzaJsonLdSerializingProvider.java +++ b/src/main/java/com/github/jsonldjava/clerezza/ClerezzaJsonLdSerializingProvider.java @@ -11,7 +11,7 @@ import java.util.LinkedHashMap; import java.util.Map; -import org.apache.clerezza.rdf.core.TripleCollection; +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; @@ -103,7 +103,7 @@ public class ClerezzaJsonLdSerializingProvider implements SerializingProvider { private boolean prettyPrint; @Override - public void serialize(OutputStream serializedGraph, TripleCollection tc, String formatIdentifier) { + public void serialize(OutputStream serializedGraph, Graph tc, String formatIdentifier) { final ClerezzaRDFParser serializer = new ClerezzaRDFParser(); try { final long start = System.currentTimeMillis(); diff --git a/src/main/java/com/github/jsonldjava/clerezza/ClerezzaRDFParser.java b/src/main/java/com/github/jsonldjava/clerezza/ClerezzaRDFParser.java index 7a27d10..0ffb062 100644 --- a/src/main/java/com/github/jsonldjava/clerezza/ClerezzaRDFParser.java +++ b/src/main/java/com/github/jsonldjava/clerezza/ClerezzaRDFParser.java @@ -3,16 +3,13 @@ import java.util.HashMap; import java.util.Map; -import org.apache.clerezza.rdf.core.BNode; -import org.apache.clerezza.rdf.core.Language; -import org.apache.clerezza.rdf.core.Literal; -import org.apache.clerezza.rdf.core.NonLiteral; -import org.apache.clerezza.rdf.core.PlainLiteral; -import org.apache.clerezza.rdf.core.Resource; -import org.apache.clerezza.rdf.core.Triple; -import org.apache.clerezza.rdf.core.TripleCollection; -import org.apache.clerezza.rdf.core.TypedLiteral; -import org.apache.clerezza.rdf.core.UriRef; +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; @@ -20,7 +17,7 @@ import com.github.jsonldjava.core.RDFParser; /** - * Converts a Clerezza {@link TripleCollection} to the {@link RDFDataset} used + * Converts a Clerezza {@link Graph} to the {@link RDFDataset} used * by the {@link JsonLdProcessor} * * @author Rupert Westenthaler @@ -35,48 +32,38 @@ public class ClerezzaRDFParser implements RDFParser { @Override public RDFDataset parse(Object input) throws JsonLdError { count = 0; - final Map bNodeMap = new HashMap(1024); + final Map blankNodeMap = new HashMap(1024); final RDFDataset result = new RDFDataset(); - if (input instanceof TripleCollection) { - for (final Triple t : ((TripleCollection) input)) { - handleStatement(result, t, bNodeMap); + if (input instanceof Graph) { + for (final Triple t : ((Graph) input)) { + handleStatement(result, t, blankNodeMap); } } - bNodeMap.clear(); // help gc + blankNodeMap.clear(); // help gc return result; } - private void handleStatement(RDFDataset result, Triple t, Map bNodeMap) { - final String subject = getResourceValue(t.getSubject(), bNodeMap); - final String predicate = getResourceValue(t.getPredicate(), bNodeMap); - final Resource object = t.getObject(); + 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 String value = ((Literal) object).getLexicalForm(); + final Literal literalObject = (Literal) object; + final String value = literalObject.getLexicalForm(); final String language; final String datatype; - if (object instanceof TypedLiteral) { - language = null; - datatype = getResourceValue(((TypedLiteral) object).getDataType(), bNodeMap); - } else if (object instanceof PlainLiteral) { - // we use RDF 1.1 literals so we do set the RDF_LANG_STRING - // datatype - datatype = RDF_LANG_STRING; - final Language l = ((PlainLiteral) object).getLanguage(); - if (l == null) { - language = null; - } else { - language = l.toString(); - } + if (literalObject.getLanguage() != null) { + language = literalObject.getLanguage().toString(); + datatype = RDF_LANG_STRING; } else { - throw new IllegalStateException("Unknown Literal class " - + object.getClass().getName()); + datatype = getResourceValue(literalObject.getDataType(), blankNodeMap); + language = null; } result.addTriple(subject, predicate, value, datatype, language); count++; } else { - result.addTriple(subject, predicate, getResourceValue((NonLiteral) object, bNodeMap)); + result.addTriple(subject, predicate, getResourceValue((BlankNodeOrIRI) object, blankNodeMap)); count++; } @@ -92,20 +79,20 @@ public long getCount() { return count; } - private String getResourceValue(NonLiteral nl, Map bNodeMap) { + private String getResourceValue(BlankNodeOrIRI nl, Map BlankNodeMap) { if (nl == null) { return null; - } else if (nl instanceof UriRef) { - return ((UriRef) nl).getUnicodeString(); - } else if (nl instanceof BNode) { - String bNodeId = bNodeMap.get(nl); - if (bNodeId == null) { - bNodeId = Integer.toString(bNodeMap.size()); - bNodeMap.put((BNode) nl, bNodeId); + } 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(bNodeId).toString(); + return new StringBuilder("_:b").append(BlankNodeId).toString(); } else { - throw new IllegalStateException("Unknwon NonLiteral type " + nl.getClass().getName() + 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 89ac284..01bf188 100644 --- a/src/main/java/com/github/jsonldjava/clerezza/ClerezzaTripleCallback.java +++ b/src/main/java/com/github/jsonldjava/clerezza/ClerezzaTripleCallback.java @@ -4,16 +4,16 @@ 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; @@ -22,15 +22,15 @@ public class ClerezzaTripleCallback implements JsonLdTripleCallback { private static final String RDF_LANG_STRING = "http://www.w3.org/1999/02/22-rdf-syntax-ns#langString"; - private MGraph mGraph = new SimpleMGraph(); - private Map bNodeMap = new HashMap(); + private Graph mGraph = new SimpleGraph(); + private Map bNodeMap = new HashMap<>(); - public void setMGraph(MGraph mGraph) { - this.mGraph = mGraph; - bNodeMap = new HashMap(); + public void setMGraph(Graph graph) { + this.mGraph = graph; + bNodeMap = new HashMap<>(); } - public MGraph getMGraph() { + public Graph getMGraph() { return mGraph; } @@ -40,41 +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 { - object = new TypedLiteralImpl(value, new UriRef(datatype)); + 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/test/java/com/github/jsonldjava/clerezza/ClerezzaJsonLdParserSerializerTest.java b/src/test/java/com/github/jsonldjava/clerezza/ClerezzaJsonLdParserSerializerTest.java index 41bff24..a34d596 100644 --- a/src/test/java/com/github/jsonldjava/clerezza/ClerezzaJsonLdParserSerializerTest.java +++ b/src/test/java/com/github/jsonldjava/clerezza/ClerezzaJsonLdParserSerializerTest.java @@ -6,15 +6,14 @@ import java.nio.charset.Charset; import java.util.ServiceLoader; -import org.apache.clerezza.rdf.core.Graph; -import org.apache.clerezza.rdf.core.Language; +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.MGraph; -import org.apache.clerezza.rdf.core.Triple; -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.serializedform.Parser; import org.apache.clerezza.rdf.core.serializedform.Serializer; import org.apache.clerezza.rdf.ontologies.FOAF; @@ -47,9 +46,9 @@ public class ClerezzaJsonLdParserSerializerTest { @BeforeClass public static void init(){ LiteralFactory lf = LiteralFactory.getInstance(); - UriRef pers1 = new UriRef("http://www.example.org/test#pers1"); - UriRef pers2 = new UriRef("http://www.example.org/test#pers2"); - MGraph data = new SimpleMGraph(); + 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 @@ -61,14 +60,14 @@ public static void init(){ 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.getGraph(); + rdfData = data; } @Test public void parserTest() { final InputStream in = getClass().getClassLoader().getResourceAsStream( "testfiles/product.jsonld"); - SimpleMGraph graph = new SimpleMGraph(); + SimpleGraph graph = new SimpleGraph(); parser.parse(graph, in, "application/ld+json"); Assert.assertEquals(13, graph.size()); } @@ -80,7 +79,7 @@ public void serializerTest(){ log.info("Serialized Graph: \n {}",new String(data,UTF8)); //Now we reparse the graph to validate it was serialized correctly - SimpleMGraph reparsed = new SimpleMGraph(); + SimpleGraph reparsed = new SimpleGraph(); parser.parse(reparsed, new ByteArrayInputStream(data), "application/ld+json"); Assert.assertEquals(7, reparsed.size()); for(Triple t : rdfData){ 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 From 8a5bccf7c44574412e07b6b0588feca353eba10e Mon Sep 17 00:00:00 2001 From: Peter Ansell Date: Sun, 8 Apr 2018 15:51:44 +1000 Subject: [PATCH 26/27] Release 0.12.0 Signed-off-by: Peter Ansell --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index 4d553ce..0b88db8 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ 4.0.0 jsonld-java-clerezza - 0.12.0-SNAPSHOT + 0.12.0 JSONLD Java :: Clerezza Integration JSON-LD Java integration module for Clerezza bundle From 7892d9e137d61065132da9f809b6fc9942a3fdf9 Mon Sep 17 00:00:00 2001 From: Peter Ansell Date: Sun, 8 Apr 2018 15:59:46 +1000 Subject: [PATCH 27/27] Bump to next development version Signed-off-by: Peter Ansell --- README.md | 4 ++-- pom.xml | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 2be5df5..09b6803 100644 --- a/README.md +++ b/README.md @@ -15,7 +15,7 @@ From Maven com.github.jsonld-java jsonld-java-clerezza - 0.10.0 + 0.12.0 (Adjust for most recent , as found in ``pom.xml``). @@ -23,7 +23,7 @@ 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. diff --git a/pom.xml b/pom.xml index 0b88db8..ddd25f0 100644 --- a/pom.xml +++ b/pom.xml @@ -8,7 +8,7 @@ 4.0.0 jsonld-java-clerezza - 0.12.0 + 0.12.1-SNAPSHOT JSONLD Java :: Clerezza Integration JSON-LD Java integration module for Clerezza bundle