Preserve comments when using JibxMarshaller

Prior to this commit, JibxMarshaller used a SAX ContentHandler to
marshal to StAX XMLEventWriters, which inadvertently resulted in the
deletion of XML comments.

After this commit, JibxMarshaller adapts the XMLEventWriter into an
XMLStreamWriter and comments are preserved.

Issue: SPR-9768
This commit is contained in:
Arjen Poutsma 2012-09-14 09:48:25 +02:00 committed by Chris Beams
parent 17c6515c0a
commit f191a55b8f
4 changed files with 18 additions and 9 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -309,6 +309,15 @@ public abstract class StaxUtils {
return new XMLEventStreamReader(eventReader);
}
/**
* Return a {@link XMLStreamWriter} that writes to a {@link XMLEventWriter}.
* @return a stream writer that writes to an event writer
* @since 3.2
*/
public static XMLStreamWriter createEventStreamWriter(XMLEventWriter eventWriter) {
return new XMLEventStreamWriter(eventWriter, XMLEventFactory.newFactory());
}
/**
* Return a {@link XMLStreamWriter} that writes to a {@link XMLEventWriter}.
* @return a stream writer that writes to an event writer

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2010 the original author or authors.
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -193,7 +193,7 @@ class XMLEventStreamWriter implements XMLStreamWriter {
private void writeStartElement(StartElement startElement) throws XMLStreamException {
eventWriter.add(startElement);
endElements.add(eventFactory.createEndElement(startElement.getName(), null));
endElements.add(eventFactory.createEndElement(startElement.getName(), startElement.getNamespaces()));
}
private void writeNamespace(Namespace namespace) throws XMLStreamException {

View File

@ -1,5 +1,5 @@
/*
* Copyright 2002-2011 the original author or authors.
* Copyright 2002-2012 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -29,8 +29,7 @@ import static org.custommonkey.xmlunit.XMLAssert.*;
public class XMLEventStreamWriterTests {
private static final String XML =
"<?pi content?><root xmlns='namespace'><prefix:child xmlns:prefix='namespace2'>content</prefix:child></root>"
;
"<?pi content?><root xmlns='namespace'><prefix:child xmlns:prefix='namespace2'><!--comment-->content</prefix:child></root>";
private XMLEventStreamWriter streamWriter;
@ -52,6 +51,7 @@ public class XMLEventStreamWriterTests {
streamWriter.writeDefaultNamespace("namespace");
streamWriter.writeStartElement("prefix", "child", "namespace2");
streamWriter.writeNamespace("prefix", "namespace2");
streamWriter.writeComment("comment");
streamWriter.writeCharacters("content");
streamWriter.writeEndElement();
streamWriter.writeEndElement();
@ -61,4 +61,4 @@ public class XMLEventStreamWriterTests {
}
}
}

View File

@ -338,8 +338,8 @@ public class JibxMarshaller extends AbstractMarshaller implements InitializingBe
@Override
protected void marshalXmlEventWriter(Object graph, XMLEventWriter eventWriter) {
ContentHandler contentHandler = StaxUtils.createContentHandler(eventWriter);
marshalSaxHandlers(graph, contentHandler, null);
XMLStreamWriter streamWriter = StaxUtils.createEventStreamWriter(eventWriter);
marshalXmlStreamWriter(graph, streamWriter);
}