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 2011/10/09 21:08:55 UTC

svn commit: r1180693 - in /incubator/jena/Scratch/AFS: Dev/trunk/.classpath Jena-Dev/trunk/src/dev/JSONResultsKW.java Jena-Dev/trunk/src/dev/JSONResultsReader.java

Author: andy
Date: Sun Oct  9 19:08:55 2011
New Revision: 1180693

URL: http://svn.apache.org/viewvc?rev=1180693&view=rev
Log: (empty)

Added:
    incubator/jena/Scratch/AFS/Jena-Dev/trunk/src/dev/JSONResultsKW.java   (with props)
    incubator/jena/Scratch/AFS/Jena-Dev/trunk/src/dev/JSONResultsReader.java   (with props)
Modified:
    incubator/jena/Scratch/AFS/Dev/trunk/.classpath

Modified: incubator/jena/Scratch/AFS/Dev/trunk/.classpath
URL: http://svn.apache.org/viewvc/incubator/jena/Scratch/AFS/Dev/trunk/.classpath?rev=1180693&r1=1180692&r2=1180693&view=diff
==============================================================================
--- incubator/jena/Scratch/AFS/Dev/trunk/.classpath (original)
+++ incubator/jena/Scratch/AFS/Dev/trunk/.classpath Sun Oct  9 19:08:55 2011
@@ -1,28 +1,27 @@
 <?xml version="1.0" encoding="UTF-8"?>
 <classpath>
