public Message copy() {
if ( envelopeReader != null ) readEnvelope(this);
try {
assert unconsumed();
consumedAt = null; // but we don't want to mark it as consumed
MutableXMLStreamBuffer xsb = new MutableXMLStreamBuffer();
StreamReaderBufferCreator c = new StreamReaderBufferCreator(xsb);
// preserving inscope namespaces from envelope, and body. Other option
// would be to create a filtering XMLStreamReader from reader+envelopeTag+bodyTag
c.storeElement(envelopeTag.nsUri, envelopeTag.localName, envelopeTag.prefix, envelopeTag.ns);
c.storeElement(bodyTag.nsUri, bodyTag.localName, bodyTag.prefix, bodyTag.ns);
if (hasPayload()) {
// Loop all the way for multi payload case
while(reader.getEventType() != XMLStreamConstants.END_DOCUMENT){
String name = reader.getLocalName();
String nsUri = reader.getNamespaceURI();
if(isBodyElement(name, nsUri) || (reader.getEventType() == XMLStreamConstants.END_DOCUMENT))
break;
c.create(reader);
// Skip whitespaces in between payload and </Body> or between elements
// those won't be in the message itself, but we store them in field bodyEpilogue
if (reader.isWhiteSpace()) {
bodyEpilogue = XMLStreamReaderUtil.currentWhiteSpaceContent(reader);
} else {
// clear it in case the existing was not the last one
// (we are interested only in the last one?)
bodyEpilogue = null;
}
}
}
c.storeEndElement(); // create structure element for </Body>
c.storeEndElement(); // create structure element for </Envelope>
c.storeEndElement(); // create structure element for END_DOCUMENT
XMLStreamReaderUtil.readRest(reader);
XMLStreamReaderUtil.close(reader);
XMLStreamReaderFactory.recycle(reader);
reader = xsb.readAsXMLStreamReader();
XMLStreamReader clone = xsb.readAsXMLStreamReader();
// advance to the start tag of the <Body> first child element
proceedToRootElement(reader);
proceedToRootElement(clone);