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 sc...@apache.org on 2008/01/15 16:09:42 UTC

svn commit: r612131 - /webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMElementImpl.java

Author: scheu
Date: Tue Jan 15 07:09:38 2008
New Revision: 612131

URL: http://svn.apache.org/viewvc?rev=612131&view=rev
Log:
WSCOMMONS-294
Contributor: Andreas Veithen
Speedup the getText() method by using a StringBuffer to concatenate the text.

Modified:
    webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMElementImpl.java

Modified: webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMElementImpl.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMElementImpl.java?rev=612131&r1=612130&r2=612131&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMElementImpl.java (original)
+++ webservices/commons/trunk/modules/axiom/modules/axiom-impl/src/main/java/org/apache/axiom/om/impl/llom/OMElementImpl.java Tue Jan 15 07:09:38 2008
@@ -726,28 +726,46 @@
     }
 
     /**
-     * Selects all the text children and concatinates them to a single string.
+     * Selects all the text children and concatenates them to a single string.
      *
      * @return Returns String.
      */
     public String getText() {
-        String childText = "";
+        String childText = null;
+        StringBuffer buffer = null;
         OMNode child = this.getFirstOMChild();
-        OMText textNode;
 
         while (child != null) {
             if (child.getType() == OMNode.TEXT_NODE) {
-                textNode = (OMText) child;
+                OMText textNode = (OMText) child;
                 String textValue = textNode.getText();
-                if (textValue != null &&
-                        !"".equals(textValue)) {
-                    childText += textValue;
+                if (textValue != null && textValue.length() != 0) {
+                    if (childText == null) {
+                        // This is the first non empty text node. Just save the string.
+                        childText = textValue;
+                    } else {
+                        // We've already seen a non empty text node before. Concatenate using
+                        // a StringBuffer.
+                        if (buffer == null) {
+                            // This is the first text node we need to append. Initialize the
+                            // StringBuffer.
+                            buffer = new StringBuffer(childText);
+                        }
+                        buffer.append(textValue);
+                    }
                 }
             }
             child = child.getNextOMSibling();
         }
 
-        return childText;
+        if (childText == null) {
+            // We didn't see any text nodes. Return an empty string.
+            return "";
+        } else if (buffer != null) {
+            return buffer.toString();
+        } else {
+            return childText;
+        }
     }
 
     public QName getTextAsQName() {
@@ -764,23 +782,40 @@
      */
     public String getTrimmedText() {
         String childText = null;
+        StringBuffer buffer = null;
         OMNode child = this.getFirstOMChild();
-        OMText textNode;
 
         while (child != null) {
             if (child.getType() == OMNode.TEXT_NODE) {
-                textNode = (OMText) child;
+                OMText textNode = (OMText) child;
                 String textValue = textNode.getText();
-                if (textValue != null &&
-                        !"".equals(textValue.trim())) {
-                    if (childText == null) childText = "";
-                    childText += textValue.trim();
+                if (textValue != null && textValue.length() != 0) {
+                    if (childText == null) {
+                        // This is the first non empty text node. Just save the string.
+                        childText = textValue.trim();
+                    } else {
+                        // We've already seen a non empty text node before. Concatenate using
+                        // a StringBuffer.
+                        if (buffer == null) {
+                            // This is the first text node we need to append. Initialize the
+                            // StringBuffer.
+                            buffer = new StringBuffer(childText);
+                        }
+                        buffer.append(textValue.trim());
+                    }
                 }
             }
             child = child.getNextOMSibling();
         }
 
-        return childText;
+        if (childText == null) {
+            // We didn't see any text nodes. Return an empty string.
+            return "";
+        } else if (buffer != null) {
+            return buffer.toString();
+        } else {
+            return childText;
+        }
     }
 
     /**



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@ws.apache.org
For additional commands, e-mail: commons-dev-help@ws.apache.org