You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ws.apache.org by ve...@apache.org on 2021/11/20 02:51:56 UTC

[ws-axiom] branch master updated: Fix the getElementText implementation

This is an automated email from the ASF dual-hosted git repository.

veithen pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ws-axiom.git


The following commit(s) were added to refs/heads/master by this push:
     new f8eb657  Fix the getElementText implementation
f8eb657 is described below

commit f8eb657ee0c9ee0fd4754ccdc6c96da20dc6de4d
Author: Andreas Veithen <ve...@apache.org>
AuthorDate: Sat Nov 20 02:51:48 2021 +0000

    Fix the getElementText implementation
---
 .../core/stream/stax/pull/output/StAXPivot.java    | 68 +++++++++++++++++-----
 .../stream/stax/pull/output/StAXPivotTest.java     | 45 ++++++++++++++
 2 files changed, 99 insertions(+), 14 deletions(-)

diff --git a/components/core-streams/src/main/java/org/apache/axiom/core/stream/stax/pull/output/StAXPivot.java b/components/core-streams/src/main/java/org/apache/axiom/core/stream/stax/pull/output/StAXPivot.java
index 12e3b98..dca2c69 100644
--- a/components/core-streams/src/main/java/org/apache/axiom/core/stream/stax/pull/output/StAXPivot.java
+++ b/components/core-streams/src/main/java/org/apache/axiom/core/stream/stax/pull/output/StAXPivot.java
@@ -143,10 +143,18 @@ public final class StAXPivot implements InternalXMLStreamReader, XmlHandler {
     private static final int STATE_COLLECT_TEXT = 2;
     
     /**
+     * Used in a CDATA section to indicate that the character data inside the CDATA section should
+     * be coalesced with the character data around the CDATA section. This state is used to
+     * implement {@link XMLStreamReader#getElementText()}. At the end of the CDATA section, the
+     * state transitions back to {@link #STATE_COLLECT_TEXT}.
+     */
+    private static final int STATE_COALESCE_CDATA_SECTION = 3;
+    
+    /**
      * Indicates that all events should be skipped until a start or end element event is
      * encountered. This state is used to implement {@link XMLStreamReader#nextTag()}.
      */
-    private static final int STATE_NEXT_TAG = 3;
+    private static final int STATE_NEXT_TAG = 4;
     
     /**
      * Indicates that all content (character data) in a comment or processing instruction should be
@@ -155,12 +163,12 @@ public final class StAXPivot implements InternalXMLStreamReader, XmlHandler {
      * is used to store the previous state, so that the state can be restored when the end of the
      * comment or processing instruction is reached.
      */
-    private static final int STATE_SKIP_CONTENT = 4;
+    private static final int STATE_SKIP_CONTENT = 5;
     
     /**
      * Indicates that an error has occurred an that the instance is no longer usable.
      */
-    private static final int STATE_ERROR = 5;
+    private static final int STATE_ERROR = 6;
     
     private final XMLStreamReaderExtensionFactory extensionFactory;
     private XmlReader reader;
@@ -347,6 +355,7 @@ public final class StAXPivot implements InternalXMLStreamReader, XmlHandler {
                 state = STATE_EVENT_COMPLETE;
                 return;
             case STATE_COLLECT_TEXT:
+            case STATE_COALESCE_CDATA_SECTION:
                 accumulator.append(data);
                 return;
             case STATE_NEXT_TAG:
@@ -424,23 +433,48 @@ public final class StAXPivot implements InternalXMLStreamReader, XmlHandler {
 
     @Override
     public void startCDATASection() throws StreamException {
-        checkState();
-        eventType = CDATA;
-        startCollectingText();
+        switch (state) {
+            case STATE_DEFAULT:
+                eventType = CDATA;
+                startCollectingText();
+                break;
+            case STATE_COLLECT_TEXT:
+                state = STATE_COALESCE_CDATA_SECTION;
+                break;
+            default:
+                throw new IllegalStateException();
+        }
     }
 
     @Override
     public void endCDATASection() throws StreamException {
-        text = stopCollectingText();
+        switch (state) {
+            case STATE_COLLECT_TEXT:
+                text = stopCollectingText();
+                break;
+            case STATE_COALESCE_CDATA_SECTION:
+                state = STATE_COLLECT_TEXT;
+                break;
+            default:
+                throw new IllegalStateException();
+        }
     }
 
     @Override
     public void processEntityReference(String name, String replacementText) throws StreamException {
-        checkState();
-        eventType = ENTITY_REFERENCE;
-        this.name = name;
-        text = replacementText;
-        state = STATE_EVENT_COMPLETE;
+        switch (state) {
+            case STATE_DEFAULT:
+                eventType = ENTITY_REFERENCE;
+                this.name = name;
+                text = replacementText;
+                state = STATE_EVENT_COMPLETE;
+                break;
+            case STATE_COLLECT_TEXT:
+                accumulator.append(replacementText);
+                break;
+            default:
+                throw new IllegalStateException();
+        }
     }
 
     @Override
@@ -591,8 +625,14 @@ public final class StAXPivot implements InternalXMLStreamReader, XmlHandler {
     @Override
     public String getElementText() throws XMLStreamException {
         startCollectingText();
-        next();
-        return stopCollectingText();
+        switch (next()) {
+            case END_ELEMENT:
+                return stopCollectingText();
+            case START_ELEMENT:
+                throw new XMLStreamException("Element text content may not contain START_ELEMENT");
+            default:
+                throw new IllegalStateException();
+        }
     }
 
     @Override
diff --git a/components/core-streams/src/test/java/org/apache/axiom/core/stream/stax/pull/output/StAXPivotTest.java b/components/core-streams/src/test/java/org/apache/axiom/core/stream/stax/pull/output/StAXPivotTest.java
index 5f6c20c..3af4e32 100644
--- a/components/core-streams/src/test/java/org/apache/axiom/core/stream/stax/pull/output/StAXPivotTest.java
+++ b/components/core-streams/src/test/java/org/apache/axiom/core/stream/stax/pull/output/StAXPivotTest.java
@@ -19,6 +19,7 @@
 package org.apache.axiom.core.stream.stax.pull.output;
 
 import static com.google.common.truth.Truth.assertThat;
+import static org.junit.Assert.assertThrows;
 
 import javax.xml.stream.XMLStreamConstants;
 import javax.xml.stream.XMLStreamException;
@@ -115,4 +116,48 @@ public class StAXPivotTest {
         assertThat(pivot.next()).isEqualTo(XMLStreamReader.END_ELEMENT);
         assertThat(pivot.next()).isEqualTo(XMLStreamReader.END_DOCUMENT);
     }
+
+    @Test
+    public void testGetElementText() throws Exception {
+        StAXPivot pivot = createStAXPivot(
+                Action.DEFAULT_START_DOCUMENT,
+                h -> h.startElement("", "root", ""),
+                XmlHandler::attributesCompleted,
+                h -> h.processCharacterData("abc", false),
+                XmlHandler::startCDATASection,
+                h -> h.processCharacterData("def", false),
+                XmlHandler::endCDATASection,
+                h -> h.processCharacterData("ghi", false),
+                XmlHandler::startComment,
+                h -> h.processCharacterData("jkl", false),
+                XmlHandler::endComment,
+                h -> h.processCharacterData("mno", false),
+                h -> h.startProcessingInstruction("pi"),
+                h -> h.processCharacterData("pqr", false),
+                XmlHandler::endProcessingInstruction,
+                h -> h.processCharacterData("stu", false),
+                h -> h.processEntityReference("ent", "vwx"),
+                h -> h.processCharacterData("yz", false),
+                XmlHandler::endElement,
+                XmlHandler::completed);
+        assertThat(pivot.getEventType()).isEqualTo(XMLStreamReader.START_DOCUMENT);
+        assertThat(pivot.next()).isEqualTo(XMLStreamReader.START_ELEMENT);
+        assertThat(pivot.getElementText()).isEqualTo("abcdefghimnostuvwxyz");
+        assertThat(pivot.getEventType()).isEqualTo(XMLStreamReader.END_ELEMENT);
+        assertThat(pivot.next()).isEqualTo(XMLStreamReader.END_DOCUMENT);
+    }
+
+    @Test
+    public void testGetElementTextUnexpectedChildElement() throws Exception {
+        StAXPivot pivot = createStAXPivot(
+                Action.DEFAULT_START_DOCUMENT,
+                h -> h.startElement("", "root", ""),
+                XmlHandler::attributesCompleted,
+                h -> h.processCharacterData("abc", false),
+                h -> h.startElement("", "child", ""),
+                XmlHandler::attributesCompleted);
+        assertThat(pivot.getEventType()).isEqualTo(XMLStreamReader.START_DOCUMENT);
+        assertThat(pivot.next()).isEqualTo(XMLStreamReader.START_ELEMENT);
+        assertThrows(XMLStreamException.class, () -> pivot.getElementText());
+    }
 }