Skip to content
Permalink
Browse files
CXF-8632: StaxTransformFeature deep-dropping doesn't work with any el…
…ement (#887)

* CXF-8632: StaxTransformFeature deep-dropping doesn't work with any element

* Simplifying the handleDeepDrop termination condition (code review comments)
  • Loading branch information
reta committed Jan 9, 2022
1 parent d967092 commit ce50d28d4ac633a70f33f00490966cbdb070ff99
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 5 deletions.
@@ -170,7 +170,7 @@ public int next() throws XMLStreamException {

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 @@ private void handleDefaultMode(QName name, QName expected) {
}

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;
@@ -69,6 +69,116 @@ public void testReplaceSimpleElement() throws Exception {
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 {

0 comments on commit ce50d28

Please sign in to comment.