Package com.pd4ml
Class ResourceProvider
- java.lang.Object
-
- com.pd4ml.util.Base
-
- com.pd4ml.ResourceProvider
-
public abstract class ResourceProvider extends com.pd4ml.util.BaseBase class for custom resource loaders, which let PD4ML load documents, images, stylesheets and other resources from sources it does not support natively: a proprietary protocol, a database, an encrypted store and so on. Register an implementation withPD4ML.addCustomResourceProvider(String).For each resource, PD4ML asks the registered loaders, before the built-in ones, whether they can load it (
canLoad(String, FileCache)), and loads it with the first one that can. A new instance may be created for each load, so an implementation needs a public no-argument constructor and should not rely on state kept between loads.
-
-
Field Summary
Fields Modifier and Type Field Description java.lang.StringmimeThe MIME type of the loaded resource, for example "image/png", ornullif unknown.java.net.URLredirectedThe final address of the resource if the request was redirected, otherwisenull.
-
Constructor Summary
Constructors Constructor Description ResourceProvider()
-
Method Summary
All Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method Description abstract booleancanLoad(java.lang.String resource, com.pd4ml.cache.FileCache cache)Tells whether this loader handles the given resource address, typically by checking its protocol or prefix.abstract voidcloseResourceStream()Releases the resources of the lastgetResourceAsStream(String, FileCache)call, for example closes a connection.booleandontCache()Tells whether loaded resources must not be kept in the resource cache, for example because they change between requests.java.lang.StringgetEncoding()Returns the character encoding of the loaded resource, as set by the implementation inencoding.java.lang.StringgetMime()Returns the MIME type of the loaded resource, as set by the implementation inmime.byte[]getResourceAsBytes(java.lang.String resource, com.pd4ml.cache.FileCache cache)Loads the resource completely.abstract java.io.BufferedInputStreamgetResourceAsStream(java.lang.String resource, com.pd4ml.cache.FileCache cache)Opens the resource for reading.booleanisDirectory()Tells whether the requested address is a directory rather than a resource, as set by the implementation indir.abstract java.io.FilelocalFile()Returns the local file that holds the resource, if any, so PD4ML can access it directly.
-
-
-
Method Detail
-
getResourceAsStream
public abstract java.io.BufferedInputStream getResourceAsStream(java.lang.String resource, com.pd4ml.cache.FileCache cache) throws java.io.IOExceptionOpens the resource for reading.- Parameters:
resource- the resource address, as written in the document or resolved against the base URL; its notation is up to the implementationcache- the resource cache of the conversion; provides the log level, error policy and other context- Returns:
- a stream with the resource content, or
nullif the resource cannot be read - Throws:
java.io.IOException- if reading fails
-
canLoad
public abstract boolean canLoad(java.lang.String resource, com.pd4ml.cache.FileCache cache) throws java.io.IOExceptionTells whether this loader handles the given resource address, typically by checking its protocol or prefix.- Parameters:
resource- the resource addresscache- the resource cache of the conversion- Returns:
trueif this loader can load the resource- Throws:
java.io.IOException- if the check fails
-
dontCache
public boolean dontCache()
Tells whether loaded resources must not be kept in the resource cache, for example because they change between requests. The default implementation returnsfalse.- Returns:
trueto load the resource anew each time it is used
-
localFile
public abstract java.io.File localFile()
Returns the local file that holds the resource, if any, so PD4ML can access it directly.- Returns:
- the file, or
nullif the resource is not available as a local file
-
closeResourceStream
public abstract void closeResourceStream()
Releases the resources of the lastgetResourceAsStream(String, FileCache)call, for example closes a connection. Called after the resource has been read.
-
getResourceAsBytes
public byte[] getResourceAsBytes(java.lang.String resource, com.pd4ml.cache.FileCache cache) throws java.io.IOExceptionLoads the resource completely. The default implementation reads the stream returned bygetResourceAsStream(String, FileCache)to the end and closes it; there is usually no need to override it.- Parameters:
resource- the resource addresscache- the resource cache of the conversion- Returns:
- the resource content, or
nullif the resource cannot be read - Throws:
java.io.IOException- if reading fails, or a problem occurred that the error policy treats as fatal
-
getMime
public java.lang.String getMime()
Returns the MIME type of the loaded resource, as set by the implementation inmime.- Returns:
- the MIME type, or
nullif unknown
-
getEncoding
public java.lang.String getEncoding()
Returns the character encoding of the loaded resource, as set by the implementation inencoding.- Returns:
- the encoding, or
nullif unknown
-
isDirectory
public boolean isDirectory()
Tells whether the requested address is a directory rather than a resource, as set by the implementation indir.- Returns:
trueif the address is a directory
-
-