Return-Path: X-Original-To: apmail-cxf-commits-archive@www.apache.org Delivered-To: apmail-cxf-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 826286C7B for ; Fri, 29 Jul 2011 13:07:52 +0000 (UTC) Received: (qmail 19108 invoked by uid 500); 29 Jul 2011 13:07:52 -0000 Delivered-To: apmail-cxf-commits-archive@cxf.apache.org Received: (qmail 19056 invoked by uid 500); 29 Jul 2011 13:07:51 -0000 Mailing-List: contact commits-help@cxf.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cxf.apache.org Delivered-To: mailing list commits@cxf.apache.org Received: (qmail 19049 invoked by uid 99); 29 Jul 2011 13:07:51 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 29 Jul 2011 13:07:51 +0000 X-ASF-Spam-Status: No, hits=-1998.0 required=5.0 tests=ALL_TRUSTED,FB_GET_MEDS X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 29 Jul 2011 13:07:42 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 35A05238890D for ; Fri, 29 Jul 2011 13:07:20 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r1152199 - in /cxf/branches/2.4.x-fixes: ./ common/common/src/main/java/org/apache/cxf/staxutils/transform/ common/common/src/test/java/org/apache/cxf/staxutils/resources/ common/common/src/test/java/org/apache/cxf/staxutils/transform/ Date: Fri, 29 Jul 2011 13:07:19 -0000 To: commits@cxf.apache.org From: dkulp@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20110729130720.35A05238890D@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: dkulp Date: Fri Jul 29 13:07:18 2011 New Revision: 1152199 URL: http://svn.apache.org/viewvc?rev=1152199&view=rev Log: Merged revisions 1151894 via svnmerge from https://svn.apache.org/repos/asf/cxf/trunk ........ r1151894 | ay | 2011-07-28 11:22:40 -0400 (Thu, 28 Jul 2011) | 1 line [CXF-3692] enhacing inTransformReader's element features ........ Added: cxf/branches/2.4.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/transform/ElementProperty.java - copied unchanged from r1151894, cxf/trunk/common/common/src/main/java/org/apache/cxf/staxutils/transform/ElementProperty.java cxf/branches/2.4.x-fixes/common/common/src/test/java/org/apache/cxf/staxutils/resources/AddRequestIn1.xml - copied unchanged from r1151894, cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/resources/AddRequestIn1.xml cxf/branches/2.4.x-fixes/common/common/src/test/java/org/apache/cxf/staxutils/resources/AddRequestIn1nospace.xml - copied unchanged from r1151894, cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/resources/AddRequestIn1nospace.xml cxf/branches/2.4.x-fixes/common/common/src/test/java/org/apache/cxf/staxutils/resources/AddRequestIn2.xml - copied unchanged from r1151894, cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/resources/AddRequestIn2.xml cxf/branches/2.4.x-fixes/common/common/src/test/java/org/apache/cxf/staxutils/resources/AddRequestIn2nospace.xml - copied unchanged from r1151894, cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/resources/AddRequestIn2nospace.xml cxf/branches/2.4.x-fixes/common/common/src/test/java/org/apache/cxf/staxutils/resources/amazonIn1.xml - copied unchanged from r1151894, cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/resources/amazonIn1.xml cxf/branches/2.4.x-fixes/common/common/src/test/java/org/apache/cxf/staxutils/resources/amazonIn1nospace.xml - copied unchanged from r1151894, cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/resources/amazonIn1nospace.xml cxf/branches/2.4.x-fixes/common/common/src/test/java/org/apache/cxf/staxutils/resources/complexReq1.xml - copied unchanged from r1151894, cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/resources/complexReq1.xml cxf/branches/2.4.x-fixes/common/common/src/test/java/org/apache/cxf/staxutils/resources/complexReq1partial.xml - copied unchanged from r1151894, cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/resources/complexReq1partial.xml cxf/branches/2.4.x-fixes/common/common/src/test/java/org/apache/cxf/staxutils/resources/complexReq2.xml - copied unchanged from r1151894, cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/resources/complexReq2.xml cxf/branches/2.4.x-fixes/common/common/src/test/java/org/apache/cxf/staxutils/resources/complexReq2partial.xml - copied unchanged from r1151894, cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/resources/complexReq2partial.xml cxf/branches/2.4.x-fixes/common/common/src/test/java/org/apache/cxf/staxutils/resources/complexReq3.xml - copied unchanged from r1151894, cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/resources/complexReq3.xml cxf/branches/2.4.x-fixes/common/common/src/test/java/org/apache/cxf/staxutils/resources/complexReq3partial.xml - copied unchanged from r1151894, cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/resources/complexReq3partial.xml cxf/branches/2.4.x-fixes/common/common/src/test/java/org/apache/cxf/staxutils/resources/complexReq4partial.xml - copied unchanged from r1151894, cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/resources/complexReq4partial.xml cxf/branches/2.4.x-fixes/common/common/src/test/java/org/apache/cxf/staxutils/resources/complexReqIn1.xml - copied unchanged from r1151894, cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/resources/complexReqIn1.xml cxf/branches/2.4.x-fixes/common/common/src/test/java/org/apache/cxf/staxutils/resources/complexReqIn2.xml - copied unchanged from r1151894, cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/resources/complexReqIn2.xml cxf/branches/2.4.x-fixes/common/common/src/test/java/org/apache/cxf/staxutils/resources/complexReqIn3.xml - copied unchanged from r1151894, cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/resources/complexReqIn3.xml cxf/branches/2.4.x-fixes/common/common/src/test/java/org/apache/cxf/staxutils/resources/complexReqIn4.xml - copied unchanged from r1151894, cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/resources/complexReqIn4.xml cxf/branches/2.4.x-fixes/common/common/src/test/java/org/apache/cxf/staxutils/resources/greetMeReq.xml - copied unchanged from r1151894, cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/resources/greetMeReq.xml cxf/branches/2.4.x-fixes/common/common/src/test/java/org/apache/cxf/staxutils/resources/greetMeReqIn1.xml - copied unchanged from r1151894, cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/resources/greetMeReqIn1.xml cxf/branches/2.4.x-fixes/common/common/src/test/java/org/apache/cxf/staxutils/resources/greetMeReqIn2.xml - copied unchanged from r1151894, cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/resources/greetMeReqIn2.xml cxf/branches/2.4.x-fixes/common/common/src/test/java/org/apache/cxf/staxutils/resources/greetMeReqIn3.xml - copied unchanged from r1151894, cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/resources/greetMeReqIn3.xml cxf/branches/2.4.x-fixes/common/common/src/test/java/org/apache/cxf/staxutils/resources/greetMeReqIn4.xml - copied unchanged from r1151894, cxf/trunk/common/common/src/test/java/org/apache/cxf/staxutils/resources/greetMeReqIn4.xml Modified: cxf/branches/2.4.x-fixes/ (props changed) cxf/branches/2.4.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/transform/InTransformReader.java cxf/branches/2.4.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/transform/QNamesMap.java cxf/branches/2.4.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/transform/TransformUtils.java cxf/branches/2.4.x-fixes/common/common/src/test/java/org/apache/cxf/staxutils/transform/InTransformReaderTest.java Propchange: cxf/branches/2.4.x-fixes/ ------------------------------------------------------------------------------ Binary property 'svnmerge-integrated' - no diff available. Modified: cxf/branches/2.4.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/transform/InTransformReader.java URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/transform/InTransformReader.java?rev=1152199&r1=1152198&r2=1152199&view=diff ============================================================================== --- cxf/branches/2.4.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/transform/InTransformReader.java (original) +++ cxf/branches/2.4.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/transform/InTransformReader.java Fri Jul 29 13:07:18 2011 @@ -18,9 +18,15 @@ */ package org.apache.cxf.staxutils.transform; +import java.util.ArrayList; import java.util.HashMap; +import java.util.HashSet; +import java.util.List; import java.util.Map; +import java.util.Set; +import java.util.Stack; +import javax.xml.XMLConstants; import javax.xml.namespace.NamespaceContext; import javax.xml.namespace.QName; import javax.xml.stream.XMLStreamConstants; @@ -34,25 +40,38 @@ public class InTransformReader extends D private static final String INTERN_NAMES = "org.codehaus.stax2.internNames"; private static final String INTERN_NS = "org.codehaus.stax2.internNsUris"; + private Stack elementStack = new Stack(); private QNamesMap inElementsMap; - private Map inAppendMap = new HashMap(5); + private QNamesMap inAttributesMap; + private Map inAppendMap = new HashMap(5); + private Set inDropSet = new HashSet(5); private Map nsMap = new HashMap(5); private QName currentQName; - private QName previousQName; + private QName pushBackQName; + private QName pushAheadQName; + private String currentText; + private String pushAheadText; + private List attributesIndexes = new ArrayList(); private int previousDepth = -1; private boolean blockOriginalReader = true; + private boolean attributesIndexed; private DelegatingNamespaceContext namespaceContext; - private boolean appendInProgress; - + public InTransformReader(XMLStreamReader reader, - Map inMap, + Map inEMap, Map appendMap, + List dropESet, + Map inAMap, boolean blockOriginalReader) { super(reader); - inElementsMap = new QNamesMap(inMap == null ? 0 : inMap.size()); + inElementsMap = new QNamesMap(inEMap == null ? 0 : inEMap.size()); + inAttributesMap = new QNamesMap(inAMap == null ? 0 : inAMap.size()); this.blockOriginalReader = blockOriginalReader; - TransformUtils.convertToQNamesMap(inMap, inElementsMap, nsMap); - TransformUtils.convertToMapOfQNames(appendMap, inAppendMap); + TransformUtils.convertToQNamesMap(inEMap, inElementsMap, nsMap); + TransformUtils.convertToQNamesMap(inAMap, inAttributesMap, null); + + TransformUtils.convertToMapOfElementProperties(appendMap, inAppendMap); + TransformUtils.convertToSetOfQNames(dropESet, inDropSet); namespaceContext = new DelegatingNamespaceContext( reader.getNamespaceContext(), nsMap); } @@ -65,19 +84,112 @@ public class InTransformReader extends D } public int next() throws XMLStreamException { - if (currentQName != null && appendInProgress) { - appendInProgress = false; + if (isAtText()) { + resetCurrentText(); + return currentText != null + ? XMLStreamConstants.CHARACTERS : XMLStreamConstants.END_ELEMENT; + } else if (isAtPushedQName()) { + resetCurrentQName(); + pushElement(); return XMLStreamConstants.START_ELEMENT; - } else if (previousDepth != -1 && previousDepth == getDepth() + 1) { + } else if (isAtMarkedDepth()) { previousDepth = -1; + popElement(); return XMLStreamConstants.END_ELEMENT; } else { - return super.next(); + final int event = super.next(); + if (event == XMLStreamConstants.START_ELEMENT) { + attributesIndexed = false; + final QName theName = super.getName(); + final ElementProperty appendProp = inAppendMap.remove(theName); + final boolean dropped = inDropSet.contains(theName); + if (appendProp != null) { + if (appendProp.isChild()) { + // append-post-* + pushAheadQName = appendProp.getName(); + } else { + // append-pre-* + currentQName = appendProp.getName(); + } + if (appendProp.getText() != null) { + // append-*-include + pushAheadText = appendProp.getText(); + } else { + // append-*-wrap + previousDepth = getDepth(); + pushElement(); + } + } else if (dropped) { + // unwrap the current element (shallow drop) + previousDepth = getDepth(); + return super.next(); + } + + QName expected = inElementsMap.get(theName); + if (expected == null) { + expected = theName; + } else if (isEmpty(expected)) { + // drop the current element (deep drop) + final int depth = getDepth(); + while (depth != getDepth() || super.next() != XMLStreamConstants.END_ELEMENT) { + // get to the matching end element event + } + popElement(); + return XMLStreamConstants.END_ELEMENT; + } + + if (appendProp != null && appendProp.isChild()) { + // append-post-* + currentQName = expected; + } else if (appendProp != null && !appendProp.isChild()) { + // append-pre-* + pushBackQName = expected; + } else { + // no append + currentQName = expected; + pushElement(); + } + } else if (event == XMLStreamConstants.END_ELEMENT) { + QName theName = super.getName(); + boolean dropped = inDropSet.contains(theName); + if (dropped) { + super.next(); + } + popElement(); + } else { + // reset the element context and content + currentQName = null; + currentText = null; + } + return event; } } + + private boolean isAtText() { + return pushAheadQName == null && (pushAheadText != null || currentText != null); + } + + private boolean isAtPushedQName() { + return pushBackQName != null || pushAheadQName != null; + } - public Object getProperty(String name) throws IllegalArgumentException { + private boolean isAtMarkedDepth() { + return previousDepth != -1 && previousDepth == getDepth() + 1; + } + + private boolean isEmpty(QName qname) { + return XMLConstants.NULL_NS_URI.equals(qname.getNamespaceURI()) && "".equals(qname.getLocalPart()); + } + + private void popElement() { + currentQName = elementStack.empty() ? null : elementStack.pop(); + } + + private void pushElement() { + elementStack.push(currentQName); + } + public Object getProperty(String name) throws IllegalArgumentException { if (INTERN_NAMES.equals(name) || INTERN_NS.equals(name)) { return Boolean.FALSE; } @@ -85,25 +197,24 @@ public class InTransformReader extends D } public String getLocalName() { - QName cQName = getCurrentName(); - if (cQName != null) { - String name = cQName.getLocalPart(); - resetCurrentQName(); - return name; + if (currentQName != null) { + return currentQName.getLocalPart(); + } else { + return super.getLocalName(); } - return super.getLocalName(); } - private QName getCurrentName() { - return currentQName != null ? currentQName - : previousQName != null ? previousQName : null; - } - private void resetCurrentQName() { - currentQName = previousQName; - previousQName = null; + currentQName = pushBackQName != null ? pushBackQName : pushAheadQName; + pushBackQName = null; + pushAheadQName = null; } + private void resetCurrentText() { + currentText = pushAheadText; + pushAheadText = null; + } + public NamespaceContext getNamespaceContext() { return namespaceContext; } @@ -138,24 +249,13 @@ public class InTransformReader extends D } public String getNamespaceURI() { - - QName theName = readCurrentElement(); - QName appendQName = inAppendMap.remove(theName); - if (appendQName != null) { - appendInProgress = true; - previousDepth = getDepth(); - previousQName = theName; - currentQName = appendQName; + if (currentQName != null) { return currentQName.getNamespaceURI(); + } else { + return super.getNamespaceURI(); } - QName expected = inElementsMap.get(theName); - if (expected == null) { - return theName.getNamespaceURI(); - } - currentQName = expected; - return currentQName.getNamespaceURI(); } - + private QName readCurrentElement() { if (currentQName != null) { return currentQName; @@ -169,4 +269,149 @@ public class InTransformReader extends D public QName getName() { return new QName(getNamespaceURI(), getLocalName()); } + + public int getAttributeCount() { + if (pushBackQName != null) { + return 0; + } + checkAttributeIndexRange(-1); + return attributesIndexes.size(); + } + + public String getAttributeLocalName(int arg0) { + if (pushBackQName != null) { + throwIndexException(arg0, 0); + } + checkAttributeIndexRange(arg0); + + return getAttributeName(arg0).getLocalPart(); + } + + public QName getAttributeName(int arg0) { + if (pushBackQName != null) { + throwIndexException(arg0, 0); + } + checkAttributeIndexRange(arg0); + QName aname = super.getAttributeName(attributesIndexes.get(arg0)); + QName expected = inAttributesMap.get(aname); + + return expected == null ? aname : expected; + } + + public String getAttributeNamespace(int arg0) { + if (pushBackQName != null) { + throwIndexException(arg0, 0); + } + checkAttributeIndexRange(arg0); + + return getAttributeName(arg0).getNamespaceURI(); + } + + public String getAttributePrefix(int arg0) { + if (pushBackQName != null) { + throwIndexException(arg0, 0); + } + checkAttributeIndexRange(arg0); + + QName aname = getAttributeName(arg0); + if (XMLConstants.NULL_NS_URI.equals(aname.getNamespaceURI())) { + return ""; + } else { + String actualNs = nsMap.get(aname.getNamespaceURI()); + if (actualNs != null) { + return namespaceContext.findUniquePrefix(actualNs); + } else { + return namespaceContext.getPrefix(aname.getNamespaceURI()); + } + } + } + + public String getAttributeType(int arg0) { + if (pushBackQName != null) { + throwIndexException(arg0, 0); + } + checkAttributeIndexRange(arg0); + return super.getAttributeType(attributesIndexes.get(arg0)); + } + + public String getAttributeValue(int arg0) { + if (pushBackQName != null) { + throwIndexException(arg0, 0); + } + checkAttributeIndexRange(arg0); + return super.getAttributeValue(attributesIndexes.get(arg0)); + } + + public String getAttributeValue(String namespace, String localName) { + if (pushBackQName != null) { + return null; + } + checkAttributeIndexRange(-1); + //TODO need reverse lookup + return super.getAttributeValue(namespace, localName); + } + + public String getText() { + if (currentText != null) { + return currentText; + } + return super.getText(); + } + + public char[] getTextCharacters() { + if (currentText != null) { + return currentText.toCharArray(); + } + return super.getTextCharacters(); + } + + public int getTextCharacters(int sourceStart, char[] target, int targetStart, int length) + throws XMLStreamException { + if (currentText != null) { + int len = currentText.length() - sourceStart; + if (len > length) { + len = length; + } + currentText.getChars(sourceStart, sourceStart + len, target, targetStart); + return len; + } + + return super.getTextCharacters(sourceStart, target, targetStart, length); + } + + public int getTextLength() { + if (currentText != null) { + return currentText.length(); + } + return super.getTextLength(); + } + + /** + * Checks the index range for the current attributes set. + * If the attributes are not indexed for the current element context, they + * will be indexed. + * @param index + */ + private void checkAttributeIndexRange(int index) { + if (!attributesIndexed) { + attributesIndexes.clear(); + final int c = super.getAttributeCount(); + for (int i = 0; i < c; i++) { + QName aname = super.getAttributeName(i); + QName expected = inAttributesMap.get(aname); + if (expected == null || !isEmpty(expected)) { + attributesIndexes.add(i); + } + } + attributesIndexed = true; + } + if (index >= attributesIndexes.size()) { + throwIndexException(index, attributesIndexes.size()); + } + } + + private void throwIndexException(int index, int size) { + throw new IllegalArgumentException("Invalid index " + index + + "; current element has only " + size + " attributes"); + } } Modified: cxf/branches/2.4.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/transform/QNamesMap.java URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/transform/QNamesMap.java?rev=1152199&r1=1152198&r2=1152199&view=diff ============================================================================== --- cxf/branches/2.4.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/transform/QNamesMap.java (original) +++ cxf/branches/2.4.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/transform/QNamesMap.java Fri Jul 29 13:07:18 2011 @@ -51,4 +51,8 @@ class QNamesMap { } return null; } + + public int size() { + return index; + } } Modified: cxf/branches/2.4.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/transform/TransformUtils.java URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/transform/TransformUtils.java?rev=1152199&r1=1152198&r2=1152199&view=diff ============================================================================== --- cxf/branches/2.4.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/transform/TransformUtils.java (original) +++ cxf/branches/2.4.x-fixes/common/common/src/main/java/org/apache/cxf/staxutils/transform/TransformUtils.java Fri Jul 29 13:07:18 2011 @@ -29,7 +29,6 @@ import javax.xml.stream.XMLStreamReader; import javax.xml.stream.XMLStreamWriter; import org.apache.cxf.helpers.XMLUtils; -import org.apache.cxf.staxutils.StaxStreamFilter; import org.apache.cxf.staxutils.StaxUtils; public final class TransformUtils { @@ -66,15 +65,24 @@ public final class TransformUtils { Map inElementsMap, Map inAppendMap, boolean blockOriginalReader) { - if (inDropElements != null) { - Set dropElements = XMLUtils.convertStringsToQNames(inDropElements); - reader = StaxUtils.createFilteredReader(createNewReaderIfNeeded(reader, is), - new StaxStreamFilter(dropElements.toArray(new QName[]{}))); - } - if (inElementsMap != null || inAppendMap != null) { + return createTransformReaderIfNeeded(reader, is, + inDropElements, inElementsMap, inAppendMap, null, blockOriginalReader); + } + + public static XMLStreamReader createTransformReaderIfNeeded(XMLStreamReader reader, + InputStream is, + List inDropElements, + Map inElementsMap, + Map inAppendMap, + Map inAttributesMap, + boolean blockOriginalReader) { + if (inElementsMap != null || inAppendMap != null || inDropElements != null + || inAttributesMap != null) { reader = new InTransformReader(createNewReaderIfNeeded(reader, is), - inElementsMap, inAppendMap, blockOriginalReader); + inElementsMap, inAppendMap, inDropElements, + inAttributesMap, blockOriginalReader); } + return reader; } @@ -93,6 +101,9 @@ public final class TransformUtils { } } + /* + * @deprecated + */ protected static void convertToMapOfQNames(Map map, Map elementsMap) { if (map != null) { @@ -104,4 +115,44 @@ public final class TransformUtils { } } + static void convertToMapOfElementProperties(Map map, + Map elementsMap) { + if (map != null) { + for (Map.Entry entry : map.entrySet()) { + String key = entry.getKey(); + String value = entry.getValue(); + String text = null; + boolean child = false; + + // if the content delimiter is present in the value, extract the content + int d = value.indexOf('}'); + d = value.indexOf(':', d < 0 ? 0 : d); + if (d > 0) { + text = value.substring(d + 1); + value = value.substring(0, d); + } + + // if the trailer delimiter is present in the key, remove it + if (key.endsWith("/")) { + key = key.substring(0, key.length() - 1); + child = true; + } + QName lname = XMLUtils.convertStringToQName(key); + QName rname = XMLUtils.convertStringToQName(value); + + ElementProperty desc = new ElementProperty(rname, text, child); + elementsMap.put(lname, desc); + } + } + } + + protected static void convertToSetOfQNames(List set, + Set elementsSet) { + if (set != null) { + for (String entry : set) { + QName name = XMLUtils.convertStringToQName(entry); + elementsSet.add(name); + } + } + } } Modified: cxf/branches/2.4.x-fixes/common/common/src/test/java/org/apache/cxf/staxutils/transform/InTransformReaderTest.java URL: http://svn.apache.org/viewvc/cxf/branches/2.4.x-fixes/common/common/src/test/java/org/apache/cxf/staxutils/transform/InTransformReaderTest.java?rev=1152199&r1=1152198&r2=1152199&view=diff ============================================================================== --- cxf/branches/2.4.x-fixes/common/common/src/test/java/org/apache/cxf/staxutils/transform/InTransformReaderTest.java (original) +++ cxf/branches/2.4.x-fixes/common/common/src/test/java/org/apache/cxf/staxutils/transform/InTransformReaderTest.java Fri Jul 29 13:07:18 2011 @@ -21,22 +21,17 @@ package org.apache.cxf.staxutils.transfo import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.InputStream; -import java.io.StringReader; +import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; +import java.util.List; import java.util.Map; import javax.xml.namespace.QName; +import javax.xml.stream.XMLStreamConstants; +import javax.xml.stream.XMLStreamException; import javax.xml.stream.XMLStreamReader; -import javax.xml.stream.XMLStreamWriter; -import org.w3c.dom.Document; -import org.w3c.dom.Element; -import org.w3c.dom.NamedNodeMap; -import org.w3c.dom.Node; -import org.w3c.dom.NodeList; - -import org.apache.cxf.helpers.DOMUtils; import org.apache.cxf.staxutils.PartialXMLStreamReader; import org.apache.cxf.staxutils.StaxUtils; @@ -44,13 +39,14 @@ import org.junit.Assert; import org.junit.Test; public class InTransformReaderTest extends Assert { + @Test public void testReadWithDefaultNamespace() throws Exception { InputStream is = new ByteArrayInputStream("".getBytes()); XMLStreamReader reader = StaxUtils.createXMLStreamReader(is); reader = new InTransformReader(reader, - Collections.singletonMap("{http://bar}test", "test2"), - null, false); + Collections.singletonMap("{http://bar}test", "test2"), + null, null, null, false); ByteArrayOutputStream bos = new ByteArrayOutputStream(); StaxUtils.copy(reader, bos); @@ -64,8 +60,8 @@ public class InTransformReaderTest exten "".getBytes()); XMLStreamReader reader = StaxUtils.createXMLStreamReader(is); reader = new InTransformReader(reader, - Collections.singletonMap("{http://bar1}subtest", "subtest"), - null, false); + Collections.singletonMap("{http://bar1}subtest", "subtest"), + null, null, null, false); ByteArrayOutputStream bos = new ByteArrayOutputStream(); StaxUtils.copy(reader, bos); @@ -73,119 +69,30 @@ public class InTransformReaderTest exten assertEquals("", value); } - - @Test - public void testTransformAndAppend() throws Exception { - InputStream is = new ByteArrayInputStream("".getBytes()); - XMLStreamReader reader = StaxUtils.createXMLStreamReader(is); - reader = new InTransformReader(reader, - Collections.singletonMap("test", "test2"), - Collections.singletonMap("test", "wrapper"), - false); - - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - StaxUtils.copy(reader, bos); - String value = bos.toString(); - System.out.println(value); - assertTrue("".equals(value)); - } - - - @Test - public void testReadWithSameNamespace() throws Exception { - InputStream is = new ByteArrayInputStream( - ("" - + "Hello").getBytes()); - XMLStreamReader reader = StaxUtils.createXMLStreamReader(is); - - Map inMap = new HashMap(); - inMap.put("{http://bar}test", "test2"); - inMap.put("{http://foo}*", "{http://foo}*"); - - reader = new InTransformReader(reader, - inMap, - null, false); - - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - StaxUtils.copy(reader, bos); - String value = bos.toString(); - assertTrue(("" - + "Hello").equals(value)); - } - @Test public void testReadWithComplexRequestSameNamespace() throws Exception { - InputStream is = new ByteArrayInputStream( - ("" - + "" - + "" - + "SIMULATION1" - + "" - + "SomeComplexValue" - + "" - + "" - + "" - + "" - + "" - + "").getBytes()); - XMLStreamReader reader = StaxUtils.createXMLStreamReader(is); - + XMLStreamReader reader = + StaxUtils.createXMLStreamReader( + InTransformReader.class.getResourceAsStream("../resources/complexReqIn1.xml")); Map inMap = new HashMap(); inMap.put("{http://cxf.apache.org/transform/header/element}*", "{http://cxf.apache.org/transform/header/element}*"); reader = new InTransformReader(reader, - inMap, + inMap, null, null, null, false); - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - StaxUtils.copy(reader, bos); - String value = bos.toString(); - - assertTrue(("" - + "" - + "SIMULATION1" - + "" - + "SomeComplexValue" - + "" - + ""). - equals(value.trim())); + XMLStreamReader reader2 = + StaxUtils.createXMLStreamReader( + InTransformReader.class.getResourceAsStream("../resources/complexReq1.xml")); + verifyReaders(reader, reader2, true); } @Test public void testReadWithComplexRequestMultipleNamespace() throws Exception { - InputStream is = new ByteArrayInputStream( - ("" - + "" - + "" - + "SIMULATION1" - + "" - + "SomeComplexValue" - + "" - + "" - + "" - + "" - + "" - + "").getBytes()); - XMLStreamReader reader = StaxUtils.createXMLStreamReader(is); + XMLStreamReader reader = + StaxUtils.createXMLStreamReader( + InTransformReader.class.getResourceAsStream("../resources/complexReqIn2.xml")); Map inMap = new HashMap(); inMap.put("{http://cxf.apache.org/transform/header/element}*", @@ -194,49 +101,20 @@ public class InTransformReaderTest exten "{http://cxf.apache.org/transform/othertest}*"); reader = new InTransformReader(reader, - inMap, - null, false); + inMap, null, null, + null, false); - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - StaxUtils.copy(reader, bos); - String value = bos.toString(); - assertTrue(("" - + "" - + "SIMULATION1" - + "" - + "SomeComplexValue" - + "" - + ""). - equals(value.trim())); + XMLStreamReader reader2 = + StaxUtils.createXMLStreamReader( + InTransformReader.class.getResourceAsStream("../resources/complexReq2.xml")); + verifyReaders(reader, reader2, true); } + @Test public void testReadWithComplexTransformationNamespace() throws Exception { - InputStream is = new ByteArrayInputStream( - ("" - + "" - + "" - + "SIMULATION1" - + "" - + "SomeComplexValue" - + "" - + "" - + "" - + "" - + "" - + "").getBytes()); - XMLStreamReader reader = StaxUtils.createXMLStreamReader(is); + XMLStreamReader reader = + StaxUtils.createXMLStreamReader( + InTransformReader.class.getResourceAsStream("../resources/complexReqIn3.xml")); Map inMap = new HashMap(); inMap.put("{http://cxf.apache.org/transform/header/element}*", @@ -246,128 +124,43 @@ public class InTransformReaderTest exten inMap.put("{http://schemas.xmlsoap.org/soap/envelope/}Envelope", "{http://schemas.xmlsoap.org/soap/envelope/}TheEnvelope"); + // set the block original reader flag to true reader = new InTransformReader(reader, - inMap, - null, false); - - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - StaxUtils.copy(reader, bos); - String value = bos.toString(); - assertTrue(("" - + "" - + "" - + "SIMULATION1" - + "" - + "SomeComplexValue" - + "" - + "" - + ""). - equals(value.trim())); + inMap, null, null, + null, true); + XMLStreamReader reader2 = + StaxUtils.createXMLStreamReader( + InTransformReader.class.getResourceAsStream("../resources/complexReq3.xml")); + verifyReaders(reader, reader2, true); } - + @Test public void testReadPartialWithComplexRequestSameNamespace() throws Exception { - InputStream is = new ByteArrayInputStream( - ("" - + "" - + "" - + "SIMULATION1" - + "" - + "SomeComplexValue" - + "" - + "" - + "" - + "" - + "" - + "").getBytes()); - XMLStreamReader reader = StaxUtils.createXMLStreamReader(is); - + XMLStreamReader reader = + StaxUtils.createXMLStreamReader( + InTransformReader.class.getResourceAsStream("../resources/complexReqIn1.xml")); Map inMap = new HashMap(); inMap.put("{http://cxf.apache.org/transform/header/element}*", "{http://cxf.apache.org/transform/header/element}*"); reader = new InTransformReader(reader, - inMap, + inMap, null, null, null, false); QName bodyTag = new QName("http://schemas.xmlsoap.org/soap/envelope/", "Body"); PartialXMLStreamReader filteredReader = new PartialXMLStreamReader(reader, bodyTag); - Document doc = StaxUtils.read(filteredReader); - - assertNotNull(doc); - - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(bos); - StaxUtils.copy(doc, writer); - writer.flush(); - - String value = bos.toString(); - - Document docTransformed = DOMUtils.readXml(new StringReader(value)); - Element env = docTransformed.getDocumentElement(); - assertEquals("http://schemas.xmlsoap.org/soap/envelope/", env.getNamespaceURI()); - assertEquals("soap", env.getPrefix()); - - Element header = getElement(env, "http://schemas.xmlsoap.org/soap/envelope/", "Header", "soap"); - - Element customHeader = getElement(header, "http://cxf.apache.org/transform/header", - "SoapHeaderIn", "ns2"); - NamedNodeMap map = customHeader.getAttributes(); - assertEquals(4, map.getLength()); - validateAttribute(map, "xmlns", "http://cxf.apache.org/transform/test"); - validateAttribute(map, "xmlns:ns2", "http://cxf.apache.org/transform/header"); - validateAttribute(map, "xmlns:ns4", "http://cxf.apache.org/transform/fault"); - validateAttribute(map, "xmlns:ps1", "http://cxf.apache.org/transform/header/element"); - - Element mode = - getElement(customHeader, "http://cxf.apache.org/transform/header", "OperationalMode", "ns2"); - assertEquals("SIMULATION1", mode.getTextContent()); - Element headerType = - getElement(customHeader, "http://cxf.apache.org/transform/header", - "SomeComplexHeaderType", "ns2"); - - Element correlationId = - getElement(headerType, "http://cxf.apache.org/transform/header/element", - "CallerCorrelationId", "ps1"); - assertEquals("SomeComplexValue", correlationId.getTextContent()); - getElement(env, "http://schemas.xmlsoap.org/soap/envelope/", "Body", "soap"); + XMLStreamReader reader2 = + StaxUtils.createXMLStreamReader( + InTransformReader.class.getResourceAsStream("../resources/complexReq1partial.xml")); + verifyReaders(filteredReader, reader2, false); } @Test public void testPartialReadWithComplexRequestMultipleNamespace() throws Exception { - InputStream is = new ByteArrayInputStream( - ("" - + "" - + "" - + "SIMULATION1" - + "" - + "SomeComplexValue" - + "" - + "" - + "" - + "" - + "" - + "").getBytes()); - XMLStreamReader reader = StaxUtils.createXMLStreamReader(is); + XMLStreamReader reader = + StaxUtils.createXMLStreamReader( + InTransformReader.class.getResourceAsStream("../resources/complexReqIn2.xml")); Map inMap = new HashMap(); inMap.put("{http://cxf.apache.org/transform/header/element}*", @@ -376,76 +169,23 @@ public class InTransformReaderTest exten "{http://cxf.apache.org/transform/othertest}*"); reader = new InTransformReader(reader, - inMap, - null, false); + inMap, null, null, + null, false); QName bodyTag = new QName("http://schemas.xmlsoap.org/soap/envelope/", "Body"); PartialXMLStreamReader filteredReader = new PartialXMLStreamReader(reader, bodyTag); - Document doc = StaxUtils.read(filteredReader); - - assertNotNull(doc); - - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(bos); - StaxUtils.copy(doc, writer); - writer.flush(); - - String value = bos.toString(); - - Document docTransformed = DOMUtils.readXml(new StringReader(value)); - Element env = docTransformed.getDocumentElement(); - assertEquals("http://schemas.xmlsoap.org/soap/envelope/", env.getNamespaceURI()); - assertEquals("soap", env.getPrefix()); - - Element header = getElement(env, "http://schemas.xmlsoap.org/soap/envelope/", "Header", "soap"); - - Element customHeader = getElement(header, "http://cxf.apache.org/transform/header", - "SoapHeaderIn", "ns2"); - NamedNodeMap map = customHeader.getAttributes(); - assertEquals(4, map.getLength()); - validateAttribute(map, "xmlns:ps2", "http://cxf.apache.org/transform/othertest"); - validateAttribute(map, "xmlns:ns2", "http://cxf.apache.org/transform/header"); - validateAttribute(map, "xmlns:ns4", "http://cxf.apache.org/transform/fault"); - validateAttribute(map, "xmlns:ps1", "http://cxf.apache.org/transform/header/otherelement"); - - Element mode = - getElement(customHeader, "http://cxf.apache.org/transform/header", "OperationalMode", "ns2"); - assertEquals("SIMULATION1", mode.getTextContent()); - Element headerType = - getElement(customHeader, "http://cxf.apache.org/transform/header", - "SomeComplexHeaderType", "ns2"); - - Element correlationId = - getElement(headerType, "http://cxf.apache.org/transform/header/otherelement", - "CallerCorrelationId", "ps1"); - assertEquals("SomeComplexValue", correlationId.getTextContent()); - getElement(env, "http://schemas.xmlsoap.org/soap/envelope/", "Body", "soap"); - + XMLStreamReader reader2 = + StaxUtils.createXMLStreamReader( + InTransformReader.class.getResourceAsStream("../resources/complexReq2partial.xml")); + verifyReaders(filteredReader, reader2, false); } @Test public void testPartialReadWithComplexTransformationNamespace() throws Exception { - InputStream is = new ByteArrayInputStream( - ("" - + "" - + "" - + "SIMULATION1" - + "" - + "SomeComplexValue" - + "" - + "" - + "" - + "" - + "" - + "").getBytes()); - XMLStreamReader reader = StaxUtils.createXMLStreamReader(is); + XMLStreamReader reader = + StaxUtils.createXMLStreamReader( + InTransformReader.class.getResourceAsStream("../resources/complexReqIn3.xml")); Map inMap = new HashMap(); inMap.put("{http://cxf.apache.org/transform/header/element}*", @@ -457,66 +197,294 @@ public class InTransformReaderTest exten // set the block original reader flag to true reader = new InTransformReader(reader, - inMap, + inMap, null, null, null, true); QName bodyTag = new QName("http://schemas.xmlsoap.org/soap/envelope/", "Body"); PartialXMLStreamReader filteredReader = new PartialXMLStreamReader(reader, bodyTag); - Document doc = StaxUtils.read(filteredReader); - - assertNotNull(doc); + XMLStreamReader reader2 = + StaxUtils.createXMLStreamReader( + InTransformReader.class.getResourceAsStream("../resources/complexReq3partial.xml")); + verifyReaders(filteredReader, reader2, false); + } + + @Test + public void testReadPartial() throws Exception { + XMLStreamReader reader = + StaxUtils.createXMLStreamReader( + InTransformReader.class.getResourceAsStream("../resources/complexReqIn4.xml")); - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(bos); - StaxUtils.copy(doc, writer); - writer.flush(); + Map inMap = new HashMap(); + inMap.put("{http://cxf.apache.org/transform/header/element}*", + "{http://cxf.apache.org/transform/header/element}*"); - String value = bos.toString(); + // set the block original reader flag to true + reader = new InTransformReader(reader, + inMap, null, null, + null, true); - Document docTransformed = DOMUtils.readXml(new StringReader(value)); - Element env = docTransformed.getDocumentElement(); - assertEquals("http://schemas.xmlsoap.org/soap/envelope/", env.getNamespaceURI()); - assertEquals("ps1", env.getPrefix()); - assertEquals("TheEnvelope", env.getLocalName()); - - Element header = getElement(env, "http://schemas.xmlsoap.org/soap/envelope/", "Header", "soap"); - - Element customHeader = getElement(header, "http://cxf.apache.org/transform/header", - "SoapHeaderIn", "ns2"); - NamedNodeMap map = customHeader.getAttributes(); - assertEquals(4, map.getLength()); - validateAttribute(map, "xmlns:ps3", "http://cxf.apache.org/transform/othertest"); - validateAttribute(map, "xmlns:ns2", "http://cxf.apache.org/transform/header"); - validateAttribute(map, "xmlns:ns4", "http://cxf.apache.org/transform/fault"); - validateAttribute(map, "xmlns:ps2", "http://cxf.apache.org/transform/header/otherelement"); - - Element mode = - getElement(customHeader, "http://cxf.apache.org/transform/header", "OperationalMode", "ns2"); - assertEquals("SIMULATION1", mode.getTextContent()); - Element headerType = - getElement(customHeader, "http://cxf.apache.org/transform/header", - "SomeComplexHeaderType", "ns2"); - - Element correlationId = - getElement(headerType, "http://cxf.apache.org/transform/header/otherelement", - "CallerCorrelationId", "ps2"); - assertEquals("SomeComplexValue", correlationId.getTextContent()); - getElement(env, "http://schemas.xmlsoap.org/soap/envelope/", "Body", "soap"); + QName bodyTag = new QName("http://schemas.xmlsoap.org/soap/envelope/", "Body"); + PartialXMLStreamReader filteredReader = new PartialXMLStreamReader(reader, bodyTag); + XMLStreamReader reader2 = + StaxUtils.createXMLStreamReader( + InTransformReader.class.getResourceAsStream("../resources/complexReq4partial.xml")); + verifyReaders(filteredReader, reader2, false); } + @Test + public void testReadWithRepaceAppend() throws Exception { + Map transformElements = new HashMap(); + transformElements.put("requestValue", + "{http://cxf.apache.org/hello_world_soap_http/types}requestType"); + + Map appendElements = new HashMap(); + appendElements.put("requestValue", + "{http://cxf.apache.org/hello_world_soap_http/types}greetMe"); + + transformStreamAndCompare("../resources/greetMeReqIn1.xml", "../resources/greetMeReq.xml", + transformElements, appendElements, null, null, null); + } + + @Test + public void testReadWithReplaceAppendDelete() throws Exception { + Map transformElements = new HashMap(); + transformElements.put("requestValue", + "{http://cxf.apache.org/hello_world_soap_http/types}requestType"); + transformElements.put("{http://cxf.apache.org/hello_world_soap_http/types}requestDate", + ""); + + Map appendElements = new HashMap(); + appendElements.put("requestValue", + "{http://cxf.apache.org/hello_world_soap_http/types}greetMe"); + + List dropElements = new ArrayList(); + dropElements.add("value"); + + Map transformAttributes = new HashMap(); + transformAttributes.put("num", ""); + transformAttributes.put("nombre", "{http://cxf.apache.org/hello_world_soap_http/types}name"); + + transformStreamAndCompare("../resources/greetMeReqIn2.xml", "../resources/greetMeReq.xml", + transformElements, appendElements, dropElements, + transformAttributes, null); + } + + @Test + public void testReadWithChangeNamespaces() throws Exception { + Map transformElements = new HashMap(); + transformElements.put("*", + "{http://cxf.apache.org/hello_world_soap_http/types}*"); + + transformStreamAndCompare("../resources/greetMeReqIn3.xml", "../resources/greetMeReq.xml", + transformElements, null, null, null, null); + } + + @Test + public void testReadWithDeleteAttributes() throws Exception { + Map transformAttributes = new HashMap(); + transformAttributes.put("{http://www.w3.org/2001/XMLSchema-instance}type", + ""); + + transformStreamAndCompare("../resources/greetMeReqIn4.xml", "../resources/greetMeReq.xml", + null, null, null, transformAttributes, null); + } - private Element getElement(Element parent, String namespace, String name, String prefix) { - NodeList list = parent.getElementsByTagNameNS(namespace, name); - assertEquals(1, list.getLength()); - Element el = (Element)list.item(0); - assertEquals(prefix, el.getPrefix()); - return el; + @Test + public void testReadWithAppendPreInclude1() throws Exception { + Map appendElements = new HashMap(); + appendElements.put("{http://xml.amazon.com/AWSECommerceService/2004-08-01}ItemId", + "{http://xml.amazon.com/AWSECommerceService/2004-08-01}IdType:ASIN"); + transformStreamAndCompare("../resources/amazonIn1.xml", "../resources/amazon.xml", + null, appendElements, null, null, null); + } + + @Test + public void testReadWithAppendPreInclude2() throws Exception { + Map appendElements = new HashMap(); + appendElements.put("{http://xml.amazon.com/AWSECommerceService/2004-08-01}ItemId", + "{http://xml.amazon.com/AWSECommerceService/2004-08-01}IdType:ASIN"); + transformStreamAndCompare("../resources/amazonIn1nospace.xml", "../resources/amazon.xml", + null, appendElements, null, null, null); + + } + + @Test + public void testReadWithAppendPreWrap1() throws Exception { + Map transformElements = new HashMap(); + transformElements.put("payload", + "{http://www.w3.org/2003/05/soap-envelope}Envelope"); + Map appendElements = new HashMap(); + appendElements.put("{http://apache.org/cxf/calculator/types}add", + "{http://www.w3.org/2003/05/soap-envelope}Body"); + transformStreamAndCompare("../resources/AddRequestIn2.xml", "../resources/AddRequest.xml", + transformElements, appendElements, null, null, null); + } + + @Test + public void testReadWithAppendPreWrap2() throws Exception { + Map transformElements = new HashMap(); + transformElements.put("payload", + "{http://www.w3.org/2003/05/soap-envelope}Envelope"); + Map appendElements = new HashMap(); + appendElements.put("{http://apache.org/cxf/calculator/types}add", + "{http://www.w3.org/2003/05/soap-envelope}Body"); + transformStreamAndCompare("../resources/AddRequestIn2nospace.xml", "../resources/AddRequest.xml", + transformElements, appendElements, null, null, null); + } + + @Test + public void testReadWithAppendPostInclude1() throws Exception { + Map appendElements = new HashMap(); + appendElements.put("{http://xml.amazon.com/AWSECommerceService/2004-08-01}Request/", + "{http://xml.amazon.com/AWSECommerceService/2004-08-01}IdType:ASIN"); + transformStreamAndCompare("../resources/amazonIn1.xml", "../resources/amazon.xml", + null, appendElements, null, null, null); + + } + + @Test + public void testReadWithAppendPostInclude2() throws Exception { + Map appendElements = new HashMap(); + appendElements.put("{http://xml.amazon.com/AWSECommerceService/2004-08-01}Request/", + "{http://xml.amazon.com/AWSECommerceService/2004-08-01}IdType:ASIN"); + transformStreamAndCompare("../resources/amazonIn1nospace.xml", "../resources/amazon.xml", + null, appendElements, null, null, null); + + } + + @Test + public void testReadWithAppendPostWrap() throws Exception { + Map appendElements = new HashMap(); + appendElements.put("{http://www.w3.org/2003/05/soap-envelope}Body/", + "{http://apache.org/cxf/calculator/types}add"); + transformStreamAndCompare("../resources/AddRequestIn1.xml", "../resources/AddRequest.xml", + null, appendElements, null, null, null); + + } + + @Test + public void testReadWithAppendPostWrap2() throws Exception { + Map appendElements = new HashMap(); + appendElements.put("{http://www.w3.org/2003/05/soap-envelope}Body/", + "{http://apache.org/cxf/calculator/types}add"); + transformStreamAndCompare("../resources/AddRequestIn1nospace.xml", "../resources/AddRequest.xml", + null, appendElements, null, null, null); + + } + + // test utilities methods - private void validateAttribute(NamedNodeMap map, String name, String value) { - Node attr = map.getNamedItem(name); - assertEquals(attr.getTextContent(), value); + private void transformStreamAndCompare(String inname, String outname, + Map transformElements, + Map appendElements, + List dropElements, + Map transformAttributes, + Map appendAttributes) + throws XMLStreamException { + + XMLStreamReader reader = + StaxUtils.createXMLStreamReader( + InTransformReader.class.getResourceAsStream(inname)); + + reader = new InTransformReader(reader, + transformElements, appendElements, dropElements, + transformAttributes, false); + + XMLStreamReader teacher = + StaxUtils.createXMLStreamReader( + InTransformReader.class.getResourceAsStream(outname)); + + verifyReaders(reader, teacher, false); + } + + /** + * Verifies the two stream events are equivalent and throws an assertion + * exception at the first mismatch. + * @param reader + * @param teacher + * @param eec + * @throws XMLStreamException + */ + private void verifyReaders(XMLStreamReader reader, XMLStreamReader teacher, + boolean eec) throws XMLStreamException { + if (true) { + System.out.println(">>>>>>>>>"); + StaxUtils.copy(reader, System.out); + System.out.println("<<<<<<<<<"); + return; + } + // compare the elements and attributes while ignoring comments, line breaks, etc + for (;;) { + int revent = getNextEvent(reader); + int tevent = getNextEvent(teacher); + + if (revent == -1 && tevent == -1) { + break; + } + assertEquals(revent, tevent); + + switch (revent) { + case XMLStreamConstants.START_ELEMENT: + assertEquals(teacher.getName(), reader.getName()); + verifyAttributes(reader, teacher); + break; + case XMLStreamConstants.END_ELEMENT: + if (eec) { + // perform end-element-check + assertEquals(teacher.getName(), reader.getName()); + } + break; + case XMLStreamConstants.CHARACTERS: + assertEquals(teacher.getText(), reader.getText()); + break; + default: + } + } + } + + private void verifyAttributes(XMLStreamReader reader, XMLStreamReader teacher) { + int acount = teacher.getAttributeCount(); + assertEquals(acount, reader.getAttributeCount()); + Map attributesMap = new HashMap(); + // temporarily store all the attributes + for (int i = 0; i < acount; i++) { + attributesMap.put(reader.getAttributeName(i), reader.getAttributeValue(i)); + } + // compares each attribute + for (int i = 0; i < acount; i++) { + String avalue = attributesMap.remove(teacher.getAttributeName(i)); + assertEquals(avalue, teacher.getAttributeValue(i)); + } + // attributes must be exhausted + assertTrue(attributesMap.isEmpty()); + } + + /** + * Returns the next relevant reader event. + * + * @param reader + * @return + * @throws XMLStreamException + */ + private int getNextEvent(XMLStreamReader reader) throws XMLStreamException { + while (reader.hasNext()) { + int e = reader.next(); + if (e == XMLStreamConstants.END_DOCUMENT) { + return e; + } + if (e == XMLStreamConstants.START_ELEMENT || e == XMLStreamConstants.END_ELEMENT) { + return e; + } else if (e == XMLStreamConstants.CHARACTERS) { + String text = reader.getText(); + if (text.trim().length() == 0) { + continue; + } + return e; + } + } + return -1; } }