Class StaxEventItemReader<T>

  • All Implemented Interfaces:
    ResourceAwareItemReaderItemStream<T>, ItemReader<T>, ItemStream, ItemStreamReader<T>, org.springframework.beans.factory.InitializingBean

    public class StaxEventItemReader<T>
    extends AbstractItemCountingItemStreamItemReader<T>
    implements ResourceAwareItemReaderItemStream<T>, org.springframework.beans.factory.InitializingBean
    Item reader for reading XML input based on StAX. It extracts fragments from the input XML document which correspond to records for processing. The fragments are wrapped with StartDocument and EndDocument events so that the fragments can be further processed like standalone XML documents. The implementation is not thread-safe.
    Author:
    Robert Kasanicky, Mahmoud Ben Hassine
    • Constructor Detail

      • StaxEventItemReader

        public StaxEventItemReader()
    • Method Detail

      • setUnmarshaller

        public void setUnmarshaller​(org.springframework.oxm.Unmarshaller unmarshaller)
        Parameters:
        unmarshaller - maps xml fragments corresponding to records to objects
      • setFragmentRootElementName

        public void setFragmentRootElementName​(java.lang.String fragmentRootElementName)
        Parameters:
        fragmentRootElementName - name of the root element of the fragment
      • setFragmentRootElementNames

        public void setFragmentRootElementNames​(java.lang.String[] fragmentRootElementNames)
        Parameters:
        fragmentRootElementNames - list of the names of the root element of the fragment
      • setXmlInputFactory

        public void setXmlInputFactory​(javax.xml.stream.XMLInputFactory xmlInputFactory)
        Set the XMLInputFactory.
        Parameters:
        xmlInputFactory - to use
      • afterPropertiesSet

        public void afterPropertiesSet()
                                throws java.lang.Exception
        Ensure that all required dependencies for the ItemReader to run are provided after all properties have been set.
        Specified by:
        afterPropertiesSet in interface org.springframework.beans.factory.InitializingBean
        Throws:
        java.lang.IllegalArgumentException - if the Resource, FragmentDeserializer or FragmentRootElementName is null, or if the root element is empty.
        java.lang.IllegalStateException - if the Resource does not exist.
        java.lang.Exception
        See Also:
        InitializingBean.afterPropertiesSet()
      • moveCursorToNextFragment

        protected boolean moveCursorToNextFragment​(javax.xml.stream.XMLEventReader reader)
                                            throws NonTransientResourceException
        Responsible for moving the cursor before the StartElement of the fragment root. This implementation simply looks for the next corresponding element, it does not care about element nesting. You will need to override this method to correctly handle composite fragments.
        Parameters:
        reader - the XMLEventReader to be used to find next fragment.
        Returns:
        true if next fragment was found, false otherwise.
        Throws:
        NonTransientResourceException - if the cursor could not be moved. This will be treated as fatal and subsequent calls to read will return null.
      • doRead

        protected T doRead()
                    throws java.io.IOException,
                           javax.xml.stream.XMLStreamException
        Move to next fragment and map it to item.
        Specified by:
        doRead in class AbstractItemCountingItemStreamItemReader<T>
        Returns:
        an item or null if the data source is exhausted
        Throws:
        java.io.IOException
        javax.xml.stream.XMLStreamException