Class AbstractResourceLoader<R>

  • Type Parameters:
    R - The return type.

    public abstract class AbstractResourceLoader<R>
    extends Object
    Helper class dedicated to data loading.
    Author:
    Damien Carbonne
    • Constructor Detail

      • AbstractResourceLoader

        public AbstractResourceLoader​(FailureReaction reaction)
        Creates a Loader with reaction to adopt in case of error.

        A logger named from the class is created.

        Parameters:
        reaction - The reaction to adopt in case of error.
    • Method Detail

      • getLogger

        public final org.apache.logging.log4j.Logger getLogger()
        Returns:
        The logger to use.
      • getReaction

        public final FailureReaction getReaction()
        Returns:
        The reaction to adopt in case of error.
      • setProgressController

        public void setProgressController​(ProgressController controller)
        Associates a progress controller to this loader.
        Parameters:
        controller - The progress controller.
      • getProgressSupplier

        public ProgressSupplier getProgressSupplier()
        Returns:
        The progress supplier, that can be used to generated progress events.
      • loadXml

        public final R loadXml​(File file,
                               Compressor compressor,
                               XmlDataReader.Feature... features)
                        throws IOException
        Loads an XML file, possibly compressed.
        Parameters:
        file - The file.
        compressor - The compressor to use (if the file is compressed).
        features - The enabled XMLDataReader features.
        Returns:
        The result.
        Throws:
        IOException - When an IO exception occurs.
      • loadXml

        public final R loadXml​(File file,
                               XmlDataReader.Feature... features)
                        throws IOException
        Loads an uncompressed XML file.
        Parameters:
        file - The file.
        features - The enabled XMLDataReader features.
        Returns:
        The result.
        Throws:
        IOException - When an IO exception occurs.
      • loadXml

        public final R loadXml​(String filename,
                               Compressor compressor,
                               XmlDataReader.Feature... features)
                        throws IOException
        Loads an XML file, possibly compressed.
        Parameters:
        filename - The file name.
        compressor - The compressor to use (if the file is compressed).
        features - The enabled XMLDataReader features.
        Returns:
        The result.
        Throws:
        IOException - When an IO exception occurs.
      • loadXml

        public final R loadXml​(String filename,
                               XmlDataReader.Feature... features)
                        throws IOException
        Loads an uncompressed XML file.
        Parameters:
        filename - The file name.
        features - The enabled XMLDataReader features.
        Returns:
        The result.
        Throws:
        IOException - When an IO exception occurs.
      • loadXml

        public final R loadXml​(URL url,
                               Compressor compressor,
                               XmlDataReader.Feature... features)
                        throws IOException
        Loads an XML URL, possibly compressed.
        Parameters:
        url - The URL.
        compressor - The compressor to use (if the file is compressed).
        features - The enabled XMLDataReader features.
        Returns:
        The result.
        Throws:
        IOException - When an IO exception occurs.
      • loadXml

        public final R loadXml​(URL url,
                               XmlDataReader.Feature... features)
                        throws IOException
        Loads an uncompressed XML URL.
        Parameters:
        url - The URL.
        features - The enabled XMLDataReader features.
        Returns:
        The result.
        Throws:
        IOException - When an IO exception occurs.
      • loadXml

        public final void loadXml​(List<URL> urls,
                                  Compressor compressor,
                                  XmlDataReader.Feature... features)
        Loads a list of XML URLs, possibly compressed.
        Parameters:
        urls - The list of URLs.
        compressor - The compressor to use (if the URLs are compressed).
        features - The enabled XMLDataReader features.
      • loadXml

        public final void loadXml​(List<URL> urls,
                                  XmlDataReader.Feature... features)
        Loads a list of uncompressed XML URLs.
        Parameters:
        urls - The list of URLs.
        features - The enabled XMLDataReader features.
      • loadXml

        public final R loadXml​(InputStream is,
                               String systemId,
                               Compressor compressor,
                               XmlDataReader.Feature... features)
                        throws IOException
        Loads an XML input stream, possibly compressed.
        Parameters:
        is - The input stream.
        systemId - The system id.
        compressor - The compressor to use (if the input stream is compressed).
        features - The enabled XMLDataReader features.
        Returns:
        The result.
        Throws:
        IOException - When an IO exception occurs.
      • loadXml

        public final R loadXml​(InputStream is,
                               String systemId,
                               XmlDataReader.Feature... features)
                        throws IOException
        Loads an uncompressed XML input stream.
        Parameters:
        is - The input stream.
        systemId - The system id.
        features - The enabled XMLDataReader features.
        Returns:
        The result.
        Throws:
        IOException - When an IO exception occurs.
      • loadRoot

        protected abstract R loadRoot​(Element root)
                               throws IOException
        Method that must be implemented by concrete classes to load the file.
        Parameters:
        root - The root element.
        Returns:
        The result.
        Throws:
        IOException - When an IO exception occurs.
      • onError

        protected final void onError​(String message)
        Function that either keeps silent, warns or raises an exception, depending on error reaction.
        Parameters:
        message - The error message.
        Throws:
        DataException - When error reaction is FailureReaction.FAIL.
      • onError

        protected final <T> T onError​(String message,
                                      T def)
        Function that either silently returns a value, warns and returns a value or raises an exception, depending on error reaction.
        Type Parameters:
        T - The return type.
        Parameters:
        message - The error message.
        def - The default return value.
        Returns:
        def if error reaction is FailureReaction.DEFAULT or FailureReaction.WARN.
        Throws:
        DataException - When error reaction is FailureReaction.FAIL.
      • onResult

        protected final <T> T onResult​(T result,
                                       String message,
                                       T def)
        Function that returns a computed value if it is not null, or silently returns a default value, warns and returns a default value or throws an exception.
        Type Parameters:
        T - The return type.
        Parameters:
        result - The computed result.
        message - The error message.
        def - The default return value.
        Returns:
        result if it is not null, or def if error reaction is FailureReaction.DEFAULT or FailureReaction.WARN.
        Throws:
        DataException - When result is null and error reaction is FailureReaction.FAIL.
      • unexpectedElement

        protected final void unexpectedElement​(Element element,
                                               String... expected)
      • unexpectedElement

        protected final <V> V unexpectedElement​(Element element,
                                                V def,
                                                String... expected)