Skip to content

Commit

Permalink
Preserve comments when using JibxMarshaller
Browse files Browse the repository at this point in the history
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
  • Loading branch information
poutsma authored and cbeams committed Sep 17, 2012
1 parent 17c6515 commit f191a55
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -309,6 +309,15 @@ public static XMLStreamReader createEventStreamReader(XMLEventReader eventReader
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
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -193,7 +193,7 @@ public void close() throws XMLStreamException {

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 {
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -29,8 +29,7 @@
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;

Expand All @@ -52,6 +51,7 @@ public void write() throws Exception {
streamWriter.writeDefaultNamespace("namespace");
streamWriter.writeStartElement("prefix", "child", "namespace2");
streamWriter.writeNamespace("prefix", "namespace2");
streamWriter.writeComment("comment");
streamWriter.writeCharacters("content");
streamWriter.writeEndElement();
streamWriter.writeEndElement();
Expand All @@ -61,4 +61,4 @@ public void write() throws Exception {
}


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -338,8 +338,8 @@ private void transformAndMarshal(Object graph, Result result) throws IOException

@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);
}


Expand Down

0 comments on commit f191a55

Please sign in to comment.