-  <classpathentry excluding="**/.svn/" kind="src" path="src/main/java"/>
-  <classpathentry excluding="**/.svn/" kind="src" path="src/test/java"/>
-  <classpathentry excluding="**/.svn/" kind="src" path="src-lib/main/java"/>
-  <classpathentry excluding="**/.svn/" kind="src" path="src-lib/test/java"/>
-  <classpathentry excluding="**/.svn/" kind="src" path="src-dev"/>
-  <classpathentry excluding="**/.svn/" kind="src" path="src-archive"/>
-  <classpathentry kind="output" path="classes"/>
-
-  <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
-  <classpathentry kind="var" path="M2_REPO/com/hp/hpl/jena/jena/2.6.4/jena-2.6.4.jar" sourcepath="M2_REPO/com/hp/hpl/jena/jena/2.6.4/jena-2.6.4-sources.jar"/>
-  <classpathentry kind="var" path="M2_REPO/com/ibm/icu/icu4j/3.4.4/icu4j-3.4.4.jar" sourcepath="M2_REPO/com/ibm/icu/icu4j/3.4.4/icu4j-3.4.4-sources.jar"/>
-  <classpathentry kind="var" path="M2_REPO/com/hp/hpl/jena/iri/0.8/iri-0.8.jar" sourcepath="M2_REPO/com/hp/hpl/jena/iri/0.8/iri-0.8-sources.jar"/>
-  <classpathentry kind="var" path="M2_REPO/junit/junit/4.8.2/junit-4.8.2.jar" sourcepath="M2_REPO/junit/junit/4.8.2/junit-4.8.2-sources.jar"/>
-  <classpathentry kind="var" path="M2_REPO/log4j/log4j/1.2.16/log4j-1.2.16.jar" sourcepath="M2_REPO/log4j/log4j/1.2.16/log4j-1.2.16-sources.jar"/>
-  <classpathentry kind="var" path="M2_REPO/org/apache/lucene/lucene-core/2.3.1/lucene-core-2.3.1.jar" sourcepath="M2_REPO/org/apache/lucene/lucene-core/2.3.1/lucene-core-2.3.1-sources.jar"/>
-  <classpathentry kind="var" path="M2_REPO/org/slf4j/slf4j-api/1.6.1/slf4j-api-1.6.1.jar" sourcepath="M2_REPO/org/slf4j/slf4j-api/1.6.1/slf4j-api-1.6.1-sources.jar"/>
-  <classpathentry kind="var" path="M2_REPO/org/slf4j/slf4j-log4j12/1.6.1/slf4j-log4j12-1.6.1.jar" sourcepath="M2_REPO/org/slf4j/slf4j-log4j12/1.6.1/slf4j-log4j12-1.6.1-sources.jar"/>
-  <classpathentry kind="var" path="M2_REPO/stax/stax-api/1.0.1/stax-api-1.0.1.jar"/>
-  <classpathentry kind="var" path="M2_REPO/org/codehaus/woodstox/wstx-asl/3.2.9/wstx-asl-3.2.9.jar" sourcepath="M2_REPO/org/codehaus/woodstox/wstx-asl/3.2.9/wstx-asl-3.2.9-sources.jar"/>
-  <classpathentry kind="var" path="M2_REPO/xerces/xercesImpl/2.7.1/xercesImpl-2.7.1.jar"/>
-  <classpathentry kind="var" path="M2_REPO/org/apache/httpcomponents/httpclient/4.1.2/httpclient-4.1.2.jar" sourcepath="M2_REPO/org/apache/httpcomponents/httpclient/4.1.2/httpclient-4.1.2-sources.jar"/>
-  <classpathentry kind="var" path="M2_REPO/org/apache/httpcomponents/httpcore/4.1.2/httpcore-4.1.2.jar" sourcepath="M2_REPO/org/apache/httpcomponents/httpcore/4.1.2/httpcore-4.1.2-sources.jar"/>
-  <classpathentry kind="var" path="M2_REPO/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar" sourcepath="M2_REPO/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1-sources.jar"/>
-  <classpathentry kind="lib" path="/ARQ/classes" sourcepath="/ARQ/src"/>
-  <classpathentry kind="lib" path="/TxTDB/classes" sourcepath="/TxTDB/src"/>
+	<classpathentry excluding="**/.svn/" kind="src" path="src/main/java"/>
+	<classpathentry excluding="**/.svn/" kind="src" path="src/test/java"/>
+	<classpathentry excluding="**/.svn/" kind="src" path="src-lib/main/java"/>
+	<classpathentry excluding="**/.svn/" kind="src" path="src-lib/test/java"/>
+	<classpathentry excluding="**/.svn/" kind="src" path="src-dev"/>
+	<classpathentry excluding="**/.svn/" kind="src" path="src-archive"/>
+	<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
+	<classpathentry kind="var" path="M2_REPO/com/hp/hpl/jena/jena/2.6.4/jena-2.6.4.jar" sourcepath="M2_REPO/com/hp/hpl/jena/jena/2.6.4/jena-2.6.4-sources.jar"/>
+	<classpathentry kind="var" path="M2_REPO/com/ibm/icu/icu4j/3.4.4/icu4j-3.4.4.jar" sourcepath="M2_REPO/com/ibm/icu/icu4j/3.4.4/icu4j-3.4.4-sources.jar"/>
+	<classpathentry kind="var" path="M2_REPO/com/hp/hpl/jena/iri/0.8/iri-0.8.jar" sourcepath="M2_REPO/com/hp/hpl/jena/iri/0.8/iri-0.8-sources.jar"/>
+	<classpathentry kind="var" path="M2_REPO/junit/junit/4.8.2/junit-4.8.2.jar" sourcepath="M2_REPO/junit/junit/4.8.2/junit-4.8.2-sources.jar"/>
+	<classpathentry kind="var" path="M2_REPO/log4j/log4j/1.2.16/log4j-1.2.16.jar" sourcepath="M2_REPO/log4j/log4j/1.2.16/log4j-1.2.16-sources.jar"/>
+	<classpathentry kind="var" path="M2_REPO/org/apache/lucene/lucene-core/2.3.1/lucene-core-2.3.1.jar" sourcepath="M2_REPO/org/apache/lucene/lucene-core/2.3.1/lucene-core-2.3.1-sources.jar"/>
+	<classpathentry kind="var" path="M2_REPO/org/slf4j/slf4j-api/1.6.1/slf4j-api-1.6.1.jar" sourcepath="M2_REPO/org/slf4j/slf4j-api/1.6.1/slf4j-api-1.6.1-sources.jar"/>
+	<classpathentry kind="var" path="M2_REPO/org/slf4j/slf4j-log4j12/1.6.1/slf4j-log4j12-1.6.1.jar" sourcepath="M2_REPO/org/slf4j/slf4j-log4j12/1.6.1/slf4j-log4j12-1.6.1-sources.jar"/>
+	<classpathentry kind="var" path="M2_REPO/stax/stax-api/1.0.1/stax-api-1.0.1.jar"/>
+	<classpathentry kind="var" path="M2_REPO/org/codehaus/woodstox/wstx-asl/3.2.9/wstx-asl-3.2.9.jar" sourcepath="M2_REPO/org/codehaus/woodstox/wstx-asl/3.2.9/wstx-asl-3.2.9-sources.jar"/>
+	<classpathentry kind="var" path="M2_REPO/xerces/xercesImpl/2.7.1/xercesImpl-2.7.1.jar"/>
+	<classpathentry kind="var" path="M2_REPO/org/apache/httpcomponents/httpclient/4.1.2/httpclient-4.1.2.jar" sourcepath="M2_REPO/org/apache/httpcomponents/httpclient/4.1.2/httpclient-4.1.2-sources.jar"/>
+	<classpathentry kind="var" path="M2_REPO/org/apache/httpcomponents/httpcore/4.1.2/httpcore-4.1.2.jar" sourcepath="M2_REPO/org/apache/httpcomponents/httpcore/4.1.2/httpcore-4.1.2-sources.jar"/>
+	<classpathentry kind="var" path="M2_REPO/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1.jar" sourcepath="M2_REPO/commons-logging/commons-logging/1.1.1/commons-logging-1.1.1-sources.jar"/>
+	<classpathentry combineaccessrules="false" kind="src" path="/ARQ"/>
+	<classpathentry combineaccessrules="false" kind="src" path="/TxTDB"/>
+	<classpathentry kind="output" path="classes"/>
 </classpath>

