You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commons-dev@ws.apache.org by ve...@apache.org on 2009/01/07 21:56:45 UTC

svn commit: r732487 - in /webservices/commons/trunk/modules/axiom/modules: axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ axiom-tests/src/test/java/org/apache/axiom/om/impl/dom/

Author: veithen
Date: Wed Jan  7 12:56:45 2009
New Revision: 732487

URL: http://svn.apache.org/viewvc?rev=732487&view=rev
Log:
WSCOMMONS-434: Implemented DOM's getTextContent and setTextContent methods (based on Xerces code).

Modified:
    webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/DocumentImpl.java
    webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/NodeImpl.java
    webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ParentNode.java
    webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/impl/dom/ElementImplTest.java

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/DocumentImpl.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/DocumentImpl.java?rev=732487&r1=732486&r2=732487&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/DocumentImpl.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/DocumentImpl.java Wed Jan  7 12:56:45 2009
@@ -506,6 +506,14 @@
     * DOM-Level 3 methods
     */
 
+    public String getTextContent() throws DOMException {
+        return null;
+    }
+
+    public void setTextContent(String textContent) throws DOMException {
+        // no-op
+    }
+
     public Node adoptNode(Node node) throws DOMException {
         //OK... I'm cheating here,  a BIG TODO
         return this.importNode(node, true);

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/NodeImpl.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/NodeImpl.java?rev=732487&r1=732486&r2=732487&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/NodeImpl.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/NodeImpl.java Wed Jan  7 12:56:45 2009
@@ -527,13 +527,19 @@
     }
 
     public String getTextContent() throws DOMException {
-        // TODO TODO
-        throw new UnsupportedOperationException("TODO");
+        return getNodeValue();  // overriden in some subclasses
     }
 
-    public void setTextContent(String arg0) throws DOMException {
-        // TODO TODO
-        throw new UnsupportedOperationException("TODO");
+    // internal method taking a StringBuffer in parameter
+    void getTextContent(StringBuffer buf) throws DOMException {
+        String content = getNodeValue();
+        if (content != null) {
+            buf.append(content);
+        }
+    }
+
+    public void setTextContent(String textContent) throws DOMException {
+        setNodeValue(textContent);  // overriden in some subclasses
     }
 
     public boolean isSameNode(Node node) {

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ParentNode.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ParentNode.java?rev=732487&r1=732486&r2=732487&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ParentNode.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-dom/src/main/java/org/apache/axiom/om/impl/dom/ParentNode.java Wed Jan  7 12:56:45 2009
@@ -613,4 +613,50 @@
             }
         }
     }
+    
+    public String getTextContent() throws DOMException {
+        Node child = getFirstChild();
+        if (child != null) {
+            Node next = child.getNextSibling();
+            if (next == null) {
+                return hasTextContent(child) ? ((NodeImpl)child).getTextContent() : "";
+            }
+            StringBuffer buf = new StringBuffer();
+            getTextContent(buf);
+            return buf.toString();
+        } else {
+            return "";
+        }
+    }
+
+    void getTextContent(StringBuffer buf) throws DOMException {
+        Node child = getFirstChild();
+        while (child != null) {
+            if (hasTextContent(child)) {
+                ((NodeImpl)child).getTextContent(buf);
+            }
+            child = child.getNextSibling();
+        }
+    }
+    
+    // internal method returning whether to take the given node's text content
+    private static boolean hasTextContent(Node child) {
+        return child.getNodeType() != Node.COMMENT_NODE &&
+            child.getNodeType() != Node.PROCESSING_INSTRUCTION_NODE /* &&
+            (child.getNodeType() != Node.TEXT_NODE ||
+             ((TextImpl) child).isIgnorableWhitespace() == false)*/;
+    }
+    
+    public void setTextContent(String textContent) throws DOMException {
+        // get rid of any existing children
+        // TODO: there is probably a better way to remove all children
+        Node child;
+        while ((child = getFirstChild()) != null) {
+            removeChild(child);
+        }
+        // create a Text node to hold the given content
+        if (textContent != null && textContent.length() != 0) {
+            addChild(factory.createOMText(textContent));
+        }
+    }
 }

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/impl/dom/ElementImplTest.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/impl/dom/ElementImplTest.java?rev=732487&r1=732486&r2=732487&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/impl/dom/ElementImplTest.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/impl/dom/ElementImplTest.java Wed Jan  7 12:56:45 2009
@@ -26,12 +26,15 @@
 import org.apache.axiom.om.impl.dom.factory.OMDOMFactory;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
+import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 import org.w3c.dom.Text;
+import org.xml.sax.InputSource;
 
 import javax.xml.parsers.DocumentBuilderFactory;
 
 import java.io.ByteArrayOutputStream;
+import java.io.StringReader;
 
 public class ElementImplTest extends OMElementTestBase {
     protected OMFactory getOMFactory() {
@@ -192,4 +195,29 @@
             }
         });
     }
+    
+    public void testGetTextContent() throws Exception {
+        DOMTestUtil.execute(new DOMTestUtil.Test() {
+            public void execute(DocumentBuilderFactory dbf) throws Exception {
+                Document doc = dbf.newDocumentBuilder().parse(new InputSource(
+                        new StringReader("<a>1<!--c--><b>2</b>3</a>")));
+                assertEquals("123", doc.getDocumentElement().getTextContent());
+            }
+        });
+    }
+    
+    public void testSetTextContent() throws Exception {
+        DOMTestUtil.execute(new DOMTestUtil.Test() {
+            public void execute(DocumentBuilderFactory dbf) throws Exception {
+                Document doc = dbf.newDocumentBuilder().parse(new InputSource(
+                        new StringReader("<a>1<!--c--><b>2</b>3</a>")));
+                Element element = doc.getDocumentElement();
+                element.setTextContent("test");
+                Node firstChild = element.getFirstChild();
+                assertTrue(firstChild instanceof Text);
+                assertEquals("test", firstChild.getNodeValue());
+                assertNull(firstChild.getNextSibling());
+            }
+        });
+    }
 }