jena-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a...@apache.org
Subject [3/4] jena git commit: JENA-816 : Adapt for RDF 1.0/RDF 1.1.
Date Mon, 01 Dec 2014 21:37:41 GMT
JENA-816 : Adapt for RDF 1.0/RDF 1.1.

Project: http://git-wip-us.apache.org/repos/asf/jena/repo
Commit: http://git-wip-us.apache.org/repos/asf/jena/commit/fdc666e0
Tree: http://git-wip-us.apache.org/repos/asf/jena/tree/fdc666e0
Diff: http://git-wip-us.apache.org/repos/asf/jena/diff/fdc666e0

Branch: refs/heads/master
Commit: fdc666e031095786f47ae29264bac8e860cf54a6
Parents: 0189468
Author: Andy Seaborne <andy@apache.org>
Authored: Mon Dec 1 20:51:39 2014 +0000
Committer: Andy Seaborne <andy@apache.org>
Committed: Mon Dec 1 20:51:39 2014 +0000

----------------------------------------------------------------------
 .../apache/jena/riot/writer/RDFJSONWriter.java  | 114 ++++++++++++++-----
 1 file changed, 87 insertions(+), 27 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/jena/blob/fdc666e0/jena-arq/src/main/java/org/apache/jena/riot/writer/RDFJSONWriter.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/riot/writer/RDFJSONWriter.java b/jena-arq/src/main/java/org/apache/jena/riot/writer/RDFJSONWriter.java
index dc62df0..400fcbe 100644
--- a/jena-arq/src/main/java/org/apache/jena/riot/writer/RDFJSONWriter.java
+++ b/jena-arq/src/main/java/org/apache/jena/riot/writer/RDFJSONWriter.java
@@ -25,18 +25,19 @@ import java.util.HashSet ;
 import java.util.Map ;
 import java.util.Set ;
 
+import org.apache.jena.atlas.io.IndentedWriter ;
+import org.apache.jena.atlas.json.io.JSWriter ;
 import org.apache.jena.atlas.lib.Pair ;
-import org.apache.jena.atlas.lib.Sink ;
 import org.apache.jena.riot.Lang ;
+import org.apache.jena.riot.RiotException ;
 import org.apache.jena.riot.system.PrefixMap ;
-import org.apache.jena.riot.system.Prologue ;
-import org.apache.jena.riot.system.SyntaxLabels ;
 
 import com.hp.hpl.jena.graph.Graph ;
 import com.hp.hpl.jena.graph.GraphUtil ;
 import com.hp.hpl.jena.graph.Node ;
 import com.hp.hpl.jena.graph.Triple ;
 import com.hp.hpl.jena.sparql.util.Context ;
+import com.hp.hpl.jena.sparql.util.NodeUtils ;
 import com.hp.hpl.jena.util.iterator.ExtendedIterator ;
 
 public class RDFJSONWriter extends WriterGraphRIOTBase
@@ -44,18 +45,35 @@ public class RDFJSONWriter extends WriterGraphRIOTBase
     public RDFJSONWriter() {}
     
 	public static void output(OutputStream out, Graph graph) {
-        Prologue prologue = Prologue.create(null, null) ; // (null, graph.getPrefixMapping())
;
-		Sink<Pair<Node, Map<Node, Set<Node>>>> sink = new SinkEntityOutput(out,
prologue, SyntaxLabels.createNodeToLabel()) ;
-		output( sink, graph ) ;
+		output(new JSWriter(out), graph ) ;
 	}
 	
 	public static void output(Writer out, Graph graph) {
-        Prologue prologue = Prologue.create(null, null) ; // (null, graph.getPrefixMapping())
;
-		Sink<Pair<Node, Map<Node, Set<Node>>>> sink = new SinkEntityOutput(out,
prologue, SyntaxLabels.createNodeToLabel()) ;
-		output( sink, graph ) ;
+		output(new JSWriter(new IndentedWriterEx(out)), graph ) ;
 	}
 
