You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-commits@xmlgraphics.apache.org by ca...@apache.org on 2007/12/07 02:01:23 UTC

svn commit: r601944 - in /xmlgraphics/batik/trunk: sources/org/apache/batik/dom/ test-resources/org/apache/batik/dom/ test-sources/org/apache/batik/dom/

Author: cam
Date: Thu Dec  6 17:01:23 2007
New Revision: 601944

URL: http://svn.apache.org/viewvc?rev=601944&view=rev
Log:
Support for ElementTraversal (http://www.w3.org/TR/ElementTraversal),
and accompanying test.

Added:
    xmlgraphics/batik/trunk/test-sources/org/apache/batik/dom/ElementTraversalTest.java
Modified:
    xmlgraphics/batik/trunk/sources/org/apache/batik/dom/AbstractElement.java
    xmlgraphics/batik/trunk/sources/org/apache/batik/dom/AbstractParentNode.java
    xmlgraphics/batik/trunk/test-resources/org/apache/batik/dom/unitTesting.xml

Modified: xmlgraphics/batik/trunk/sources/org/apache/batik/dom/AbstractElement.java
URL: http://svn.apache.org/viewvc/xmlgraphics/batik/trunk/sources/org/apache/batik/dom/AbstractElement.java?rev=601944&r1=601943&r2=601944&view=diff
==============================================================================
--- xmlgraphics/batik/trunk/sources/org/apache/batik/dom/AbstractElement.java (original)
+++ xmlgraphics/batik/trunk/sources/org/apache/batik/dom/AbstractElement.java Thu Dec  6 17:01:23 2007
@@ -27,6 +27,7 @@
 import org.w3c.dom.Attr;
 import org.w3c.dom.DOMException;
 import org.w3c.dom.Element;
+import org.w3c.dom.ElementTraversal;
 import org.w3c.dom.NamedNodeMap;
 import org.w3c.dom.Node;
 import org.w3c.dom.TypeInfo;
@@ -40,7 +41,7 @@
  */
 public abstract class AbstractElement
     extends    AbstractParentChildNode
-    implements Element {
+    implements Element, ElementTraversal {
 
     /**
      * The attributes of this element.
@@ -664,6 +665,72 @@
      * Called when an attribute has been removed.
      */
     protected void attrRemoved(Attr node, String oldv) {
+    }
+
+    // ElementTraversal //////////////////////////////////////////////////////
+
+    /**
+     * <b>DOM</b>: Implements {@link ElementTraversal#getFirstElementChild()}.
+     */
+    public Element getFirstElementChild() {
+        Node n = getFirstChild();
+        while (n != null) {
+            if (n.getNodeType() == Node.ELEMENT_NODE) {
+                return (Element) n;
+            }
+            n = n.getNextSibling();
+        }
+        return null;
+    }
+
+    /**
+     * <b>DOM</b>: Implements {@link ElementTraversal#getLastElementChild()}.
+     */
+    public Element getLastElementChild() {
+        Node n = getLastChild();
+        while (n != null) {
+            if (n.getNodeType() == Node.ELEMENT_NODE) {
+                return (Element) n;
+            }
+            n = n.getPreviousSibling();
+        }
+        return null;
+    }
+
+    /**
+     * <b>DOM</b>: Implements {@link ElementTraversal#getNextElementSibling()}.
+     */
+    public Element getNextElementSibling() {
+        Node n = getNextSibling();
+        while (n != null) {
+            if (n.getNodeType() == Node.ELEMENT_NODE) {
+                return (Element) n;
+            }
+            n = n.getNextSibling();
+        }
+        return null;
+    }
+
+    /**
+     * <b>DOM</b>: Implements {@link ElementTraversal#getPreviousElementSibling()}.
+     */
+    public Element getPreviousElementSibling() {
+        Node n = getPreviousSibling();
+        while (n != null) {
+            if (n.getNodeType() == Node.ELEMENT_NODE) {
+                return (Element) n;
+            }
+            n = n.getPreviousSibling();
+        }
+        return (Element) n;
+    }
+
+    /**
+     * <b>DOM</b>: Implements {@link ElementTraversal#getChildElementCount()}.
+     */
+    public int getChildElementCount() {
+        getChildNodes();
+        return childNodes.elementChildren;
     }
 
     /**

Modified: xmlgraphics/batik/trunk/sources/org/apache/batik/dom/AbstractParentNode.java
URL: http://svn.apache.org/viewvc/xmlgraphics/batik/trunk/sources/org/apache/batik/dom/AbstractParentNode.java?rev=601944&r1=601943&r2=601944&view=diff
==============================================================================
--- xmlgraphics/batik/trunk/sources/org/apache/batik/dom/AbstractParentNode.java (original)
+++ xmlgraphics/batik/trunk/sources/org/apache/batik/dom/AbstractParentNode.java Thu Dec  6 17:01:23 2007
@@ -728,6 +728,11 @@
         protected int children;
 
         /**
+         * The number of Element children.
+         */
+        protected int elementChildren;
+
+        /**
          * Creates a new ChildNodes object.
          */
         public ChildNodes() {
@@ -776,6 +781,9 @@
             }
             lastChild = n;
             children++;
+            if (n.getNodeType() == Node.ELEMENT_NODE) {
+                elementChildren++;
+            }
             return n;
         }
 
@@ -792,6 +800,9 @@
                 n.setNextSibling(firstChild);
                 firstChild = n;
                 children++;
+                if (n.getNodeType() == Node.ELEMENT_NODE) {
+                    elementChildren++;
+                }
                 return n;
             }
             if (r == lastChild) {
@@ -801,6 +812,9 @@
                 n.setNextSibling(r);
                 n.setPreviousSibling(ps);
                 children++;
+                if (n.getNodeType() == Node.ELEMENT_NODE) {
+                    elementChildren++;
+                }
                 return n;
             }
 
@@ -812,6 +826,9 @@
                 n.setNextSibling(r);
                 r.setPreviousSibling(n);
                 children++;
+                if (n.getNodeType() == Node.ELEMENT_NODE) {
+                    elementChildren++;
+                }
                 return n;
             }
 
