Skip to content

Commit 852de0b

Browse files
committed
Merge pull request #2 from jsonld-java/westei-changes
Port jsonld-java/jsonld-java#146 to jsonld-java-clerezza
2 parents 39361b3 + 63eff34 commit 852de0b

File tree

9 files changed

+551
-7
lines changed

9 files changed

+551
-7
lines changed

README.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ JSONLD-Java Clerezza Integration module
44
[![Build Status](https://travis-ci.org/jsonld-java/jsonld-java-clerezza.svg?branch=master)](https://travis-ci.org/jsonld-java/jsonld-java-clerezza)
55
[![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)
66

7+
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.
8+
79
USAGE
810
=====
911

@@ -24,3 +26,32 @@ ClerezzaTripleCallback
2426
The ClerezzaTripleCallback returns an instance of `org.apache.clerezza.rdf.core.MGraph`
2527

2628
See [ClerezzaTripleCallbackTest.java](./src/test/java/com/github/jsonldjava/clerezza/ClerezzaTripleCallbackTest.java) for example Usage.
29+
30+
31+
From OSGI
32+
---------
33+
34+
Assuming the above Bundle is active in the OSGI Environment one can simple inject the `Serializer` and/or `Parser` service.
35+
36+
@Reference
37+
private Serializer serializer;
38+
39+
@Reference
40+
private Parser parser;
41+
42+
43+
Normal Java
44+
-----------
45+
46+
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.
47+
48+
Serializer serializer = Serializer.getInstance();
49+
50+
Parser parser = Parser.getInstance();
51+
52+
Supported Formats
53+
-----------------
54+
55+
The JSON-LD parser implementation supports `application/ld+json`. The serializer supports both `application/ld+json` and `application/json`.
56+
57+
The rational behind this is that the parser can not parse any JSON however the Serializer does generate valid JSON.

pom.xml

Lines changed: 61 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
<developer>
2323
<name>Peter Ansell</name>
2424
</developer>
25+
<developer>
26+
<name>Rupert Westenthaler</name>
27+
</developer>
2528
</developers>
2629

2730
<properties>
@@ -36,6 +39,19 @@
3639
<type>jar</type>
3740
<scope>compile</scope>
3841
</dependency>
42+
<dependency>
43+
<groupId>org.apache.clerezza</groupId>
44+
<artifactId>rdf.core</artifactId>
45+
<version>${clerezza.version}</version>
46+
</dependency>
47+
<!-- OSGI related -->
48+
<dependency>
49+
<groupId>org.apache.felix</groupId>
50+
<artifactId>org.apache.felix.scr.annotations</artifactId>
51+
<version>1.9.12</version>
52+
</dependency>
53+
54+
<!-- Test dependencies -->
3955
<dependency>
4056
<groupId>${project.groupId}</groupId>
4157
<artifactId>jsonld-java</artifactId>
@@ -45,8 +61,9 @@
4561
</dependency>
4662
<dependency>
4763
<groupId>org.apache.clerezza</groupId>
48-
<artifactId>rdf.core</artifactId>
49-
<version>${clerezza.version}</version>
64+
<artifactId>rdf.ontologies</artifactId>
65+
<version>0.12</version>
66+
<scope>test</scope>
5067
</dependency>
5168
<dependency>
5269
<groupId>junit</groupId>
@@ -74,6 +91,48 @@
7491
<groupId>org.jacoco</groupId>
7592
<artifactId>jacoco-maven-plugin</artifactId>
7693
</plugin>
94+
<plugin>
95+
<groupId>org.apache.felix</groupId>
96+
<artifactId>maven-bundle-plugin</artifactId>
97+
<inherited>true</inherited>
98+
<configuration>
99+
<instructions>
100+
<Bundle-DocURL>https://github.com/jsonld-java/jsonld-java-clerezza</Bundle-DocURL>
101+
<Bundle-Vendor>JSONLD-JAVA Clerezza Integration</Bundle-Vendor>
102+
<Bundle-SymbolicName>${project.artifactId}</Bundle-SymbolicName>
103+
<_versionpolicy>$${version;===;${@}}</_versionpolicy>
104+
<_provider-policy>[$(version;===;$(@)),$(version;=+;$(@)))</_provider-policy>
105+
<_consumer-policy>[$(version;===;$(@)),$(version;+;$(@)))</_consumer-policy>
106+
</instructions>
107+
</configuration>
108+
</plugin>
109+
<plugin>
110+
<groupId>org.apache.felix</groupId>
111+
<artifactId>maven-scr-plugin</artifactId>
112+
<version>1.21.0</version>
113+
<!-- slf4j impl is needed when QDox inspects/loads classes that use a
114+
static field for the logger, so that those classes can be loaded. -->
115+
<dependencies>
116+
<dependency>
117+
<groupId>org.slf4j</groupId>
118+
<artifactId>slf4j-simple</artifactId>
119+
<version>${slf4j.version}</version>
120+
</dependency>
121+
</dependencies>
122+
<executions>
123+
<execution>
124+
<id>generate-scr-scrdescriptor</id>
125+
<goals>
126+
<goal>scr</goal>
127+
</goals>
128+
<configuration>
129+
<properties>
130+
<service.vendor>JSONLD-JAVA</service.vendor>
131+
</properties>
132+
</configuration>
133+
</execution>
134+
</executions>
135+
</plugin>
77136
</plugins>
78137
</build>
79138
</project>
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package com.github.jsonldjava.clerezza;
2+
3+
import java.io.IOException;
4+
import java.io.InputStream;
5+
6+
import org.apache.clerezza.rdf.core.MGraph;
7+
import org.apache.clerezza.rdf.core.UriRef;
8+
import org.apache.clerezza.rdf.core.serializedform.ParsingProvider;
9+
import org.apache.clerezza.rdf.core.serializedform.SupportedFormat;
10+
import org.apache.felix.scr.annotations.Component;
11+
import org.apache.felix.scr.annotations.ConfigurationPolicy;
12+
import org.apache.felix.scr.annotations.Service;
13+
import org.slf4j.Logger;
14+
import org.slf4j.LoggerFactory;
15+
16+
import com.github.jsonldjava.core.JsonLdError;
17+
import com.github.jsonldjava.core.JsonLdProcessor;
18+
import com.github.jsonldjava.utils.JsonUtils;
19+
20+
/**
21+
* A {@link org.apache.clerezza.rdf.core.serializedform.ParsingProvider} for
22+
* JSON-LD (application/ld+json) based on the java-jsonld library
23+
*
24+
* @author Rupert Westenthaler
25+
*
26+
*/
27+
@Component(immediate = true, policy = ConfigurationPolicy.OPTIONAL)
28+
@Service
29+
@SupportedFormat("application/ld+json")
30+
public class ClerezzaJsonLdParsingProvider implements ParsingProvider {
31+
32+
private final Logger logger = LoggerFactory.getLogger(getClass());
33+
34+
@Override
35+
public void parse(MGraph target, InputStream serializedGraph, String formatIdentifier,
36+
UriRef baseUri) {
37+
// The callback will add parsed triples to the target MGraph
38+
final ClerezzaTripleCallback ctc = new ClerezzaTripleCallback();
39+
ctc.setMGraph(target);
40+
Object input;
41+
int startSize = 0;
42+
if (logger.isDebugEnabled()) {
43+
startSize = target.size();
44+
}
45+
final long start = System.currentTimeMillis();
46+
try {
47+
input = JsonUtils.fromInputStream(serializedGraph, "UTF-8");
48+
} catch (final IOException e) {
49+
logger.error("Unable to read from the parsed input stream", e);
50+
throw new RuntimeException(e.getMessage(), e);
51+
}
52+
try {
53+
JsonLdProcessor.toRDF(input, ctc);
54+
} catch (final JsonLdError e) {
55+
logger.error("Unable to parse JSON-LD from the parsed input stream", e);
56+
throw new RuntimeException(e.getMessage(), e);
57+
}
58+
if (logger.isDebugEnabled()) {
59+
logger.debug(" - parsed {} triples in {}ms", target.size() - startSize,
60+
System.currentTimeMillis() - start);
61+
}
62+
}
63+
64+
}
65+
Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,183 @@
1+
package com.github.jsonldjava.clerezza;
2+
3+
import java.io.IOException;
4+
import java.io.OutputStream;
5+
import java.io.OutputStreamWriter;
6+
import java.io.Writer;
7+
import java.nio.charset.Charset;
8+
import java.util.Collections;
9+
import java.util.Dictionary;
10+
import java.util.HashMap;
11+
import java.util.LinkedHashMap;
12+
import java.util.Map;
13+
14+
import org.apache.clerezza.rdf.core.TripleCollection;
15+
import org.apache.clerezza.rdf.core.serializedform.SerializingProvider;
16+
import org.apache.clerezza.rdf.core.serializedform.SupportedFormat;
17+
import org.apache.felix.scr.annotations.Activate;
18+
import org.apache.felix.scr.annotations.Component;
19+
import org.apache.felix.scr.annotations.ConfigurationPolicy;
20+
import org.apache.felix.scr.annotations.Deactivate;
21+
import org.apache.felix.scr.annotations.Property;
22+
import org.apache.felix.scr.annotations.PropertyOption;
23+
import org.apache.felix.scr.annotations.Service;
24+
import org.osgi.service.component.ComponentContext;
25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
27+
28+
import com.github.jsonldjava.core.JsonLdError;
29+
import com.github.jsonldjava.core.JsonLdOptions;
30+
import com.github.jsonldjava.core.JsonLdProcessor;
31+
import com.github.jsonldjava.utils.JsonUtils;
32+
33+
/**
34+
* A {@link org.apache.clerezza.rdf.core.serializedform.SerializingProvider} for
35+
* JSON-LD (application/ld+json) based on the java-jsonld library
36+
*
37+
* @author Rupert Westenthaler
38+
*/
39+
@Component(immediate = true, policy = ConfigurationPolicy.OPTIONAL)
40+
@Service
41+
@SupportedFormat(value = { "application/ld+json", "application/json" })
42+
public class ClerezzaJsonLdSerializingProvider implements SerializingProvider {
43+
44+
private final Logger logger = LoggerFactory.getLogger(getClass());
45+
46+
private static final Charset UTF8 = Charset.forName("UTF-8");
47+
48+
private static final String MODE_EXPAND = "expand";
49+
private static final String MODE_FLATTEN = "flatten";
50+
private static final String MODE_COMPACT = "compact";
51+
52+
@Property(value = "", options = {
53+
@PropertyOption(value = "%mode.option.none", name = ""),
54+
@PropertyOption(value = "%mode.option.flatten", name = "flatten"),
55+
@PropertyOption(value = "%mode.option.compact", name = "compact"),
56+
@PropertyOption(value = "%mode.option.expand", name = MODE_EXPAND) })
57+
private static final String PROP_MODE = "mode";
58+
59+
@Property(boolValue = false)
60+
private static final String PROP_USE_RDF_TYPE = "useRdfTye";
61+
62+
@Property(boolValue = false)
63+
private static final String PROP_USE_NATIVE_TYPES = "useNativeTypes";
64+
65+
@Property(boolValue = true)
66+
private static final String PROP_PRETTY_PRINT = "prettyPrint";
67+
68+
// TODO: make configurable or read the whole prefix.cc list from a file and
69+
// search for really used namespaces while parsing the triples in the
70+
// ClerezzaRDFParser
71+
private static Map<String, String> DEFAULT_NAMESPACES;
72+
static {
73+
// core ontologies, top from prefixcc and some stanbol specific
74+
final Map<String, String> ns = new LinkedHashMap<String, String>();
75+
// core schemas
76+
ns.put("xsd", "http://www.w3.org/2001/XMLSchema#");
77+
ns.put("owl", "http://www.w3.org/2002/07/owl#");
78+
ns.put("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#");
79+
ns.put("rdfs", "http://www.w3.org/2000/01/rdf-schema#");
80+
// well known ontologies
81+
ns.put("skos", "http://www.w3.org/2004/02/skos/core#");
82+
ns.put("geo", "http://www.w3.org/2003/01/geo/wgs84_pos#");
83+
ns.put("dc", "http://purl.org/dc/elements/1.1/");
84+
ns.put("foaf", "http://xmlns.com/foaf/0.1/");
85+
ns.put("ma", "http://www.w3.org/ns/ma-ont#");
86+
// big datasets
87+
ns.put("dbo", "http://dbpedia.org/ontology/");
88+
ns.put("dbp", "http://dbpedia.org/property/");
89+
ns.put("yago", "http://yago-knowledge.org/resource/");
90+
ns.put("fb", "http://rdf.freebase.com/ns/");
91+
ns.put("geonames", "http://www.geonames.org/ontology#");
92+
// stanbol specific
93+
ns.put("fise", "http://fise.iks-project.eu/ontology/");
94+
ns.put("enhancer", "http://stanbol.apache.org/ontology/enhancer/enhancer#");
95+
ns.put("entityhub", "http://stanbol.apache.org/ontology/entityhub/entityhub#");
96+
97+
DEFAULT_NAMESPACES = Collections.unmodifiableMap(ns);
98+
}
99+
100+
private JsonLdOptions opts = null;
101+
private String mode;
102+
103+
private boolean prettyPrint;
104+
105+
@Override
106+
public void serialize(OutputStream serializedGraph, TripleCollection tc, String formatIdentifier) {
107+
final ClerezzaRDFParser serializer = new ClerezzaRDFParser();
108+
try {
109+
final long start = System.currentTimeMillis();
110+
Object output = JsonLdProcessor.fromRDF(tc, serializer);
111+
112+
if (MODE_EXPAND.equalsIgnoreCase(mode)) {
113+
logger.debug(" - mode: {}", MODE_EXPAND);
114+
output = JsonLdProcessor.expand(output, opts);
115+
}
116+
if (MODE_FLATTEN.equalsIgnoreCase(mode)) {
117+
logger.debug(" - mode: {}", MODE_FLATTEN);
118+
// TODO: Allow inframe config
119+
final Object inframe = null;
120+
output = JsonLdProcessor.flatten(output, inframe, opts);
121+
}
122+
if (MODE_COMPACT.equalsIgnoreCase(mode)) {
123+
logger.debug(" - mode: {}", MODE_COMPACT);
124+
// TODO: collect namespaces used in the triples in the
125+
// ClerezzaRDFParser
126+
final Map<String, Object> localCtx = new HashMap<String, Object>();
127+
localCtx.put("@context", DEFAULT_NAMESPACES);
128+
output = JsonLdProcessor.compact(output, localCtx, opts);
129+
}
130+
final Writer writer = new OutputStreamWriter(serializedGraph, UTF8);
131+
logger.debug(" - prettyPrint: {}", prettyPrint);
132+
if (prettyPrint) {
133+
JsonUtils.writePrettyPrint(writer, output);
134+
} else {
135+
JsonUtils.write(writer, output);
136+
}
137+
if (logger.isDebugEnabled()) {
138+
logger.debug(" - serialized {} triples in {}ms", serializer.getCount(),
139+
System.currentTimeMillis() - start);
140+
}
141+
} catch (final JsonLdError e) {
142+
throw new RuntimeException(e.getMessage(), e);
143+
} catch (final IOException e) {
144+
throw new RuntimeException(e.getMessage(), e);
145+
}
146+
}
147+
148+
@Activate
149+
protected void activate(ComponentContext ctx) {
150+
opts = new JsonLdOptions();
151+
@SuppressWarnings("unchecked")
152+
final Dictionary<String, Object> config = ctx.getProperties();
153+
// boolean properties
154+
opts.setUseRdfType(getState(config.get(PROP_USE_RDF_TYPE), false));
155+
opts.setUseNativeTypes(getState(config.get(PROP_USE_NATIVE_TYPES), false));
156+
prettyPrint = getState(config.get(PROP_PRETTY_PRINT), true);
157+
// parse the string mode
158+
final Object value = config.get(PROP_MODE);
159+
mode = value == null ? null : value.toString();
160+
}
161+
162+
@Deactivate
163+
protected void deactivate(ComponentContext ctx) {
164+
opts = null;
165+
mode = null;
166+
prettyPrint = false;
167+
}
168+
169+
/**
170+
* @param value
171+
*/
172+
private boolean getState(Object value, boolean defaultState) {
173+
if (value instanceof Boolean) {
174+
return ((Boolean) value).booleanValue();
175+
} else if (value != null) {
176+
return Boolean.parseBoolean(value.toString());
177+
} else {
178+
return defaultState;
179+
}
180+
}
181+
182+
}
183+

0 commit comments

Comments
 (0)