jena-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From a...@apache.org
Subject [01/11] jena git commit: JENA-1454: Introduce builder pattern for result set reading and writing.
Date Sun, 31 Dec 2017 10:52:39 GMT
Repository: jena
Updated Branches:
  refs/heads/master 08d231d8c -> 0366da2c3


http://git-wip-us.apache.org/repos/asf/jena/blob/716b86cf/jena-arq/src/main/java/org/apache/jena/sparql/resultset/XMLOutputResultSet.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/resultset/XMLOutputResultSet.java
b/jena-arq/src/main/java/org/apache/jena/sparql/resultset/XMLOutputResultSet.java
deleted file mode 100644
index a0e1b99..0000000
--- a/jena-arq/src/main/java/org/apache/jena/sparql/resultset/XMLOutputResultSet.java
+++ /dev/null
@@ -1,286 +0,0 @@
-/*
- * 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 org.apache.jena.sparql.resultset;
-
-import java.io.OutputStream ;
-import java.util.HashMap ;
-import java.util.Map ;
-
-import org.apache.jena.atlas.io.IndentedWriter ;
-import org.apache.jena.atlas.logging.Log ;
-import org.apache.jena.query.ARQ ;
-import org.apache.jena.query.QuerySolution ;
-import org.apache.jena.query.ResultSet ;
-import org.apache.jena.rdf.model.Literal ;
-import org.apache.jena.rdf.model.RDFNode ;
-import org.apache.jena.rdf.model.Resource ;
-import org.apache.jena.rdf.model.impl.Util ;
-
-/** XML Output (ResultSet format) */
-
-
-public class XMLOutputResultSet implements ResultSetProcessor, XMLResults
-{
-    static boolean outputExplicitUnbound = false ;
-    
-    boolean outputGraphBNodeLabels = ARQ.isTrue(ARQ.outputGraphBNodeLabels) ;
-
-    int index = 0 ;                     // First index is 1 
-    String stylesheetURL = null ;
-    boolean xmlInst = true ;
-
-    IndentedWriter  out ;
-    int bNodeCounter = 0 ;
-    Map<Resource, String> bNodeMap = new HashMap<>() ;
-    
-    XMLOutputResultSet(OutputStream outStream) {
-        this(new IndentedWriter(outStream));
-    }
-
-    XMLOutputResultSet(IndentedWriter indentedOut) {
-        out = indentedOut;
-    }
-
-    @Override
-    public void start(ResultSet rs) {
-        if ( xmlInst )
-            out.println("<?xml version=\"1.0\"?>");
-
-        if ( stylesheetURL != null ) {
-            out.print("<?xml-stylesheet type=\"text/xsl\" href=\"");
-            out.print(stylesheetURL);
-            out.println("\"?>");
-        }
-
-        // ---- Root
-        out.print("<");
-        out.print(dfRootTag);
-        out.print(" xmlns=\"");
-        out.print(dfNamespace);
-        out.println("\">");
-
-        // ---- Header
-
-        out.incIndent(INDENT);
-        out.print("<");
-        out.print(dfHead);
-        out.println(">");
-
-        if ( false ) {
-            String link = "UNSET";
-            out.print("<link href=\"");
-            out.print(link);
-            out.println("\"/>");
-        }
-
-        for ( String n : rs.getResultVars() ) {
-            out.incIndent(INDENT);
-            out.print("<");
-            out.print(dfVariable);
-            out.print(" ");
-            out.print(dfAttrVarName);
-            out.print("=\"");
-            out.print(n);
-            out.print("\"");
-            out.println("/>");
-            out.decIndent(INDENT);
-        }
-        out.print("</");
-        out.print(dfHead);
-        out.println(">");
-        out.decIndent(INDENT);
-
-        // Start results proper
-        out.incIndent(INDENT);
-        out.print("<");
-        out.print(dfResults);
-        out.println(">");
-        out.incIndent(INDENT);
-    }
-
-    @Override
-    public void finish(ResultSet rs) {
-        out.decIndent(INDENT);
-        out.print("</");
-        out.print(dfResults);
-        out.println(">");
-        out.decIndent(INDENT);
-        out.print("</");
-        out.print(dfRootTag);
-        out.println(">");
-        out.flush();
-    }
-
-    @Override
-    public void start(QuerySolution qs) {
-        out.print("<");
-        out.print(dfSolution);
-        out.println(">");
-        index++;
-        out.incIndent(INDENT);
-    }
-
-    @Override
-    public void finish(QuerySolution qs) {
-        out.decIndent(INDENT);
-        out.print("</");
-        out.print(dfSolution);
-        out.println(">");
-    }
-
-    @Override
-    public void binding(String varName, RDFNode node) {
-        if ( node == null && !outputExplicitUnbound )
-            return;
-
-        out.print("<");
-        out.print(dfBinding);
-        out.print(" name=\"");
-        out.print(varName);
-        out.println("\">");
-        out.incIndent(INDENT);
-        printBindingValue(node);
-        out.decIndent(INDENT);
-        out.print("</");
-        out.print(dfBinding);
-        out.println(">");
-    }
-
-    void printBindingValue(RDFNode node) {
-        if ( node == null ) {
-            // Unbound
-            out.print("<");
-            out.print(dfUnbound);
-            out.println("/>");
-            return;
-        }
-
-        if ( node instanceof Literal ) {
-            printLiteral((Literal)node);
-            return;
-        }
-
-        if ( node instanceof Resource ) {
-            printResource((Resource)node);
-            return;
-        }
-
-        Log.warn(this, "Unknown RDFNode type in result set: " + node.getClass());
-    }
-
-    void printLiteral(Literal literal) {
-        out.print("<");
-        out.print(dfLiteral);
-
-        if ( Util.isLangString(literal) ) {
-            String lang = literal.getLanguage();
-            out.print(" xml:lang=\"");
-            out.print(literal.getLanguage());
-            out.print("\"");
-        } else if ( !Util.isSimpleString(literal) ) {
-            // Datatype
-            // (RDF 1.1) not xsd:string nor rdf:langString.
-            // (RDF 1.0) any datatype.
-            String datatype = literal.getDatatypeURI();
-            out.print(" ");
-            out.print(dfAttrDatatype);
-            out.print("=\"");
-            out.print(datatype);
-            out.print("\"");
-        }
-
-        out.print(">");
-        out.print(xml_escape(literal.getLexicalForm()));
-        out.print("</");
-        out.print(dfLiteral);
-        out.println(">");
-    }
-
-    void printResource(Resource r) {
-        if ( r.isAnon() ) {
-            String label;
-
-            if ( outputGraphBNodeLabels )
-                label = r.asNode().getBlankNodeId().getLabelString();
-            else {
-                if ( !bNodeMap.containsKey(r) )
-                    bNodeMap.put(r, "b" + (bNodeCounter++));
-                label = bNodeMap.get(r);
-            }
-            out.print("<");
-            out.print(dfBNode);
-            out.print(">");
-            out.print(xml_escape(label));
-            out.print("</");
-            out.print(dfBNode);
-            out.println(">");
-        } else {
-            out.print("<");
-            out.print(dfURI);
-            out.print(">");
-            out.print(xml_escape(r.getURI()));
-            out.print("</");
-            out.print(dfURI);
-            out.println(">");
-        }
-    }
-
-    private static String xml_escape(String string) {
-        final StringBuilder sb = new StringBuilder(string);
-
-        int offset = 0;
-        String replacement;
-        char found;
-        for (int i = 0; i < string.length(); i++) {
-            found = string.charAt(i);
-            
-            switch (found) {
-                case '&' : replacement = "&amp;"; break;
-                case '<' : replacement = "&lt;"; break;
-                case '>' : replacement = "&gt;"; break;
-                case '\r': replacement = "&#x0D;"; break;
-                case '\n': replacement = "&#x0A;"; break;
-                default  : replacement = null;
-            }
-            
-            if (replacement != null) {
-                sb.replace(offset + i, offset + i + 1, replacement);
-                offset += replacement.length() - 1; // account for added chars
-            }
-        }
-        
-        return sb.toString();
-    }
-
-    /** @return Returns the stylesheetURL. */
-    public String getStylesheetURL()
-    { return stylesheetURL ; }
-
-    /** @param stylesheetURL The stylesheetURL to set. */
-    public void setStylesheetURL(String stylesheetURL)
-    { this.stylesheetURL = stylesheetURL ; }
-
-    /** @return Returns the xmlInst. */
-    public boolean getXmlInst()
-    { return xmlInst ; }
-
-    /** @param xmlInst The xmlInst to set. */
-    public void setXmlInst(boolean xmlInst)
-    { this.xmlInst = xmlInst ; }
-}

http://git-wip-us.apache.org/repos/asf/jena/blob/716b86cf/jena-arq/src/main/java/org/apache/jena/sparql/resultset/XMLResults.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/resultset/XMLResults.java b/jena-arq/src/main/java/org/apache/jena/sparql/resultset/XMLResults.java
deleted file mode 100644
index 1a5f2d2..0000000
--- a/jena-arq/src/main/java/org/apache/jena/sparql/resultset/XMLResults.java
+++ /dev/null
@@ -1,50 +0,0 @@
-/*
- * 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 org.apache.jena.sparql.resultset;
-
-import org.apache.jena.sparql.ARQConstants ;
-
-
-public interface XMLResults
-{
-    public static final int INDENT = 2 ;
-
-    public static final String baseNamespace   = ARQConstants.srxPrefix ;
-    public static final String xsBaseURI       = ARQConstants.XML_SCHEMA_NS ;
-    
-    public static final String dfAttrVarName   = "name" ;
-    public static final String dfAttrDatatype  = "datatype" ;
-    
-    public static final String dfNamespace  = baseNamespace ;
-    public static final String dfRootTag    = "sparql" ;
-    public static final String dfHead       = "head" ;
-    public static final String dfVariable   = "variable" ;
-    public static final String dfLink       = "link" ;
-    public static final String dfResults    = "results" ;
-    public static final String dfSolution   = "result" ;
-    public static final String dfBinding    = "binding" ;
-    
-    public static final String dfBNode      = "bnode" ;
-    public static final String dfURI        = "uri" ;
-    public static final String dfLiteral    = "literal" ;
-    
-    public static final String dfUnbound    = "unbound" ;
-
-    public static final String dfBoolean    = "boolean" ;
-}

http://git-wip-us.apache.org/repos/asf/jena/blob/716b86cf/jena-arq/src/main/java/org/apache/jena/sparql/util/LabelToNodeMap.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/util/LabelToNodeMap.java b/jena-arq/src/main/java/org/apache/jena/sparql/util/LabelToNodeMap.java
index 1856b3d..9a3ddde 100644
--- a/jena-arq/src/main/java/org/apache/jena/sparql/util/LabelToNodeMap.java
+++ b/jena-arq/src/main/java/org/apache/jena/sparql/util/LabelToNodeMap.java
@@ -28,8 +28,7 @@ import org.apache.jena.sparql.ARQConstants ;
 import org.apache.jena.sparql.core.VarAlloc ;
 
 
-/** Map from _:* form to bNodes */
-
+/** Map from _:* form to bNodes or variables. */
 public class LabelToNodeMap
 {
     // Wil be replaced by LabelToNode in RIOT
@@ -44,8 +43,9 @@ public class LabelToNodeMap
     /** Create blank nodes, with the same blank node returned for the same label.  
      * 
      * @return LabelToNodeMap
+     * @deprecated use LabelToNode 
      */
-
+    @Deprecated
     public static LabelToNodeMap createBNodeMap()
     { return new LabelToNodeMap(false, null) ; }
     

http://git-wip-us.apache.org/repos/asf/jena/blob/716b86cf/jena-arq/src/main/java/org/apache/jena/sparql/util/QueryExecUtils.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/main/java/org/apache/jena/sparql/util/QueryExecUtils.java b/jena-arq/src/main/java/org/apache/jena/sparql/util/QueryExecUtils.java
index c76d1d4..dc495b0 100644
--- a/jena-arq/src/main/java/org/apache/jena/sparql/util/QueryExecUtils.java
+++ b/jena-arq/src/main/java/org/apache/jena/sparql/util/QueryExecUtils.java
@@ -104,7 +104,6 @@ public class QueryExecUtils {
         outputResultSet(results, null, outputFormat) ;
     }
 
-    @SuppressWarnings("deprecation")
     public static void outputResultSet(ResultSet results, Prologue prologue, ResultsFormat
outputFormat) {
         // Proper ResultSet formats.
         Lang lang = ResultsFormat.convert(outputFormat) ;
@@ -176,11 +175,6 @@ public class QueryExecUtils {
             done = true ;
         }
 
-        if ( outputFormat.equals(ResultsFormat.FMT_RS_BIO) ) {
-            ResultSetFormatter.outputAsBIO(System.out, results) ;
-            done = true ;
-        }
-
         if ( !done )
             System.err.println("Unknown format request: " + outputFormat) ;
         results = null ;

http://git-wip-us.apache.org/repos/asf/jena/blob/716b86cf/jena-arq/src/test/java/org/apache/jena/sparql/resultset/TestResultSet.java
----------------------------------------------------------------------
diff --git a/jena-arq/src/test/java/org/apache/jena/sparql/resultset/TestResultSet.java b/jena-arq/src/test/java/org/apache/jena/sparql/resultset/TestResultSet.java
index 46e6796..3f8da9e 100644
--- a/jena-arq/src/test/java/org/apache/jena/sparql/resultset/TestResultSet.java
+++ b/jena-arq/src/test/java/org/apache/jena/sparql/resultset/TestResultSet.java
@@ -20,6 +20,7 @@ package org.apache.jena.sparql.resultset;
 
 import java.io.ByteArrayInputStream ;
 import java.io.ByteArrayOutputStream ;
+import java.io.InputStream;
 import java.util.ArrayList ;
 import java.util.List ;
 import java.util.NoSuchElementException;
@@ -45,12 +46,15 @@ import org.apache.jena.sparql.sse.SSE ;
 import org.apache.jena.sparql.sse.builders.BuilderResultSet ;
 import org.apache.jena.sparql.util.NodeFactoryExtra ;
 import org.apache.jena.sparql.util.ResultSetUtils ;
+import org.apache.jena.system.JenaSystem;
 import org.junit.AfterClass;
 import org.junit.BeforeClass;
 import org.junit.Test ;
 
 public class TestResultSet extends BaseTest
 {
+    static { JenaSystem.init(); }
+    
     @BeforeClass
     public static void setup() {
         // Disable warnings these tests will produce
@@ -80,7 +84,8 @@ public class TestResultSet extends BaseTest
         ResultSetRewindable rs1 = new ResultSetMem() ;
         String x = ResultSetFormatter.asXMLString(rs1) ;
         rs1.reset() ;
-        ResultSet rs2 = ResultSetFactory.fromXML(x) ;
+        InputStream in = new ByteArrayInputStream(StrUtils.asUTF8bytes(x));
+        ResultSet rs2 = ResultSetFactory.fromXML(in) ;
         assertTrue(ResultSetCompare.equalsByTerm(rs1, rs2)) ;
     }
 
@@ -100,7 +105,8 @@ public class TestResultSet extends BaseTest
         ResultSetRewindable rs1 = makeRewindable("x", org.apache.jena.graph.NodeFactory.createURI("tag:local"))
;
         String x = ResultSetFormatter.asXMLString(rs1) ;
         rs1.reset() ;
-        ResultSet rs2 = ResultSetFactory.fromXML(x) ;
+        InputStream in = new ByteArrayInputStream(StrUtils.asUTF8bytes(x));
+        ResultSet rs2 = ResultSetFactory.fromXML(in) ;
         assertTrue(ResultSetCompare.equalsByTerm(rs1, rs2)) ;
     }
 


Mime
View raw message