Added XML serializer
authorVinayak Ravindra Borkar <vinayakb@apache.org>
Wed, 4 Jul 2012 08:48:45 +0000 (08:48 +0000)
committerVinayak Ravindra Borkar <vinayakb@apache.org>
Wed, 4 Jul 2012 08:48:45 +0000 (08:48 +0000)
git-svn-id: https://svn.apache.org/repos/asf/incubator/vxquery/branches/vxquery_algebricks@1357173 13f79535-47bb-0310-9956-ffa450edef68

17 files changed:
vxquery-core/src/main/java/org/apache/vxquery/compiler/algebricks/VXQueryPrinterFactory.java
vxquery-core/src/main/java/org/apache/vxquery/datamodel/accessors/PointableCache.java [new file with mode: 0644]
vxquery-core/src/main/java/org/apache/vxquery/datamodel/accessors/PointablePool.java [new file with mode: 0644]
vxquery-core/src/main/java/org/apache/vxquery/datamodel/accessors/PointablePoolFactory.java [new file with mode: 0644]
vxquery-core/src/main/java/org/apache/vxquery/datamodel/accessors/SequencePointable.java
vxquery-core/src/main/java/org/apache/vxquery/datamodel/accessors/TaggedValuePointable.java
vxquery-core/src/main/java/org/apache/vxquery/datamodel/accessors/atomic/CodedQNamePointable.java
vxquery-core/src/main/java/org/apache/vxquery/datamodel/accessors/nodes/AttributeNodePointable.java
vxquery-core/src/main/java/org/apache/vxquery/datamodel/accessors/nodes/DocumentNodePointable.java
vxquery-core/src/main/java/org/apache/vxquery/datamodel/accessors/nodes/ElementNodePointable.java
vxquery-core/src/main/java/org/apache/vxquery/datamodel/accessors/nodes/NodeTreePointable.java
vxquery-core/src/main/java/org/apache/vxquery/datamodel/accessors/nodes/PINodePointable.java
vxquery-core/src/main/java/org/apache/vxquery/datamodel/accessors/nodes/TextOrCommentNodePointable.java
vxquery-core/src/main/java/org/apache/vxquery/datamodel/builders/base/IDMBuilderPool.java [deleted file]
vxquery-core/src/main/java/org/apache/vxquery/datamodel/builders/nodes/ElementNodeBuilder.java
vxquery-core/src/main/java/org/apache/vxquery/serializer/XMLSerializer.java [new file with mode: 0644]
vxquery-core/src/main/java/org/apache/vxquery/xmlparser/SAXContentHandler.java

index 74643b1..ed891cd 100644 (file)
@@ -1,95 +1,15 @@
 package org.apache.vxquery.compiler.algebricks;
 
-import java.io.PrintStream;
+import org.apache.vxquery.serializer.XMLSerializer;
 
-import org.apache.vxquery.datamodel.accessors.SequencePointable;
-import org.apache.vxquery.datamodel.accessors.TaggedValuePointable;
-import org.apache.vxquery.datamodel.values.ValueTag;
-
-import edu.uci.ics.hyracks.algebricks.common.exceptions.AlgebricksException;
 import edu.uci.ics.hyracks.algebricks.data.IPrinter;
 import edu.uci.ics.hyracks.algebricks.data.IPrinterFactory;
-import edu.uci.ics.hyracks.data.std.primitive.BooleanPointable;
-import edu.uci.ics.hyracks.data.std.primitive.DoublePointable;
-import edu.uci.ics.hyracks.data.std.primitive.LongPointable;
-import edu.uci.ics.hyracks.data.std.primitive.UTF8StringPointable;
-import edu.uci.ics.hyracks.data.std.primitive.VoidPointable;
 
 public class VXQueryPrinterFactory implements IPrinterFactory {
     private static final long serialVersionUID = 1L;
 
     @Override
     public IPrinter createPrinter() {
-        return new IPrinter() {
-            private final TaggedValuePointable tvp = new TaggedValuePointable();
-
-            private final UTF8StringPointable utf8sp = (UTF8StringPointable) UTF8StringPointable.FACTORY
-                    .createPointable();
-
-            private final LongPointable lp = (LongPointable) LongPointable.FACTORY.createPointable();
-
-            private final DoublePointable dp = (DoublePointable) DoublePointable.FACTORY.createPointable();
-
-            private final BooleanPointable bp = (BooleanPointable) BooleanPointable.FACTORY.createPointable();
-
-            private final SequencePointable seqp = new SequencePointable();
-
-            private final VoidPointable vp = (VoidPointable) VoidPointable.FACTORY.createPointable();
-
-            @Override
-            public void print(byte[] b, int s, int l, PrintStream ps) throws AlgebricksException {
-                tvp.set(b, s, l);
-                byte tag = tvp.getTag();
-                switch ((int) tag) {
-                    case ValueTag.XS_STRING_TAG: {
-                        tvp.getValue(utf8sp);
-                        int utfLen = utf8sp.getUTFLength();
-                        int offset = 2;
-                        while (utfLen > 0) {
-                            char c = utf8sp.charAt(offset);
-                            ps.append(c);
-                            int cLen = UTF8StringPointable.getModifiedUTF8Len(c);
-                            offset += cLen;
-                            utfLen -= cLen;
-                        }
-                        break;
-                    }
-
-                    case ValueTag.XS_INTEGER_TAG: {
-                        tvp.getValue(lp);
-                        ps.print(lp.longValue());
-                        break;
-                    }
-
-                    case ValueTag.XS_DOUBLE_TAG: {
-                        tvp.getValue(dp);
-                        ps.print(dp.doubleValue());
-                        break;
-                    }
-
-                    case ValueTag.XS_BOOLEAN_TAG: {
-                        tvp.getValue(bp);
-                        ps.print(bp.getBoolean());
-                        break;
-                    }
-
-                    case ValueTag.SEQUENCE_TAG: {
-                        tvp.getValue(seqp);
-                        int len = seqp.getEntryCount();
-                        for (int i = 0; i < len; ++i) {
-                            if (i > 0) {
-                                ps.append(' ');
-                            }
-                            seqp.getEntry(i, vp);
-                            print(vp.getByteArray(), vp.getStartOffset(), vp.getLength(), ps);
-                        }
-                    }
-                }
-            }
-
-            @Override
-            public void init() throws AlgebricksException {
-            }
-        };
+        return new XMLSerializer();
     }
 }
\ No newline at end of file
diff --git a/vxquery-core/src/main/java/org/apache/vxquery/datamodel/accessors/PointableCache.java b/vxquery-core/src/main/java/org/apache/vxquery/datamodel/accessors/PointableCache.java
new file mode 100644 (file)
index 0000000..c65ad86
--- /dev/null
@@ -0,0 +1,25 @@
+package org.apache.vxquery.datamodel.accessors;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import edu.uci.ics.hyracks.data.std.api.IPointable;
+
+public class PointableCache<T extends IPointable> {
+    private final List<T> list;
+
+    public PointableCache() {
+        list = new ArrayList<T>();
+    }
+
+    public T takeOne() {
+        if (list.isEmpty()) {
+            return null;
+        }
+        return list.remove(list.size() - 1);
+    }
+
+    public void giveBack(T pointable) {
+        list.add(pointable);
+    }
+}
\ No newline at end of file
diff --git a/vxquery-core/src/main/java/org/apache/vxquery/datamodel/accessors/PointablePool.java b/vxquery-core/src/main/java/org/apache/vxquery/datamodel/accessors/PointablePool.java
new file mode 100644 (file)
index 0000000..aeb4126
--- /dev/null
@@ -0,0 +1,40 @@
+package org.apache.vxquery.datamodel.accessors;
+
+import java.util.HashMap;
+import java.util.Map;
+
+import edu.uci.ics.hyracks.data.std.api.IPointable;
+import edu.uci.ics.hyracks.data.std.api.IPointableFactory;
+
+public class PointablePool {
+    private final Map<Class<? extends IPointable>, IPointableFactory> pfMap;
+
+    private final Map<Class<? extends IPointable>, PointableCache<? extends IPointable>> pcMap;
+
+    public PointablePool() {
+        pfMap = new HashMap<Class<? extends IPointable>, IPointableFactory>();
+        pcMap = new HashMap<Class<? extends IPointable>, PointableCache<? extends IPointable>>();
+    }
+
+    public <T extends IPointable> void register(Class<T> klass, IPointableFactory factory) {
+        pfMap.put(klass, factory);
+        pcMap.put(klass, new PointableCache<T>());
+    }
+
+    @SuppressWarnings("unchecked")
+    public <T extends IPointable> T takeOne(Class<T> klass) {
+        PointableCache<T> pc = (PointableCache<T>) pcMap.get(klass);
+        T p = pc.takeOne();
+        if (p != null) {
+            return p;
+        }
+        IPointableFactory pf = pfMap.get(klass);
+        return (T) pf.createPointable();
+    }
+
+    @SuppressWarnings("unchecked")
+    public <T extends IPointable> void giveBack(T pointable) {
+        PointableCache<T> pc = (PointableCache<T>) pcMap.get(pointable.getClass());
+        pc.giveBack(pointable);
+    }
+}
\ No newline at end of file
diff --git a/vxquery-core/src/main/java/org/apache/vxquery/datamodel/accessors/PointablePoolFactory.java b/vxquery-core/src/main/java/org/apache/vxquery/datamodel/accessors/PointablePoolFactory.java
new file mode 100644 (file)
index 0000000..b9b4848
--- /dev/null
@@ -0,0 +1,52 @@
+package org.apache.vxquery.datamodel.accessors;
+
+import org.apache.vxquery.datamodel.accessors.atomic.CodedQNamePointable;
+import org.apache.vxquery.datamodel.accessors.nodes.AttributeNodePointable;
+import org.apache.vxquery.datamodel.accessors.nodes.DocumentNodePointable;
+import org.apache.vxquery.datamodel.accessors.nodes.ElementNodePointable;
+import org.apache.vxquery.datamodel.accessors.nodes.NodeTreePointable;
+import org.apache.vxquery.datamodel.accessors.nodes.PINodePointable;
+import org.apache.vxquery.datamodel.accessors.nodes.TextOrCommentNodePointable;
+
+import edu.uci.ics.hyracks.data.std.primitive.BooleanPointable;
+import edu.uci.ics.hyracks.data.std.primitive.BytePointable;
+import edu.uci.ics.hyracks.data.std.primitive.DoublePointable;
+import edu.uci.ics.hyracks.data.std.primitive.FloatPointable;
+import edu.uci.ics.hyracks.data.std.primitive.IntegerPointable;
+import edu.uci.ics.hyracks.data.std.primitive.LongPointable;
+import edu.uci.ics.hyracks.data.std.primitive.ShortPointable;
+import edu.uci.ics.hyracks.data.std.primitive.UTF8StringPointable;
+import edu.uci.ics.hyracks.data.std.primitive.VoidPointable;
+
+public class PointablePoolFactory {
+    public static final PointablePoolFactory INSTANCE = new PointablePoolFactory();
+
+    private PointablePoolFactory() {
+    }
+
+    public PointablePool createPointablePool() {
+        PointablePool pp = new PointablePool();
+
+        pp.register(TaggedValuePointable.class, TaggedValuePointable.FACTORY);
+        pp.register(BooleanPointable.class, BooleanPointable.FACTORY);
+        pp.register(BytePointable.class, BytePointable.FACTORY);
+        pp.register(ShortPointable.class, ShortPointable.FACTORY);
+        pp.register(IntegerPointable.class, IntegerPointable.FACTORY);
+        pp.register(LongPointable.class, LongPointable.FACTORY);
+        pp.register(FloatPointable.class, FloatPointable.FACTORY);
+        pp.register(DoublePointable.class, DoublePointable.FACTORY);
+        pp.register(UTF8StringPointable.class, UTF8StringPointable.FACTORY);
+        pp.register(SequencePointable.class, SequencePointable.FACTORY);
+        pp.register(VoidPointable.class, VoidPointable.FACTORY);
+        pp.register(CodedQNamePointable.class, CodedQNamePointable.FACTORY);
+
+        pp.register(NodeTreePointable.class, NodeTreePointable.FACTORY);
+        pp.register(DocumentNodePointable.class, DocumentNodePointable.FACTORY);
+        pp.register(ElementNodePointable.class, ElementNodePointable.FACTORY);
+        pp.register(AttributeNodePointable.class, AttributeNodePointable.FACTORY);
+        pp.register(TextOrCommentNodePointable.class, TextOrCommentNodePointable.FACTORY);
+        pp.register(PINodePointable.class, PINodePointable.FACTORY);
+
+        return pp;
+    }
+}
\ No newline at end of file
index 0181cdd..0708c3b 100644 (file)
@@ -1,12 +1,28 @@
 package org.apache.vxquery.datamodel.accessors;
 
