You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@jena.apache.org by an...@apache.org on 2013/02/09 21:14:25 UTC

svn commit: r1444420 - /jena/Experimental/riot-output/src/main/java/riot/system/RiotLib.java

Author: andy
Date: Sat Feb  9 20:14:25 2013
New Revision: 1444420

URL: http://svn.apache.org/r1444420
Log:
Wiring up the API.

Added:
    jena/Experimental/riot-output/src/main/java/riot/system/RiotLib.java

Added: jena/Experimental/riot-output/src/main/java/riot/system/RiotLib.java
URL: http://svn.apache.org/viewvc/jena/Experimental/riot-output/src/main/java/riot/system/RiotLib.java?rev=1444420&view=auto
==============================================================================
--- jena/Experimental/riot-output/src/main/java/riot/system/RiotLib.java (added)
+++ jena/Experimental/riot-output/src/main/java/riot/system/RiotLib.java Sat Feb  9 20:14:25 2013
@@ -0,0 +1,118 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package riot.system;
+
+import static riot.writer.WriterConst.PREFIX_IRI ;
+
+import java.util.ArrayList ;
+import java.util.Collection ;
+import java.util.List ;
+import java.util.Map ;
+
+import org.apache.jena.atlas.io.IndentedWriter ;
+import org.apache.jena.atlas.iterator.Iter ;
+import org.apache.jena.riot.system.PrefixMap ;
+import org.apache.jena.riot.system.PrefixMapFactory ;
+
+import com.hp.hpl.jena.graph.Graph ;
+import com.hp.hpl.jena.graph.Node ;
+import com.hp.hpl.jena.graph.Triple ;
+import com.hp.hpl.jena.sparql.core.DatasetGraph ;
+import com.hp.hpl.jena.sparql.core.DatasetGraphFactory ;
+import com.hp.hpl.jena.util.iterator.ExtendedIterator ;
+
+public class RiotLib
+{
+    public static Collection<Triple> triplesOfSubject(Graph graph, Node subj)
+    {
+        return triples(graph, subj, Node.ANY, Node.ANY) ;
+    }
+
+    /* Get all the triples for the graph.find */
+    public static List<Triple> triples(Graph graph, Node s, Node p, Node o)
+    {
+        List<Triple> acc =  new ArrayList<Triple>() ;
+        accTriples(acc, graph, s, p, o) ;
+        return acc ;
+    }
+
+    /* Get all the triples for the graph.find */
+    public static long countTriples(Graph graph, Node s, Node p, Node o)
+    {
+        ExtendedIterator<Triple> iter = graph.find(s, p, o) ;
+        try { return Iter.count(iter) ; }
+        finally { iter.close() ; }
+    }
+
+    public static void accTriples(Collection<Triple> acc, Graph graph, Node s, Node p, Node o)
+    {
+        ExtendedIterator<Triple> iter = graph.find(s, p, o) ;
+        for ( ; iter.hasNext() ; )
+            acc.add(iter.next()) ;
+        iter.close() ;
+    }
+
+    /* Get a triple or null. */
+    public static Triple triple1(Graph graph, Node s, Node p, Node o)
+    {
+        ExtendedIterator<Triple> iter = graph.find(s, p, o) ;
+        try {
+            if ( ! iter.hasNext() )
+                return null ;
+            Triple t = iter.next() ;
+            if (  iter.hasNext() )
+                return null ;
+            return t ;
+        } finally { iter.close() ; }
+    }
+
+    public static boolean strSafeFor(String str, char ch) { return str.indexOf(ch) == -1 ; }
+
+    public static void writePrefixes(IndentedWriter out, PrefixMap prefixMap)
+    {        
+        if ( ! prefixMap.isEmpty() )
+        {
+            for ( Map.Entry <String, String> e : prefixMap.getMappingCopyStr().entrySet() )
+            {
+                out.print("@prefix ") ;
+                out.print(e.getKey()) ;
+                out.print(": ") ;
+                out.pad(PREFIX_IRI) ;
+                out.print("<") ;
+                out.print(e.getValue()) ;
+                out.print(">") ;
+                out.print(" .") ;
+                out.println() ;
+            }
+        }
+    }
+
+    /** Returns dataset that wraps a graph */
+    public static DatasetGraph dataset(Graph graph)
+    {
+        return DatasetGraphFactory.createOneGraph(graph) ;
+    }        
+    
+    public static PrefixMap prefixMap(DatasetGraph dsg)
+    {
+        return PrefixMapFactory.create(dsg.getDefaultGraph().getPrefixMapping()) ;
+    }
+
+}
+