You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by sk...@apache.org on 2008/03/15 15:45:06 UTC

svn commit: r637421 - in /commons/proper/digester/trunk/src: java/org/apache/commons/digester/NodeCreateRule.java test/org/apache/commons/digester/NodeCreateRuleTestCase.java

Author: skitching
Date: Sat Mar 15 07:45:04 2008
New Revision: 637421

URL: http://svn.apache.org/viewvc?rev=637421&view=rev
Log:
Fix DIGESTER-120: NodeCreateRule loses whitespace when node text includes xml entities.
Thanks to Nguyen Thanh Son Daniel for reporting and diagnosing the problem.

Modified:
    commons/proper/digester/trunk/src/java/org/apache/commons/digester/NodeCreateRule.java
    commons/proper/digester/trunk/src/test/org/apache/commons/digester/NodeCreateRuleTestCase.java

Modified: commons/proper/digester/trunk/src/java/org/apache/commons/digester/NodeCreateRule.java
URL: http://svn.apache.org/viewvc/commons/proper/digester/trunk/src/java/org/apache/commons/digester/NodeCreateRule.java?rev=637421&r1=637420&r2=637421&view=diff
==============================================================================
--- commons/proper/digester/trunk/src/java/org/apache/commons/digester/NodeCreateRule.java (original)
+++ commons/proper/digester/trunk/src/java/org/apache/commons/digester/NodeCreateRule.java Sat Mar 15 07:45:04 2008
@@ -150,12 +150,56 @@
          */
         protected Node top = null;
 
+        /**
+         * The text content of the current top DOM node.
+         */
+        protected StringBuffer topText = new StringBuffer();
+
+
+        // --------------------------------------------- Helper Methods
+
+        /**
+         * Appends a {@link org.w3c.dom.Text Text} node to the current node
+         * if the content reported by the parser is not purely whitespace.
+         */
+        private void addTextIfPresent() throws SAXException {
+        	if (topText.length() > 0) {
+        		String str = topText.toString();
+        		topText.setLength(0);
+
+        		if (str.trim().length() > 0) {
+                    // The contained text is not *pure* whitespace, so create
+                    // a text node to hold it. Note that the "untrimmed" text
+                    // is stored in the node.
+                    try {
+                        top.appendChild(doc.createTextNode(str));
+                    } catch (DOMException e) {
+                        throw new SAXException(e.getMessage());
+                    }
+                }
+            }
+        }
 
         // --------------------------------------------- ContentHandler Methods
 
 
         /**
-         * Appends a {@link org.w3c.dom.Text Text} node to the current node.
+         * Handle notification about text embedded within the current node.
+         * <p>
+         * An xml parser calls this when text is found. We need to ensure that this
+         * text gets attached to the new Node we are creating - except in the case
+         * where the only text in the node is whitespace.
+         * <p>
+         * There is a catch, however. According to the sax specification, a parser
+         * does not need to pass all of the text content of a node in one go; it can
+         * make multiple calls passing part of the data on each call. In particular,
+         * when the body of an element includes xml entity-references, at least some
+         * parsers make a separate call to this method to pass just the entity content.
+         * <p>
+         * In this method, we therefore just append the provided text to a
+         * "current text" buffer. When the element end is found, or a child element
+         * is found then we can check whether we have all-whitespace. See method
+         * addTextIfPresent. 
          * 
          * @param ch the characters from the XML document
          * @param start the start position in the array
@@ -165,15 +209,7 @@
         public void characters(char[] ch, int start, int length)
             throws SAXException {
 
-            try {
-                String str = new String(ch, start, length);
-                if (str.trim().length() > 0) { 
-                    top.appendChild(doc.createTextNode(str));
-                }
-            } catch (DOMException e) {
-                throw new SAXException(e.getMessage());
-            }
-
+        	topText.append(ch, start, length);
         }
 
 
@@ -189,6 +225,8 @@
                                String qName)
             throws SAXException {
             
+            addTextIfPresent();
+
             try {
                 if (depth == 0) {
                     getDigester().setCustomContentHandler(oldContentHandler);
@@ -240,6 +278,8 @@
         public void startElement(String namespaceURI, String localName,
                                  String qName, Attributes atts)
             throws SAXException {
+
+            addTextIfPresent();
 
             try {
                 Node previousTop = top;

Modified: commons/proper/digester/trunk/src/test/org/apache/commons/digester/NodeCreateRuleTestCase.java
URL: http://svn.apache.org/viewvc/commons/proper/digester/trunk/src/test/org/apache/commons/digester/NodeCreateRuleTestCase.java?rev=637421&r1=637420&r2=637421&view=diff
==============================================================================
--- commons/proper/digester/trunk/src/test/org/apache/commons/digester/NodeCreateRuleTestCase.java (original)
+++ commons/proper/digester/trunk/src/test/org/apache/commons/digester/NodeCreateRuleTestCase.java Sat Mar 15 07:45:04 2008
@@ -463,6 +463,28 @@
 
     }
 
+    /**
+     * This unit test checks that text nodes are correctly created when
+     * xml entities are used. In particular, this usually causes the xml
+     * parser to make multiple invocations of the characters(..) sax
+     * callback, rather than just one.
+     */
+    public void testEntityText() throws Exception {
+        String TEST_XML2 =
+            "<?xml version='1.0'?><root><alpha>&#65; &#65;</alpha></root>";
+
+        digester.addRule("root/alpha", new NodeCreateRule());
+        Object result = digester.parse(new StringReader(TEST_XML2));
+
+        assertNotNull(result);
+        assertTrue(result instanceof Element);
+        Element element = (Element)result;
+        assertEquals("alpha", element.getNodeName());
+        assertNull(((Element)element).getLocalName());
+        assertNull(((Element)element).getNamespaceURI());
+        assertEquals(1, element.getChildNodes().getLength());
+        assertEquals("A A", element.getFirstChild().getNodeValue());
+    }
     // ------------------------------------------------ Utility Support Methods