svn commit: r518453 - /xml/xindice/trunk/java/src/org/apache/xindice/xml/dom/DOMCompressor.java

[email protected]
Newsgroups gmane.text.xml.xindice.devel
Message-ID <[email protected]>
Author: vgritsenko
Date: Wed Mar 14 20:16:21 2007
New Revision: 518453

URL: http://svn.apache.org/viewvc?view=rev&rev=518453
Log:
fix bug in writeNode (from bug #41808).
rename Compress to compress.

Modified:
    xml/xindice/trunk/java/src/org/apache/xindice/xml/dom/DOMCompressor.java

Modified: xml/xindice/trunk/java/src/org/apache/xindice/xml/dom/DOMCompressor.java
URL: http://svn.apache.org/viewvc/xml/xindice/trunk/java/src/org/apache/xindice/xml/dom/DOMCompressor.java?view=diff&rev=518453&r1=518452&r2=518453
==============================================================================
--- xml/xindice/trunk/java/src/org/apache/xindice/xml/dom/DOMCompressor.java (original)
+++ xml/xindice/trunk/java/src/org/apache/xindice/xml/dom/DOMCompressor.java Wed Mar 14 20:16:21 2007
@@ -21,17 +21,16 @@
 
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
-import org.apache.xindice.util.XindiceException;
 import org.apache.xindice.xml.Signatures;
 import org.apache.xindice.xml.SymbolTable;
 import org.apache.xindice.xml.XMLCompressedOutput;
 
 import org.w3c.dom.Attr;
+import org.w3c.dom.Document;
 import org.w3c.dom.NamedNodeMap;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 
-import java.io.BufferedOutputStream;
 import java.io.ByteArrayOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
@@ -58,9 +57,19 @@
      * @throws IOException If the write failed
      */
     public void writeNode(Node node) throws IOException {
+        // Check if it's node of ours
         if (node instanceof NodeImpl) {
             NodeImpl impl = (NodeImpl) node;
-            if (!impl.dirty && impl.data != null) {
+            Document doc = node.getOwnerDocument();
+
+            SymbolTable docSymbols = null;
+            if (doc instanceof DocumentImpl) {
+                docSymbols = ((DocumentImpl) doc).getSymbols();
+            }
+
+            // If document is compressed with exact same symbol table
+            // and is not dirty, use its compressed bytes.
+            if (!impl.dirty && impl.data != null && docSymbols == st) {
                 write(impl.data, impl.pos, impl.len);
                 flush();
                 return;
@@ -79,7 +88,7 @@
                     } else {
                         symbolID = st.getSymbol(node.getNodeName(), true);
                     }
-                    
+
                     byte signature = (byte) (Signatures.Elem << 0x6);
                     int attrLen = node.getAttributes().getLength();
                     int attrSize = getSizeType(attrLen);
@@ -265,8 +274,7 @@
      */
     private byte[] buildChildren(Node node) throws IOException {
         ByteArrayOutputStream bos = new ByteArrayOutputStream();
-        BufferedOutputStream buf = new BufferedOutputStream(bos, 4096);
-        DOMCompressor out = new DOMCompressor(buf, st);
+        DOMCompressor out = new DOMCompressor(bos, st);
         if (node.getNodeType() == Node.ELEMENT_NODE) {
             NamedNodeMap attrs = node.getAttributes();
             int len = attrs.getLength();
@@ -281,7 +289,6 @@
         for (int i = 0; i < len; i++) {
             out.writeNode(children.item(i));
         }
-        out.flush();
         return bos.toByteArray();
     }
 
@@ -289,24 +296,33 @@
      * Compress is a convenience method that compresses a Node into a byte
      * array with a single call.
      *
+     * @deprecated renamed to {@link #compress(Node, SymbolTable)}.
+     * @param node The Node to compress
+     * @param symbols The Symbol Table to use
+     * @return The resulting byte array
+     */
+    public static byte[] Compress(Node node, SymbolTable symbols) {
+        return compress(node, symbols);
+    }
+
+    /**
+     * Compress is a convenience method that compresses a Node into a byte
+     * array with a single call.
+     *
      * @param node The Node to compress
      * @param symbols The Symbol Table to use
      * @return The resulting byte array
-     * @throws XindiceException if an Exception occurs
      */
-    public static byte[] Compress(Node node, SymbolTable symbols) throws XindiceException {
-        // FIXME ByteArrayOutputStream throws no IOExceptions, so throws clause on method isn't necessary.
+    public static byte[] compress(Node node, SymbolTable symbols) {
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        DOMCompressor xco = new DOMCompressor(bos, symbols);
+        node.normalize();
         try {
-            ByteArrayOutputStream bos = new ByteArrayOutputStream();
-            BufferedOutputStream buf = new BufferedOutputStream(bos, 4096);
-            DOMCompressor xco = new DOMCompressor(buf, symbols);
-            node.normalize();
             xco.writeNode(node);
-            xco.flush();
-            return bos.toByteArray();
-        } catch (Exception e) {
-            throw new XindiceException("XML Compression Error", e);
+        } catch (IOException e) {
+            // ByteArrayOutputStream does not throw IOException, ignore
         }
+        return bos.toByteArray();
     }
 }
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.