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/08/16 15:43:28 UTC

svn commit: r1158271 [2/2] - in /incubator/jena/Jena2/ARQ/trunk: src-test/org/openjena/atlas/ src-test/org/openjena/atlas/data/ src/com/hp/hpl/jena/sparql/engine/binding/ src/com/hp/hpl/jena/sparql/resultset/ src/org/openjena/atlas/data/ src/org/openje...

Added: incubator/jena/Jena2/ARQ/trunk/src/org/openjena/atlas/iterator/IteratorResourceClosing.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src/org/openjena/atlas/iterator/IteratorResourceClosing.java?rev=1158271&view=auto
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src/org/openjena/atlas/iterator/IteratorResourceClosing.java (added)
+++ incubator/jena/Jena2/ARQ/trunk/src/org/openjena/atlas/iterator/IteratorResourceClosing.java Tue Aug 16 13:43:27 2011
@@ -0,0 +1,90 @@
+/**
+ * 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.openjena.atlas.iterator;
+
+import java.util.Iterator;
+
+import org.openjena.atlas.AtlasException;
+import org.openjena.atlas.io.IO;
+import org.openjena.atlas.lib.Closeable;
+
+/**
+ * This iterator will automatically close a {@link java.io.Closeable} resource when the iterator is exhausted.
+ * Alternatively, the resource will be closed when {@link #close()} is called.  An {@link AtlasException} will
+ * be thrown if access is attempted after {@link #close()} has been called.
+ */
+public class IteratorResourceClosing<T> implements Iterator<T>, Closeable
+{
+    private final Iterator<T> iter ;
+    private final java.io.Closeable resource ;
+    private boolean finished;
+    
+    public IteratorResourceClosing(Iterator<T> iter, java.io.Closeable resource)
+    {
+        this.iter = iter;
+        this.resource = resource ;
+        this.finished = false;
+    }
+    
+    private void checkFinished()
+    {
+        if (finished) throw new AtlasException("IteratorResourceClosing is closed, no further operations can be performed on it.") ;
+    }
+
+    //@Override
+    public boolean hasNext()
+    {
+        if (finished)
+            return false;
+        
+        boolean toReturn = iter.hasNext();
+        
+        // Clean up if we are done
+        if (!toReturn)
+        {
+            close();
+        }
+        return toReturn ;
+    }
+
+    //@Override
+    public T next()
+    {
+        checkFinished();
+        return iter.next() ;
+    }
+
+    //@Override
+    public void remove()
+    {
+        checkFinished();
+        iter.remove() ;
+    }
+
+    //@Override
+    public void close()
+    {
+        if (!finished)
+        {
+            Iter.close(iter) ;
+            IO.close(resource) ;
+            finished = true;
+        }
+    }
+}

Propchange: incubator/jena/Jena2/ARQ/trunk/src/org/openjena/atlas/iterator/IteratorResourceClosing.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Added: incubator/jena/Jena2/ARQ/trunk/src/org/openjena/riot/SerializationFactoryFinder.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src/org/openjena/riot/SerializationFactoryFinder.java?rev=1158271&view=auto
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src/org/openjena/riot/SerializationFactoryFinder.java (added)
+++ incubator/jena/Jena2/ARQ/trunk/src/org/openjena/riot/SerializationFactoryFinder.java Tue Aug 16 13:43:27 2011
@@ -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 org.openjena.riot;
+
+import java.io.InputStream ;
+import java.io.OutputStream ;
+import java.util.Iterator ;
+
+import org.openjena.atlas.data.SerializationFactory ;
+import org.openjena.atlas.lib.Sink ;
+import org.openjena.riot.lang.LabelToNode ;
+import org.openjena.riot.lang.LangNQuads ;
+import org.openjena.riot.lang.LangNTriples ;
+import org.openjena.riot.out.NodeToLabel ;
+import org.openjena.riot.out.SinkQuadOutput ;
+import org.openjena.riot.out.SinkTripleOutput ;
+import org.openjena.riot.system.IRIResolver ;
+import org.openjena.riot.system.ParserProfileBase ;
+import org.openjena.riot.system.Prologue ;
+import org.openjena.riot.tokens.Tokenizer ;
+import org.openjena.riot.tokens.TokenizerFactory ;
+
+import com.hp.hpl.jena.graph.Triple ;
+import com.hp.hpl.jena.sparql.core.Quad ;
+import com.hp.hpl.jena.sparql.engine.binding.Binding ;
+import com.hp.hpl.jena.sparql.engine.binding.BindingInputStream ;
+import com.hp.hpl.jena.sparql.engine.binding.BindingOutputStream ;
+
+public class SerializationFactoryFinder
+{
+    public static SerializationFactory<Binding> bindingSerializationFactory()
+    {
+        return new SerializationFactory<Binding>()
+        {
+            public Sink<Binding> createSerializer(OutputStream out)
+            {
+                return new BindingOutputStream(out);
+            }
+            
+            public Iterator<Binding> createDeserializer(InputStream in)
+            {
+                return new BindingInputStream(in);
+            }
+
+            public long getEstimatedMemorySize(Binding item)
+            {
+                // TODO traverse the binding, and add up the variable + node sizes + object overhead
+                return 0 ;
+            }
+        };
+    }
+    
+    public static SerializationFactory<Triple> tripleSerializationFactory()
+    {
+        return new SerializationFactory<Triple>()
+        {
+            public Sink<Triple> createSerializer(OutputStream out)
+            {
+                return new SinkTripleOutput(out, null, NodeToLabel.createBNodeByLabelEncoded()) ;
+            }
+            
+            public Iterator<Triple> createDeserializer(InputStream in)
+            {
+                Tokenizer tokenizer = TokenizerFactory.makeTokenizerASCII(in) ;
+                ParserProfileBase profile = new ParserProfileBase(new Prologue(null, IRIResolver.createNoResolve()), null, LabelToNode.createUseLabelEncoded()) ;
+                LangNTriples parser = new LangNTriples(tokenizer, profile, null) ;
+                return parser ;
+            }
+            
+            public long getEstimatedMemorySize(Triple item)
+            {
+                // TODO
+                return 0 ;
+            }
+        };
+    }
+    
+    public static SerializationFactory<Quad> quadSerializationFactory()
+    {
+        return new SerializationFactory<Quad>()
+        {
+            public Sink<Quad> createSerializer(OutputStream out)
+            {
+                return new SinkQuadOutput(out, null, NodeToLabel.createBNodeByLabelEncoded()) ;
+            }
+            
+            public Iterator<Quad> createDeserializer(InputStream in)
+            {
+                Tokenizer tokenizer = TokenizerFactory.makeTokenizerASCII(in) ;
+                ParserProfileBase profile = new ParserProfileBase(new Prologue(null, IRIResolver.createNoResolve()), null, LabelToNode.createUseLabelEncoded()) ;
+                LangNQuads parser = new LangNQuads(tokenizer, profile, null) ;
+                return parser ;
+            }
+            
+            public long getEstimatedMemorySize(Quad item)
+            {
+                // TODO
+                return 0 ;
+            }
+        };
+    }
+}