-	private static void output(Sink<Pair<Node, Map<Node, Set<Node>>>>
sink, Graph graph) {
+	@Override
+    public Lang getLang()
+    {
+        return Lang.RDFJSON ;
+    }
+
+    @Override
+    public void write(Writer out, Graph graph, PrefixMap prefixMap, String baseURI, Context
context)
+    {
+        output(out, graph) ;
+    }
+
+    @Override
+    public void write(OutputStream out, Graph graph, PrefixMap prefixMap, String baseURI,
Context context)
+    {
+        output(out, graph) ;
+    }
+
+    private static void output(JSWriter out, Graph graph) {
+	    out.startOutput() ;
+	    out.startObject();
+	    
 		ExtendedIterator<Node> subjects = GraphUtil.listSubjects(graph, Node.ANY, Node.ANY)
;
 		try {
 			Map<Node, Set<Node>> predicates = new HashMap<>() ;
@@ -77,31 +95,73 @@ public class RDFJSONWriter extends WriterGraphRIOTBase
 				} finally {
 					if ( triples != null ) triples.close() ;
 				}
-				sink.send(new Pair<>(subject, predicates)) ;
+				send(out, new Pair<>(subject, predicates)) ;
 				predicates.clear() ;
 			}			
 		} finally {
 			if ( subjects != null ) subjects.close() ;
-			sink.close() ;
+			out.finishObject();
+			out.finishOutput() ;
 		}
 	}
 
-    @Override
-    public Lang getLang()
-    {
-        return Lang.RDFJSON ;
-    }
-
-    @Override
-    public void write(Writer out, Graph graph, PrefixMap prefixMap, String baseURI, Context
context)
-    {
-        output(out, graph) ;
-    }
+    private static void send(JSWriter out, Pair<Node, Map<Node, Set<Node>>>
item) {
+        Node s = item.getLeft() ;
+        if ( s.isBlank() ) {
+            out.key("_:" + s.getBlankNodeLabel()) ;
+        } else if ( s.isURI() ) {
+            out.key(s.getURI()) ;
+        } else {
+            throw new RiotException ("Only URIs or blank nodes are legal subjects.") ;
+        }
 
-    @Override
-    public void write(OutputStream out, Graph graph, PrefixMap prefixMap, String baseURI,
Context context)
-    {
-        output(out, graph) ;
+        out.startObject() ;
+        // out.pair(key, value) ;
+        Map<Node, Set<Node>> predicates = item.getRight() ;
+        for (Node p : predicates.keySet() ) {
+            out.key(p.getURI()) ;
+            out.startArray() ;
+            Set<Node> objects = predicates.get(p) ;
+            int i = 0;
+            for ( Node o : objects ) {
+                out.startObject() ;
+                if ( o.isBlank() ) {
+                    out.pair("type", "bnode") ;
+                    out.pair("value", "_:" + o.getBlankNodeLabel()) ;
+                } else if ( o.isURI() ) {
+                    out.pair("type", "uri") ;
+                    out.pair("value", o.getURI()) ;                 
+                } else if ( o.isLiteral() ) {
+                    String lex = o.getLiteralLexicalForm() ;
+                    out.pair("type", "literal") ;
+                    out.pair("value", lex) ;
+                    
+                    if ( NodeUtils.isSimpleString(o) ) {
+                        // No-op.
+                    } else if ( NodeUtils.isLangString(o) ) {
+                        String lang = o.getLiteralLanguage() ;    
+                        out.pair("lang", lang) ;
+                    } else {
+                        // Datatype, nothing special.
+                        String dt = o.getLiteralDatatypeURI() ;
+                        out.pair("datatype", dt) ;
+                    }
+                }
+                out.finishObject() ;
+                if (i < objects.size() - 1)
+                {
+                    out.arraySep();
+                }
+                i++;
+            }
+            out.finishArray() ;
+        }
+        out.finishObject() ;
     }
 	
+    private static class IndentedWriterEx extends IndentedWriter {
+        public IndentedWriterEx(Writer writer) {
+            super(writer);
+        }
+    }
 }


Mime
View raw message