commonsrdf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From st...@apache.org
Subject [01/50] incubator-commonsrdf git commit: Added JsonLdLiteral
Date Tue, 04 Oct 2016 01:30:22 GMT
Repository: incubator-commonsrdf
Updated Branches:
  refs/heads/rdf4j bbdba83b8 -> 49feb7c02


Added JsonLdLiteral


Project: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/commit/286813cc
Tree: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/tree/286813cc
Diff: http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/diff/286813cc

Branch: refs/heads/rdf4j
Commit: 286813cc9ff98c7c926f4e3740124e503be89c15
Parents: fe7ed27
Author: Stian Soiland-Reyes <stain@apache.org>
Authored: Mon Apr 4 16:58:18 2016 +0100
Committer: Stian Soiland-Reyes <stain@apache.org>
Committed: Mon Apr 4 16:58:18 2016 +0100

----------------------------------------------------------------------
 .../commons/rdf/jsonldjava/JsonLdBlankNode.java |  8 +-
 .../commons/rdf/jsonldjava/JsonLdIRI.java       | 13 +++-
 .../commons/rdf/jsonldjava/JsonLdLiteral.java   | 80 ++++++++++++++++++++
 .../rdf/jsonldjava/JsonLdRDFTermFactory.java    | 38 +++++++---
 .../commons/rdf/jsonldjava/JsonLdTerm.java      | 14 ++++
 5 files changed, 137 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/286813cc/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdBlankNode.java
----------------------------------------------------------------------
diff --git a/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdBlankNode.java
b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdBlankNode.java
index 383bb94..f93d3b4 100644
--- a/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdBlankNode.java
+++ b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdBlankNode.java
@@ -21,13 +21,15 @@ import org.apache.commons.rdf.api.BlankNode;
 
 import com.github.jsonldjava.core.RDFDataset.Node;
 
