Author: fchrist
Date: Wed Jun 22 15:31:06 2011
New Revision: 1138505
URL: http://svn.apache.org/viewvc?rev=1138505&view=rev
Log:
STANBOL-220 Splittet parser for JSON-LD and JSON-LD profiles. These are also
preparations for STANBOL-221 to update the implementation for the new JSON-LD
spec.
Added:
incubator/stanbol/trunk/commons/jsonld/src/main/java/org/apache/stanbol/commons/jsonld/JsonLdCommon.java
incubator/stanbol/trunk/commons/jsonld/src/main/java/org/apache/stanbol/commons/jsonld/JsonLdParserCommon.java
incubator/stanbol/trunk/commons/jsonld/src/main/java/org/apache/stanbol/commons/jsonld/JsonLdProfile.java
incubator/stanbol/trunk/commons/jsonld/src/main/java/org/apache/stanbol/commons/jsonld/JsonLdProfileParser.java
incubator/stanbol/trunk/commons/jsonld/src/test/java/org/apache/stanbol/commons/jsonld/JsonLdProfileParserTest.java
incubator/stanbol/trunk/commons/jsonld/src/test/java/org/apache/stanbol/commons/jsonld/JsonLdProfileTest.java
Modified:
incubator/stanbol/trunk/commons/jsonld/src/main/java/org/apache/stanbol/commons/jsonld/JsonLd.java
incubator/stanbol/trunk/commons/jsonld/src/main/java/org/apache/stanbol/commons/jsonld/JsonLdParser.java
incubator/stanbol/trunk/commons/jsonld/src/test/java/org/apache/stanbol/commons/jsonld/JsonLdParserTest.java
incubator/stanbol/trunk/commons/jsonld/src/test/java/org/apache/stanbol/commons/jsonld/JsonLdTest.java
Modified:
incubator/stanbol/trunk/commons/jsonld/src/main/java/org/apache/stanbol/commons/jsonld/JsonLd.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/trunk/commons/jsonld/src/main/java/org/apache/stanbol/commons/jsonld/JsonLd.java?rev=1138505&r1=1138504&r2=1138505&view=diff
==============================================================================
---
incubator/stanbol/trunk/commons/jsonld/src/main/java/org/apache/stanbol/commons/jsonld/JsonLd.java
(original)
+++
incubator/stanbol/trunk/commons/jsonld/src/main/java/org/apache/stanbol/commons/jsonld/JsonLd.java
Wed Jun 22 15:31:06 2011
@@ -6,6 +6,7 @@ import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
+import java.util.Set;
import java.util.TreeMap;
/**
@@ -17,27 +18,12 @@ import java.util.TreeMap;
*
* @author Fabian Christ
*/
-public class JsonLd {
-
- public static final String CONTEXT = "@context";
- public static final String TYPES = "#types";
- public static final String PROFILE = "@profile";
- public static final String SUBJECT = "@";
- public static final String IRI = "@iri";
-
- // Map Namespace -> Prefix
- private Map<String,String> namespacePrefixMap = new
HashMap<String,String>();
+public class JsonLd extends JsonLdCommon {
// Map Subject -> Resource
private Map<String,JsonLdResource> resourceMap = new
TreeMap<String,JsonLdResource>(new JsonComparator());
/**
- * Flag to control whether the namespace prefix map should be used to
shorten URIs to CURIEs during
- * serialization. Default value is <code>true</code>.
- */
- private boolean applyNamespaces = true;
-
- /**
* Flag to control whether the serialized JSON-LD output will use joint or
disjoint graphs for subjects
* and namespaces. Default value is <code>true</code>.
*/
@@ -50,19 +36,6 @@ public class JsonLd {
private boolean useTypeCoercion = false;
/**
- * Flag that indicates whether this JSON-LD object represents a JSON-LD
profile.
- */
- private final boolean representsProfile;
-
- public JsonLd() {
- this.representsProfile = false;
- }
-
- public JsonLd(boolean representsProfile) {
- this.representsProfile = representsProfile;
- }
-
- /**
* Adds the given resource to this JsonLd object using the resource's
subject as key. If the key is NULL
* and there does not exist a resource with an empty String as key the
resource will be added using
* an empty String ("") as key. Otherwise an @IllegalArgumentException is
thrown.
@@ -191,13 +164,11 @@ public class JsonLd {
JsonLdResource resource = resourceMap.get(subject);
- // put subject if this is not a profile
- if (!this.representsProfile) {
- if (resource.getSubject() != null &&
!resource.getSubject().isEmpty()) {
- subjectObject.put(SUBJECT,
handleCURIEs(resource.getSubject()));
- }
+ // put subject
+ if (resource.getSubject() != null &&
!resource.getSubject().isEmpty()) {
+ subjectObject.put(SUBJECT,
handleCURIEs(resource.getSubject()));
}
-
+
// put profile
if (resource.getProfile() != null &&
!resource.getProfile().isEmpty()) {
subjectObject.put(PROFILE,
handleCURIEs(resource.getProfile()));
@@ -228,14 +199,14 @@ public class JsonLd {
}
// put the namespaces
- if (!this.namespacePrefixMap.isEmpty() || (this.useTypeCoercion &&
!coercionMap.isEmpty())) {
+ if (!this.namespacePrefixMap.isEmpty() || (!coercionMap.isEmpty() &&
this.useTypeCoercion)) {
Map<String,Object> nsObject = new TreeMap<String,Object>(new
JsonComparator());
for (String ns : namespacePrefixMap.keySet()) {
nsObject.put(namespacePrefixMap.get(ns), ns);
}
- if (this.useTypeCoercion && !coercionMap.isEmpty()) {
+ if (!coercionMap.isEmpty() && this.useTypeCoercion) {
putCoercionTypes(nsObject, coercionMap);
}
json.put("#", nsObject);
@@ -427,79 +398,14 @@ public class JsonLd {
}
/**
- * Convert URI to CURIE if namespaces should be applied and CURIEs to URIs
if namespaces should not be
- * applied.
- *
- * @param uri
- * That may be in CURIE form.
- * @return
- */
- private String handleCURIEs(String uri) {
- if (this.applyNamespaces) {
- uri = doCURIE(uri);
- } else {
- uri = unCURIE(uri);
- }
-
- return uri;
- }
-
- private String doCURIE(String uri) {
- for (String namespace : namespacePrefixMap.keySet()) {
- String prefix = namespacePrefixMap.get(namespace) + ":";
- if (!uri.startsWith(prefix)) {
- uri = uri.replace(namespace, prefix);
- }
- }
- return uri;
- }
-
- private String unCURIE(String uri) {
- for (String namespace : namespacePrefixMap.keySet()) {
- String prefix = namespacePrefixMap.get(namespace) + ":";
- if (uri.startsWith(prefix)) {
- uri = uri.replace(prefix, namespace);
- }
- }
- return uri;
- }
-
- /**
* Return the JSON-LD resource for the given subject.
*/
public JsonLdResource getResource(String subject) {
- return resourceMap.get(subject);
- }
-
- /**
- * Get the known namespace to prefix mapping.
- *
- * @return A {@link Map} from namespace String to prefix String.
- */
- public Map<String,String> getNamespacePrefixMap() {
- return namespacePrefixMap;
- }
-
- /**
- * Sets the known namespaces for the serializer.
- *
- * @param namespacePrefixMap
- * A {@link Map} from namespace String to prefix String.
- */
- public void setNamespacePrefixMap(Map<String,String> namespacePrefixMap) {
- this.namespacePrefixMap = namespacePrefixMap;
+ return this.resourceMap.get(subject);
}
-
- /**
- * Adds a new namespace and its prefix to the list of used namespaces for
this JSON-LD instance.
- *
- * @param namespace
- * A namespace IRI.
- * @param prefix
- * A prefix to use and identify this namespace in serialized
JSON-LD.
- */
- public void addNamespacePrefix(String namespace, String prefix) {
- namespacePrefixMap.put(namespace, prefix);
+
+ public Set<String> getResourceSubjects() {
+ return this.resourceMap.keySet();
}
/**
@@ -521,30 +427,6 @@ public class JsonLd {
}
/**
- * Flag to control whether the namespace prefix map should be used to
shorten IRIs to prefix notation
- * during serialization. Default value is <code>true</code>.
- * <p>
- * If you already put values into this JSON-LD instance with prefix
notation, you should set this to
- * <code>false</code> before starting the serialization.
- *
- * @return <code>True</code> if namespaces are applied during
serialization, <code>false</code> otherwise.
- */
- public boolean isApplyNamespaces() {
- return applyNamespaces;
- }
-
- /**
- * Control whether namespaces from the namespace prefix map are applied to
URLs during serialization.
- * <p>
- * Set this to <code>false</code> if you already have shortened IRIs with
prefixes.
- *
- * @param applyNamespaces
- */
- public void setApplyNamespaces(boolean applyNamespaces) {
- this.applyNamespaces = applyNamespaces;
- }
-
- /**
* Flag to control whether type coercion is applied or not.
*
* @return <code>True</code> if type coercion is applied,
<code>false</code> otherwise.
@@ -563,12 +445,4 @@ public class JsonLd {
this.useTypeCoercion = useTypeCoercion;
}
- /**
- * Check whether this JSON-LD object represents a JSON-LD profile.
- *
- * @return <code>true</code> if this is a profile, <code>false</code>
otherwise.
- */
- public boolean representsProfile() {
- return this.representsProfile;
- }
}
Added:
incubator/stanbol/trunk/commons/jsonld/src/main/java/org/apache/stanbol/commons/jsonld/JsonLdCommon.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/trunk/commons/jsonld/src/main/java/org/apache/stanbol/commons/jsonld/JsonLdCommon.java?rev=1138505&view=auto
==============================================================================
---
incubator/stanbol/trunk/commons/jsonld/src/main/java/org/apache/stanbol/commons/jsonld/JsonLdCommon.java
(added)
+++
incubator/stanbol/trunk/commons/jsonld/src/main/java/org/apache/stanbol/commons/jsonld/JsonLdCommon.java
Wed Jun 22 15:31:06 2011
@@ -0,0 +1,114 @@
+package org.apache.stanbol.commons.jsonld;
+
+import java.util.HashMap;
+import java.util.Map;
+
+public abstract class JsonLdCommon {
+
+ public static final String CONTEXT = "@context";
+ public static final String COERCION = "@coercion";
+ public static final String IRI = "@iri";
+ public static final String PROFILE = "@profile";
+ public static final String SUBJECT = "@";
+ public static final String TYPES = "#types";
+
+ protected Map<String,String> namespacePrefixMap = new
HashMap<String,String>();
+ /**
+ * Flag to control whether the namespace prefix map should be used to
shorten URIs to CURIEs during
+ * serialization. Default value is <code>true</code>.
+ */
+ protected boolean applyNamespaces = true;
+
+ /**
+ * Get the known namespace to prefix mapping.
+ *
+ * @return A {@link Map} from namespace String to prefix String.
+ */
+ public Map<String,String> getNamespacePrefixMap() {
+ return this.namespacePrefixMap;
+ }
+
+ /**
+ * Sets the known namespaces for the serializer.
+ *
+ * @param namespacePrefixMap
+ * A {@link Map} from namespace String to prefix String.
+ */
+ public void setNamespacePrefixMap(Map<String,String> namespacePrefixMap) {
+ this.namespacePrefixMap = namespacePrefixMap;
+ }
+
+ /**
+ * Adds a new namespace and its prefix to the list of used namespaces for
this JSON-LD instance.
+ *
+ * @param namespace
+ * A namespace IRI.
+ * @param prefix
+ * A prefix to use and identify this namespace in serialized
JSON-LD.
+ */
+ public void addNamespacePrefix(String namespace, String prefix) {
+ namespacePrefixMap.put(namespace, prefix);
+ }
+
+ /**
+ * Flag to control whether the namespace prefix map should be used to
shorten IRIs to prefix notation
+ * during serialization. Default value is <code>true</code>.
+ * <p>
+ * If you already put values into this JSON-LD instance with prefix
notation, you should set this to
+ * <code>false</code> before starting the serialization.
+ *
+ * @return <code>True</code> if namespaces are applied during
serialization, <code>false</code> otherwise.
+ */
+ public boolean isApplyNamespaces() {
+ return applyNamespaces;
+ }
+
+ /**
+ * Control whether namespaces from the namespace prefix map are applied to
URLs during serialization.
+ * <p>
+ * Set this to <code>false</code> if you already have shortened IRIs with
prefixes.
+ *
+ * @param applyNamespaces
+ */
+ public void setApplyNamespaces(boolean applyNamespaces) {
+ this.applyNamespaces = applyNamespaces;
+ }
+
+ /**
+ * Convert URI to CURIE if namespaces should be applied and CURIEs to URIs
if namespaces should not be
+ * applied.
+ *
+ * @param uri
+ * That may be in CURIE form.
+ * @return
+ */
+ protected String handleCURIEs(String uri) {
+ if (this.applyNamespaces) {
+ uri = doCURIE(uri);
+ } else {
+ uri = unCURIE(uri);
+ }
+
+ return uri;
+ }
+
+ public String doCURIE(String uri) {
+ for (String namespace : namespacePrefixMap.keySet()) {
+ String prefix = namespacePrefixMap.get(namespace) + ":";
+ if (!uri.startsWith(prefix)) {
+ uri = uri.replace(namespace, prefix);
+ }
+ }
+ return uri;
+ }
+
+ public String unCURIE(String uri) {
+ for (String namespace : namespacePrefixMap.keySet()) {
+ String prefix = namespacePrefixMap.get(namespace) + ":";
+ if (uri.startsWith(prefix)) {
+ uri = uri.replace(prefix, namespace);
+ }
+ }
+ return uri;
+ }
+}
Modified:
incubator/stanbol/trunk/commons/jsonld/src/main/java/org/apache/stanbol/commons/jsonld/JsonLdParser.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/trunk/commons/jsonld/src/main/java/org/apache/stanbol/commons/jsonld/JsonLdParser.java?rev=1138505&r1=1138504&r2=1138505&view=diff
==============================================================================
---
incubator/stanbol/trunk/commons/jsonld/src/main/java/org/apache/stanbol/commons/jsonld/JsonLdParser.java
(original)
+++
incubator/stanbol/trunk/commons/jsonld/src/main/java/org/apache/stanbol/commons/jsonld/JsonLdParser.java
Wed Jun 22 15:31:06 2011
@@ -15,7 +15,7 @@ import org.slf4j.LoggerFactory;
*
* @author Fabian Christ
*/
-public class JsonLdParser {
+public class JsonLdParser extends JsonLdParserCommon {
private static Logger logger = LoggerFactory.getLogger(JsonLdParser.class);
@@ -25,28 +25,18 @@ public class JsonLdParser {
* @param jsonLdString A JSON-LD String.
* @return JSON-LD data structure.
*/
- public static JsonLd parse(String jsonLdString) {
+ public static JsonLd parse(String jsonLdString) throws Exception {
JsonLd jld = null;
JSONObject jo = parseJson(jsonLdString);
if (jo != null) {
- if (jo.has(JsonLd.CONTEXT)) {
- try {
- JSONObject ctx = jo.getJSONObject(JsonLd.CONTEXT);
- if (ctx.has(JsonLd.TYPES)) {
- jld = new JsonLd(true);
- }
- } catch (JSONException e) { /* ignore */}
- }
- if (jld == null) {
- jld = new JsonLd();
- }
+ jld = new JsonLd();
parseSubject(jo, jld, 1, null);
}
return jld;
}
-
+
/**
* Parses a single subject.
*
@@ -63,11 +53,11 @@ public class JsonLdParser {
JsonLdResource subject = new JsonLdResource();
try {
- if (jo.has(JsonLd.CONTEXT)) {
- JSONObject context = jo.getJSONObject(JsonLd.CONTEXT);
+ if (jo.has(JsonLdCommon.CONTEXT)) {
+ JSONObject context = jo.getJSONObject(JsonLdCommon.CONTEXT);
for (int i = 0; i < context.names().length(); i++) {
String name = context.names().getString(i).toLowerCase();
- if (name.equals(JsonLd.TYPES)) {
+ if (name.equals(JsonLdCommon.TYPES)) {
JSONObject typeObject = context.getJSONObject(name);
for (int j = 0; j < typeObject.names().length(); j++) {
String property = typeObject.names().getString(j);
@@ -79,21 +69,21 @@ public class JsonLdParser {
}
}
- jo.remove(JsonLd.CONTEXT);
+ jo.remove(JsonLdCommon.CONTEXT);
}
// If there is a local profile specified for this subject, we
// use that one. Otherwise we assign the profile given by the
parameter.
- if (jo.has(JsonLd.PROFILE)) {
- String localProfile = unCURIE(jo.getString(JsonLd.PROFILE),
jld.getNamespacePrefixMap());
+ if (jo.has(JsonLdCommon.PROFILE)) {
+ String localProfile =
unCURIE(jo.getString(JsonLdCommon.PROFILE), jld.getNamespacePrefixMap());
profile = localProfile;
- jo.remove(JsonLd.PROFILE);
+ jo.remove(JsonLdCommon.PROFILE);
}
subject.setProfile(profile);
- if (jo.has(JsonLd.SUBJECT)) {
+ if (jo.has(JsonLdCommon.SUBJECT)) {
// Check for N subjects
- Object subjectObject = jo.get(JsonLd.SUBJECT);
+ Object subjectObject = jo.get(JsonLdCommon.SUBJECT);
if (subjectObject instanceof JSONArray) {
// There is an array of subjects. We create all subjects
// in sequence.
@@ -102,10 +92,10 @@ public class JsonLdParser {
parseSubject(subjects.getJSONObject(i), jld,
bnodeCount++, profile);
}
} else {
- String subjectName = unCURIE(jo.getString(JsonLd.SUBJECT),
jld.getNamespacePrefixMap());
+ String subjectName =
unCURIE(jo.getString(JsonLdCommon.SUBJECT), jld.getNamespacePrefixMap());
subject.setSubject(subjectName);
}
- jo.remove(JsonLd.SUBJECT);
+ jo.remove(JsonLdCommon.SUBJECT);
} else {
// No subject specified. We create a dummy bnode
// and add this subject.
@@ -155,9 +145,9 @@ public class JsonLdParser {
JSONObject jo = (JSONObject) input;
// Handle IRIs
- if (jo.has(JsonLd.IRI)) {
+ if (jo.has(JsonLdCommon.IRI)) {
try {
- return new JsonLdIRI(unCURIE(jo.getString(JsonLd.IRI),
namespacePrefixMap));
+ return new
JsonLdIRI(unCURIE(jo.getString(JsonLdCommon.IRI), namespacePrefixMap));
} catch (JSONException e) {
return null;
}
@@ -195,24 +185,6 @@ public class JsonLdParser {
}
/**
- * Uses the underlying Jettison to parse a JSON object.
- *
- * @param jsonString
- * JSON String representation.
- * @return
- */
- private static JSONObject parseJson(String jsonString) {
- JSONObject jo = null;
- try {
- jo = new JSONObject(jsonString);
- } catch (JSONException e) {
- logger.debug("Could not parse JSON string: {}", jsonString, e);
- }
-
- return jo;
- }
-
- /**
* Replaces the CURIE prefixes with the namespace to create full qualified
IRIs.
*
* @param curie
Added:
incubator/stanbol/trunk/commons/jsonld/src/main/java/org/apache/stanbol/commons/jsonld/JsonLdParserCommon.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/trunk/commons/jsonld/src/main/java/org/apache/stanbol/commons/jsonld/JsonLdParserCommon.java?rev=1138505&view=auto
==============================================================================
---
incubator/stanbol/trunk/commons/jsonld/src/main/java/org/apache/stanbol/commons/jsonld/JsonLdParserCommon.java
(added)
+++
incubator/stanbol/trunk/commons/jsonld/src/main/java/org/apache/stanbol/commons/jsonld/JsonLdParserCommon.java
Wed Jun 22 15:31:06 2011
@@ -0,0 +1,30 @@
+package org.apache.stanbol.commons.jsonld;
+
+import org.codehaus.jettison.json.JSONException;
+import org.codehaus.jettison.json.JSONObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public abstract class JsonLdParserCommon {
+
+ private static Logger logger =
LoggerFactory.getLogger(JsonLdParserCommon.class);
+
+ /**
+ * Uses the underlying Jettison to parse a JSON object.
+ *
+ * @param jsonString
+ * JSON String representation.
+ * @return
+ */
+ protected static JSONObject parseJson(String jsonString) throws Exception {
+ JSONObject jo = null;
+ try {
+ jo = new JSONObject(jsonString);
+ } catch (JSONException e) {
+ logger.info("Could not parse JSON string: {}", jsonString, e);
+ throw new Exception("Could not parse JSON string: " + jsonString,
e);
+ }
+
+ return jo;
+ }
+}
Added:
incubator/stanbol/trunk/commons/jsonld/src/main/java/org/apache/stanbol/commons/jsonld/JsonLdProfile.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/trunk/commons/jsonld/src/main/java/org/apache/stanbol/commons/jsonld/JsonLdProfile.java?rev=1138505&view=auto
==============================================================================
---
incubator/stanbol/trunk/commons/jsonld/src/main/java/org/apache/stanbol/commons/jsonld/JsonLdProfile.java
(added)
+++
incubator/stanbol/trunk/commons/jsonld/src/main/java/org/apache/stanbol/commons/jsonld/JsonLdProfile.java
Wed Jun 22 15:31:06 2011
@@ -0,0 +1,80 @@
+package org.apache.stanbol.commons.jsonld;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.TreeMap;
+
+import org.codehaus.jettison.json.JSONArray;
+
+public class JsonLdProfile extends JsonLdCommon {
+
+ private Map<String,List<String>> typesMap = new
HashMap<String,List<String>>();
+
+ public void addType(String property, String type) {
+ if (this.typesMap.get(property) == null) {
+ this.typesMap.put(property, new ArrayList<String>());
+ }
+ List<String> types = this.typesMap.get(property);
+ types.add(type);
+ }
+
+ public void addTypes(String property, List<String> types) {
+ if (this.typesMap.get(property) == null) {
+ this.typesMap.put(property, new ArrayList<String>());
+ }
+ this.typesMap.get(property).addAll(types);
+ }
+
+ public List<String> getTypes(String property) {
+ return this.typesMap.get(property);
+ }
+
+ public Set<String> getTypes() {
+ return typesMap.keySet();
+ }
+
+ public String toString() {
+ Map<String,Object> json = createJson();
+
+ return JsonSerializer.toString(json);
+ }
+
+ public String toString(int indent) {
+ Map<String,Object> json = createJson();
+
+ return JsonSerializer.toString(json, indent);
+ }
+
+ private Map<String,Object> createJson() {
+ Map<String,Object> json = new TreeMap<String,Object>(new
JsonComparator());
+
+ // put the namespaces
+ Map<String,Object> contextObject = new TreeMap<String,Object>(new
JsonComparator());
+ for (String ns : namespacePrefixMap.keySet()) {
+ contextObject.put(namespacePrefixMap.get(ns), ns);
+ }
+
+ // put types
+ Map<String,Object> typesObject = new TreeMap<String,Object>(new
JsonComparator());
+ for (String property : this.typesMap.keySet()) {
+ List<String> types = this.typesMap.get(property);
+ if (types.size() == 1) {
+ typesObject.put(handleCURIEs(property),
handleCURIEs(types.get(0)));
+ } else {
+ JSONArray typesArray = new JSONArray();
+ for (String type : types) {
+ typesArray.put(handleCURIEs(type));
+ }
+ typesObject.put(handleCURIEs(property), typesArray);
+ }
+ }
+ contextObject.put(JsonLdCommon.TYPES, typesObject);
+
+ json.put(JsonLdCommon.CONTEXT, contextObject);
+
+ return json;
+ }
+}
Added:
incubator/stanbol/trunk/commons/jsonld/src/main/java/org/apache/stanbol/commons/jsonld/JsonLdProfileParser.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/trunk/commons/jsonld/src/main/java/org/apache/stanbol/commons/jsonld/JsonLdProfileParser.java?rev=1138505&view=auto
==============================================================================
---
incubator/stanbol/trunk/commons/jsonld/src/main/java/org/apache/stanbol/commons/jsonld/JsonLdProfileParser.java
(added)
+++
incubator/stanbol/trunk/commons/jsonld/src/main/java/org/apache/stanbol/commons/jsonld/JsonLdProfileParser.java
Wed Jun 22 15:31:06 2011
@@ -0,0 +1,76 @@
+package org.apache.stanbol.commons.jsonld;
+
+import org.codehaus.jettison.json.JSONArray;
+import org.codehaus.jettison.json.JSONObject;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class JsonLdProfileParser extends JsonLdParserCommon {
+
+ private static Logger logger =
LoggerFactory.getLogger(JsonLdProfileParser.class);
+
+ public static JsonLdProfile parseProfile(String jsonLdProfileString)
throws Exception {
+ logger.debug("Parsing {} " + jsonLdProfileString);
+
+ JsonLdProfile profile = null;
+
+ JSONObject jo = parseJson(jsonLdProfileString);
+ if (jo != null) {
+ if (jo.has(JsonLdCommon.CONTEXT)) {
+ JSONObject ctx = jo.getJSONObject(JsonLdCommon.CONTEXT);
+ if (ctx.has(JsonLdCommon.TYPES)) {
+ profile = new JsonLdProfile();
+ parseSubject(jo, profile, 1);
+ }
+ } else {
+ logger.error("A JSON-LD Profile must have a context element "
+ JsonLdCommon.CONTEXT);
+ throw new IllegalArgumentException("A JSON-LD Profile must
have a context element "
+ + JsonLdCommon.CONTEXT);
+ }
+ } else {
+ logger.error("Could not parse JSON-LD Profile '{}'" +
jsonLdProfileString);
+ throw new IllegalArgumentException("Could not parse JSON-LD
Profile String");
+ }
+
+ return profile;
+ }
+
+ /**
+ * Parses a single subject.
+ *
+ * @param jo
+ * JSON object that holds the subject's data.
+ * @param profile
+ * JsonLd object to add the created subject resource.
+ */
+ private static void parseSubject(JSONObject jo, JsonLdProfile profile, int
bnodeCount) throws Exception {
+ if (jo.has(JsonLdCommon.CONTEXT)) {
+ JSONObject context = jo.getJSONObject(JsonLdCommon.CONTEXT);
+ for (int i = 0; i < context.names().length(); i++) {
+ String name = context.names().getString(i).toLowerCase();
+ if (name.equals(JsonLdCommon.TYPES)) {
+ JSONObject typesObject = context.getJSONObject(name);
+ for (int j = 0; j < typesObject.names().length(); j++) {
+ String property = typesObject.names().getString(j);
+
+ Object typeObject = typesObject.get(property);
+ if (typeObject instanceof String) {
+ String typeStr = (String) typeObject;
+ profile.addType(property, typeStr);
+ } else if (typeObject instanceof JSONArray) {
+ JSONArray typesArray = (JSONArray) typeObject;
+ for (int t = 0; t < typesArray.length(); t++) {
+ profile.addType(property,
typesArray.getString(t));
+ }
+ }
+ }
+ } else {
+ profile.addNamespacePrefix(context.getString(name), name);
+ }
+ }
+
+ jo.remove(JsonLdCommon.CONTEXT);
+ }
+ }
+
+}
Modified:
incubator/stanbol/trunk/commons/jsonld/src/test/java/org/apache/stanbol/commons/jsonld/JsonLdParserTest.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/trunk/commons/jsonld/src/test/java/org/apache/stanbol/commons/jsonld/JsonLdParserTest.java?rev=1138505&r1=1138504&r2=1138505&view=diff
==============================================================================
---
incubator/stanbol/trunk/commons/jsonld/src/test/java/org/apache/stanbol/commons/jsonld/JsonLdParserTest.java
(original)
+++
incubator/stanbol/trunk/commons/jsonld/src/test/java/org/apache/stanbol/commons/jsonld/JsonLdParserTest.java
Wed Jun 22 15:31:06 2011
@@ -7,21 +7,7 @@ import org.junit.Test;
public class JsonLdParserTest {
@Test
- public void testParseProfile() {
- String jsonldInput =
"{\"@context\":{\"iks\":\"http://iks-project.eu/ont/\",\"#types\":{\"person\":\"iks:person\",\"organization\":\"iks:organization\"}}}";
-
- JsonLd jsonLd = JsonLdParser.parse(jsonldInput);
- jsonLd.setUseTypeCoercion(true);
-
- String actual = jsonLd.toString();
- String expected =
"{\"#\":{\"iks\":\"http:\\/\\/iks-project.eu\\/ont\\/\",\"#types\":{\"organization\":\"iks:organization\",\"person\":\"iks:person\"}}}";
- assertEquals(expected, actual);
- assertTrue(jsonLd.representsProfile());
- assertNotNull(jsonLd);
- }
-
- @Test
- public void testParseWithProfile() {
+ public void testParseWithProfile() throws Exception {
String jsonldInput =
"{\"@context\":{\"iks\":\"http://iks-project.eu/ont/\",\"upb\":\"http://upb.de/persons/\"},\"@profile\":\"iks:employeeOf\",\"person\":{\"@iri\":\"upb:bnagel\"},\"organization\":{\"@iri\":\"http://uni-paderborn.de\"}}";
JsonLd jsonLd = JsonLdParser.parse(jsonldInput);
@@ -30,12 +16,11 @@ public class JsonLdParserTest {
String actual = jsonLd.toString();
String expected =
"{\"#\":{\"iks\":\"http:\\/\\/iks-project.eu\\/ont\\/\",\"upb\":\"http:\\/\\/upb.de\\/persons\\/\"},\"@\":\"_:bnode1\",\"@profile\":\"iks:employeeOf\",\"organization\":{\"@iri\":\"http:\\/\\/uni-paderborn.de\"},\"person\":{\"@iri\":\"upb:bnagel\"}}";
assertEquals(expected, actual);
- assertFalse(jsonLd.representsProfile());
assertNotNull(jsonLd);
}
@Test
- public void testParse3() {
+ public void testParse3() throws Exception {
String jsonldInput =
"{\"@context\":{\"iks\":\"http://iks-project.eu/ont/\",\"upb\":\"http://upb.de/persons/\"},\"@profile\":\"iks:employeeOf\",\"@\":[{\"person\":{\"@iri\":\"upb:bnagel\"},\"organization\":{\"@iri\":\"http://uni-paderborn.de\"}},{\"person\":{\"@iri\":\"upb:fchrist\"},\"organization\":{\"@iri\":\"http://uni-paderborn.de\"}}]}";
JsonLd jsonLd = JsonLdParser.parse(jsonldInput);
@@ -45,12 +30,11 @@ public class JsonLdParserTest {
String actual = jsonLd.toString();
String expected =
"{\"#\":{\"iks\":\"http:\\/\\/iks-project.eu\\/ont\\/\",\"upb\":\"http:\\/\\/upb.de\\/persons\\/\"},\"@\":[{\"@\":\"_:bnode1\",\"@profile\":\"http:\\/\\/iks-project.eu\\/ont\\/employeeOf\",\"organization\":{\"@iri\":\"http:\\/\\/uni-paderborn.de\"},\"person\":{\"@iri\":\"http:\\/\\/upb.de\\/persons\\/bnagel\"}},{\"@\":\"_:bnode2\",\"@profile\":\"http:\\/\\/iks-project.eu\\/ont\\/employeeOf\",\"organization\":{\"@iri\":\"http:\\/\\/uni-paderborn.de\"},\"person\":{\"@iri\":\"http:\\/\\/upb.de\\/persons\\/fchrist\"}}]}";
assertEquals(expected, actual);
- assertFalse(jsonLd.representsProfile());
assertNotNull(jsonLd);
}
@Test
- public void testParse4() {
+ public void testParse4() throws Exception {
String jsonldInput =
"{\"@context\":{\"iks\":\"http://iks-project.eu/ont/\",\"upb\":\"http://upb.de/persons/\"},\"@profile\":\"iks:employeeOf\",\"@\":[{\"person\":{\"@iri\":\"upb:bnagel\"},\"organization\":{\"@iri\":\"http://uni-paderborn.de\"}},{\"person\":{\"@iri\":\"upb:fchrist\"},\"organization\":{\"@iri\":\"http://uni-paderborn.de\"}}]}";
JsonLd jsonLd = JsonLdParser.parse(jsonldInput);
@@ -60,12 +44,11 @@ public class JsonLdParserTest {
String actual = jsonLd.toString();
String expected =
"{\"#\":{\"iks\":\"http:\\/\\/iks-project.eu\\/ont\\/\",\"upb\":\"http:\\/\\/upb.de\\/persons\\/\"},\"@\":[{\"@\":\"_:bnode1\",\"@profile\":\"iks:employeeOf\",\"organization\":{\"@iri\":\"http:\\/\\/uni-paderborn.de\"},\"person\":{\"@iri\":\"upb:bnagel\"}},{\"@\":\"_:bnode2\",\"@profile\":\"iks:employeeOf\",\"organization\":{\"@iri\":\"http:\\/\\/uni-paderborn.de\"},\"person\":{\"@iri\":\"upb:fchrist\"}}]}";
assertEquals(expected, actual);
- assertFalse(jsonLd.representsProfile());
assertNotNull(jsonLd);
}
@Test
- public void testParse5() {
+ public void testParse5() throws Exception {
String jsonldInput =
"{\"@context\":{\"iks\":\"http://iks-project.eu/ont/\",\"upb\":\"http://upb.de/persons/\"},\"@profile\":\"iks:employeeOf\",\"@\":[{\"person\":{\"@iri\":\"upb:bnagel\"},\"organization\":{\"@iri\":\"http://uni-paderborn.de\"}},{\"person\":{\"@iri\":\"upb:fchrist\"},\"organization\":{\"@iri\":\"http://uni-paderborn.de\"}}]}";
JsonLd jsonLd = JsonLdParser.parse(jsonldInput);
@@ -76,12 +59,11 @@ public class JsonLdParserTest {
String actual = jsonLd.toString();
String expected =
"[{\"#\":{\"iks\":\"http:\\/\\/iks-project.eu\\/ont\\/\",\"upb\":\"http:\\/\\/upb.de\\/persons\\/\"},\"@\":\"_:bnode1\",\"@profile\":\"iks:employeeOf\",\"organization\":{\"@iri\":\"http:\\/\\/uni-paderborn.de\"},\"person\":{\"@iri\":\"upb:bnagel\"}},{\"#\":{\"iks\":\"http:\\/\\/iks-project.eu\\/ont\\/\",\"upb\":\"http:\\/\\/upb.de\\/persons\\/\"},\"@\":\"_:bnode2\",\"@profile\":\"iks:employeeOf\",\"organization\":{\"@iri\":\"http:\\/\\/uni-paderborn.de\"},\"person\":{\"@iri\":\"upb:fchrist\"}}]";
assertEquals(expected, actual);
- assertFalse(jsonLd.representsProfile());
assertNotNull(jsonLd);
}
@Test
- public void testParse6() {
+ public void testParse6() throws Exception {
String jsonldInput =
"{\"@context\":{\"iks\":\"http://iks-project.eu/ont/\",\"upb\":\"http://upb.de/persons/\"},\"@\":[{\"@profile\":\"iks:employeeOf\",\"person\":{\"@iri\":\"upb:bnagel\"},\"organization\":{\"@iri\":\"http://uni-paderborn.de\"}},{\"@profile\":\"iks:friendOf\",\"person\":{\"@iri\":\"upb:bnagel\"},\"friend\":{\"@iri\":\"upb:fchrist\"}}]}";
JsonLd jsonLd = JsonLdParser.parse(jsonldInput);
@@ -92,12 +74,11 @@ public class JsonLdParserTest {
String actual = jsonLd.toString();
String expected =
"{\"#\":{\"iks\":\"http:\\/\\/iks-project.eu\\/ont\\/\",\"upb\":\"http:\\/\\/upb.de\\/persons\\/\"},\"@\":[{\"@\":\"_:bnode1\",\"@profile\":\"iks:employeeOf\",\"organization\":{\"@iri\":\"http:\\/\\/uni-paderborn.de\"},\"person\":{\"@iri\":\"upb:bnagel\"}},{\"@\":\"_:bnode2\",\"@profile\":\"iks:friendOf\",\"friend\":{\"@iri\":\"upb:fchrist\"},\"person\":{\"@iri\":\"upb:bnagel\"}}]}";
assertEquals(expected, actual);
- assertFalse(jsonLd.representsProfile());
assertNotNull(jsonLd);
}
@Test
- public void testParse7() {
+ public void testParse7() throws Exception {
String jsonldInput =
"{\"@context\":{\"iks\":\"http://iks-project.eu/ont/\",\"upb\":\"http://upb.de/persons/\"},\"@profile\":\"iks:employeeOf\",\"person\":\"Benjamin\",\"organization\":\"UniPaderborn\"}";
JsonLd jsonLd = JsonLdParser.parse(jsonldInput);
@@ -108,7 +89,6 @@ public class JsonLdParserTest {
String actual = jsonLd.toString();
String expected =
"{\"#\":{\"iks\":\"http:\\/\\/iks-project.eu\\/ont\\/\",\"upb\":\"http:\\/\\/upb.de\\/persons\\/\"},\"@\":\"_:bnode1\",\"@profile\":\"iks:employeeOf\",\"organization\":\"UniPaderborn\",\"person\":\"Benjamin\"}";
assertEquals(expected, actual);
- assertFalse(jsonLd.representsProfile());
assertNotNull(jsonLd);
}
Added:
incubator/stanbol/trunk/commons/jsonld/src/test/java/org/apache/stanbol/commons/jsonld/JsonLdProfileParserTest.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/trunk/commons/jsonld/src/test/java/org/apache/stanbol/commons/jsonld/JsonLdProfileParserTest.java?rev=1138505&view=auto
==============================================================================
---
incubator/stanbol/trunk/commons/jsonld/src/test/java/org/apache/stanbol/commons/jsonld/JsonLdProfileParserTest.java
(added)
+++
incubator/stanbol/trunk/commons/jsonld/src/test/java/org/apache/stanbol/commons/jsonld/JsonLdProfileParserTest.java
Wed Jun 22 15:31:06 2011
@@ -0,0 +1,44 @@
+package org.apache.stanbol.commons.jsonld;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import org.junit.Test;
+
+
+public class JsonLdProfileParserTest {
+
+ @Test
+ public void testParseProfile() throws Exception {
+ String jsonldInput =
"{\"@context\":{\"iks\":\"http://iks-project.eu/ont/\",\"#types\":{\"person\":\"iks:person\",\"organization\":\"iks:organization\"}}}";
+
+ JsonLdProfile profile = JsonLdProfileParser.parseProfile(jsonldInput);
+
+ String actual = profile.toString();
+ String expected =
"{\"@context\":{\"iks\":\"http:\\/\\/iks-project.eu\\/ont\\/\",\"#types\":{\"organization\":\"iks:organization\",\"person\":\"iks:person\"}}}";
+ assertEquals(expected, actual);
+ assertNotNull(profile);
+ }
+
+ @Test
+ public void testParseProfileMultiType() throws Exception {
+ String jsonldInput =
"{\"@context\":{\"#types\":{\"organization\":\"http:\\/\\/iks-project.eu\\/ont\\/organization\",\"person\":[\"http:\\/\\/iks-project.eu\\/ont\\/person\",\"http:\\/\\/www.schema.org\\/Person\"]}}}";
+
+ JsonLdProfile profile = JsonLdProfileParser.parseProfile(jsonldInput);
+
+ String expected =
"{\"@context\":{\"#types\":{\"organization\":\"http:\\/\\/iks-project.eu\\/ont\\/organization\",\"person\":[\"http:\\/\\/iks-project.eu\\/ont\\/person\",\"http:\\/\\/www.schema.org\\/Person\"]}}}";
+ String actual = profile.toString();
+ assertEquals(expected, actual);
+ assertNotNull(profile);
+ }
+
+ @SuppressWarnings("unused")
+ private void toConsole(String actual) {
+ System.out.println(actual);
+ String s = actual;
+ s = s.replaceAll("\\\\", "\\\\\\\\");
+ s = s.replace("\"", "\\\"");
+ s = s.replace("\n", "\\n");
+ System.out.println(s);
+ }
+}
Added:
incubator/stanbol/trunk/commons/jsonld/src/test/java/org/apache/stanbol/commons/jsonld/JsonLdProfileTest.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/trunk/commons/jsonld/src/test/java/org/apache/stanbol/commons/jsonld/JsonLdProfileTest.java?rev=1138505&view=auto
==============================================================================
---
incubator/stanbol/trunk/commons/jsonld/src/test/java/org/apache/stanbol/commons/jsonld/JsonLdProfileTest.java
(added)
+++
incubator/stanbol/trunk/commons/jsonld/src/test/java/org/apache/stanbol/commons/jsonld/JsonLdProfileTest.java
Wed Jun 22 15:31:06 2011
@@ -0,0 +1,57 @@
+package org.apache.stanbol.commons.jsonld;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.Test;
+
+
+public class JsonLdProfileTest {
+
+ @Test
+ public void testDefineProfile() {
+ JsonLdProfile profile = new JsonLdProfile();
+ profile.addNamespacePrefix("http://iks-project.eu/ont/", "iks");
+
+ profile.addType("person", "iks:person");
+ profile.addType("organization", "iks:organization");
+
+ String actual = profile.toString();
+ String expected =
"{\"@context\":{\"iks\":\"http:\\/\\/iks-project.eu\\/ont\\/\",\"#types\":{\"organization\":\"iks:organization\",\"person\":\"iks:person\"}}}";
+ assertEquals(expected, actual);
+ }
+
+ @Test
+ public void testDefineProfileNoNS() {
+ JsonLdProfile profile = new JsonLdProfile();
+
+ profile.addType("person", "http://iks-project.eu/ont/person");
+ profile.addType("organization",
"http://iks-project.eu/ont/organization");
+
+ String actual = profile.toString();
+ String expected =
"{\"@context\":{\"#types\":{\"organization\":\"http:\\/\\/iks-project.eu\\/ont\\/organization\",\"person\":\"http:\\/\\/iks-project.eu\\/ont\\/person\"}}}";
+ assertEquals(expected, actual);
+ }
+
+ @Test
+ public void testDefineProfileNoNSMultiTypes() {
+ JsonLdProfile profile = new JsonLdProfile();
+
+ profile.addType("person", "http://iks-project.eu/ont/person");
+ profile.addType("person", "http://www.schema.org/Person");
+ profile.addType("organization",
"http://iks-project.eu/ont/organization");
+
+ String actual = profile.toString(0);
+ String expected =
"{\"@context\":{\"#types\":{\"organization\":\"http:\\/\\/iks-project.eu\\/ont\\/organization\",\"person\":[\"http:\\/\\/iks-project.eu\\/ont\\/person\",\"http:\\/\\/www.schema.org\\/Person\"]}}}";
+ assertEquals(expected, actual);
+ }
+
+ @SuppressWarnings("unused")
+ private void toConsole(String actual) {
+ System.out.println(actual);
+ String s = actual;
+ s = s.replaceAll("\\\\", "\\\\\\\\");
+ s = s.replace("\"", "\\\"");
+ s = s.replace("\n", "\\n");
+ System.out.println(s);
+ }
+}
Modified:
incubator/stanbol/trunk/commons/jsonld/src/test/java/org/apache/stanbol/commons/jsonld/JsonLdTest.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/trunk/commons/jsonld/src/test/java/org/apache/stanbol/commons/jsonld/JsonLdTest.java?rev=1138505&r1=1138504&r2=1138505&view=diff
==============================================================================
---
incubator/stanbol/trunk/commons/jsonld/src/test/java/org/apache/stanbol/commons/jsonld/JsonLdTest.java
(original)
+++
incubator/stanbol/trunk/commons/jsonld/src/test/java/org/apache/stanbol/commons/jsonld/JsonLdTest.java
Wed Jun 22 15:31:06 2011
@@ -241,7 +241,7 @@ public class JsonLdTest {
r1.setSubject("<http://example.org/people#joebob>");
String nick = "\"stu\"^^http://www.w3.org/2001/XMLSchema#string";
r1.putProperty("http://xmlns.com/foaf/0.1/nick", nick);
- r1.getCoercionMap().put("http://xmlns.com/foaf/0.1/nick",
"xsd:string");
+ r1.putCoercionType("http://xmlns.com/foaf/0.1/nick", "xsd:string");
jsonLd.put("r1", r1);
String actual = jsonLd.toString();
@@ -262,7 +262,7 @@ public class JsonLdTest {
r1.setSubject("<http://example.org/people#joebob>");
String nick = "\"stu\"^^http://www.w3.org/2001/XMLSchema#string";
r1.putProperty("http://xmlns.com/foaf/0.1/nick", nick);
- r1.getCoercionMap().put("http://xmlns.com/foaf/0.1/nick",
"xsd:string");
+ r1.putCoercionType("http://xmlns.com/foaf/0.1/nick", "xsd:string");
jsonLd.put("r1", r1);
String actual = jsonLd.toString();
@@ -283,7 +283,7 @@ public class JsonLdTest {
r1.setSubject("<http://example.org/people#joebob>");
String nick = "\"stu\"^^http://www.w3.org/2001/XMLSchema#string";
r1.putProperty("http://xmlns.com/foaf/0.1/nick", nick);
- r1.getCoercionMap().put("http://xmlns.com/foaf/0.1/nick",
"xsd:string");
+ r1.putCoercionType("http://xmlns.com/foaf/0.1/nick", "xsd:string");
jsonLd.put("r1", r1);
String actual = jsonLd.toString();
@@ -304,7 +304,7 @@ public class JsonLdTest {
r1.setSubject("<http://example.org/people#joebob>");
String nick = "\"stu\"^^http://www.w3.org/2001/XMLSchema#string";
r1.putProperty("http://xmlns.com/foaf/0.1/nick", nick);
- r1.getCoercionMap().put("http://xmlns.com/foaf/0.1/nick",
"xsd:string");
+ r1.putCoercionType("http://xmlns.com/foaf/0.1/nick", "xsd:string");
jsonLd.put("r1", r1);
String actual = jsonLd.toString();
@@ -477,8 +477,8 @@ public class JsonLdTest {
}
@Test
- public void testProfile() {
- JsonLd jsonLd = new JsonLd(true);
+ public void testUseProfile() {
+ JsonLd jsonLd = new JsonLd();
jsonLd.addNamespacePrefix("http://www.w3.org/2001/XMLSchema#", "xsd");
jsonLd.addNamespacePrefix("http://xmlns.com/foaf/0.1/", "foaf");
@@ -488,10 +488,8 @@ public class JsonLdTest {
r1.setSubject("_:bnode1");
jsonLd.put(r1);
- assertTrue(jsonLd.representsProfile());
-
String actual = jsonLd.toString();
- String expected =
"{\"#\":{\"foaf\":\"http:\\/\\/xmlns.com\\/foaf\\/0.1\\/\",\"xsd\":\"http:\\/\\/www.w3.org\\/2001\\/XMLSchema#\"},\"@profile\":\"testprofile\"}";
+ String expected =
"{\"#\":{\"foaf\":\"http:\\/\\/xmlns.com\\/foaf\\/0.1\\/\",\"xsd\":\"http:\\/\\/www.w3.org\\/2001\\/XMLSchema#\"},\"@\":\"_:bnode1\",\"@profile\":\"testprofile\"}";
assertEquals(expected, actual);
}