Propchange: incubator/jena/Jena2/ARQ/trunk/src/org/openjena/riot/SerializationFactoryFinder.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain

Modified: incubator/jena/Jena2/ARQ/trunk/src/org/openjena/riot/lang/LabelToNode.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src/org/openjena/riot/lang/LabelToNode.java?rev=1158271&r1=1158270&r2=1158271&view=diff
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src/org/openjena/riot/lang/LabelToNode.java (original)
+++ incubator/jena/Jena2/ARQ/trunk/src/org/openjena/riot/lang/LabelToNode.java Tue Aug 16 13:43:27 2011
@@ -11,6 +11,8 @@ import java.util.HashMap ;
 import java.util.Map ;
 
 import org.openjena.riot.SysRIOT ;
+import org.openjena.riot.out.NodeFmtLib ;
+import org.openjena.riot.out.NodeToLabel ;
 import org.openjena.riot.system.MapWithScope ;
 import org.openjena.riot.system.SyntaxLabels ;
 
@@ -32,9 +34,18 @@ public class LabelToNode extends MapWith
     public static LabelToNode createScopeByGraph()
     { return new LabelToNode(new GraphScopePolicy(), nodeMaker) ; }
 
-    /** Allocation using syntax label. */
+    /** Allocation using syntax label; output is unsafe for reading (use 
+     * {@link #createUseLabelEncoded()} for output-input).
+     * The reverse operation is provided by {@link NodeToLabel#createBNodeByLabelAsGiven()} */
     public static LabelToNode createUseLabelAsGiven()
     { return new LabelToNode(new SingleScopePolicy(), nodeMakerByLabel) ; }
+    
+    /** Allocation using an encoded syntax label 
+     * (i.e. _:B&lt;encoded&gt; format from {@link NodeFmtLib#encodeBNodeLabel}).
+     * The reverse operation is provided by {@link NodeToLabel#createBNodeByLabelEncoded()}
+     */
+    public static LabelToNode createUseLabelEncoded()
+    { return new LabelToNode(new SingleScopePolicy(), nodeMakerByLabelEncoded) ; }
 
     /** Allocation, globally scoped, that uses a incrementing field to create new nodes */  
     public static LabelToNode createIncremental()
@@ -112,6 +123,20 @@ public class LabelToNode extends MapWith
 
         public void reset()     {}
     } ;
+    
+    private static Allocator<String, Node> nodeMakerByLabelEncoded = new Allocator<String, Node>()
+    {
+        private long counter = 0 ;
+        
+        public Node create(String label)
+        {
+            if ( label == null )
+                label = SysRIOT.BNodeGenIdPrefix+counter++ ;
+            return Node.createAnon(new AnonId(NodeFmtLib.decodeBNodeLabel(label))) ;
+        }
+
+        public void reset()     {}
+    } ;
 }
 
 /*

Modified: incubator/jena/Jena2/ARQ/trunk/src/org/openjena/riot/out/NodeToLabel.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src/org/openjena/riot/out/NodeToLabel.java?rev=1158271&r1=1158270&r2=1158271&view=diff
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src/org/openjena/riot/out/NodeToLabel.java (original)
+++ incubator/jena/Jena2/ARQ/trunk/src/org/openjena/riot/out/NodeToLabel.java Tue Aug 16 13:43:27 2011
@@ -31,18 +31,18 @@ public class NodeToLabel extends MapWith
 //    { return new NodeToLabel(new GraphScopePolicy(), new AllocatorIncLabel()) ; }
 
     /** Allocation as per internal label, with an encoded safe label. */