-final class JsonLdBlankNode implements BlankNode {
-	private final Node node;
+final class JsonLdBlankNode extends JsonLdTerm implements BlankNode {
 	private String blankNodePrefix;
 
 	JsonLdBlankNode(Node node, String blankNodePrefix) {
+		super(node);
 		this.blankNodePrefix = blankNodePrefix;
-		this.node = node;
+		if (! node.isBlankNode()) {
+			throw new IllegalArgumentException("Node is not a BlankNode:" + node);
+		}
 	}
 
 	@Override

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/286813cc/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdIRI.java
----------------------------------------------------------------------
diff --git a/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdIRI.java b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdIRI.java
index 3e14ad5..e21f55c 100644
--- a/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdIRI.java
+++ b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdIRI.java
@@ -19,13 +19,20 @@ package org.apache.commons.rdf.jsonldjava;
 
 import org.apache.commons.rdf.api.IRI;
 
+import com.github.jsonldjava.core.RDFDataset;
 import com.github.jsonldjava.core.RDFDataset.Node;
 
-final class JsonLdIRI implements IRI {
-	private final Node node;
+final class JsonLdIRI extends JsonLdTerm implements IRI {
 
 	JsonLdIRI(Node node) {
-		this.node = node;
+		super(node);
+		if (! node.isIRI()) {
+			throw new IllegalArgumentException("Node is not an IRI:" + node);
+		}
+	}
+
+	public JsonLdIRI(String iri) {
+		super(new RDFDataset.IRI(iri));
 	}
 
 	@Override

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/286813cc/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdLiteral.java
----------------------------------------------------------------------
diff --git a/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdLiteral.java
b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdLiteral.java
new file mode 100644
index 0000000..87e688a
--- /dev/null
+++ b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdLiteral.java
@@ -0,0 +1,80 @@
+package org.apache.commons.rdf.jsonldjava;
+
+import java.util.Objects;
+import java.util.Optional;
+
+import org.apache.commons.rdf.api.IRI;
+import org.apache.commons.rdf.api.Literal;
+import org.apache.commons.rdf.simple.Types;
+
+import com.github.jsonldjava.core.RDFDataset.Node;
+
+public class JsonLdLiteral extends JsonLdTerm implements Literal {
+
+	JsonLdLiteral(Node node) {
+		super(node);
+		if (! node.isLiteral()) {
+			throw new IllegalArgumentException("Node is not a Literal:" + node);
+		}
+	}
+
+	@Override
+	public String ntriplesString() {
+        StringBuilder sb = new StringBuilder();
+        sb.append('"');
+        // Escape special characters
+        sb.append(getLexicalForm().replace("\\", "\\\\"). // escaped to \\
+                replace("\"", "\\\""). // escaped to \"
+                replace("\r", "\\r"). // escaped to \r
+                replace("\n", "\\n")); // escaped to \n
+        sb.append('"');
+
+        if (getLanguageTag().isPresent()) {
+            sb.append("@");
+            sb.append(getLanguageTag().get());
+        } else if (!getDatatype().equals(Types.XSD_STRING)) {
+            sb.append("^^");
+            sb.append(getDatatype().ntriplesString());
+        }
+        return sb.toString();
+    }
+
+	@Override
+	public String getLexicalForm() {
+		return node.getValue();
+	}
+
+	@Override
+	public IRI getDatatype() {
+		return new JsonLdIRI(node.getDatatype());
+	}
+
+	@Override
+	public Optional<String> getLanguageTag() {
+		return Optional.ofNullable(node.getLanguage());
+	}
+
+	@Override
+	public int hashCode() {
+		// Should be the same as 
+		// Objects.hash(getLexicalForm(), getDatatype(), getLanguageTag());
+		return Objects.hash(node.getValue(), node.getDatatype(), node.getLanguage());
+	}
+	
+	@Override
+	public boolean equals(Object obj) {
+		if (obj instanceof JsonLdLiteral) {
+			JsonLdLiteral other = (JsonLdLiteral) obj;
+			return asNode().compareTo(other.asNode()) == 0;
+		}
+		if (obj instanceof Literal) {
+			Literal other = (Literal) obj;
+			return getLexicalForm().equals(other.getLexicalForm()) &&
+					getDatatype().equals(other.getDatatype()) &&
+					getLanguageTag().equals(other.getLanguageTag());
+		}
+		return false;
+		
+	}
+	
+}

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/286813cc/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdRDFTermFactory.java
----------------------------------------------------------------------
diff --git a/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdRDFTermFactory.java
b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdRDFTermFactory.java
index 99927ad..1d9f6d7 100644
--- a/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdRDFTermFactory.java
+++ b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdRDFTermFactory.java
@@ -25,13 +25,13 @@ import org.apache.commons.rdf.api.Graph;
 import org.apache.commons.rdf.api.IRI;
 import org.apache.commons.rdf.api.Literal;
 import org.apache.commons.rdf.api.RDFTerm;
+import org.apache.commons.rdf.api.RDFTermFactory;
 import org.apache.commons.rdf.api.Triple;
-import org.apache.commons.rdf.simple.SimpleRDFTermFactory;
 
 import com.github.jsonldjava.core.RDFDataset;
 import com.github.jsonldjava.core.RDFDataset.Node;
 
-final class JsonLdRDFTermFactory extends SimpleRDFTermFactory {
+final class JsonLdRDFTermFactory implements RDFTermFactory {
 	
 	public JsonLdRDFTermFactory() {
 		// An "outside Graph" bnodePrefix
@@ -51,7 +51,7 @@ final class JsonLdRDFTermFactory extends SimpleRDFTermFactory {
 	
 	@Override
 	public IRI createIRI(String iri) {
-		return new JsonLdIRI(new RDFDataset.IRI(iri));
+		return new JsonLdIRI(iri);
 	}
 	
 	@Override
@@ -69,11 +69,27 @@ final class JsonLdRDFTermFactory extends SimpleRDFTermFactory {
 	
 	@Override
 	public Triple createTriple(BlankNodeOrIRI subject, IRI predicate, RDFTerm object) {
-		return super.createTriple(subject, predicate, object);
+		return new JsonLdTriple(asJsonLdQuad(subject, predicate, object), bnodePrefix);
+	}
+	
+	@Override
+	public Literal createLiteral(String literal) {		
+		return new JsonLdLiteral(new RDFDataset.Literal(literal, null, null));
+	}
+	@Override
+	public Literal createLiteral(String literal, IRI dataType) {
+		return new JsonLdLiteral(new RDFDataset.Literal(literal, dataType.getIRIString(), null));
}
+	@Override
+	public Literal createLiteral(String literal, String language) {
+		return new JsonLdLiteral(new RDFDataset.Literal(literal, null, language));		
 	}
 
 
 	private Node asJsonLdNode(RDFTerm term) {
+		if (term instanceof JsonLdTerm) {
+			// Return original Node
+			return ((JsonLdTerm)term).asNode();
+		}
 		if (term instanceof IRI) {
 			return new RDFDataset.IRI( ((IRI)term).getIRIString() );
 		}
@@ -96,19 +112,21 @@ final class JsonLdRDFTermFactory extends SimpleRDFTermFactory {
 	}
 	
 
-	public RDFDataset.Quad asJsonLdQuad(Triple triple) {
-		Node subject = asJsonLdNode(triple.getSubject());
-		Node predicate = asJsonLdNode(triple.getPredicate());
-		Node object = asJsonLdNode(triple.getObject());
+	public RDFDataset.Quad asJsonLdQuad(BlankNodeOrIRI subject, IRI predicate, RDFTerm object)
{
 		String graph = null;
 		// TODO: Add Quad to commons-rdf
 //		if (triple instanceof Quad) {
 //			String graph = triple.getGraph().getIRIString();
 //		}
-		return new RDFDataset.Quad(subject, predicate, object, graph);
-		
+		return new RDFDataset.Quad(asJsonLdNode(subject), asJsonLdNode(predicate), asJsonLdNode(object),
graph);
 	}
+
+
 	
+	public RDFDataset.Quad asJsonLdQuad(Triple triple) {
+		return asJsonLdQuad(triple.getSubject(), triple.getPredicate(), triple.getObject());
+	}
+
 	RDFTerm asTerm(final Node node, String blankNodePrefix) {		
 		if (node.isIRI()) {
 			return new JsonLdIRI(node);

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/286813cc/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdTerm.java
----------------------------------------------------------------------
diff --git a/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdTerm.java b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdTerm.java
new file mode 100644
index 0000000..f1c4718
--- /dev/null
+++ b/jsonld-java/src/main/java/org/apache/commons/rdf/jsonldjava/JsonLdTerm.java
@@ -0,0 +1,14 @@
+package org.apache.commons.rdf.jsonldjava;
+
+import com.github.jsonldjava.core.RDFDataset.Node;
+
+public abstract class JsonLdTerm {
+	final Node node;
+	JsonLdTerm(Node node) {
+		this.node = node;
+	}
+	public Node asNode() {
+		return node;
+	}
+	
+}


Mime
View raw message