You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cxf.apache.org by re...@apache.org on 2022/01/09 17:05:56 UTC

[cxf] branch master updated: CXF-8632: StaxTransformFeature deep-dropping doesn't work with any element (#887)

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

reta pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/cxf.git


The following commit(s) were added to refs/heads/master by this push:
     new ce50d28  CXF-8632: StaxTransformFeature deep-dropping doesn't work with any element (#887)
ce50d28 is described below

commit ce50d28d4ac633a70f33f00490966cbdb070ff99
Author: Andriy Redko <dr...@gmail.com>
AuthorDate: Sun Jan 9 12:05:44 2022 -0500

    CXF-8632: StaxTransformFeature deep-dropping doesn't work with any element (#887)
    
    * CXF-8632: StaxTransformFeature deep-dropping doesn't work with any element
    
    * Simplifying the handleDeepDrop termination condition (code review comments)
---
 .../cxf/staxutils/transform/InTransformReader.java |  19 +++-
 .../staxutils/transform/InTransformReaderTest.java | 110 +++++++++++++++++++++
 2 files changed, 124 insertions(+), 5 deletions(-)

diff --git a/core/src/main/java/org/apache/cxf/staxutils/transform/InTransformReader.java b/core/src/main/java/org/apache/cxf/staxutils/transform/InTransformReader.java
index bd9b4b5..3b8b477 100644
--- a/core/src/main/java/org/apache/cxf/staxutils/transform/InTransformReader.java
+++ b/core/src/main/java/org/apache/cxf/staxutils/transform/InTransformReader.java
@@ -170,7 +170,7 @@ public class InTransformReader extends DepthXMLStreamReader {
 
             namespaceContext.up();
             final boolean dropped = inDropSet.contains(theName);
-            if (!dropped) {
+            if (!dropped && !pushedAheadEvents.isEmpty()) {
                 List<ParsingEvent> pe = pushedAheadEvents.remove(0);
                 if (null != pe) {
                     if (doDebug) {
@@ -261,13 +261,22 @@ public class InTransformReader extends DepthXMLStreamReader {
     }
 
     private void handleDeepDrop() throws XMLStreamException {
-        final int depth = getDepth();
-        while (depth != getDepth() || super.next() != XMLStreamConstants.END_ELEMENT) {
-            // get to the matching end element event
+        int read = 0;
+        while (hasNext()) {
+            // get to the matching end element event (accounting for all inner elements)
+            final int event = super.next();
+            if (event == XMLStreamConstants.START_ELEMENT) {
+                ++read;
+            } else if (event == XMLStreamConstants.END_ELEMENT) {
+                if (read == 0) {
+                    break; 
+                } else {
+                    --read;
+                }
+            }
         }
     }
 
-
     public Object getProperty(String name) {
         if (INTERN_NAMES.equals(name) || INTERN_NS.equals(name)) {
             return Boolean.FALSE;
diff --git a/core/src/test/java/org/apache/cxf/staxutils/transform/InTransformReaderTest.java b/core/src/test/java/org/apache/cxf/staxutils/transform/InTransformReaderTest.java
index 8a5b9cf..343ae89 100644
--- a/core/src/test/java/org/apache/cxf/staxutils/transform/InTransformReaderTest.java
+++ b/core/src/test/java/org/apache/cxf/staxutils/transform/InTransformReaderTest.java
@@ -69,6 +69,116 @@ public class InTransformReaderTest {
         String value = bos.toString();
         assertEquals("<ns:test xmlns:ns=\"http://bar\"><ns:a>1 2 3</ns:a></ns:test>", value);
     }
+    
+    @Test
+    public void testDropComplexElement() throws Exception {
+        InputStream is = new ByteArrayInputStream(new String(
+              "<emp:EmployeeByNameRequest xmlns:emp=\"http://www.jpworks.com/employee\">"
+            + "<emp:firstname>Alex</emp:firstname>"
+            + "<emp:lastname>Just</emp:lastname>"
+            + "<emp:skippedElement>"
+            + "<age>35</age>"
+            + "</emp:skippedElement>"
+            + "<emp:email>alex.just@nowhere</emp:email>"
+            + "</emp:EmployeeByNameRequest>").getBytes());
+        XMLStreamReader reader = StaxUtils.createXMLStreamReader(is);
+        reader = new InTransformReader(reader,
+                                        null,
+                                        null,
+                                        Collections.singletonList("{http://www.jpworks.com/employee}skippedElement"),
+                                        null, false);
+
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        StaxUtils.copy(reader, bos);
+        String value = bos.toString();
+        assertEquals("<emp:EmployeeByNameRequest xmlns:emp=\"http://www.jpworks.com/employee\">"
+                + "<emp:firstname>Alex</emp:firstname>"
+                + "<emp:lastname>Just</emp:lastname>"
+                + "<age>35</age>"
+                + "<emp:email>alex.just@nowhere</emp:email>"
+                + "</emp:EmployeeByNameRequest>", value);
+    }
+    
+    @Test
+    public void testDropComplexElementByReplacement() throws Exception {
+        InputStream is = new ByteArrayInputStream(new String(
+              "<emp:EmployeeByNameRequest xmlns:emp=\"http://www.jpworks.com/employee\">"
+            + "<emp:firstname>Alex</emp:firstname>"
+            + "<emp:lastname>Just</emp:lastname>"
+            + "<emp:skippedElement>"
+            + "<age>35</age>"
+            + "</emp:skippedElement>"
+            + "<emp:email>alex.just@nowhere</emp:email>"
+            + "</emp:EmployeeByNameRequest>").getBytes());
+        XMLStreamReader reader = StaxUtils.createXMLStreamReader(is);
+        reader = new InTransformReader(reader,
+                                        Collections.singletonMap("{http://www.jpworks.com/employee}skippedElement", ""),
+                                        null,
+                                        null,
+                                        null, false);
+
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        StaxUtils.copy(reader, bos);
+        String value = bos.toString();
+        assertEquals("<emp:EmployeeByNameRequest xmlns:emp=\"http://www.jpworks.com/employee\">"
+                + "<emp:firstname>Alex</emp:firstname>"
+                + "<emp:lastname>Just</emp:lastname>"
+                + "<emp:email>alex.just@nowhere</emp:email>"
+                + "</emp:EmployeeByNameRequest>", value);
+    }
+
+    @Test
+    public void testDropComplexElementLastByReplacement() throws Exception {
+        InputStream is = new ByteArrayInputStream(new String(
+              "<emp:EmployeeByNameRequest xmlns:emp=\"http://www.jpworks.com/employee\">"
+            + "<emp:firstname>Alex</emp:firstname>"
+            + "<emp:lastname>Just</emp:lastname>"
+            + "<emp:skippedElement>"
+            + "<age>35</age>"
+            + "</emp:skippedElement>"
+            + "</emp:EmployeeByNameRequest>").getBytes());
+        XMLStreamReader reader = StaxUtils.createXMLStreamReader(is);
+        reader = new InTransformReader(reader,
+                                        Collections.singletonMap("{http://www.jpworks.com/employee}skippedElement", ""),
+                                        null,
+                                        null,
+                                        null, false);
+
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        StaxUtils.copy(reader, bos);
+        String value = bos.toString();
+        assertEquals("<emp:EmployeeByNameRequest xmlns:emp=\"http://www.jpworks.com/employee\">"
+                + "<emp:firstname>Alex</emp:firstname>"
+                + "<emp:lastname>Just</emp:lastname>"
+                + "</emp:EmployeeByNameRequest>", value);
+    }
+
+    @Test
+    public void testDropSimpleElementByReplacement() throws Exception {
+        InputStream is = new ByteArrayInputStream(new String(
+              "<emp:EmployeeByNameRequest xmlns:emp=\"http://www.jpworks.com/employee\">"
+            + "<emp:firstname>Alex</emp:firstname>"
+            + "<emp:lastname>Just</emp:lastname>"
+            + "<emp:skippedElement>"
+            + "<age>35</age>"
+            + "</emp:skippedElement>"
+            + "</emp:EmployeeByNameRequest>").getBytes());
+        XMLStreamReader reader = StaxUtils.createXMLStreamReader(is);
+        reader = new InTransformReader(reader,
+                                        Collections.singletonMap("age", ""),
+                                        null,
+                                        null,
+                                        null, false);
+
+        ByteArrayOutputStream bos = new ByteArrayOutputStream();
+        StaxUtils.copy(reader, bos);
+        String value = bos.toString();
+        assertEquals("<emp:EmployeeByNameRequest xmlns:emp=\"http://www.jpworks.com/employee\">"
+                + "<emp:firstname>Alex</emp:firstname>"
+                + "<emp:lastname>Just</emp:lastname>"
+                + "<emp:skippedElement/>"
+                + "</emp:EmployeeByNameRequest>", value);
+    }
 
     @Test
     public void testTransformAndReplaceSimpleElement() throws Exception {