@@ -836,6 +853,12 @@
                 }
                 firstChild.setNextSibling(null);
                 firstChild = n;
+                if (o.getNodeType() == Node.ELEMENT_NODE) {
+                    elementChildren--;
+                }
+                if (n.getNodeType() == Node.ELEMENT_NODE) {
+                    elementChildren++;
+                }
                 return o;
             }
 
@@ -845,6 +868,12 @@
                 t.setNextSibling(n);
                 lastChild.setPreviousSibling(null);
                 lastChild = n;
+                if (o.getNodeType() == Node.ELEMENT_NODE) {
+                    elementChildren--;
+                }
+                if (n.getNodeType() == Node.ELEMENT_NODE) {
+                    elementChildren++;
+                }
                 return o;
             }
 
@@ -861,6 +890,12 @@
                 ns.setPreviousSibling(n);
                 o.setPreviousSibling(null);
                 o.setNextSibling(null);
+                if (o.getNodeType() == Node.ELEMENT_NODE) {
+                    elementChildren--;
+                }
+                if (n.getNodeType() == Node.ELEMENT_NODE) {
+                    elementChildren++;
+                }
                 return o;
             }
             throw createDOMException
@@ -879,11 +914,17 @@
                     firstChild = null;
                     lastChild  = null;
                     children--;
+                    if (n.getNodeType() == Node.ELEMENT_NODE) {
+                        elementChildren--;
+                    }
                     return n;
                 }
                 firstChild = (ExtendedNode)firstChild.getNextSibling();
                 firstChild.setPreviousSibling(null);
                 n.setNextSibling(null);
+                if (n.getNodeType() == Node.ELEMENT_NODE) {
+                    elementChildren--;
+                }
                 children--;
                 return n;
             }
@@ -893,6 +934,9 @@
                 lastChild.setNextSibling(null);
                 n.setPreviousSibling(null);
                 children--;
+                if (n.getNodeType() == Node.ELEMENT_NODE) {
+                    elementChildren--;
+                }
                 return n;
             }
 
@@ -907,6 +951,9 @@
                 n.setPreviousSibling(null);
                 n.setNextSibling(null);
                 children--;
+                if (n.getNodeType() == Node.ELEMENT_NODE) {
+                    elementChildren--;
+                }
                 return n;
             }
             throw createDOMException

Modified: xmlgraphics/batik/trunk/test-resources/org/apache/batik/dom/unitTesting.xml
URL: http://svn.apache.org/viewvc/xmlgraphics/batik/trunk/test-resources/org/apache/batik/dom/unitTesting.xml?rev=601944&r1=601943&r2=601944&view=diff
==============================================================================
--- xmlgraphics/batik/trunk/test-resources/org/apache/batik/dom/unitTesting.xml (original)
+++ xmlgraphics/batik/trunk/test-resources/org/apache/batik/dom/unitTesting.xml Thu Dec  6 17:01:23 2007
@@ -171,4 +171,9 @@
     <test id="DOM3.Text.wholeText" class="org.apache.batik.dom.TextWholeTextTest"/>
     <test id="DOM3.Text.replaceWholeText" class="org.apache.batik.dom.TextReplaceWholeTextTest"/>
     <test id="DOM3.EventTarget.addEventListenerNS" class="org.apache.batik.dom.EventTargetAddEventListenerNSTest"/>
+
+    <!-- ========================================================================== -->
+    <!-- ElementTraversal test                                                      -->
+    <!-- ========================================================================== -->
+    <test id="ElementTraversal" class="org.apache.batik.dom.ElementTraversalTest"/>
 </testSuite>