+import edu.uci.ics.hyracks.api.dataflow.value.ITypeTraits;
 import edu.uci.ics.hyracks.data.std.api.AbstractPointable;
 import edu.uci.ics.hyracks.data.std.api.IPointable;
+import edu.uci.ics.hyracks.data.std.api.IPointableFactory;
 import edu.uci.ics.hyracks.data.std.primitive.IntegerPointable;
+import edu.uci.ics.hyracks.data.std.primitive.VoidPointable;
 
 public class SequencePointable extends AbstractPointable {
     private static final int ENTRY_COUNT_SIZE = 4;
     private static final int SLOT_SIZE = 4;
+    public static final IPointableFactory FACTORY = new IPointableFactory() {
+        private static final long serialVersionUID = 1L;
+
+        @Override
+        public ITypeTraits getTypeTraits() {
+            return VoidPointable.TYPE_TRAITS;
+        }
+
+        @Override
+        public IPointable createPointable() {
+            return new SequencePointable();
+        }
+    };
 
     public static int getSequenceLength(byte[] bytes, int start) {
         int entryCount = getEntryCount(bytes, start);
index 7ee23bd..04ec9b0 100644 (file)
@@ -1,10 +1,27 @@
 package org.apache.vxquery.datamodel.accessors;
 
+import edu.uci.ics.hyracks.api.dataflow.value.ITypeTraits;
 import edu.uci.ics.hyracks.data.std.api.AbstractPointable;
 import edu.uci.ics.hyracks.data.std.api.IPointable;
+import edu.uci.ics.hyracks.data.std.api.IPointableFactory;
 import edu.uci.ics.hyracks.data.std.primitive.BytePointable;
+import edu.uci.ics.hyracks.data.std.primitive.VoidPointable;
 
 public class TaggedValuePointable extends AbstractPointable {
+    public static final IPointableFactory FACTORY = new IPointableFactory() {
+        private static final long serialVersionUID = 1L;
+
+        @Override
+        public ITypeTraits getTypeTraits() {
+            return VoidPointable.TYPE_TRAITS;
+        }
+
+        @Override
+        public IPointable createPointable() {
+            return new TaggedValuePointable();
+        }
+    };
+
     public byte getTag() {
         return BytePointable.getByte(bytes, start);
     }
index a4a5db6..fbc1096 100644 (file)
@@ -1,6 +1,9 @@
 package org.apache.vxquery.datamodel.accessors.atomic;
 
+import edu.uci.ics.hyracks.api.dataflow.value.ITypeTraits;
 import edu.uci.ics.hyracks.data.std.api.AbstractPointable;
+import edu.uci.ics.hyracks.data.std.api.IPointable;
+import edu.uci.ics.hyracks.data.std.api.IPointableFactory;
 import edu.uci.ics.hyracks.data.std.primitive.IntegerPointable;
 
 public class CodedQNamePointable extends AbstractPointable {
@@ -10,6 +13,34 @@ public class CodedQNamePointable extends AbstractPointable {
     private static final int OFF_NS = 4;
     private static final int OFF_LOCAL = 8;
 
+    public static final ITypeTraits TYPE_TRAITS = new ITypeTraits() {
+        private static final long serialVersionUID = 1L;
+
+        @Override
+        public boolean isFixedLength() {
+            return true;
+        }
+
+        @Override
+        public int getFixedLength() {
+            return SIZE;
+        }
+    };
+
+    public static final IPointableFactory FACTORY = new IPointableFactory() {
+        private static final long serialVersionUID = 1L;
+
+        @Override
+        public ITypeTraits getTypeTraits() {
+            return TYPE_TRAITS;
+        }
+
+        @Override
+        public IPointable createPointable() {
+            return new CodedQNamePointable();
+        }
+    };
+
     public int getPrefixCode() {
         return IntegerPointable.getInteger(bytes, start + OFF_PREFIX);
     }
index aafe684..c3b361c 100644 (file)
@@ -3,8 +3,12 @@ package org.apache.vxquery.datamodel.accessors.nodes;
 import org.apache.vxquery.datamodel.accessors.TaggedValuePointable;
 import org.apache.vxquery.datamodel.accessors.atomic.CodedQNamePointable;
 
+import edu.uci.ics.hyracks.api.dataflow.value.ITypeTraits;
 import edu.uci.ics.hyracks.data.std.api.AbstractPointable;
+import edu.uci.ics.hyracks.data.std.api.IPointable;
+import edu.uci.ics.hyracks.data.std.api.IPointableFactory;
 import edu.uci.ics.hyracks.data.std.primitive.IntegerPointable;
+import edu.uci.ics.hyracks.data.std.primitive.VoidPointable;
 
 /*
  * Attribute {
@@ -16,6 +20,19 @@ import edu.uci.ics.hyracks.data.std.primitive.IntegerPointable;
  */
 public class AttributeNodePointable extends AbstractPointable {
     private static final int LOCAL_NODE_ID_SIZE = 4;
+    public static final IPointableFactory FACTORY = new IPointableFactory() {
+        private static final long serialVersionUID = 1L;
+
+        @Override
+        public ITypeTraits getTypeTraits() {
+            return VoidPointable.TYPE_TRAITS;
+        }
+
+        @Override
+        public IPointable createPointable() {
+            return new AttributeNodePointable();
+        }
+    };
 
     public void getName(CodedQNamePointable name) {
         name.set(bytes, getNameOffset(), getNameSize());
index 4bac393..a156740 100644 (file)
@@ -2,8 +2,12 @@ package org.apache.vxquery.datamodel.accessors.nodes;
 
 import org.apache.vxquery.datamodel.accessors.SequencePointable;
 
+import edu.uci.ics.hyracks.api.dataflow.value.ITypeTraits;
 import edu.uci.ics.hyracks.data.std.api.AbstractPointable;
+import edu.uci.ics.hyracks.data.std.api.IPointable;
+import edu.uci.ics.hyracks.data.std.api.IPointableFactory;
 import edu.uci.ics.hyracks.data.std.primitive.IntegerPointable;
+import edu.uci.ics.hyracks.data.std.primitive.VoidPointable;
 
 /*
  * Document {
@@ -13,6 +17,19 @@ import edu.uci.ics.hyracks.data.std.primitive.IntegerPointable;
  */
 public class DocumentNodePointable extends AbstractPointable {
     private static final int LOCAL_NODE_ID_SIZE = 4;
+    public static final IPointableFactory FACTORY = new IPointableFactory() {
+        private static final long serialVersionUID = 1L;
+
+        @Override
+        public ITypeTraits getTypeTraits() {
+            return VoidPointable.TYPE_TRAITS;
+        }
+
+        @Override
+        public IPointable createPointable() {
+            return new DocumentNodePointable();
+        }
+    };
 
     public int getLocalNodeId(NodeTreePointable nodeTree) {
         return nodeTree.nodeIdExists() ? IntegerPointable.getInteger(bytes, getLocalNodeIdOffset()) : -1;
index dc363e4..d06246c 100644 (file)
@@ -3,9 +3,13 @@ package org.apache.vxquery.datamodel.accessors.nodes;
 import org.apache.vxquery.datamodel.accessors.SequencePointable;
 import org.apache.vxquery.datamodel.accessors.atomic.CodedQNamePointable;
 
+import edu.uci.ics.hyracks.api.dataflow.value.ITypeTraits;
 import edu.uci.ics.hyracks.data.std.api.AbstractPointable;
+import edu.uci.ics.hyracks.data.std.api.IPointable;
+import edu.uci.ics.hyracks.data.std.api.IPointableFactory;
 import edu.uci.ics.hyracks.data.std.primitive.BytePointable;
 import edu.uci.ics.hyracks.data.std.primitive.IntegerPointable;
+import edu.uci.ics.hyracks.data.std.primitive.VoidPointable;
 
 /*
  * Element {
@@ -46,6 +50,19 @@ public class ElementNodePointable extends AbstractPointable {
     private static final int LOCAL_NODE_ID_SIZE = 4;
     private static final int NS_ENTRY_SIZE = 4 * 2;
     private static final int NS_CHUNK_SIZE_SIZE = 4;
+    public static final IPointableFactory FACTORY = new IPointableFactory() {
+        private static final long serialVersionUID = 1L;
+
+        @Override
+        public ITypeTraits getTypeTraits() {
+            return VoidPointable.TYPE_TRAITS;
+        }
+
+        @Override
+        public IPointable createPointable() {
+            return new ElementNodePointable();
+        }
+    };
 
     public boolean nsChunkExists() {
         return (getHeader() & NS_CHUNK_EXISTS_MASK) != 0;
index d0e9e3c..3a44472 100644 (file)
@@ -2,18 +2,22 @@ package org.apache.vxquery.datamodel.accessors.nodes;
 
 import org.apache.vxquery.datamodel.accessors.TaggedValuePointable;
 
+import edu.uci.ics.hyracks.api.dataflow.value.ITypeTraits;
 import edu.uci.ics.hyracks.data.std.algorithms.BinarySearchAlgorithm;
 import edu.uci.ics.hyracks.data.std.api.AbstractPointable;
+import edu.uci.ics.hyracks.data.std.api.IPointable;
+import edu.uci.ics.hyracks.data.std.api.IPointableFactory;
 import edu.uci.ics.hyracks.data.std.collections.api.IValueReferenceVector;
 import edu.uci.ics.hyracks.data.std.primitive.BytePointable;
 import edu.uci.ics.hyracks.data.std.primitive.IntegerPointable;
 import edu.uci.ics.hyracks.data.std.primitive.LongPointable;
 import edu.uci.ics.hyracks.data.std.primitive.UTF8StringPointable;
+import edu.uci.ics.hyracks.data.std.primitive.VoidPointable;
 
 public class NodeTreePointable extends AbstractPointable {
-    public static final int HEADER_NODEID_EXISTS_MASK = 0x01;
-    public static final int HEADER_DICTIONARY_EXISTS_MASK = 0x02;
-    public static final int HEADER_TYPE_EXISTS_MASK = 0x03;
+    public static final int HEADER_NODEID_EXISTS_MASK = (1 << 0);
+    public static final int HEADER_DICTIONARY_EXISTS_MASK = (1 << 1);
+    public static final int HEADER_TYPE_EXISTS_MASK = (1 << 2);
 
     private static final int HEADER_OFFSET = 0;
     private static final int HEADER_SIZE = 1;
@@ -24,6 +28,20 @@ public class NodeTreePointable extends AbstractPointable {
     private static final int IDX_PTR_SLOT_SIZE = 4;
     private static final int SORTED_PTR_SLOT_SIZE = 4;
 
+    public static final IPointableFactory FACTORY = new IPointableFactory() {
+        private static final long serialVersionUID = 1L;
+
+        @Override
+        public ITypeTraits getTypeTraits() {
+            return VoidPointable.TYPE_TRAITS;
+        }
+
+        @Override
+        public IPointable createPointable() {
+            return new NodeTreePointable();
+        }
+    };
+
     private final IValueReferenceVector sortedStringVector = new IValueReferenceVector() {
         @Override
         public int getSize() {
@@ -78,8 +96,8 @@ public class NodeTreePointable extends AbstractPointable {
             throw new IllegalArgumentException(idx + " not within [0, " + nEntries + ")");
         }
         int dataAreaStart = getDictionaryDataAreaStartOffset();
-        int idxSlotValue = IntegerPointable.getInteger(bytes, getDictionaryIndexPointerArrayOffset() + idx
-                * IDX_PTR_SLOT_SIZE);
+        int idxSlotValue = idx == 0 ? 0 : IntegerPointable.getInteger(bytes, getDictionaryIndexPointerArrayOffset()
+                + (idx - 1) * IDX_PTR_SLOT_SIZE);
         int strLen = UTF8StringPointable.getUTFLength(bytes, dataAreaStart + idxSlotValue);
         string.set(bytes, dataAreaStart + idxSlotValue, strLen + 2);
     }
index 405e0c4..729ceb7 100644 (file)
@@ -1,8 +1,12 @@
 package org.apache.vxquery.datamodel.accessors.nodes;
 
+import edu.uci.ics.hyracks.api.dataflow.value.ITypeTraits;
 import edu.uci.ics.hyracks.data.std.api.AbstractPointable;
+import edu.uci.ics.hyracks.data.std.api.IPointable;
+import edu.uci.ics.hyracks.data.std.api.IPointableFactory;
 import edu.uci.ics.hyracks.data.std.primitive.IntegerPointable;
 import edu.uci.ics.hyracks.data.std.primitive.UTF8StringPointable;
+import edu.uci.ics.hyracks.data.std.primitive.VoidPointable;
 
 /*
  * PI {
@@ -13,6 +17,19 @@ import edu.uci.ics.hyracks.data.std.primitive.UTF8StringPointable;
  */
 public class PINodePointable extends AbstractPointable {
     private static final int LOCAL_NODE_ID_SIZE = 4;
+    public static final IPointableFactory FACTORY = new IPointableFactory() {
+        private static final long serialVersionUID = 1L;
+
+        @Override
+        public ITypeTraits getTypeTraits() {
+            return VoidPointable.TYPE_TRAITS;
+        }
+
+        @Override
+        public IPointable createPointable() {
+            return new PINodePointable();
+        }
+    };
 
     public int getLocalNodeId(NodeTreePointable nodeTree) {
         return nodeTree.nodeIdExists() ? IntegerPointable.getInteger(bytes, getLocalNodeIdOffset()) : -1;
@@ -22,7 +39,7 @@ public class PINodePointable extends AbstractPointable {
         target.set(bytes, getTargetOffset(nodeTree), getTargetSize(nodeTree));
     }
 
-    public void getContentCode(NodeTreePointable nodeTree, UTF8StringPointable content) {
+    public void getContent(NodeTreePointable nodeTree, UTF8StringPointable content) {
         content.set(bytes, getContentOffset(nodeTree), getContentSize(nodeTree));
     }
 
index 48ed393..bfd4db5 100644 (file)
@@ -1,24 +1,39 @@
 package org.apache.vxquery.datamodel.accessors.nodes;
 
-import org.apache.vxquery.datamodel.accessors.TaggedValuePointable;
-
+import edu.uci.ics.hyracks.api.dataflow.value.ITypeTraits;
 import edu.uci.ics.hyracks.data.std.api.AbstractPointable;
+import edu.uci.ics.hyracks.data.std.api.IPointable;
+import edu.uci.ics.hyracks.data.std.api.IPointableFactory;
 import edu.uci.ics.hyracks.data.std.primitive.IntegerPointable;
+import edu.uci.ics.hyracks.data.std.primitive.VoidPointable;
 
 /*
  * Text | Comment {
  *  NodeId nodeId?;
- *  TaggedValue value;
+ *  UTF8String value;
  * }
  */
 public class TextOrCommentNodePointable extends AbstractPointable {
     private static final int LOCAL_NODE_ID_SIZE = 4;
+    public static final IPointableFactory FACTORY = new IPointableFactory() {
+        private static final long serialVersionUID = 1L;
+
+        @Override
+        public ITypeTraits getTypeTraits() {
+            return VoidPointable.TYPE_TRAITS;
+        }
+
+        @Override
+        public IPointable createPointable() {
+            return new TextOrCommentNodePointable();
+        }
+    };
 
     public int getLocalNodeId(NodeTreePointable nodeTree) {
         return nodeTree.nodeIdExists() ? IntegerPointable.getInteger(bytes, getLocalNodeIdOffset()) : -1;
     }
 
-    public void getValue(NodeTreePointable nodeTree, TaggedValuePointable value) {
+    public void getValue(NodeTreePointable nodeTree, IPointable value) {
         value.set(bytes, getValueOffset(nodeTree), getValueSize(nodeTree));
     }
 
diff --git a/vxquery-core/src/main/java/org/apache/vxquery/datamodel/builders/base/IDMBuilderPool.java b/vxquery-core/src/main/java/org/apache/vxquery/datamodel/builders/base/IDMBuilderPool.java
deleted file mode 100644 (file)
index 5415d36..0000000
+++ /dev/null
@@ -1,34 +0,0 @@
-package org.apache.vxquery.datamodel.builders.base;
-
-import org.apache.vxquery.datamodel.builders.nodes.AttributeNodeBuilder;
-import org.apache.vxquery.datamodel.builders.nodes.CommentNodeBuilder;
-import org.apache.vxquery.datamodel.builders.nodes.DictionaryBuilder;
-import org.apache.vxquery.datamodel.builders.nodes.ElementNodeBuilder;
-import org.apache.vxquery.datamodel.builders.nodes.PINodeBuilder;
-import org.apache.vxquery.datamodel.builders.nodes.TextNodeBuilder;
-
-public interface IDMBuilderPool {
-    public DictionaryBuilder getDictionaryBuilder();
-
-    public void returnDictionaryBuilder(DictionaryBuilder db);
-
-    public ElementNodeBuilder getElementNodeBuilder();
-
-    public void returnElementNodeBuilder(ElementNodeBuilder enb);
-
-    public AttributeNodeBuilder getAttributeNodeBuilder();
-
-    public void returnAttributeNodeBuilder(AttributeNodeBuilder anb);
-
-    public CommentNodeBuilder getCommentNodeBuilder();
-
-    public void returnCommentNodeBuilder(CommentNodeBuilder cnb);
-
-    public TextNodeBuilder getTextNodeBuilder();
-
-    public void returnTextNodeBuilder(TextNodeBuilder tnb);
-
-    public PINodeBuilder getPINodeBuilder();
-
-    public void returnPINodeBuilder(PINodeBuilder pnb);
-}
\ No newline at end of file
index cd3dbef..df55f7c 100644 (file)
@@ -45,8 +45,8 @@ public class ElementNodeBuilder extends AbstractNodeBuilder {
     public void reset(ArrayBackedValueStorage abvs) throws IOException {
         this.abvs = abvs;
         out = abvs.getDataOutput();
-        headerOffset = abvs.getLength();
         out.write(ValueTag.ELEMENT_NODE_TAG);
+        headerOffset = abvs.getLength();
         out.write(0);
     }
 
diff --git a/vxquery-core/src/main/java/org/apache/vxquery/serializer/XMLSerializer.java b/vxquery-core/src/main/java/org/apache/vxquery/serializer/XMLSerializer.java
new file mode 100644 (file)
index 0000000..24cd016
--- /dev/null
@@ -0,0 +1,358 @@
+package org.apache.vxquery.serializer;
+
+import java.io.PrintStream;
+
+import org.apache.vxquery.datamodel.accessors.PointablePool;
+import org.apache.vxquery.datamodel.accessors.PointablePoolFactory;
+import org.apache.vxquery.datamodel.accessors.SequencePointable;
+import org.apache.vxquery.datamodel.accessors.TaggedValuePointable;
+import org.apache.vxquery.datamodel.accessors.atomic.CodedQNamePointable;
+import org.apache.vxquery.datamodel.accessors.nodes.AttributeNodePointable;
+import org.apache.vxquery.datamodel.accessors.nodes.DocumentNodePointable;
+import org.apache.vxquery.datamodel.accessors.nodes.ElementNodePointable;
+import org.apache.vxquery.datamodel.accessors.nodes.NodeTreePointable;
+import org.apache.vxquery.datamodel.accessors.nodes.PINodePointable;
+import org.apache.vxquery.datamodel.accessors.nodes.TextOrCommentNodePointable;
+import org.apache.vxquery.datamodel.values.ValueTag;
+
+import edu.uci.ics.hyracks.algebricks.common.exceptions.AlgebricksException;
+import edu.uci.ics.hyracks.algebricks.data.IPrinter;
+import edu.uci.ics.hyracks.data.std.primitive.BooleanPointable;
+import edu.uci.ics.hyracks.data.std.primitive.DoublePointable;
+import edu.uci.ics.hyracks.data.std.primitive.LongPointable;
+import edu.uci.ics.hyracks.data.std.primitive.UTF8StringPointable;
+import edu.uci.ics.hyracks.data.std.primitive.VoidPointable;
+
+public class XMLSerializer implements IPrinter {
+    private final PointablePool pp;
+
+    private NodeTreePointable ntp;
+
+    public XMLSerializer() {
+        pp = PointablePoolFactory.INSTANCE.createPointablePool();
+    }
+
+    @Override
+    public void print(byte[] b, int s, int l, PrintStream ps) {
+        TaggedValuePointable tvp = pp.takeOne(TaggedValuePointable.class);
+        try {
+            tvp.set(b, s, l);
+            printTaggedValuePointable(ps, tvp);
+        } finally {
+            pp.giveBack(tvp);
+        }
+    }
+
+    private void printTaggedValuePointable(PrintStream ps, TaggedValuePointable tvp) {
+        byte tag = tvp.getTag();
+        switch ((int) tag) {
+            case ValueTag.XS_STRING_TAG:
+                printString(ps, tvp);
+                break;
+
+            case ValueTag.XS_UNTYPED_ATOMIC_TAG:
+                printString(ps, tvp);
+                break;
+
+            case ValueTag.XS_INTEGER_TAG:
+                printInteger(ps, tvp);
+                break;
+
+            case ValueTag.XS_DOUBLE_TAG:
+                printDouble(ps, tvp);
+                break;
+
+            case ValueTag.XS_BOOLEAN_TAG:
+                printBoolean(ps, tvp);
+                break;
+
+            case ValueTag.SEQUENCE_TAG:
+                printSequence(ps, tvp);
+                break;
+
+            case ValueTag.NODE_TREE_TAG:
+                printNodeTree(ps, tvp);
+                break;
+
+            case ValueTag.DOCUMENT_NODE_TAG:
+                printDocumentNode(ps, tvp);
+                break;
+
+            case ValueTag.ELEMENT_NODE_TAG:
+                printElementNode(ps, tvp);
+                break;
+
+            case ValueTag.ATTRIBUTE_NODE_TAG:
+                printAttributeNode(ps, tvp);
+                break;
+
+            case ValueTag.TEXT_NODE_TAG:
+                printTextNode(ps, tvp);
+                break;
+
+            case ValueTag.COMMENT_NODE_TAG:
+                printCommentNode(ps, tvp);
+                break;
+
+            case ValueTag.PI_NODE_TAG:
+                printPINode(ps, tvp);
+                break;
+
+            default:
+                throw new UnsupportedOperationException("Encountered tag: " + tvp.getTag());
+        }
+    }
+
+    private void printNodeTree(PrintStream ps, TaggedValuePointable tvp) {
+        ntp = pp.takeOne(NodeTreePointable.class);
+        TaggedValuePointable rootTVP = pp.takeOne(TaggedValuePointable.class);
+        try {
+            tvp.getValue(ntp);
+            ntp.getRootNode(rootTVP);
+            printTaggedValuePointable(ps, rootTVP);
+        } finally {
+            pp.giveBack(rootTVP);
+            pp.giveBack(ntp);
+            ntp = null;
+        }
+    }
+
+    private void printPINode(PrintStream ps, TaggedValuePointable tvp) {
+        PINodePointable pnp = pp.takeOne(PINodePointable.class);
+        UTF8StringPointable utf8sp = pp.takeOne(UTF8StringPointable.class);
+        try {
+            tvp.getValue(pnp);
+            ps.append("<?");
+            pnp.getTarget(ntp, utf8sp);
+            printString(ps, utf8sp);
+            ps.append(' ');
+            pnp.getContent(ntp, utf8sp);
+            printString(ps, utf8sp);
+            ps.append("?>");
+        } finally {
+            pp.giveBack(pnp);
+            pp.giveBack(utf8sp);
+        }
+    }
+
+    private void printCommentNode(PrintStream ps, TaggedValuePointable tvp) {
+        TextOrCommentNodePointable tcnp = pp.takeOne(TextOrCommentNodePointable.class);
+        UTF8StringPointable utf8sp = pp.takeOne(UTF8StringPointable.class);
+        try {
+            tvp.getValue(tcnp);
+            tcnp.getValue(ntp, utf8sp);
+            ps.append("<!--");
+            printString(ps, utf8sp);
+            ps.append("-->");
+        } finally {
+            pp.giveBack(tcnp);
+            pp.giveBack(utf8sp);
+        }
+    }
+
+    private void printTextNode(PrintStream ps, TaggedValuePointable tvp) {
+        TextOrCommentNodePointable tcnp = pp.takeOne(TextOrCommentNodePointable.class);
+        UTF8StringPointable utf8sp = pp.takeOne(UTF8StringPointable.class);
+        try {
+            tvp.getValue(tcnp);
+            tcnp.getValue(ntp, utf8sp);
+            printString(ps, utf8sp);
+        } finally {
+            pp.giveBack(tcnp);
+            pp.giveBack(utf8sp);
+        }
+    }
+
+    private void printAttributeNode(PrintStream ps, TaggedValuePointable tvp) {
+        AttributeNodePointable anp = pp.takeOne(AttributeNodePointable.class);
+        CodedQNamePointable cqp = pp.takeOne(CodedQNamePointable.class);
+        UTF8StringPointable utf8sp = pp.takeOne(UTF8StringPointable.class);
+        TaggedValuePointable valueTVP = pp.takeOne(TaggedValuePointable.class);
+        try {
+            tvp.getValue(anp);
+            anp.getName(cqp);
+            printPrefixedQName(ps, cqp, utf8sp);
+            ps.append("=\"");
+            anp.getValue(ntp, valueTVP);
+            printTaggedValuePointable(ps, valueTVP);
+            ps.append('"');
+        } finally {
+            pp.giveBack(valueTVP);
+            pp.giveBack(utf8sp);
+            pp.giveBack(anp);
+            pp.giveBack(cqp);
+        }
+    }
+
+    private void printElementNode(PrintStream ps, TaggedValuePointable tvp) {
+        ElementNodePointable enp = pp.takeOne(ElementNodePointable.class);
+        CodedQNamePointable cqp = pp.takeOne(CodedQNamePointable.class);
+        UTF8StringPointable utf8sp = pp.takeOne(UTF8StringPointable.class);
+        SequencePointable seqp = pp.takeOne(SequencePointable.class);
+        try {
+            tvp.getValue(enp);
+            enp.getName(cqp);
+            ps.append('<');
+            printPrefixedQName(ps, cqp, utf8sp);
+
+            int nsCount = enp.getNamespaceEntryCount(ntp);
+            for (int i = 0; i < nsCount; ++i) {
+                ps.append(" xmlns:");
+                ntp.getString(enp.getNamespacePrefixCode(ntp, i), utf8sp);
+                printString(ps, utf8sp);
+                ps.append("=\"");
+                ntp.getString(enp.getNamespaceURICode(ntp, i), utf8sp);
+                printString(ps, utf8sp);
+                ps.append("\"");
+            }
+
+            enp.getAttributeSequence(ntp, seqp);
+            if (seqp.getByteArray() != null && seqp.getEntryCount() > 0) {
+                ps.append(' ');
+                printSequence(ps, seqp);
+            }
+
+            ps.append('>');
+            enp.getChildrenSequence(ntp, seqp);
+            if (seqp.getByteArray() != null) {
+                printSequence(ps, seqp);
+            }
+            ps.append("</");
+            printPrefixedQName(ps, cqp, utf8sp);
+            ps.append('>');
+        } finally {
+            pp.giveBack(seqp);
+            pp.giveBack(utf8sp);
+            pp.giveBack(cqp);
+            pp.giveBack(enp);
+        }
+    }
+
+    private void printPrefixedQName(PrintStream ps, CodedQNamePointable cqp, UTF8StringPointable utf8sp) {
+        ntp.getString(cqp.getPrefixCode(), utf8sp);
+        if (utf8sp.getStringLength() > 0) {
+            printString(ps, utf8sp);
+            ps.append(':');
+        }
+        ntp.getString(cqp.getLocalCode(), utf8sp);
+        printString(ps, utf8sp);
+    }
+
+    private void printDocumentNode(PrintStream ps, TaggedValuePointable tvp) {
+        DocumentNodePointable dnp = pp.takeOne(DocumentNodePointable.class);
+        SequencePointable seqp = pp.takeOne(SequencePointable.class);
+        try {
+            ps.append("<?xml version=\"1.0\"?>\n");
+            tvp.getValue(dnp);
+            dnp.getContent(ntp, seqp);
+            printSequence(ps, seqp);
+        } finally {
+            pp.giveBack(dnp);
+        }
+    }
+
+    private void printSequence(PrintStream ps, TaggedValuePointable tvp) {
+        SequencePointable seqp = pp.takeOne(SequencePointable.class);
+        try {
+            printSequence(ps, seqp);
+        } finally {
+            pp.giveBack(seqp);
+        }
+    }
+
+    private void printSequence(PrintStream ps, SequencePointable seqp) {
+        VoidPointable vp = pp.takeOne(VoidPointable.class);
+        try {
+            int len = seqp.getEntryCount();
+            for (int i = 0; i < len; ++i) {
+                if (i > 0) {
+                    ps.append(' ');
+                }
+                seqp.getEntry(i, vp);
+                print(vp.getByteArray(), vp.getStartOffset(), vp.getLength(), ps);
+            }
+        } finally {
+            pp.giveBack(vp);
+        }
+    }
+
+    private void printBoolean(PrintStream ps, TaggedValuePointable tvp) {
+        BooleanPointable bp = pp.takeOne(BooleanPointable.class);
+        try {
+            tvp.getValue(bp);
+            ps.print(bp.getBoolean());
+        } finally {
+            pp.giveBack(bp);
+        }
+    }
+
+    private void printDouble(PrintStream ps, TaggedValuePointable tvp) {
+        DoublePointable dp = pp.takeOne(DoublePointable.class);
+        try {
+            tvp.getValue(dp);
+            ps.print(dp.doubleValue());
+        } finally {
+            pp.giveBack(dp);
+        }
+    }
+
+    private void printInteger(PrintStream ps, TaggedValuePointable tvp) {
+        LongPointable lp = pp.takeOne(LongPointable.class);
+        try {
+            tvp.getValue(lp);
+            ps.print(lp.longValue());
+        } finally {
+            pp.giveBack(lp);
+        }
+    }
+
+    private void printString(PrintStream ps, TaggedValuePointable tvp) {
+        UTF8StringPointable utf8sp = pp.takeOne(UTF8StringPointable.class);
+        try {
+            tvp.getValue(utf8sp);
+            printString(ps, utf8sp);
+        } finally {
+            pp.giveBack(utf8sp);
+        }
+    }
+
+    private void printString(PrintStream ps, UTF8StringPointable utf8sp) {
+        int utfLen = utf8sp.getUTFLength();
+        int offset = 2;
+        while (utfLen > 0) {
+            char c = utf8sp.charAt(offset);
+            switch (c) {
+                case '<':
+                    ps.append("&lt;");
+                    break;
+
+                case '>':
+                    ps.append("&gt;");
+                    break;
+
+                case '&':
+                    ps.append("&amp;");
+                    break;
+
+                case '"':
+                    ps.append("&quot;");
+                    break;
+
+                case '\'':
+                    ps.append("&apos;");
+                    break;
+
+                default:
+                    ps.append(c);
+                    break;
+            }
+            int cLen = UTF8StringPointable.getModifiedUTF8Len(c);
+            offset += cLen;
+            utfLen -= cLen;
+        }
+    }
+
+    @Override
+    public void init() throws AlgebricksException {
+    }
+}
\ No newline at end of file
index c375823..7653fa1 100644 (file)
@@ -186,7 +186,9 @@ public class SAXContentHandler implements ContentHandler, LexicalHandler {
                 int aUriCode = db.lookup(atts.getURI(i));
                 String aValue = atts.getValue(i);
                 tempABVS.reset();
-                tempABVS.getDataOutput().writeUTF(aValue);
+                DataOutput tempOut = tempABVS.getDataOutput();
+                tempOut.write(ValueTag.XS_UNTYPED_ATOMIC_TAG);
+                tempOut.writeUTF(aValue);
                 enb.startAttribute(anb);
                 anb.setName(aUriCode, aLocalNameCode, aPrefixCode);
                 if (attachTypes) {