Uploaded image for project: 'Spring Web Services'
  1. Spring Web Services
  2. SWS-181

Option to allow marshaller skip xml declarations

    Details

    • Type: Task
    • Status: Closed
    • Priority: Major
    • Resolution: Won't Fix
    • Affects Version/s: None
    • Fix Version/s: 1.0.1
    • Component/s: OXM
    • Labels:
      None

      Description

      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

          Activity

          Hide
          bosakm Martin Bosak added a comment -

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

          }

          Show
          bosakm Martin Bosak added a comment - 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); } }
          Hide
          arjen.poutsma Arjen Poutsma added a comment -

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

          Show
          arjen.poutsma Arjen Poutsma added a comment - 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);
          Hide
          arjen.poutsma Arjen Poutsma added a comment -

          Closing 1.0.1 issues.

          Show
          arjen.poutsma Arjen Poutsma added a comment - Closing 1.0.1 issues.

            People

            • Assignee:
              arjen.poutsma Arjen Poutsma
              Reporter:
              peter.zozom Peter Zozom
            • Votes:
              0 Vote for this issue
              Watchers:
              0 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: