Author: fchrist
Date: Thu May 5 09:54:26 2011
New Revision: 1099740
URL: http://svn.apache.org/viewvc?rev=1099740&view=rev
Log:
STANBOL-190 Added JSON-LD type coercion support; Fixed STANBOL-131
Modified:
incubator/stanbol/trunk/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/writers/JsonLdSerializerProvider.java
incubator/stanbol/trunk/commons/web/base/src/test/java/org/apache/stanbol/commons/web/base/writers/JsonLdSerializerProviderTest.java
incubator/stanbol/trunk/enhancer/generic/jsonld/src/main/java/org/apache/stanbol/jsonld/JsonComparator.java
incubator/stanbol/trunk/enhancer/generic/jsonld/src/main/java/org/apache/stanbol/jsonld/JsonLd.java
incubator/stanbol/trunk/enhancer/generic/jsonld/src/main/java/org/apache/stanbol/jsonld/JsonLdResource.java
incubator/stanbol/trunk/enhancer/generic/jsonld/src/main/java/org/apache/stanbol/jsonld/JsonSerializer.java
Modified:
incubator/stanbol/trunk/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/writers/JsonLdSerializerProvider.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/trunk/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/writers/JsonLdSerializerProvider.java?rev=1099740&r1=1099739&r2=1099740&view=diff
==============================================================================
---
incubator/stanbol/trunk/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/writers/JsonLdSerializerProvider.java
(original)
+++
incubator/stanbol/trunk/commons/web/base/src/main/java/org/apache/stanbol/commons/web/base/writers/JsonLdSerializerProvider.java
Thu May 5 09:54:26 2011
@@ -14,6 +14,7 @@ import org.apache.clerezza.rdf.core.BNod
import org.apache.clerezza.rdf.core.NonLiteral;
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.rdf.core.serializedform.SerializingProvider;
import org.apache.clerezza.rdf.core.serializedform.SupportedFormat;
@@ -49,6 +50,7 @@ public class JsonLdSerializerProvider im
private Map<String, String> namespacePrefixMap = new HashMap<String,
String>();
private int indentation = 2;
+ private boolean useTypeCoercion = false;
@Override
public void serialize(OutputStream serializedGraph, TripleCollection tc,
String formatIdentifier) {
@@ -79,8 +81,15 @@ public class JsonLdSerializerProvider im
}
String property =
currentTriple.getPredicate().getUnicodeString();
- String value = currentTriple.getObject().toString();
- resource.putProperty(property, value);
+ String strValue = currentTriple.getObject().toString();
+ if (currentTriple.getObject() instanceof TypedLiteral) {
+ TypedLiteral typedObject = (TypedLiteral)
currentTriple.getObject();
+ String type =
typedObject.getDataType().getUnicodeString();
+ strValue = typedObject.getLexicalForm();
+ resource.putCoercionType(property, type);
+ }
+
+ resource.putProperty(property, convertValueType(strValue));
}
}
@@ -89,6 +98,7 @@ public class JsonLdSerializerProvider im
try {
BufferedWriter writer = new BufferedWriter(new
OutputStreamWriter(serializedGraph));
+ jsonLd.setUseTypeCoercion(this.useTypeCoercion);
writer.write(jsonLd.toString(this.indentation));
writer.flush();
} catch (IOException ioe) {
@@ -96,7 +106,7 @@ public class JsonLdSerializerProvider im
throw new RuntimeException(ioe.getMessage());
}
}
-
+
private Map<NonLiteral, String> createSubjectsMap(TripleCollection tc) {
Map<NonLiteral, String> subjects = new HashMap<NonLiteral, String>();
int bNodeCounter = 0;
@@ -113,6 +123,21 @@ public class JsonLdSerializerProvider im
}
return subjects;
}
+
+ private Object convertValueType(String strValue) {
+ // check if value can be interpreted as integer
+ try {
+ return Integer.valueOf(strValue);
+ }
+ catch (Throwable t) {};
+
+ // check if value can be interpreted as boolean
+ if (strValue.equalsIgnoreCase("true") ||
strValue.equalsIgnoreCase("false")) {
+ return Boolean.valueOf(strValue);
+ }
+
+ return strValue;
+ }
/**
* Get the known namespace to prefix mapping.
@@ -151,4 +176,24 @@ public class JsonLdSerializerProvider im
public void setIndentation(int indentation) {
this.indentation = indentation;
}
+
+ /**
+ * Check if JSON-LD type coercion is applied on serialization.
+ *
+ * @return
+ */
+ public boolean isUseTypeCoercion() {
+ return useTypeCoercion;
+ }
+
+ /**
+ * If JSON-LD type coercion should be applied set this
+ * to <code>true</code>.
+ *
+ * @param useTypeCoercion
+ */
+ public void setUseTypeCoercion(boolean useTypeCoercion) {
+ this.useTypeCoercion = useTypeCoercion;
+ }
+
}
Modified:
incubator/stanbol/trunk/commons/web/base/src/test/java/org/apache/stanbol/commons/web/base/writers/JsonLdSerializerProviderTest.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/trunk/commons/web/base/src/test/java/org/apache/stanbol/commons/web/base/writers/JsonLdSerializerProviderTest.java?rev=1099740&r1=1099739&r2=1099740&view=diff
==============================================================================
---
incubator/stanbol/trunk/commons/web/base/src/test/java/org/apache/stanbol/commons/web/base/writers/JsonLdSerializerProviderTest.java
(original)
+++
incubator/stanbol/trunk/commons/web/base/src/test/java/org/apache/stanbol/commons/web/base/writers/JsonLdSerializerProviderTest.java
Thu May 5 09:54:26 2011
@@ -46,6 +46,34 @@ public class JsonLdSerializerProviderTes
jsonldProvider.serialize(serializedGraph, ci.getMetadata(),
"application/format+notsupported");
}
+
+ @Test
+ public void testEmptyGraph() {
+ MGraph emptyGraph = new SimpleMGraph();
+
+ OutputStream serializedGraph = new ByteArrayOutputStream();
+ jsonldProvider.setIndentation(0);
+ jsonldProvider.serialize(serializedGraph, emptyGraph,
formatIdentifier);
+
+ String expected = "{}";
+ String result = serializedGraph.toString();
+ Assert.assertEquals(expected, result);
+ }
+
+ @Test
+ public void testEmptyGraphWithIndent() {
+ MGraph emptyGraph = new SimpleMGraph();
+
+ OutputStream serializedGraph = new ByteArrayOutputStream();
+ jsonldProvider.setIndentation(4);
+ jsonldProvider.serialize(serializedGraph, emptyGraph,
formatIdentifier);
+
+ String expected = "{\n\n}";
+ String result = serializedGraph.toString();
+ toConsole(result);
+ Assert.assertEquals(expected, result);
+ }
+
@Test
public void testSingleSubjectSerializeNoNs() {
String context = "Dr. Patrick Marshall (1869 - November 1950) was a
geologist who lived "
@@ -64,6 +92,24 @@ public class JsonLdSerializerProviderTes
}
@Test
+ public void testSingleSubjectSerializeNoNsWithCoercion() {
+ String context = "Dr. Patrick Marshall (1869 - November 1950) was a
geologist who lived "
+ + "in New Zealand and worked at the University of Otago.";
+
+ ContentItem ci =
getContentItem("urn:iks-project:enhancer:test:content-item:person", context);
+ getTextAnnotation(ci, "Person", "Patrick Marshall", context,
OntologicalClasses.DBPEDIA_PERSON);
+
+ OutputStream serializedGraph = new ByteArrayOutputStream();
+ jsonldProvider.setIndentation(0);
+ jsonldProvider.setUseTypeCoercion(true);
+ jsonldProvider.serialize(serializedGraph, ci.getMetadata(),
formatIdentifier);
+
+ String expected =
"{\"#\":{\"#types\":{\"http:\\/\\/fise.iks-project.eu\\/ontology\\/end\":\"http:\\/\\/www.w3.org\\/2001\\/XMLSchema#int\",\"http:\\/\\/fise.iks-project.eu\\/ontology\\/selected-text\":\"http:\\/\\/www.w3.org\\/2001\\/XMLSchema#string\",\"http:\\/\\/fise.iks-project.eu\\/ontology\\/selection-context\":\"http:\\/\\/www.w3.org\\/2001\\/XMLSchema#string\",\"http:\\/\\/fise.iks-project.eu\\/ontology\\/start\":\"http:\\/\\/www.w3.org\\/2001\\/XMLSchema#int\",\"http:\\/\\/purl.org\\/dc\\/terms\\/created\":\"http:\\/\\/www.w3.org\\/2001\\/XMLSchema#dateTime\"}},\"@\":\"<urn:iks-project:enhancer:test:text-annotation:Person>\",\"a\":[\"<http:\\/\\/fise.iks-project.eu\\/ontology\\/Enhancement>\",\"<http:\\/\\/fise.iks-project.eu\\/ontology\\/TextAnnotation>\"],\"http:\\/\\/fise.iks-project.eu\\/ontology\\/end\":20,\"http:\\/\\/fise.iks-project.eu\\/ontology\\/selected-text\":\"Patrick
Marshall\",\"http:\\/\\/fise.iks-project.eu\\/ontology\\/selection-context\"
:\"Dr. Patrick Marshall (1869 - November 1950) was a geologist who lived in
New Zealand and worked at the University of
Otago.\",\"http:\\/\\/fise.iks-project.eu\\/ontology\\/start\":4,\"http:\\/\\/purl.org\\/dc\\/terms\\/created\":\"2010-10-27T14:00:00+02:00\",\"http:\\/\\/purl.org\\/dc\\/terms\\/creator\":\"<urn:iks-project:enhancer:test:dummyEngine>\",\"http:\\/\\/purl.org\\/dc\\/terms\\/type\":\"<http:\\/\\/dbpedia.org\\/ontology\\/Person>\"}";
+ String result = serializedGraph.toString();
+ Assert.assertEquals(expected, result);
+ }
+
+ @Test
public void testSingleSubjectSerializeNoNsWithIndent() {
String context = "Dr. Patrick Marshall (1869 - November 1950) was a
geologist who lived in New Zealand and worked at the University of Otago.";
@@ -99,6 +145,29 @@ public class JsonLdSerializerProviderTes
String result = serializedGraph.toString();
Assert.assertEquals(expected, result);
}
+
+ @Test
+ public void testSingleSubjectSerializeWithNsWithCoercion() {
+ String context = "Dr. Patrick Marshall (1869 - November 1950) was a
geologist who lived in New Zealand and worked at the University of Otago.";
+
+ ContentItem ci =
getContentItem("urn:iks-project:enhancer:test:content-item:person", context);
+ getTextAnnotation(ci, "Person", "Patrick Marshall", context,
OntologicalClasses.DBPEDIA_PERSON);
+
+ OutputStream serializedGraph = new ByteArrayOutputStream();
+ Map<String, String> nsMap = new HashMap<String, String>();
+ nsMap.put("http://fise.iks-project.eu/ontology/", "enhancer");
+ nsMap.put("http://www.w3.org/2001/XMLSchema#", "xmlns");
+ nsMap.put("http://dbpedia.org/ontology/", "dbpedia");
+ nsMap.put("http://purl.org/dc/terms", "dcterms");
+ jsonldProvider.setIndentation(0);
+ jsonldProvider.setNamespacePrefixMap(nsMap);
+ jsonldProvider.setUseTypeCoercion(true);
+ jsonldProvider.serialize(serializedGraph, ci.getMetadata(),
formatIdentifier);
+
+ String expected =
"{\"#\":{\"dbpedia\":\"http:\\/\\/dbpedia.org\\/ontology\\/\",\"dcterms\":\"http:\\/\\/purl.org\\/dc\\/terms\",\"enhancer\":\"http:\\/\\/fise.iks-project.eu\\/ontology\\/\",\"xmlns\":\"http:\\/\\/www.w3.org\\/2001\\/XMLSchema#\",\"#types\":{\"http:\\/\\/fise.iks-project.eu\\/ontology\\/end\":\"xmlns:int\",\"http:\\/\\/fise.iks-project.eu\\/ontology\\/selected-text\":\"xmlns:string\",\"http:\\/\\/fise.iks-project.eu\\/ontology\\/selection-context\":\"xmlns:string\",\"http:\\/\\/fise.iks-project.eu\\/ontology\\/start\":\"xmlns:int\",\"http:\\/\\/purl.org\\/dc\\/terms\\/created\":\"xmlns:dateTime\"}},\"@\":\"<urn:iks-project:enhancer:test:text-annotation:Person>\",\"a\":[\"<enhancer:Enhancement>\",\"<enhancer:TextAnnotation>\"],\"dcterms:\\/created\":\"2010-10-27T14:00:00+02:00\",\"dcterms:\\/creator\":\"<urn:iks-project:enhancer:test:dummyEngine>\",\"dcterms:\\/type\":\"<dbpedia:Person>\",\"enhancer:end\":20,\"enhancer:selected-text\":\"Patrick
Marsha
ll\",\"enhancer:selection-context\":\"Dr. Patrick Marshall (1869 - November
1950) was a geologist who lived in New Zealand and worked at the University of
Otago.\",\"enhancer:start\":4}";
+ String result = serializedGraph.toString();
+ Assert.assertEquals(expected, result);
+ }
@Test
public void testSingleSubjectSerializeWithNsWithIndent() {
@@ -121,6 +190,29 @@ public class JsonLdSerializerProviderTes
String result = serializedGraph.toString();
Assert.assertEquals(expected, result);
}
+
+ @Test
+ public void testSingleSubjectSerializeWithNsWithIndentWithCoercion() {
+ String context = "Dr. Patrick Marshall (1869 - November 1950) was a
geologist who lived in New Zealand and worked at the University of Otago.";
+
+ ContentItem ci =
getContentItem("urn:iks-project:enhancer:test:content-item:person", context);
+ getTextAnnotation(ci, "Person", "Patrick Marshall", context,
OntologicalClasses.DBPEDIA_PERSON);
+
+ OutputStream serializedGraph = new ByteArrayOutputStream();
+ Map<String, String> nsMap = new HashMap<String, String>();
+ nsMap.put("http://fise.iks-project.eu/ontology/", "enhancer");
+ nsMap.put("http://www.w3.org/2001/XMLSchema#", "xmlns");
+ nsMap.put("http://dbpedia.org/ontology/", "dbpedia");
+ nsMap.put("http://purl.org/dc/terms", "dcterms");
+ jsonldProvider.setIndentation(4);
+ jsonldProvider.setNamespacePrefixMap(nsMap);
+ jsonldProvider.setUseTypeCoercion(true);
+ jsonldProvider.serialize(serializedGraph, ci.getMetadata(),
formatIdentifier);
+
+ String expected = "{\n \"#\": {\n \"dbpedia\":
\"http:\\/\\/dbpedia.org\\/ontology\\/\",\n \"dcterms\":
\"http:\\/\\/purl.org\\/dc\\/terms\",\n \"enhancer\":
\"http:\\/\\/fise.iks-project.eu\\/ontology\\/\",\n \"xmlns\":
\"http:\\/\\/www.w3.org\\/2001\\/XMLSchema#\",\n \"#types\": {\n
\"http:\\/\\/fise.iks-project.eu\\/ontology\\/end\": \"xmlns:int\",\n
\"http:\\/\\/fise.iks-project.eu\\/ontology\\/selected-text\":
\"xmlns:string\",\n
\"http:\\/\\/fise.iks-project.eu\\/ontology\\/selection-context\":
\"xmlns:string\",\n
\"http:\\/\\/fise.iks-project.eu\\/ontology\\/start\": \"xmlns:int\",\n
\"http:\\/\\/purl.org\\/dc\\/terms\\/created\": \"xmlns:dateTime\"\n
}\n },\n \"@\":
\"<urn:iks-project:enhancer:test:text-annotation:Person>\",\n \"a\": [\n
\"<enhancer:Enhancement>\",\n \"<enhancer:TextAnnotation>\"\n ],\n
\"dcterms:\\/created\": \"201
0-10-27T14:00:00+02:00\",\n \"dcterms:\\/creator\":
\"<urn:iks-project:enhancer:test:dummyEngine>\",\n \"dcterms:\\/type\":
\"<dbpedia:Person>\",\n \"enhancer:end\": 20, \"enhancer:selected-text\":
\"Patrick Marshall\",\n \"enhancer:selection-context\": \"Dr. Patrick
Marshall (1869 - November 1950) was a geologist who lived in New Zealand and
worked at the University of Otago.\",\n \"enhancer:start\": \n}";
+ String result = serializedGraph.toString();
+ Assert.assertEquals(expected, result);
+ }
private ContentItem getContentItem(final String id, final String text) {
return new ContentItem() {
Modified:
incubator/stanbol/trunk/enhancer/generic/jsonld/src/main/java/org/apache/stanbol/jsonld/JsonComparator.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/generic/jsonld/src/main/java/org/apache/stanbol/jsonld/JsonComparator.java?rev=1099740&r1=1099739&r2=1099740&view=diff
==============================================================================
---
incubator/stanbol/trunk/enhancer/generic/jsonld/src/main/java/org/apache/stanbol/jsonld/JsonComparator.java
(original)
+++
incubator/stanbol/trunk/enhancer/generic/jsonld/src/main/java/org/apache/stanbol/jsonld/JsonComparator.java
Thu May 5 09:54:26 2011
@@ -35,6 +35,10 @@ public class JsonComparator implements C
value = -1;
} else if (arg1.equals("#vocab")) {
value = 1;
+ } else if (arg0.equals("#types")) {
+ value = 1;
+ } else if (arg1.equals("#types")) {
+ value = -1;
} else {
value =
String.valueOf(arg0).toLowerCase().compareTo(String.valueOf(arg1).toLowerCase());
}
Modified:
incubator/stanbol/trunk/enhancer/generic/jsonld/src/main/java/org/apache/stanbol/jsonld/JsonLd.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/generic/jsonld/src/main/java/org/apache/stanbol/jsonld/JsonLd.java?rev=1099740&r1=1099739&r2=1099740&view=diff
==============================================================================
---
incubator/stanbol/trunk/enhancer/generic/jsonld/src/main/java/org/apache/stanbol/jsonld/JsonLd.java
(original)
+++
incubator/stanbol/trunk/enhancer/generic/jsonld/src/main/java/org/apache/stanbol/jsonld/JsonLd.java
Thu May 5 09:54:26 2011
@@ -16,39 +16,34 @@ import java.util.TreeMap;
public class JsonLd {
// Map Namespace -> Prefix
- private Map<String, String> namespacePrefixMap = new HashMap<String,
String>();
+ private Map<String,String> namespacePrefixMap = new
HashMap<String,String>();
// Map Subject -> Resource
- private Map<String, JsonLdResource> resourceMap = new TreeMap<String,
JsonLdResource>(new JsonComparator());
+ private Map<String,JsonLdResource> resourceMap = new
TreeMap<String,JsonLdResource>(new JsonComparator());
/**
- * 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>.<br />
+ * 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>.<br />
* <br />
- * <b>Note:</b> 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.
+ * <b>Note:</b> 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.
*/
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>.
+ * 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>.
*/
private boolean useJointGraphs = true;
-
+
/**
- * Flag to control whether type coercion should be applied. Default
- * value is <code>true</code>.
+ * Flag to control whether type coercion should be applied. Default value
is <code>true</code>.
*/
- private boolean useTypeCoercion = true;
+ private boolean useTypeCoercion = false;
/**
- * Add the given resource to this JsonLd object using the resourceId
- * as key.
- *
+ * Add the given resource to this JsonLd object using the resourceId as
key.
+ *
* @param resourceId
* @param resource
*/
@@ -59,11 +54,10 @@ public class JsonLd {
@Override
public String toString() {
if (useJointGraphs) {
- Map<String, Object> json = createJointGraph();
+ Map<String,Object> json = createJointGraph();
return JsonSerializer.toString(json);
- }
- else {
+ } else {
List<Object> json = createDisjointGraph();
return JsonSerializer.toString(json);
@@ -72,11 +66,10 @@ public class JsonLd {
public String toString(int indent) {
if (useJointGraphs) {
- Map<String, Object> json = createJointGraph();
+ Map<String,Object> json = createJointGraph();
return JsonSerializer.toString(json, indent);
- }
- else {
+ } else {
List<Object> json = createDisjointGraph();
return JsonSerializer.toString(json, indent);
@@ -88,19 +81,21 @@ public class JsonLd {
if (!resourceMap.isEmpty()) {
for (String subject : resourceMap.keySet()) {
- Map<String, Object> subjectObject = new TreeMap<String,
Object>(new JsonComparator());
+ Map<String,Object> subjectObject = new
TreeMap<String,Object>(new JsonComparator());
+ JsonLdResource resource = resourceMap.get(subject);
// put the namespaces
- if (!namespacePrefixMap.isEmpty()) {
- Map<String, Object> nsObject = new TreeMap<String,
Object>(new JsonComparator());
- for (String ns : namespacePrefixMap.keySet()) {
- nsObject.put(namespacePrefixMap.get(ns), ns);
+ if ((this.applyNamespaces &&
!this.namespacePrefixMap.isEmpty()) || this.useTypeCoercion) {
+ Map<String,Object> nsObject = new
TreeMap<String,Object>(new JsonComparator());
+ for (String ns : this.namespacePrefixMap.keySet()) {
+ nsObject.put(this.namespacePrefixMap.get(ns), ns);
+ }
+ if (this.useTypeCoercion) {
+ putCoercionTypes(nsObject, resource.getCoercionMap());
}
subjectObject.put("#", nsObject);
}
- JsonLdResource resource = resourceMap.get(subject);
-
// put subject
if (resource.getSubject() != null) {
subjectObject.put("@", resource.getSubject());
@@ -122,14 +117,16 @@ public class JsonLd {
}
@SuppressWarnings("unchecked")
- private Map<String, Object> createJointGraph() {
- Map<String, Object> json = new TreeMap<String, Object>(new
JsonComparator());
+ private Map<String,Object> createJointGraph() {
+ Map<String,Object> json = new TreeMap<String,Object>(new
JsonComparator());
+ Map<String,String> coercionMap = new TreeMap<String,String>(new
JsonComparator());
+
if (!resourceMap.isEmpty()) {
List<Object> subjects = new ArrayList<Object>();
for (String subject : resourceMap.keySet()) {
// put subject
- Map<String, Object> subjectObject = new TreeMap<String,
Object>(new JsonComparator());
+ Map<String,Object> subjectObject = new
TreeMap<String,Object>(new JsonComparator());
JsonLdResource resource = resourceMap.get(subject);
@@ -141,6 +138,10 @@ public class JsonLd {
// put types
putTypes(subjectObject, resource);
+ if (this.useTypeCoercion) {
+ coercionMap.putAll(resource.getCoercionMap());
+ }
+
// put properties = objects
putProperties(subjectObject, resource);
@@ -151,7 +152,7 @@ public class JsonLd {
// put subjects
if (!subjects.isEmpty()) {
if (subjects.size() == 1) {
- json = (Map<String, Object>) subjects.get(0);
+ json = (Map<String,Object>) subjects.get(0);
} else {
json.put("@", subjects);
}
@@ -159,18 +160,24 @@ public class JsonLd {
}
// put the namespaces
- if (!namespacePrefixMap.isEmpty()) {
- Map<String, Object> nsObject = new TreeMap<String, Object>(new
JsonComparator());
+ if ((this.applyNamespaces && !this.namespacePrefixMap.isEmpty())
+ || (this.useTypeCoercion && !coercionMap.isEmpty())) {
+
+ 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()) {
+ putCoercionTypes(nsObject, coercionMap);
+ }
json.put("#", nsObject);
}
return json;
}
- private void putTypes(Map<String, Object> subjectObject, JsonLdResource
resource) {
+ private void putTypes(Map<String,Object> subjectObject, JsonLdResource
resource) {
if (!resource.getTypes().isEmpty()) {
List<String> types = new ArrayList<String>();
for (String type : resource.getTypes()) {
@@ -178,8 +185,7 @@ public class JsonLd {
}
if (types.size() == 1) {
subjectObject.put("a", types.get(0));
- }
- else {
+ } else {
Collections.sort(types, new Comparator<String>() {
@Override
@@ -192,37 +198,69 @@ public class JsonLd {
}
}
}
+
+ private void putCoercionTypes(Map<String,Object> jsonObject,
Map<String,String> coercionMap) {
+ if (!coercionMap.isEmpty()) {
+ if (this.applyNamespaces) {
+ Map<String,String> nsCoercionMap = new
TreeMap<String,String>(new JsonComparator());
+ for (String property : coercionMap.keySet()) {
+ nsCoercionMap.put(property,
applyNamespace(coercionMap.get(property)));
+ }
+ jsonObject.put("#types", nsCoercionMap);
+ }
+ else {
+ jsonObject.put("#types", coercionMap);
+ }
+ }
+ }
- private void putProperties(Map<String, Object> jsonObject, JsonLdResource
resource) {
+ private void putProperties(Map<String,Object> jsonObject, JsonLdResource
resource) {
for (String property : resource.getPropertyMap().keySet()) {
Object value = resource.getPropertyMap().get(property);
if (value instanceof String) {
- value = applyNamespace((String) value);
+ String strValue = (String) value;
+ if (!this.useTypeCoercion) {
+ String type = resource.getCoercionTypeOf(property);
+ if (type != null) {
+ strValue = formatWithType(strValue, type);
+ }
+ }
+ value = applyNamespace(strValue);
jsonObject.put(applyNamespace(property), value);
- }
- else if (value instanceof String[]) {
+ } else if (value instanceof String[]) {
String[] stringArray = (String[]) value;
List<String> valueList = new ArrayList<String>();
for (String uri : stringArray) {
- valueList.add(applyNamespace(uri));
+ valueList.add(uri);
}
List<Object> jsonArray = new ArrayList<Object>(valueList);
jsonObject.put(applyNamespace(property), jsonArray);
- }
- else if (value instanceof Object[]) {
+ } else if (value instanceof Object[]) {
Object[] objectArray = (Object[]) value;
List<Object> jsonArray = new ArrayList<Object>();
for (Object object : objectArray) {
jsonArray.add(object);
}
jsonObject.put(applyNamespace(property), jsonArray);
- }
- else {
- jsonObject.put(applyNamespace(property), value);
+ } else {
+ if (!this.useTypeCoercion) {
+ String type = resource.getCoercionTypeOf(property);
+ if (type != null) {
+ String strValue = formatWithType(value.toString(),
type);
+ jsonObject.put(applyNamespace(property),
applyNamespace(strValue));
+ }
+ } else {
+ jsonObject.put(applyNamespace(property), value);
+ }
}
}
}
+ private String formatWithType(String strValue, String type) {
+ strValue = "\"" + strValue + "\"^^<" + type + ">";
+ return strValue;
+ }
+
private String applyNamespace(String uri) {
if (applyNamespaces) {
for (String namespace : namespacePrefixMap.keySet()) {
@@ -242,30 +280,30 @@ public class JsonLd {
/**
* Get the known namespace to prefix mapping.
- *
+ *
* @return A {@link Map} from namespace String to prefix String.
*/
- public Map<String, String> getNamespacePrefixMap() {
+ 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.
+ * A {@link Map} from namespace String to prefix String.
*/
- public void setNamespacePrefixMap(Map<String, String> namespacePrefixMap) {
+ 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.
+ * 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);
@@ -273,7 +311,7 @@ public class JsonLd {
/**
* Determine whether currently joint or disjoint graphs are serialized
with this JSON-LD instance.
- *
+ *
* @return <code>True</code> if joint graphs are used,
<code>False</code>otherwise.
*/
public boolean isUseJointGraphs() {
@@ -282,7 +320,7 @@ public class JsonLd {
/**
* Set to <code>true</code> if you want to use joint graphs (default) or
<code>false</code> otherwise.
- *
+ *
* @param useJointGraphs
*/
public void setUseJointGraphs(boolean useJointGraphs) {
@@ -290,14 +328,12 @@ 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>.
+ * 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.
- *
+ * 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() {
@@ -305,12 +341,10 @@ public class JsonLd {
}
/**
- * Control whether namespaces from the namespace prefix map are
- * applied to URLs during serialization.
+ * 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.
- *
+ * Set this to <code>false</code> if you already have shortened IRIs with
prefixes.
+ *
* @param applyNamespaces
*/
public void setApplyNamespaces(boolean applyNamespaces) {
@@ -327,8 +361,8 @@ public class JsonLd {
}
/**
- * Control whether type coercion should be applied. Set this to
<code>false</code>
- * if you don't want to use type coercion in the output.
+ * Control whether type coercion should be applied. Set this to
<code>false</code> if you don't want to
+ * use type coercion in the output.
*
* @param useTypeCoercion
*/
Modified:
incubator/stanbol/trunk/enhancer/generic/jsonld/src/main/java/org/apache/stanbol/jsonld/JsonLdResource.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/generic/jsonld/src/main/java/org/apache/stanbol/jsonld/JsonLdResource.java?rev=1099740&r1=1099739&r2=1099740&view=diff
==============================================================================
---
incubator/stanbol/trunk/enhancer/generic/jsonld/src/main/java/org/apache/stanbol/jsonld/JsonLdResource.java
(original)
+++
incubator/stanbol/trunk/enhancer/generic/jsonld/src/main/java/org/apache/stanbol/jsonld/JsonLdResource.java
Thu May 5 09:54:26 2011
@@ -13,6 +13,7 @@ public class JsonLdResource {
private String subject;
private List<String> types = new ArrayList<String>();
+ private Map<String, String> coercionMap = new HashMap<String, String>();
private Map<String, Object> propertyMap = new HashMap<String, Object>();
public String getSubject() {
@@ -30,6 +31,18 @@ public class JsonLdResource {
public void addAllTypes(List<String> types) {
this.types.addAll(types);
}
+
+ public void putCoercionType(String property, String type) {
+ this.coercionMap.put(property, type);
+ }
+
+ public String getCoercionTypeOf(String property) {
+ return this.coercionMap.get(property);
+ }
+
+ public Map<String, String> getCoercionMap() {
+ return this.coercionMap;
+ }
public List<String> getTypes() {
return types;
Modified:
incubator/stanbol/trunk/enhancer/generic/jsonld/src/main/java/org/apache/stanbol/jsonld/JsonSerializer.java
URL:
http://svn.apache.org/viewvc/incubator/stanbol/trunk/enhancer/generic/jsonld/src/main/java/org/apache/stanbol/jsonld/JsonSerializer.java?rev=1099740&r1=1099739&r2=1099740&view=diff
==============================================================================
---
incubator/stanbol/trunk/enhancer/generic/jsonld/src/main/java/org/apache/stanbol/jsonld/JsonSerializer.java
(original)
+++
incubator/stanbol/trunk/enhancer/generic/jsonld/src/main/java/org/apache/stanbol/jsonld/JsonSerializer.java
Thu May 5 09:54:26 2011
@@ -45,7 +45,7 @@ public class JsonSerializer {
return sb.toString();
}
-
+
private static void appendJsonMap(Map<String, Object> jsonMap,
StringBuffer sb, int indent, int level) {
sb.append('{');
level = increaseIndentationLevel(sb, indent, level);
@@ -176,10 +176,20 @@ public class JsonSerializer {
}
}
+ /**
+ * During the serialization there are added ',' and line breaks '\n' by
+ * default that need to be deleted when not needed, e.g. at the end
+ * of a list.
+ *
+ * @param sb
+ * @param indent
+ */
private static void removeOddChars(StringBuffer sb, int indent) {
- sb.deleteCharAt(sb.length()-1);
- if (indent > 0) {
+ if (sb.length() > 2) {
sb.deleteCharAt(sb.length()-1);
+ if (indent > 0) {
+ sb.deleteCharAt(sb.length()-1);
+ }
}
}
}