Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Option to allow marshaller skip xml declarations [SWS-181] #335

Closed
gregturn opened this issue Aug 28, 2007 · 3 comments
Closed

Option to allow marshaller skip xml declarations [SWS-181] #335

gregturn opened this issue Aug 28, 2007 · 3 comments

Comments

@gregturn
Copy link
Contributor

Peter Zozom opened SWS-181 and commented

For Spring Batch's Xml IO sources will be helpful to have method setXmlDecl(boolean) in org.springframework.oxm.Marshaller interface, which will configure marshaller to turn on/off writing StartDocument event and EndDocument event for each call of marshall() method.

See new XML I/O sources attached as SVN patch in issue:
http://opensource.atlassian.com/projects/spring/browse/BATCH-116


Issue Links:

  • BATCH-116 Create XML input/output source which will work directly with StAX parser
@gregturn
Copy link
Contributor Author

Martin Bosak commented

The same functionality needs to be done for Jibx Marshaller.

Here's how I patched the JibxMarshaller.java class to do it:

protected void marshalOutputStream(Object graph, OutputStream outputStream)
        throws XmlMappingException, IOException {
    try {
        IMarshallingContext marshallingContext = createMarshallingContext();
    marshallingContext.setOutput(outputStream, encoding);
        marshallingContext.marshalDocument(graph);
    }
    catch (JiBXException ex) {
        throw convertJibxException(ex, true);
    }
}

protected void marshalWriter(Object graph, Writer writer) throws XmlMappingException, IOException {
    try {
        IMarshallingContext marshallingContext = createMarshallingContext();
    marshallingContext.setOutput(writer);
        marshallingContext.marshalDocument(graph);
    }
    catch (JiBXException ex) {
        throw convertJibxException(ex, true);
    }
}

@gregturn
Copy link
Contributor Author

Arjen Poutsma commented

The reason I cannot fix the Marshaller like you suggested in http://opensource.atlassian.com/projects/spring/browse/SWS-181 is this: the Marshaller is very generic: it can marshal to DOM documents, SAX ContentHandlers, file streams, and Stax XMLEventReader/XMLEventWriters. Filtering out the Start/EndDocument events would only work for Stax, and perhaps SAX. Adding a property to this effect would very confusing, since it does not work in other cases (marshalling to DOM). This would break the principle of least astonishment.

But there is a way you can filter out these events, by creating a delegating XMLStreamWriter, which ignores start and end document events, but passes through everything else:

import javax.xml.stream.*;

public class NoStartEndDocumentStreamWriter implements XMLStreamWriter {

private XMLStreamWriter delegate;

public NoStartEndDocumentStreamWriter(XMLStreamWriter delegate) {
this.delegate = delegate;
}

void writeStartDocument() throws XMLStreamException {
// ignore
}

void writeStartDocument(String version) throws XMLStreamException {
// ignore
}

void writeStartDocument(String encoding,String version) throws XMLStreamException {
// ignore
}

void writeEndDocument() throws XMLStreamException {
// ignore
}

void close() throws XMLStreamException {
// ignore
}

void writeStartElement(String localName) throws XMLStreamException {
delegate.writeStartElement(localName);
}

void writeStartElement(String namespaceURI,
String localName)
throws XMLStreamException {
delegate.writeStartElement(namespaceURI, localName);
}

// et cetera, delegating all the other methods of the stream writer to the delegate field

}

Then you can create a XMLStreamReader that uses this filter:

streamWriter = outputFactory.createXMLStreamWriter(os, encoding);
streamWriter = new NoStartEndDocumentStreamWriter(streamWriter);

@gregturn
Copy link
Contributor Author

Arjen Poutsma commented

Closing 1.0.1 issues.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants