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 aj...@apache.org on 2006/07/25 16:27:07 UTC

svn commit: r425401 - in /webservices/commons/trunk/modules/axiom: src/org/apache/axiom/om/impl/builder/StAXOMBuilder.java src/org/apache/axiom/om/impl/llom/OMStAXWrapper.java test/org/apache/axiom/om/impl/llom/OMElementGetElementTextTest.java

Author: ajith
Date: Tue Jul 25 07:27:06 2006
New Revision: 425401

URL: http://svn.apache.org/viewvc?rev=425401&view=rev
Log:
1. Implementing the getElementText support as per the API contract
 I. Changed the getElementText impl in OMStAXWrapper.java using the code from the API doc
 II Added a new test case to test this
2. Slightly improved the code in the StAXOMBuilder.java

 

Added:
    webservices/commons/trunk/modules/axiom/test/org/apache/axiom/om/impl/llom/OMElementGetElementTextTest.java
Modified:
    webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/builder/StAXOMBuilder.java
    webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/llom/OMStAXWrapper.java

Modified: webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/builder/StAXOMBuilder.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/builder/StAXOMBuilder.java?rev=425401&r1=425400&r2=425401&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/builder/StAXOMBuilder.java (original)
+++ webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/builder/StAXOMBuilder.java Tue Jul 25 07:27:06 2006
@@ -302,12 +302,12 @@
 
     protected void endElement() {
         if (lastNode.isComplete()) {
-            OMElement parent = (OMElement) lastNode.getParent();
-            ((OMNodeEx) parent).setComplete(true);
+            OMNodeEx parent = (OMNodeEx) lastNode.getParent();
+            parent.setComplete(true);
             lastNode = parent;
         } else {
-            OMElement e = (OMElement) lastNode;
-            ((OMNodeEx) e).setComplete(true);
+            OMNodeEx e = (OMNodeEx) lastNode;
+            e.setComplete(true);
         }
 
         //return lastNode;

Modified: webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/llom/OMStAXWrapper.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/llom/OMStAXWrapper.java?rev=425401&r1=425400&r2=425401&view=diff
==============================================================================
--- webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/llom/OMStAXWrapper.java (original)
+++ webservices/commons/trunk/modules/axiom/src/org/apache/axiom/om/impl/llom/OMStAXWrapper.java Tue Jul 25 07:27:06 2006
@@ -853,13 +853,39 @@
                 throw new OMStreamingException(e);
             }
         } else {
-            if (currentNode.getType() == OMNode.ELEMENT_NODE) {
-                return ((OMElement)currentNode).getText();
-            }else if (currentNode.getType() == OMNode.TEXT_NODE){
-                 return ((OMText)currentNode).getText();
+            ///////////////////////////////////////////////////////
+            //// Code block directly from the API documentation ///
+            if(getEventType() != XMLStreamConstants.START_ELEMENT) {
+                throw new XMLStreamException(
+                        "parser must be on START_ELEMENT to read next text", getLocation());
             }
+            int eventType = next();
+            StringBuffer content = new StringBuffer();
+            while(eventType != XMLStreamConstants.END_ELEMENT ) {
+                if(eventType == XMLStreamConstants.CHARACTERS
+                        || eventType == XMLStreamConstants.CDATA
+                        || eventType == XMLStreamConstants.SPACE
+                        || eventType == XMLStreamConstants.ENTITY_REFERENCE) {
+                    content.append(getText());
+                } else if(eventType == XMLStreamConstants.PROCESSING_INSTRUCTION
+                        || eventType == XMLStreamConstants.COMMENT) {
+                    // skipping
+                } else if(eventType == XMLStreamConstants.END_DOCUMENT) {
+                    throw new XMLStreamException(
+                            "unexpected end of document when reading element text content");
+                } else if(eventType == XMLStreamConstants.START_ELEMENT) {
+                    throw new XMLStreamException(
+                            "element text content may not contain START_ELEMENT");
+                } else {
+                    throw new XMLStreamException(
+                            "Unexpected event type "+eventType, getLocation());
+                }
+                eventType = next();
+            }
+            return content.toString();
+            ///////////////////////////////////////////////////////////////
         }
-        return "";
+
     }
 
     /**

Added: webservices/commons/trunk/modules/axiom/test/org/apache/axiom/om/impl/llom/OMElementGetElementTextTest.java
URL: http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/test/org/apache/axiom/om/impl/llom/OMElementGetElementTextTest.java?rev=425401&view=auto
==============================================================================
--- webservices/commons/trunk/modules/axiom/test/org/apache/axiom/om/impl/llom/OMElementGetElementTextTest.java (added)
+++ webservices/commons/trunk/modules/axiom/test/org/apache/axiom/om/impl/llom/OMElementGetElementTextTest.java Tue Jul 25 07:27:06 2006
@@ -0,0 +1,60 @@
+package org.apache.axiom.om.impl.llom;
+
+import junit.framework.TestCase;
+import org.apache.axiom.om.OMFactory;
+import org.apache.axiom.om.OMAbstractFactory;
+import org.apache.axiom.om.OMNamespace;
+import org.apache.axiom.om.OMElement;
+import org.apache.axiom.om.OMComment;
+
+import javax.xml.stream.XMLStreamException;
+import javax.xml.stream.XMLStreamReader;
+/*
+ * Copyright 2004,2005 The Apache Software Foundation.
+ *
+ * Licensed 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.
+ */
+
+public class OMElementGetElementTextTest extends TestCase {
+
+    protected OMElement documentElement;
+    public void setUp() {
+        OMFactory factory = OMAbstractFactory.getOMFactory();
+
+        OMNamespace namespace = factory.createOMNamespace("http://testuri.org","test");
+        documentElement = factory.createOMElement("DocumentElement",namespace);
+        factory.createOMText(documentElement,"this is a TEXT");
+        factory.createOMComment(documentElement,"this is a comment");
+        factory.createOMText(documentElement,"this is a TEXT block 2");
+
+    }
+
+    /**
+     *
+     */
+     public void testGetElementTextProperly(){
+        try {
+            XMLStreamReader xmlStreamReader = documentElement.getXMLStreamReader();
+            //move to the Start_Element
+            while(xmlStreamReader.getEventType()!=XMLStreamReader.START_ELEMENT){
+                xmlStreamReader.next();
+            }
+
+            String elementText = xmlStreamReader.getElementText();
+            assertEquals("this is a TEXTthis is a TEXT block 2",elementText);
+        } catch (XMLStreamException e) {
+            e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
+        }
+    }
+
+}



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