Package cdc.util.data.util
Class AbstractResourceLoader<R>
- java.lang.Object
-
- cdc.util.data.util.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 Summary
Constructors Constructor Description AbstractResourceLoader(FailureReaction reaction)Creates a Loader with reaction to adopt in case of error.
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description org.apache.logging.log4j.LoggergetLogger()ProgressSuppliergetProgressSupplier()FailureReactiongetReaction()protected abstract RloadRoot(Element root)Method that must be implemented by concrete classes to load the file.RloadXml(File file, Compressor compressor, XmlDataReader.Feature... features)Loads an XML file, possibly compressed.RloadXml(File file, XmlDataReader.Feature... features)Loads an uncompressed XML file.RloadXml(InputStream is, String systemId, Compressor compressor, XmlDataReader.Feature... features)Loads an XML input stream, possibly compressed.RloadXml(InputStream is, String systemId, XmlDataReader.Feature... features)Loads an uncompressed XML input stream.RloadXml(String filename, Compressor compressor, XmlDataReader.Feature... features)Loads an XML file, possibly compressed.RloadXml(String filename, XmlDataReader.Feature... features)Loads an uncompressed XML file.RloadXml(URL url, Compressor compressor, XmlDataReader.Feature... features)Loads an XML URL, possibly compressed.RloadXml(URL url, XmlDataReader.Feature... features)Loads an uncompressed XML URL.voidloadXml(List<URL> urls, Compressor compressor, XmlDataReader.Feature... features)Loads a list of XML URLs, possibly compressed.voidloadXml(List<URL> urls, XmlDataReader.Feature... features)Loads a list of uncompressed XML URLs.protected voidonError(String message)Function that either keeps silent, warns or raises an exception, depending on error reaction.protected <T> TonError(String message, T def)Function that either silently returns a value, warns and returns a value or raises an exception, depending on error reaction.protected <T> TonResult(T result, String message, T def)Function that returns a computed value if it is notnull, or silently returns a default value, warns and returns a default value or throws an exception.voidsetProgressController(ProgressController controller)Associates a progress controller to this loader.protected voidunexpectedElement(Element element, String... expected)protected <V> VunexpectedElement(Element element, V def, String... expected)
-
-
-
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 isFailureReaction.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:
defif error reaction isFailureReaction.DEFAULTorFailureReaction.WARN.- Throws:
DataException- When error reaction isFailureReaction.FAIL.
-
onResult
protected final <T> T onResult(T result, String message, T def)Function that returns a computed value if it is notnull, 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:
resultif it is notnull, ordefif error reaction isFailureReaction.DEFAULTorFailureReaction.WARN.- Throws:
DataException- Whenresultisnulland error reaction isFailureReaction.FAIL.
-
-