Package com.pd4ml

Class ResourceProvider


  • public abstract class ResourceProvider
    extends com.pd4ml.util.Base
    Base 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 with PD4ML.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.String mime
      The MIME type of the loaded resource, for example "image/png", or null if unknown.
      java.net.URL redirected
      The final address of the resource if the request was redirected, otherwise null.
      • Fields inherited from class com.pd4ml.util.Base

        AES_128BIT, AES_256BIT, LOG_ALL, LOG_CACHE, LOG_CSS, LOG_DEBUG, LOG_ERROR, LOG_FATAL, LOG_FONTS, LOG_INFO, LOG_MINIMAL, LOG_PARSE, LOG_TRACE, LOG_USAGE, LOG_VERBOSE, LOG_WARN, NO_LOG, PD4ML_LOG_LEVEL, RC4_128BIT, RC4_40BIT
    • Method Summary

      All Methods Instance Methods Abstract Methods Concrete Methods 
      Modifier and Type Method Description
      abstract boolean canLoad​(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 void closeResourceStream()
      Releases the resources of the last getResourceAsStream(String, FileCache) call, for example closes a connection.
      boolean dontCache()
      Tells whether loaded resources must not be kept in the resource cache, for example because they change between requests.
      java.lang.String getEncoding()
      Returns the character encoding of the loaded resource, as set by the implementation in encoding.
      java.lang.String getMime()
      Returns the MIME type of the loaded resource, as set by the implementation in mime.
      byte[] getResourceAsBytes​(java.lang.String resource, com.pd4ml.cache.FileCache cache)
      Loads the resource completely.
      abstract java.io.BufferedInputStream getResourceAsStream​(java.lang.String resource, com.pd4ml.cache.FileCache cache)
      Opens the resource for reading.
      boolean isDirectory()
      Tells whether the requested address is a directory rather than a resource, as set by the implementation in dir.
      abstract java.io.File localFile()
      Returns the local file that holds the resource, if any, so PD4ML can access it directly.
      • Methods inherited from class com.pd4ml.util.Base

        log, log2, printLog, shouldForceException
      • Methods inherited from class java.lang.Object

        equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • mime

        public java.lang.String mime
        The MIME type of the loaded resource, for example "image/png", or null if unknown.
      • redirected

        public java.net.URL redirected
        The final address of the resource if the request was redirected, otherwise null.
    • Constructor Detail

      • ResourceProvider

        public ResourceProvider()
    • Method Detail

      • getResourceAsStream

        public abstract java.io.BufferedInputStream getResourceAsStream​(java.lang.String resource,
                                                                        com.pd4ml.cache.FileCache cache)
                                                                 throws java.io.IOException
        Opens 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 implementation
        cache - the resource cache of the conversion; provides the log level, error policy and other context
        Returns:
        a stream with the resource content, or null if 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.IOException
        Tells whether this loader handles the given resource address, typically by checking its protocol or prefix.
        Parameters:
        resource - the resource address
        cache - the resource cache of the conversion
        Returns:
        true if 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 returns false.
        Returns:
        true to 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 null if the resource is not available as a local file
      • closeResourceStream

        public abstract void closeResourceStream()
        Releases the resources of the last getResourceAsStream(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.IOException
        Loads the resource completely. The default implementation reads the stream returned by getResourceAsStream(String, FileCache) to the end and closes it; there is usually no need to override it.
        Parameters:
        resource - the resource address
        cache - the resource cache of the conversion
        Returns:
        the resource content, or null if 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 in mime.
        Returns:
        the MIME type, or null if unknown
      • getEncoding

        public java.lang.String getEncoding()
        Returns the character encoding of the loaded resource, as set by the implementation in encoding.
        Returns:
        the encoding, or null if unknown
      • isDirectory

        public boolean isDirectory()
        Tells whether the requested address is a directory rather than a resource, as set by the implementation in dir.
        Returns:
        true if the address is a directory