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 2008/12/06 17:26:08 UTC

svn commit: r724004 - in /webservices/commons/trunk/modules/axiom/modules: axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMStAXWrapper.java axiom-tests/src/test/java/org/apache/axiom/om/impl/llom/OMStAXWrapperTest.java

Author: veithen
Date: Sat Dec  6 08:26:08 2008
New Revision: 724004

URL: http://svn.apache.org/viewvc?rev=724004&view=rev
Log:
WSCOMMONS-393: Fixed the getText* methods to make sure that the behavior is consistent, namely that getText() and getTextCharacters() return the same data. In particular getTextCharacters() and related methods now work correctly for comment nodes. Also fixed an issue in getTextCharacters(int, char[], int, int) that triggered ArrayOutOfBoundsExceptions and caused invalid values (for the number of copied chars) to be returned.

Modified:
    webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMStAXWrapper.java
    webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/impl/llom/OMStAXWrapperTest.java

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMStAXWrapper.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMStAXWrapper.java?rev=724004&r1=724003&r2=724004&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMStAXWrapper.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMStAXWrapper.java Sat Dec  6 08:26:08 2008
@@ -358,14 +358,12 @@
      * @see javax.xml.stream.XMLStreamReader#getTextLength()
      */
     public int getTextLength() {
-        int returnLength = 0;
         if (parser != null) {
-            returnLength = parser.getTextLength();
+            return parser.getTextLength();
         } else {
-            OMText textNode = (OMText) getNode();
-            returnLength = textNode.getText().length();
+            String text = getTextFromNode();
+            return text == null ? 0 : text.length();
         }
-        return returnLength;
     }
 
     /**
@@ -373,41 +371,42 @@
      * @see javax.xml.stream.XMLStreamReader#getTextStart()
      */
     public int getTextStart() {
-        int returnLength = 0;
         if (parser != null) {
-            returnLength = parser.getTextStart();
+            return parser.getTextStart();
+        } else {
+            // getTextCharacters always returns a new char array and the start
+            // index is therefore always 0
+            return 0;
         }
-
-        // Note - this has no relevant method in the OM
-        return returnLength;
     }
 
     /**
-     * @param i
-     * @param chars
-     * @param i1
-     * @param i2
+     * @param sourceStart
+     * @param target
+     * @param targetStart
+     * @param length
      * @return Returns int.
      * @throws XMLStreamException
      * @see javax.xml.stream.XMLStreamReader#getTextCharacters(int, char[], int, int)
      */
-    public int getTextCharacters(int i, char[] chars, int i1, int i2)
+    public int getTextCharacters(int sourceStart, char[] target, int targetStart, int length)
             throws XMLStreamException {
-        int returnLength = 0;
         if (parser != null) {
             try {
-                returnLength = parser.getTextCharacters(i, chars, i1, i2);
+                return parser.getTextCharacters(sourceStart, target, targetStart, length);
             } catch (XMLStreamException e) {
                 throw new OMStreamingException(e);
             }
         } else {
-            if (hasText()) {
-                OMText textNode = (OMText) getNode();
-                String str = textNode.getText();
-                str.getChars(i, i + i2, chars, i1);
+            String text = getTextFromNode();
+            if (text != null) {
+                int copied = Math.min(length, text.length()-sourceStart);
+                text.getChars(sourceStart, sourceStart + copied, target, targetStart);
+                return copied;
+            } else {
+                return 0;
             }
         }
-        return returnLength;
     }
 
     /**
@@ -415,17 +414,12 @@
      * @see javax.xml.stream.XMLStreamReader#getTextCharacters()
      */
     public char[] getTextCharacters() {
-        char[] returnArray = null;
         if (parser != null) {
-            returnArray = parser.getTextCharacters();
+            return parser.getTextCharacters();
         } else {
-            if (hasText()) {
-                OMText textNode = (OMText) getNode();
-                String str = textNode.getText();
-                returnArray = str.toCharArray();
-            }
+            String text = getTextFromNode();
+            return text == null ? null : text.toCharArray();
         }
-        return returnArray;
     }
 
     /**
@@ -433,19 +427,23 @@
      * @see javax.xml.stream.XMLStreamReader#getText()
      */
     public String getText() {
-        String returnString = null;
         if (parser != null) {
-            returnString = parser.getText();
+            return parser.getText();
         } else {
-            if (hasText()) {
-                if (getNode() instanceof OMText) {
-                    returnString = ((OMText) getNode()).getText();
-                } else if (getNode() instanceof OMComment) {
-                    returnString = ((OMComment) getNode()).getValue();
-                }
+            return getTextFromNode();
+        }
+    }
+    
+    private String getTextFromNode() {
+        if (hasText()) {
+            OMNode node = getNode();
+            if (node instanceof OMText) {
+                return ((OMText)node).getText();
+            } else if (node instanceof OMComment) {
+                return ((OMComment)node).getValue();
             }
         }
-        return returnString;
+        return null;
     }
 
     /**

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/impl/llom/OMStAXWrapperTest.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/impl/llom/OMStAXWrapperTest.java?rev=724004&r1=724003&r2=724004&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/impl/llom/OMStAXWrapperTest.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-tests/src/test/java/org/apache/axiom/om/impl/llom/OMStAXWrapperTest.java Sat Dec  6 08:26:08 2008
@@ -33,6 +33,7 @@
 import org.apache.axiom.om.OMText;
 import org.apache.axiom.om.impl.builder.StAXOMBuilder;
 import org.apache.axiom.om.impl.llom.factory.OMLinkedListImplFactory;
+import org.apache.axiom.om.impl.llom.util.AXIOMUtil;
 import org.apache.axiom.om.util.StAXUtils;
 
 public class OMStAXWrapperTest extends TestCase {
@@ -92,4 +93,22 @@
         assertTrue(Arrays.equals("hello world".toCharArray(), reader2.getTextCharacters())); // WSCOMMONS-338
         assertEquals(XMLStreamReader.END_ELEMENT, reader2.next());
     }
+    
+    public void testCommentEvent() throws Exception {
+        OMStAXWrapper reader = (OMStAXWrapper)AXIOMUtil.stringToOM("<a><!--comment text--></a>").getXMLStreamReader();
+        assertEquals(XMLStreamReader.START_ELEMENT, reader.next());
+        assertEquals(XMLStreamReader.COMMENT, reader.next());
+        assertEquals("comment text", reader.getText());
+        assertEquals("comment text", new String(reader.getTextCharacters(), reader.getTextStart(), reader.getTextLength()));
+        StringBuffer text = new StringBuffer();
+        char[] buf = new char[5];
+        for (int sourceStart = 0; ; sourceStart += buf.length) {
+            int nCopied = reader.getTextCharacters(sourceStart, buf, 0, buf.length);
+            text.append(buf, 0, nCopied);
+            if (nCopied < buf.length) {
+                break;
+            }
+        }
+        assertEquals("comment text", text.toString());
+    }
 }