Added: incubator/jena/Scratch/AFS/Jena-Dev/trunk/src/dev/JSONResultsKW.java
URL: http://svn.apache.org/viewvc/incubator/jena/Scratch/AFS/Jena-Dev/trunk/src/dev/JSONResultsKW.java?rev=1180693&view=auto
==============================================================================
--- incubator/jena/Scratch/AFS/Jena-Dev/trunk/src/dev/JSONResultsKW.java (added)
+++ incubator/jena/Scratch/AFS/Jena-Dev/trunk/src/dev/JSONResultsKW.java Sun Oct  9 19:08:55 2011
@@ -0,0 +1,42 @@
+/**
+ * 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 dev;
+
+//Keywords for JSON:
+//Taken from:
+//From http://www.w3.org/TR/sparql11-results-json/ (Oct 2011)
+
+public class JSONResultsKW
+{
+    public static String kHead          = "head" ;
+    public static String kVars          = "vars" ;
+    public static String kLink          = "link" ;
+    public static String kResults       = "results" ;
+    public static String kBindings      = "bindings" ;
+    public static String kType          = "type" ;
+    public static String kUri           = "uri"  ;
+    public static String kValue         = "value" ;
+    public static String kLiteral       = "literal" ;
+    public static String kTypedLiteral  = "typed-literal" ;
+    public static String kXmlLang       = "xml:lang" ;
+    public static String kDatatype      = "datatype" ;
+    public static String kBnode         = "bnode" ;
+    public static String kBoolean       = "boolean" ;
+}
+

Propchange: incubator/jena/Scratch/AFS/Jena-Dev/trunk/src/dev/JSONResultsKW.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/jena/Scratch/AFS/Jena-Dev/trunk/src/dev/JSONResultsReader.java
URL: http://svn.apache.org/viewvc/incubator/jena/Scratch/AFS/Jena-Dev/trunk/src/dev/JSONResultsReader.java?rev=1180693&view=auto
==============================================================================
--- incubator/jena/Scratch/AFS/Jena-Dev/trunk/src/dev/JSONResultsReader.java (added)
+++ incubator/jena/Scratch/AFS/Jena-Dev/trunk/src/dev/JSONResultsReader.java Sun Oct  9 19:08:55 2011
@@ -0,0 +1,329 @@
+/**
+ * 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 dev;
+
+import static dev.JSONResultsKW.kBindings ;
+import static dev.JSONResultsKW.kBnode ;
+import static dev.JSONResultsKW.kBoolean ;
+import static dev.JSONResultsKW.kDatatype ;
+import static dev.JSONResultsKW.kHead ;
+import static dev.JSONResultsKW.kLink ;
+import static dev.JSONResultsKW.kLiteral ;
+import static dev.JSONResultsKW.kResults ;
+import static dev.JSONResultsKW.kType ;
+import static dev.JSONResultsKW.kTypedLiteral ;
+import static dev.JSONResultsKW.kUri ;
+import static dev.JSONResultsKW.kValue ;
+import static dev.JSONResultsKW.kVars ;
+import static dev.JSONResultsKW.kXmlLang ;
+
+import java.io.FileInputStream ;
+import java.io.InputStream ;
+import java.util.* ;
+
+import org.openjena.atlas.iterator.IteratorSlotted ;
+import org.openjena.atlas.json.* ;
+import org.openjena.atlas.json.io.JSONHandler ;
+import org.openjena.atlas.json.io.parser.JSONParser ;
+import org.openjena.riot.lang.LabelToNode ;
+
+import com.hp.hpl.jena.datatypes.RDFDatatype ;
+import com.hp.hpl.jena.datatypes.TypeMapper ;
+import com.hp.hpl.jena.graph.Node ;
+import com.hp.hpl.jena.sparql.core.Var ;
+import com.hp.hpl.jena.sparql.engine.binding.Binding ;
+import com.hp.hpl.jena.sparql.engine.binding.BindingFactory ;
+import com.hp.hpl.jena.sparql.engine.binding.BindingMap ;
+import com.hp.hpl.jena.sparql.resultset.ResultSetException ;
+import com.hp.hpl.jena.sparql.resultset.SPARQLResult ;
+
+public class JSONResultsReader extends SPARQLResult
+{
+//    public JSONResultsReader(InputStream in)
+//    {
+//        this(in, null) ;
+//    }
+//
+//    public JSONResultsReader(InputStream in, Model model)
+//    {
+//        if ( model == null )
+//            model = GraphFactory.makeJenaDefaultModel() ;
+//    }
+
+    static public void main(String... args) throws Exception
+    {
+        //String FILE = "../ARQ/testing/ResultSet/output.srj" ;
+        String FILE = "../ARQ/testing/ARQ/Ask/ask-2.srj" ;
+        InputStream in = new FileInputStream(FILE) ;
+        
+        new JSONResultsReader().proto1(in) ;
+    }
+    
+    static void proto(InputStream in)
+    {
+        JSONProc proc = new JSONProc() ; 
+        JSONParser.parse(in, proc) ;
+    }
+
+    // TODO
+    //  checkContains
+    //  streaming
+    
+    void proto1(InputStream in)
+    {
+        JsonObject obj = JSON.parse(in) ;
+        
+        if ( obj.hasKey(kBoolean) )
+        {
+            checkContains(obj, kHead, kBoolean) ;
+            // TODO check kHead is empty.
+            boolean b = obj.get(kBoolean).getAsBoolean().value() ;
+            
+            System.out.println("Boolean : "+b) ;
+            return ;
+            
+        }
+        
+        checkContains(obj, kHead, kResults) ;
+
+        if ( ! obj.hasKey(kHead) )    throw new ResultSetException("No 'head' for results") ;
+        if ( ! obj.hasKey(kResults) ) throw new ResultSetException("No 'results' for results") ;
+        
+        // process head
+        if ( ! obj.get(kHead).isObject() )
+            throw new ResultSetException("Key 'head' must have a JSON object as value: found: "+obj.get(kHead)) ;
+        JsonObject head = obj.get(kHead).getAsObject() ;
+        
+        // ---- Head
+        // -- Link
+        String link = stringOrNull(head, kLink) ;
+        // -- Vars
+        List<Var> vars = parseVars(head) ;
+        System.out.println("Vars: "+vars) ;
+
+        // ---- Results
+        JsonObject results = obj.get(kResults).getAsObject() ;
+        if ( ! results.get(kBindings).isArray() )
+            throw new ResultSetException("'bindings' must be an array") ;
+        JsonArray array = results.get(kBindings).getAsArray() ;
+        Iterator<JsonValue> iter = array.iterator() ;
+        List<Binding> rows = new ArrayList<Binding>() ;
+        
+        for ( ; iter.hasNext() ; )
+        {
+            
+            BindingMap b = BindingFactory.create() ;
+            JsonValue v = iter.next() ;
+            if ( ! v.isObject() )
+                throw new ResultSetException("Entry in 'bindings' array must be an object {}")  ;
+            JsonObject x = v.getAsObject() ;
+            Set<String> varNames = x.keys() ;
+            for ( String vn : varNames )
+            {
+                if ( ! vars.contains(vn) )
+                    ; // Warning
+                JsonValue vt = x.get(vn) ;
+                if ( ! vt.isObject() )
+                    throw new ResultSetException("Binding for variable '"+vn+"' is not a JSON object: "+vt)  ;
+                Node n = parseOneTerm(vt.getAsObject()) ;
+                b.add(Var.alloc(vn), n) ;
+            }
+            rows.add(b) ;
+            System.out.println("Row: "+b) ;
+        }
+    }
+    
+    private List<Var> parseVars(JsonObject obj)
+    {
+        if ( ! obj.get(kVars).isArray() )
+            throw new ResultSetException("Key 'vars' must be a JSON array") ;
+        JsonArray a = obj.get(kVars).getAsArray() ;
+        Iterator<JsonValue> iter = a.iterator() ;
+        List<Var> vars = new ArrayList<Var>() ;
+        for ( ; iter.hasNext() ; )
+        {
+            JsonValue v = iter.next() ;
+            if ( ! v.isString() )
+                throw new ResultSetException("Entries in vars array must be strings") ;
+            Var var = Var.alloc(v.getAsString().value()) ;
+            vars.add(var) ;
+        }
+        return vars ;
+    }
+
+    LabelToNode labelMap = LabelToNode.createScopeByDocument() ;
+    private Node parseOneTerm(JsonObject term)
+    {
+        checkContains(term, kType, kValue, kXmlLang, kDatatype) ;
+        
+        String type = stringOrNull(term, kType) ;
+        String v = stringOrNull(term, kValue) ;
+        
+        if ( kUri.equals(type) )
+        {
+            checkContains(term, kValue) ;
+            String uri = v ;
+            Node n = Node.createURI(v) ;
+            return n ;
+        }
+        
+        if ( kLiteral.equals(type) || kTypedLiteral.equals(type) )
+        {
+            String lang = stringOrNull(term, kXmlLang) ;
+            String dtStr = stringOrNull(term, kDatatype) ;
+            if ( lang != null && dtStr != null )
+                throw new ResultSetException("Both language and datatype defined: "+term) ;
+            RDFDatatype dt = TypeMapper.getInstance().getSafeTypeByName(dtStr) ;
+            return Node.createLiteral(v, lang, dt)  ;
+        }
+        
+        if ( kBnode.equals(type) )
+            return labelMap.get(null, v) ;
+        
+        throw new ResultSetException("Object key not recognized as valid for an RDF term: "+term) ;
+    }
+    
+    private static String stringOrNull(JsonObject obj, String key)
+    {
+        JsonValue v = obj.get(key) ;
+        if ( v == null ) return null ;
+        if ( ! v.isString() )
+            throw new ResultSetException("Not a string") ;
+        return v.getAsString().value();
+        
+    }
+    
+    private static void checkContains(JsonObject term, String...keys)
+    {}
+
+    static class JSONProc implements JSONHandler
+    {
+        
+        boolean knownForm = false ; 
+        boolean isResultSet = false ;
+        boolean isBooleanResult = false ;
+        
+        // Last seen item
+        private JsonValue value = null ;
+        
+        // The depth of this stack is the object depth.
+        private Stack<String> keys = new Stack<String>();
+        
+        
+        @Override public void startParse()      { }
+
+        @Override public void finishParse()     { }
+
+        @Override public void startObject()     { }
+
+        @Override public void finishObject()    { }
+
+        @Override public void startPair()       { }
+
+        @Override
+        public void keyPair()
+        {
+            if ( ! value.isString() )
+                throw new ResultSetException("Object key is not a string") ;
+            // value is the key. 
+            // On state, process K and V
+            keys.push(value.getAsString().value()) ;
+        }
+
+        @Override public void finishPair()
+        {
+            // Value is the value of a key pair.
+            String key = keys.pop() ;
+            System.out.println("KV: "+key+" : "+value) ;
+        }
+
+        @Override
+        public void startArray()
+        {}
+
+        @Override
+        public void element()
+        {}
+
+        @Override
+        public void finishArray()
+        {}
+
+        //@Override
+        public void valueBoolean(boolean b)
+        { 
+            value = new JsonBoolean(b) ;
+        }
+
+        //@Override
+        public void valueDecimal(String image)
+        {
+            value = JsonNumber.valueDecimal(image) ;
+        }
+
+        //@Override
+        public void valueDouble(String image)
+        {
+            value = JsonNumber.valueDouble(image) ;
+        }
+
+        //@Override
+        public void valueInteger(String image)
+        {
+            value = JsonNumber.valueInteger(image) ;
+        }
+
+        //@Override
+        public void valueNull()
+        {
+            value = JsonNull.instance ;
+        }
+
+        //@Override
+        public void valueString(String image)
+        {
+            value = new JsonString(image) ;
+        }
+
+        
+    }
+    
+    // Iterator of Bindings.
+    // or boolean.
+    
+    // ResultSetStream(List<String> resultVars, Model m, QueryIterator iter)
+    
+    static class JSONBindings extends IteratorSlotted<Binding>
+    {
+        JSONBindings() {}
+
+        @Override
+        protected Binding moveToNext()
+        {
+            return null ;
+        }
+
+        @Override
+        protected boolean hasMore()
+        {
+            return false ;
+        }
+    }
+}
+

Propchange: incubator/jena/Scratch/AFS/Jena-Dev/trunk/src/dev/JSONResultsReader.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain