* and can be changed at any time. Current implementation uses W3C DOM tree as the backing implementations, but others
* are possible as well.
*
+ * <p>
+ * Note that the DOMSource is expected to contain a top-level synthetic element, which acts as holder of namespace
+ * declarations coming from parsing context but is otherwise ignored. Parser-side of things is expected to reuse the
+ * {@code anydata} element name for this purpose. Writer-side of things is expected to skip this element except for
+ * its namespace declarations.
+ *
* @author Robert Varga
*/
@NonNullByDefault
void anydataWriteStreamReader(final XMLStreamReader reader) throws XMLStreamException {
flushElement();
+ // Do not emit top-level element
+ int depth = 0;
while (reader.hasNext()) {
final int event = reader.next();
switch (event) {
case XMLStreamConstants.START_ELEMENT:
- forwardStartElement(reader);
+ if (depth != 0) {
+ forwardStartElement(reader);
+ } else {
+ // anydata: forward namespaces only
+ forwardNamespaces(reader);
+ }
+ ++depth;
break;
case XMLStreamConstants.END_ELEMENT:
- writer.writeEndElement();
+ if (depth != 0) {
+ writer.writeEndElement();
+ }
+ --depth;
break;
case XMLStreamConstants.CHARACTERS:
writer.writeCharacters(reader.getText());
break;
case XMLStreamConstants.COMMENT:
- writer.writeComment(reader.getText());
- break;
case XMLStreamConstants.SPACE:
- // Ignore insignificant whitespace
+ // Ignore comments and insignificant whitespace
break;
case XMLStreamConstants.START_DOCUMENT:
case XMLStreamConstants.END_DOCUMENT:
forwardStartElement(reader);
} else {
forwardNamespaces(reader);
+ // anyxml, hence we need to forward attributes
forwardAttributes(reader);
}
++depth;
}
static DOMSourceAnydata toDOMSource(final String str) throws IOException, SAXException {
- return new DOMSourceAnydata(new DOMSource(readXmlToDocument(toInputStream(str)).getDocumentElement()));
+ return new DOMSourceAnydata(new DOMSource(
+ // DOMSource must have a single document element, which we are ignoring
+ readXmlToDocument(toInputStream("<IGNORED>" + str + "</IGNORED>")).getDocumentElement()));
}
static InputStream toInputStream(final String str) {