Added: xmlgraphics/batik/trunk/test-sources/org/apache/batik/dom/ElementTraversalTest.java
URL: http://svn.apache.org/viewvc/xmlgraphics/batik/trunk/test-sources/org/apache/batik/dom/ElementTraversalTest.java?rev=601944&view=auto
==============================================================================
--- xmlgraphics/batik/trunk/test-sources/org/apache/batik/dom/ElementTraversalTest.java (added)
+++ xmlgraphics/batik/trunk/test-sources/org/apache/batik/dom/ElementTraversalTest.java Thu Dec  6 17:01:23 2007
@@ -0,0 +1,119 @@
+/*
+
+   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.batik.dom;
+
+import java.io.StringReader;
+
+import org.apache.batik.dom.AbstractElement;
+import org.apache.batik.dom.util.SAXDocumentFactory;
+import org.apache.batik.test.AbstractTest;
+import org.apache.batik.util.XMLResourceDescriptor;
+
+import org.w3c.dom.Document;
+import org.w3c.dom.ElementTraversal;
+
+/**
+ * Tests the {@link ElementTraversal} interface.
+ *
+ * @author <a href="mailto:cam%40mcc%2eid%2eau">Cameron McCormack</a>
+ * @version $Id$
+ */
+public class ElementTraversalTest extends AbstractTest {
+
+    private String DOC = "<a><b/><c>.<?x?>.</c><d>.<?x?><e/><f/><?x?>.</d><g><h/>.<i/></g></a>";
+
+    public boolean runImplBasic() throws Exception {
+        String parser = XMLResourceDescriptor.getXMLParserClassName();
+        SAXDocumentFactory df = new SAXDocumentFactory(GenericDOMImplementation.getDOMImplementation(), parser);
+        Document doc = df.createDocument("http://example.org/", new StringReader(DOC));
+
+        AbstractElement a = (AbstractElement) doc.getDocumentElement();
+        AbstractElement b = (AbstractElement) a.getFirstChild();
+        AbstractElement c = (AbstractElement) b.getNextSibling();
+        AbstractElement d = (AbstractElement) c.getNextSibling();
+        AbstractElement g = (AbstractElement) d.getNextSibling();
+
+        // (1) Test firstElementChild with no children
+        ensure(1, b.getFirstElementChild() == null);
+
+        // (2) Test firstElementChild with children but no element children
+        ensure(2, c.getFirstElementChild() == null);
+
+        // (3) Test firstElementChild with children but element child is not first
+        AbstractElement e = (AbstractElement) d.getFirstElementChild();
+        ensure(3, e != null && e.getNodeName().equals("e"));
+
+        // (4) Test firstElementChild with children and element child is first
+        AbstractElement h = (AbstractElement) g.getFirstElementChild();
+        ensure(4, h != null && h.getNodeName().equals("h"));
+
+        // (5) Test lastElementChild with no children
+        ensure(5, b.getLastElementChild() == null);
+
+        // (6) Test lastElementChild with children but no element children
+        ensure(6, c.getLastElementChild() == null);
+
+        // (7) Test lastElementChild with children but element child is not last
+        AbstractElement f = (AbstractElement) d.getLastElementChild();
+        ensure(7, f != null && f.getNodeName().equals("f"));
+
+        // (8) Test lastElementChild with children and element child is last
+        AbstractElement i = (AbstractElement) g.getLastElementChild();
+        ensure(8, i != null && i.getNodeName().equals("i"));
+
+        // (9) Test nextElementSibling with no next sibling
+        ensure(9, a.getNextElementSibling() == null);
+
+        // (10) Test nextElementSibling with next siblings but no element next sibling
+        ensure(10, f.getNextElementSibling() == null);
+
+        // (11) Test nextElementSibling with next element sibling but not first
+        ensure(11, h.getNextElementSibling() == i);
+
+        // (12) Test nextElementSibling with next element sibling which is first
+        ensure(12, e.getNextElementSibling() == f);
+
+        // (13) Test previousElementSibling with no previous sibling
+        ensure(13, a.getPreviousElementSibling() == null);
+
+        // (14) Test previousElementSibling with previous siblings but no element previous sibling
+        ensure(14, e.getPreviousElementSibling() == null);
+
+        // (15) Test previousElementSibling with previous element sibling but not first
+        ensure(15, i.getPreviousElementSibling() == h);
+
+        // (16) Test previousElementSibling with previous element sibling which is first
+        ensure(16, f.getPreviousElementSibling() == e);
+
+        // (17-20) Test childElementCount for a few cases
+        ensure(17, a.getChildElementCount() == 4);
+        ensure(18, b.getChildElementCount() == 0);
+        ensure(19, c.getChildElementCount() == 0);
+        ensure(20, d.getChildElementCount() == 2);
+
+        return true;
+    }
+
+    protected void ensure(int subTestNumber, boolean b) {
+        if (!b) {
+            throw new RuntimeException("Assertion failure in sub-test " + subTestNumber);
+        }
+    }
+}