commonsrdf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From st...@apache.org
Subject [05/33] incubator-commonsrdf git commit: ToGraph
Date Mon, 28 Mar 2016 04:55:01 GMT
ToGraph


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

Branch: refs/heads/jena
Commit: d6c2356f029eb6670da528fced1cff997915105e
Parents: 5f8a497
Author: Andy Seaborne <andy@seaborne.org>
Authored: Wed Aug 26 16:14:36 2015 +0100
Committer: Andy Seaborne <andy@seaborne.org>
Committed: Wed Aug 26 16:14:36 2015 +0100

----------------------------------------------------------------------
 .../java/org/apache/jena/commons/Example1.java  | 19 ++++++--
 .../java/org/apache/jena/commons/ToGraph.java   | 46 ++++++++++++++++++--
 2 files changed, 59 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/d6c2356f/src/main/java/org/apache/jena/commons/Example1.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/jena/commons/Example1.java b/src/main/java/org/apache/jena/commons/Example1.java
index 36ccbb9..8c66fc3 100644
--- a/src/main/java/org/apache/jena/commons/Example1.java
+++ b/src/main/java/org/apache/jena/commons/Example1.java
@@ -20,18 +20,31 @@ package org.apache.jena.commons;
 
 import org.apache.commons.rdf.api.Graph ;
 import org.apache.commons.rdf.api.RDFTermFactory ;
+import org.apache.jena.atlas.logging.LogCtl ;
 import org.apache.jena.riot.RDFDataMgr ;
 
 public class Example1 {
+    static { LogCtl.setCmdLogging();}
+    
+    public static void main(String... args) {
+        main_ex2() ;
+    }
+    
+    public static void main_ex1(String... args) {
+        RDFTermFactory rft = new RDFTermFactoryJena() ;
+        ToGraph dest = new ToGraph(rft) ;
+        RDFDataMgr.parse(dest, "D.ttl") ;
+        dest.getGraph().getTriples().forEach(System.out::println) ;
+    }
 
-    public static void main(String[] args) {
+    public static void main_ex2(String... args) {
         RDFTermFactory rft = new RDFTermFactoryJena() ;
         Graph graph = rft.createGraph() ;
-        ToGraph dest = new ToGraph(graph) ;
+        ToGraph dest = new ToGraph(graph, rft) ;
         RDFDataMgr.parse(dest, "D.ttl") ;
-        
         graph.getTriples().forEach(System.out::println) ;
     }
 
+    
 }
 

http://git-wip-us.apache.org/repos/asf/incubator-commonsrdf/blob/d6c2356f/src/main/java/org/apache/jena/commons/ToGraph.java
----------------------------------------------------------------------
diff --git a/src/main/java/org/apache/jena/commons/ToGraph.java b/src/main/java/org/apache/jena/commons/ToGraph.java
index 618c5e0..604b4f2 100644
--- a/src/main/java/org/apache/jena/commons/ToGraph.java
+++ b/src/main/java/org/apache/jena/commons/ToGraph.java
@@ -18,14 +18,22 @@
 
 package org.apache.jena.commons;
 
-import org.apache.commons.rdf.api.Graph ;
+import org.apache.commons.rdf.api.* ;
+import org.apache.jena.datatypes.xsd.XSDDatatype ;
+import org.apache.jena.graph.Node ;
 import org.apache.jena.riot.system.StreamRDF ;
 
 public class ToGraph implements StreamRDF {
 
     private Graph graph;
+    private RDFTermFactory factory;
 
-    public ToGraph(Graph graph) {
+    public ToGraph(RDFTermFactory factory) {
+        this(factory.createGraph(), factory) ;
+    }
+
+    public ToGraph(Graph graph, RDFTermFactory factory) {
+        this.factory = factory ; 
         this.graph = graph ;
     }
 
@@ -34,9 +42,35 @@ public class ToGraph implements StreamRDF {
 
     @Override
     public void triple(org.apache.jena.graph.Triple triple) {
-        graph.add(JCR_Factory.fromJena(triple));
+        graph.add((BlankNodeOrIRI)fromJena(triple.getSubject()), 
+                  (IRI)fromJena(triple.getPredicate()),
+                  fromJena(triple.getObject())) ;
     }
 
+    // jena to commonsrdf
+    public RDFTerm fromJena(Node node) {
+        if ( node.isURI() )
+            return factory.createIRI(node.getURI()) ;
+        if ( node.isLiteral() ) {
+            String lang = node.getLiteralLanguage() ;
+            if ( lang != null && lang.isEmpty() )
+                return factory.createLiteral(node.getLiteralLexicalForm(), lang) ;
+            if ( node.getLiteralDatatype().equals(XSDDatatype.XSDstring) )
+                return factory.createLiteral(node.getLiteralLexicalForm()) ;
+            IRI dt = factory.createIRI(node.getLiteralDatatype().getURI()) ;
+            return factory.createLiteral(node.getLiteralLexicalForm(), dt);
+        }
+        if ( node.isBlank() )
+            return factory.createBlankNode(node.getBlankNodeLabel()) ;
+        error("Node is not a concrete RDF Term: "+node) ;
+        return null ;
+    }
+
+    private static void error(String msg) {
+        System.err.println("Error: "+msg) ;
+        throw new RuntimeException(msg) ;
+    }
+    
     @Override
     public void quad(org.apache.jena.sparql.core.Quad quad) {
         throw new UnsupportedOperationException() ;
@@ -50,6 +84,12 @@ public class ToGraph implements StreamRDF {
 
     @Override
     public void finish() {}
+
+
+
+    public Graph getGraph() {
+        return graph;
+    }
     
 }
 


Mime
View raw message