Class JAXBReader


  • public class JAXBReader
    extends java.lang.Object
    Reads an XML document and creates a DOM4J tree from SAX parsing events. JAXBObjectHandlerobjects can be registered to automatically receive unmarshalled XML fragments. Registered ElementHandler implementations are notified when a certain element path is encountered
    See Also:
    SAXReader, JAXBContext
    • Constructor Summary

      Constructors 
      Constructor Description
      JAXBReader​(java.lang.String contextPath)
      Creates a new JAXBReader for the given JAXB context path.
      JAXBReader​(java.lang.String contextPath, java.lang.ClassLoader classloader)
      Creates a new JAXBReader for the given JAXB context path, using the specified ClassLoader.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void addHandler​(java.lang.String path, ElementHandler handler)
      Adds the ElementHandler to be called when the specified path is encounted.
      void addObjectHandler​(java.lang.String path, JAXBObjectHandler handler)
      Registers a JAXBObjectHandlerthat will be supplied with the unmarshalled representation of the xml fragment whenever the specified path is encounted.
      boolean isPruneElements()
      When 'true', the DOM4J document will not be kept in memory while parsing.
      protected Element marshal​(javax.xml.bind.Element element)
      Marshals the given Elementin to its DOM4J counterpart.
      Document read​(java.io.File source)
      Parses the specified File
      Document read​(java.io.File file, java.nio.charset.Charset charset)
      Parses the specified File, using the given Charset.
      Document read​(java.io.InputStream source)
      Parses the specified InputStream
      Document read​(java.io.InputStream source, java.lang.String systemId)
      Parses the specified InputStream
      Document read​(java.io.Reader source)
      Parses the specified Reader
      Document read​(java.io.Reader source, java.lang.String systemId)
      Parses the specified Reader
      Document read​(java.lang.String source)
      Parses the the given URL or filename.
      Document read​(java.net.URL source)
      Parses the the given URL.
      Document read​(org.xml.sax.InputSource source)
      Parses the specified InputSource
      void removeHandler​(java.lang.String path)
      Removes the ElementHandler from the event based processor, for the specified path.
      void removeObjectHandler​(java.lang.String path)
      Removes the JAXBObjectHandlerfrom the event based processor, for the specified element path.
      void resetHandlers()
      Removes all registered JAXBObjectHandlerand ElementHandler instances from the event based processor.
      void setPruneElements​(boolean pruneElements)
      Set to true when DOM4J elements must immediately be pruned from the tree.
      protected javax.xml.bind.Element unmarshal​(Element element)
      Unmarshalls the specified DOM4J Elementinto a Element
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • JAXBReader

        public JAXBReader​(java.lang.String contextPath)
        Creates a new JAXBReader for the given JAXB context path. This is the Java package where JAXB can find the generated XML classes. This package MUST contain jaxb.properties!
        Parameters:
        contextPath - context path to be used
        See Also:
        JAXBContext
      • JAXBReader

        public JAXBReader​(java.lang.String contextPath,
                          java.lang.ClassLoader classloader)
        Creates a new JAXBReader for the given JAXB context path, using the specified ClassLoader. This is the Java package where JAXB can find the generated XML classes. This package MUST contain jaxb.properties!
        Parameters:
        contextPath - to be used
        classloader - to be used
        See Also:
        JAXBContext
    • Method Detail

      • read

        public Document read​(java.io.File source)
                      throws DocumentException
        Parses the specified File
        Parameters:
        source - the file to parse
        Returns:
        the resulting DOM4J document
        Throws:
        DocumentException - when an error occurs while parsing
      • read

        public Document read​(java.io.File file,
                             java.nio.charset.Charset charset)
                      throws DocumentException
        Parses the specified File, using the given Charset.
        Parameters:
        file - the file to parse
        charset - the charset to be used
        Returns:
        the resulting DOM4J document
        Throws:
        DocumentException - when an error occurs while parsing
      • read

        public Document read​(org.xml.sax.InputSource source)
                      throws DocumentException
        Parses the specified InputSource
        Parameters:
        source - the source to parse
        Returns:
        the resulting DOM4J document
        Throws:
        DocumentException - when an error occurs while parsing
      • read

        public Document read​(java.io.InputStream source)
                      throws DocumentException
        Parses the specified InputStream
        Parameters:
        source - the input stream to parse
        Returns:
        the resulting DOM4J document
        Throws:
        DocumentException - when an error occurs while parsing
      • read

        public Document read​(java.io.InputStream source,
                             java.lang.String systemId)
                      throws DocumentException
        Parses the specified InputStream
        Parameters:
        source - the input stream to parse
        systemId - is the URI for the input
        Returns:
        the resulting DOM4J document
        Throws:
        DocumentException - when an error occurs while parsing
      • read

        public Document read​(java.io.Reader source)
                      throws DocumentException
        Parses the specified Reader
        Parameters:
        source - the input reader to use
        Returns:
        the resulting DOM4J document
        Throws:
        DocumentException - when an error occurs while parsing
      • read

        public Document read​(java.io.Reader source,
                             java.lang.String systemId)
                      throws DocumentException
        Parses the specified Reader
        Parameters:
        source - the input reader to parse
        systemId - is the URI for the input
        Returns:
        the resulting DOM4J document
        Throws:
        DocumentException - when an error occurs while parsing
      • read

        public Document read​(java.lang.String source)
                      throws DocumentException
        Parses the the given URL or filename.
        Parameters:
        source - the location to parse
        Returns:
        the resulting DOM4J document
        Throws:
        DocumentException - when an error occurs while parsing
      • read

        public Document read​(java.net.URL source)
                      throws DocumentException
        Parses the the given URL.
        Parameters:
        source - the URL to parse
        Returns:
        the resulting DOM4J document
        Throws:
        DocumentException - when an error occurs while parsing
      • addObjectHandler

        public void addObjectHandler​(java.lang.String path,
                                     JAXBObjectHandler handler)
        Registers a JAXBObjectHandlerthat will be supplied with the unmarshalled representation of the xml fragment whenever the specified path is encounted.
        Parameters:
        path - the path to listen for
        handler - the handler to be notified
      • removeObjectHandler

        public void removeObjectHandler​(java.lang.String path)
        Removes the JAXBObjectHandlerfrom the event based processor, for the specified element path.
        Parameters:
        path - The path to remove the JAXBObjectHandlerfor
      • addHandler

        public void addHandler​(java.lang.String path,
                               ElementHandler handler)
        Adds the ElementHandler to be called when the specified path is encounted.
        Parameters:
        path - is the path to be handled
        handler - is the ElementHandler to be called by the event based processor.
      • removeHandler

        public void removeHandler​(java.lang.String path)
        Removes the ElementHandler from the event based processor, for the specified path.
        Parameters:
        path - is the path to remove the ElementHandler for.
      • isPruneElements

        public boolean isPruneElements()
        When 'true', the DOM4J document will not be kept in memory while parsing.
        Returns:
        Returns the pruneElements.
      • setPruneElements

        public void setPruneElements​(boolean pruneElements)
        Set to true when DOM4J elements must immediately be pruned from the tree. The Documentwill not be available afterwards!
        Parameters:
        pruneElements - DOCUMENT ME!
      • marshal

        protected Element marshal​(javax.xml.bind.Element element)
                           throws javax.xml.bind.JAXBException
        Marshals the given Elementin to its DOM4J counterpart.
        Parameters:
        element - JAXB Element to be marshalled
        Returns:
        the marshalled DOM4J Element
        Throws:
        javax.xml.bind.JAXBException - when an error occurs
      • unmarshal

        protected javax.xml.bind.Element unmarshal​(Element element)
                                            throws javax.xml.bind.JAXBException
        Unmarshalls the specified DOM4J Elementinto a Element
        Parameters:
        element - the DOM4J element to unmarshall
        Returns:
        the unmarshalled JAXB object
        Throws:
        javax.xml.bind.JAXBException - when an error occurs