-    public static NodeToLabel createBNodeByLabel() 
-    { return new NodeToLabel(new SingleScopePolicy(), new AllocatorInternal()) ; }
+    public static NodeToLabel createBNodeByLabelEncoded() 
+    { return new NodeToLabel(new SingleScopePolicy(), new AllocatorInternalSafe()) ; }
 
     /** Allocation as per internal label */
-    public static NodeToLabel createBNodeByLabelRaw() 
+    public static NodeToLabel createBNodeByLabelAsGiven() 
     { return new NodeToLabel(new SingleScopePolicy(), new AllocatorInternalRaw()) ; }
 
     /** Allocation as per internal label */
     public static NodeToLabel createBNodeByIRI() 
     { return new NodeToLabel(new SingleScopePolicy(), new AllocatorBNodeAsIRI()) ; }
 
-    private static final NodeToLabel _internal = createBNodeByLabel() ;
+    private static final NodeToLabel _internal = createBNodeByLabelEncoded() ;
     public static NodeToLabel labelByInternal() { return _internal ; }  
     
 
@@ -105,10 +105,7 @@ public class NodeToLabel extends MapWith
             return "<"+node.getURI()+">" ;
         }
 
-        protected String labelForBlank(Node node)
-        {
-            return "_:"+NodeFmtLib.encodeBNodeLabel(node.getBlankNodeLabel()) ;
-        }
+        protected abstract String labelForBlank(Node node) ;
 
         protected String labelForLiteral(Node node)
         {
@@ -128,15 +125,18 @@ public class NodeToLabel extends MapWith
         @Override
         protected String labelForBlank(Node node)
         {
-            // NodeFmtLib.safeBNodeLabel adds a "B"
-            // This may not be a legal label if it starts with a number, say.  
             return "_:"+node.getBlankNodeLabel() ;
         }
     }
     
-    private static class AllocatorInternal extends AllocatorBase
+    private static class AllocatorInternalSafe extends AllocatorBase
     {
-       
+        @Override
+        protected String labelForBlank(Node node)
+        {
+            // NodeFmtLib.safeBNodeLabel adds a "B"
+            return "_:"+NodeFmtLib.encodeBNodeLabel(node.getBlankNodeLabel()) ;
+        }
     }
     
     private static class AllocatorIncLabel extends AllocatorBase

Modified: incubator/jena/Jena2/ARQ/trunk/src/org/openjena/riot/out/OutputLangUtils.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src/org/openjena/riot/out/OutputLangUtils.java?rev=1158271&r1=1158270&r2=1158271&view=diff
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src/org/openjena/riot/out/OutputLangUtils.java (original)
+++ incubator/jena/Jena2/ARQ/trunk/src/org/openjena/riot/out/OutputLangUtils.java Tue Aug 16 13:43:27 2011
@@ -105,10 +105,9 @@ public class OutputLangUtils
         if ( node.isBlank() )
         {
             if ( labelPolicy == null )
+                // This is encoded to be safe.
                 labelPolicy = NodeToLabel.labelByInternal() ;
             
-            // N-triples is quite restrictive about the labels. [A-Za-z][A-Za-z0-9]*
-            // Our format is _:B<encoded>
 //            String label = node.getBlankNodeLabel() ;
 //            label = NodeFmtLib.safeBNodeLabel(label) ;
             

Modified: incubator/jena/Jena2/ARQ/trunk/src/riotcmd/CmdLangParse.java
URL: http://svn.apache.org/viewvc/incubator/jena/Jena2/ARQ/trunk/src/riotcmd/CmdLangParse.java?rev=1158271&r1=1158270&r2=1158271&view=diff
==============================================================================
--- incubator/jena/Jena2/ARQ/trunk/src/riotcmd/CmdLangParse.java (original)
+++ incubator/jena/Jena2/ARQ/trunk/src/riotcmd/CmdLangParse.java Tue Aug 16 13:43:27 2011
@@ -262,9 +262,7 @@ public abstract class CmdLangParse exten
         
         NodeToLabel labels = SyntaxLabels.createNodeToLabel() ;
         if ( labelsAsGiven )
-            labels = NodeToLabel.createBNodeByLabel() ;
-        //!!
-        //labels = NodeToLabel.createBNodeByIRI() ;
+            labels = NodeToLabel.createBNodeByLabelEncoded() ;
         
         // Uglyness because quads and triples aren't subtype of some Tuple<Node>
         // That would change a lot (Triples came several years before Quads).