Uploaded image for project: 'Spring Batch'
  1. Spring Batch
  2. BATCH-1835

StaxEventItemWriter fails to create formatted output

    Details

    • Type: Improvement
    • Status: Open
    • Priority: Minor
    • Resolution: Unresolved
    • Affects Version/s: 2.1.8
    • Fix Version/s: None
    • Component/s: Infrastructure
    • Labels:
      None
    • Environment:
      JDK 1.6, Windows 7

      Description

      A Jaxb2Marshaller used in a StaxEventItemWriter fails to create formatted XML output (one element per line, ident lines) even if the Jaxb2Marshaller has been configured to do so.
      See the sample application (STS project, maven build) to reproduce: this application reads some sample data from a csv file in data/in and creates two XML files in the directory data/out: one XML file created by a StaxEventItemWriter and the other created by a "home made" XmlItemWriter. Both ItemWriters use the same Jaxb2Marshaller bean for marshalliung but produce different output: the output of the StaxEventItemWriter fails to be formatted.

      1. CustomStaxEventItemWriter.java
        22 kB
        Smail Louahchi

        Activity

        Hide
        davidkarlsen David J. M. Karlsen added a comment -

        Any chance of getting this fixed?

        Show
        davidkarlsen David J. M. Karlsen added a comment - Any chance of getting this fixed?
        Hide
        jpraet Jimmy Praet added a comment -

        There is no standard support for XML formatting in stax. But a third-party library called stax-utils (https://java.net/projects/stax-utils/) does provide this functionality (with javanet.staxutils.IndentingXMLEventWriter).

        If you really need this you could extend StaxEventItemWriter and override the createXmlEventWriter method to return an IndentingXMLEventWriter.

        Show
        jpraet Jimmy Praet added a comment - There is no standard support for XML formatting in stax. But a third-party library called stax-utils ( https://java.net/projects/stax-utils/ ) does provide this functionality (with javanet.staxutils.IndentingXMLEventWriter). If you really need this you could extend StaxEventItemWriter and override the createXmlEventWriter method to return an IndentingXMLEventWriter.
        Hide
        davidkarlsen David J. M. Karlsen added a comment -

        Yes, this is what I ended up with if the project wants it:

        +import java.io.Writer;
        +
        +import javanet.staxutils.IndentingXMLEventWriter;
        +
        +import javax.xml.stream.XMLEventWriter;
        +import javax.xml.stream.XMLOutputFactory;
        +import javax.xml.stream.XMLStreamException;
        +
        +import org.springframework.batch.item.xml.StaxEventItemWriter;
        +
        +
        +public class ExtendedStaxEventItemWriter<T>
        +    extends StaxEventItemWriter<T>
        +{
        +    
        +    public void setIndenting( boolean indenting )
        +    {
        +        this.indenting = indenting;
        +    }
        +    
        +    public boolean isIndenting()
        +    {
        +        return indenting;
        +    }
        +    
        +    @Override
        +    protected XMLEventWriter createXmlEventWriter( XMLOutputFactory outputFactory, Writer writer )
        +        throws XMLStreamException
        +    {
        +        if ( isIndenting() ) {
        +            return new IndentingXMLEventWriter( super.createXmlEventWriter( outputFactory, writer ) );
        +        }
        +        else {
        +            return super.createXmlEventWriter( outputFactory, writer );
        +        }
        +    }
        +
        +}

        Show
        davidkarlsen David J. M. Karlsen added a comment - Yes, this is what I ended up with if the project wants it: +import java.io.Writer; + +import javanet.staxutils.IndentingXMLEventWriter; + +import javax.xml.stream.XMLEventWriter; +import javax.xml.stream.XMLOutputFactory; +import javax.xml.stream.XMLStreamException; + +import org.springframework.batch.item.xml.StaxEventItemWriter; + + +public class ExtendedStaxEventItemWriter<T> + extends StaxEventItemWriter<T> +{ + + public void setIndenting( boolean indenting ) + { + this.indenting = indenting; + } + + public boolean isIndenting() + { + return indenting; + } + + @Override + protected XMLEventWriter createXmlEventWriter( XMLOutputFactory outputFactory, Writer writer ) + throws XMLStreamException + { + if ( isIndenting() ) { + return new IndentingXMLEventWriter( super.createXmlEventWriter( outputFactory, writer ) ); + } + else { + return super.createXmlEventWriter( outputFactory, writer ); + } + } + +}
        Hide
        sebastienlorber Sebastien Lorber added a comment -
        Show
        sebastienlorber Sebastien Lorber added a comment - Check also this issue: https://jira.springsource.org/browse/BATCH-2113
        Hide
        slouahchi Smail Louahchi added a comment -

        Please check, if its possible to add prettyPrint Feature to StaxEventItemWriter with IndentingXMLEventWriter.

        In my CustomStaxEventItemWriter the user has the choice with an boolean flag for pretty print.
        The close method only requires an CR/LF for the end tag in mode 'prettyPrint'.

        pom.xml

        Show all

        1
        <dependency>
        2
        	<groupId>net.java.dev.stax-utils</groupId>
        3
        	<artifactId>stax-utils</artifactId>
        4
        	<version>20040917</version>
        5
        </dependency>

        org.springframework.batch.item.xml.StaxEventItemWriter

        Show all

        1
        private boolean prettyPrint = false;
        2
        ...
        3
        public void setPrettyPrint(boolean prettyPrint)
        4
        {
        5
        	this.prettyPrint = prettyPrint;
        6
        }
        7
        ...
        8
        protected XMLEventWriter createXmlEventWriter(XMLOutputFactory outputFactory, Writer writer) throws XMLStreamException
        9
        {
        10
        	XMLEventWriter result = outputFactory.createXMLEventWriter(writer);
        11
        	if (prettyPrint)
        12
        	{
        13
        		return new IndentingXMLEventWriter(result);
        14
        	}
        15
        	return result;
        16
        }
        17
        ...
        18
        public void close() {
        19
        	super.close();
        20
         
        21
        	XMLEventFactory factory = createXmlEventFactory();
        22
        	try {
        23
        		delegateEventWriter.add(factory.createCharacters(prettyPrint ? "\n" : ""));
        24
        	}
        25
        ...

        context.xml

        Show all

        1
        <bean id="staxItemWriter" class="org.springframework.batch.item.xml.StaxEventItemWriter">
        2
        	...
        3
        	<property name="prettyPrint" value="true" />
        4
        </bean>

        Show
        slouahchi Smail Louahchi added a comment - Please check, if its possible to add prettyPrint Feature to StaxEventItemWriter with IndentingXMLEventWriter. In my CustomStaxEventItemWriter the user has the choice with an boolean flag for pretty print. The close method only requires an CR/LF for the end tag in mode 'prettyPrint'. pom.xml Show all 1 <dependency> 2 <groupId>net.java.dev.stax-utils</groupId> 3 <artifactId>stax-utils</artifactId> 4 <version>20040917</version> 5 </dependency> org.springframework.batch.item.xml.StaxEventItemWriter Show all 1 private boolean prettyPrint = false; 2 ... 3 public void setPrettyPrint(boolean prettyPrint) 4 { 5 this.prettyPrint = prettyPrint; 6 } 7 ... 8 protected XMLEventWriter createXmlEventWriter(XMLOutputFactory outputFactory, Writer writer) throws XMLStreamException 9 { 10 XMLEventWriter result = outputFactory.createXMLEventWriter(writer); 11 if (prettyPrint) 12 { 13 return new IndentingXMLEventWriter(result); 14 } 15 return result; 16 } 17 ... 18 public void close() { 19 super.close(); 20   21 XMLEventFactory factory = createXmlEventFactory(); 22 try { 23 delegateEventWriter.add(factory.createCharacters(prettyPrint ? "\n" : "")); 24 } 25 ... context.xml Show all 1 <bean id="staxItemWriter" class="org.springframework.batch.item.xml.StaxEventItemWriter"> 2 ... 3 <property name="prettyPrint" value="true" /> 4 </bean>

          People

          • Assignee:
            Unassigned
            Reporter:
            oliver.theissen Oliver Theissen
          • Votes:
            4 Vote for this issue
            Watchers:
            7 Start watching this issue

            Dates

            • Created:
              Updated: