Author: sergeyb
Date: Mon Oct 3 17:19:23 2011
New Revision: 1178464
URL: http://svn.apache.org/viewvc?rev=1178464&view=rev
Log:
[CXF-3843] Updating InTransformReader to optionally replace a text content
Modified:
cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/transform/InTransformReader.java
cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/transform/InTransformReaderTest.java
Modified:
cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/transform/InTransformReader.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/transform/InTransformReader.java?rev=1178464&r1=1178463&r2=1178464&view=diff
==============================================================================
---
cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/transform/InTransformReader.java
(original)
+++
cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/transform/InTransformReader.java
Mon Oct 3 17:19:23 2011
@@ -49,6 +49,7 @@ public class InTransformReader extends D
private QName currentQName;
private QName pushBackQName;
private QName pushAheadQName;
+ private String replaceText;
private String currentText;
private String pushAheadText;
private List<Integer> attributesIndexes = new ArrayList<Integer>();
@@ -110,8 +111,11 @@ public class InTransformReader extends D
attributesIndexed = false;
final QName theName = super.getName();
final ElementProperty appendProp = inAppendMap.remove(theName);
+ final boolean replaced = appendProp != null &&
theName.equals(appendProp.getName());
+
final boolean dropped = inDropSet.contains(theName);
- if (appendProp != null) {
+ if (appendProp != null && !replaced) {
+
if (appendProp.isChild()) {
// append-post-*
pushAheadQName = appendProp.getName();
@@ -149,12 +153,16 @@ public class InTransformReader extends D
if (appendProp != null && appendProp.isChild()) {
// append-post-*
currentQName = expected;
- } else if (appendProp != null && !appendProp.isChild()) {
+ } else if (appendProp != null && !appendProp.isChild()
+ && !replaced) {
// append-pre-*
pushBackQName = expected;
} else {
// no append
currentQName = expected;
+ if (replaced) {
+ replaceText = appendProp.getText();
+ }
pushElement();
}
} else if (event == XMLStreamConstants.END_ELEMENT) {
@@ -363,14 +371,24 @@ public class InTransformReader extends D
if (currentText != null) {
return currentText;
}
- return super.getText();
+ String superText = super.getText();
+ if (replaceText != null) {
+ superText = replaceText;
+ replaceText = null;
+ }
+ return superText;
}
public char[] getTextCharacters() {
if (currentText != null) {
return currentText.toCharArray();
}
- return super.getTextCharacters();
+ char[] superChars = super.getTextCharacters();
+ if (replaceText != null) {
+ superChars = replaceText.toCharArray();
+ replaceText = null;
+ }
+ return superChars;
}
public int getTextCharacters(int sourceStart, char[] target, int
targetStart, int length)
Modified:
cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/transform/InTransformReaderTest.java
URL:
http://svn.apache.org/viewvc/cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/transform/InTransformReaderTest.java?rev=1178464&r1=1178463&r2=1178464&view=diff
==============================================================================
---
cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/transform/InTransformReaderTest.java
(original)
+++
cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/transform/InTransformReaderTest.java
Mon Oct 3 17:19:23 2011
@@ -55,6 +55,41 @@ public class InTransformReaderTest exten
}
@Test
+ public void testReplaceSimpleElement() throws Exception {
+ InputStream is = new ByteArrayInputStream(
+ "<ns:test
xmlns:ns=\"http://bar\"><ns:a>1</ns:a></ns:test>".getBytes());
+ XMLStreamReader reader = StaxUtils.createXMLStreamReader(is);
+ reader = new InTransformReader(reader,
+ null,
+
Collections.singletonMap("{http://bar}a", "{http://bar}a:1 2 3"),
+ null,
+ null, false);
+
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ StaxUtils.copy(reader, bos);
+ String value = bos.toString();
+ assertEquals("<ns:test xmlns:ns=\"http://bar\"><ns:a>1 2
3</ns:a></ns:test>", value);
+ }
+
+ @Test
+ public void testTransformAndReplaceSimpleElement() throws Exception {
+ InputStream is = new ByteArrayInputStream(
+ "<ns:test
xmlns:ns=\"http://bar\"><ns:a>1</ns:a></ns:test>".getBytes());
+ XMLStreamReader reader = StaxUtils.createXMLStreamReader(is);
+ reader = new InTransformReader(reader,
+
Collections.singletonMap("{http://bar}*", "{http://foo}*"),
+
Collections.singletonMap("{http://bar}a", "{http://bar}a:1 2 3"),
+ null,
+ null, false);
+
+ ByteArrayOutputStream bos = new ByteArrayOutputStream();
+ StaxUtils.copy(reader, bos);
+ String value = bos.toString();
+ assertEquals(
+ "<ps1:test xmlns:ps1=\"http://foo\"><ps1:a>1 2
3</ps1:a></ps1:test>", value);
+ }
+
+ @Test
public void testReadWithParentDefaultNamespace() throws Exception {
InputStream is = new ByteArrayInputStream(
"<test xmlns=\"http://bar\"><ns:subtest
xmlns:ns=\"http://bar1\"/></test>".getBytes());
@@ -211,7 +246,7 @@ public class InTransformReaderTest exten
@Test
- public void testReadWithRepaceAppend() throws Exception {
+ public void testReadWithReplaceAppend() throws Exception {
Map<String, String> transformElements = new HashMap<String, String>();
transformElements.put("requestValue",
"{http://cxf.apache.org/hello_world_soap_http/types}requestType");