Class ResourceResolver

java.lang.Object
com.itextpdf.styledxmlparser.resolver.resource.ResourceResolver

public class ResourceResolver extends Object
Utilities class to resolve resources.
  • Field Details

    • BASE64_IDENTIFIER

      public static final String BASE64_IDENTIFIER
      Identifier string used when loading in base64 images.
      See Also:
    • DATA_SCHEMA_PREFIX

      public static final String DATA_SCHEMA_PREFIX
      Identifier string used to detect that the source is under data URI scheme.
      See Also:
  • Constructor Details

    • ResourceResolver

      public ResourceResolver (String baseUri)
      Creates a new ResourceResolver instance. If baseUri is a string that represents an absolute URI with any schema except "file" - resources url values will be resolved exactly as "new URL(baseUrl, uriString)". Otherwise base URI will be handled as path in local file system.

      If empty string or relative URI string is passed as base URI, then it will be resolved against current working directory of this application instance.

      Parameters:
      baseUri - base URI against which all relative resource URIs will be resolved
    • ResourceResolver

      public ResourceResolver (String baseUri, IResourceRetriever retriever)
      Creates a new ResourceResolver instance. If baseUri is a string that represents an absolute URI with any schema except "file" - resources url values will be resolved exactly as "new URL(baseUrl, uriString)". Otherwise base URI will be handled as path in local file system.

      If empty string or relative URI string is passed as base URI, then it will be resolved against current working directory of this application instance.

      Parameters:
      baseUri - base URI against which all relative resource URIs will be resolved
      retriever - the resource retriever with the help of which data from resources will be retrieved
  • Method Details

    • getRetriever

      public IResourceRetriever getRetriever()
      Gets the resource retriever. The retriever is used to retrieve data from resources by URL.
      Returns:
      the resource retriever
    • setRetriever

      public ResourceResolver setRetriever (IResourceRetriever retriever)
      Sets the resource retriever. The retriever is used to retrieve data from resources by URL.
      Parameters:
      retriever - the resource retriever
      Returns:
      the ResourceResolver instance
    • retrieveImage

      public PdfXObject retrieveImage (String src)
      Retrieve image as either PdfImageXObject, or PdfFormXObject.
      Parameters:
      src - either link to file or base64 encoded stream
      Returns:
      PdfXObject on success, otherwise null
    • retrieveBytesFromResource

      public byte[] retrieveBytesFromResource (String src)
      Retrieve a resource as a byte array from a source that can either be a link to a file, or a base64 encoded String.
      Parameters:
      src - either link to file or base64 encoded stream
      Returns:
      byte[] on success, otherwise null
    • retrieveResourceAsInputStream

      public InputStream retrieveResourceAsInputStream (String src)
      Retrieve the resource found in src as an InputStream
      Parameters:
      src - path to the resource
      Returns:
      InputStream for the resource on success, otherwise null
    • isDataSrc

      public static boolean isDataSrc (String src)
      Checks if source is under data URI scheme. (eg data:[][;base64],).
      Parameters:
      src - string to test
      Returns:
      true if source is under data URI scheme
    • resolveAgainstBaseUri

      public URL resolveAgainstBaseUri (String uri) throws MalformedURLException
      Resolves a given URI against the base URI.
      Parameters:
      uri - the uri
      Returns:
      the url
      Throws:
      MalformedURLException - the malformed URL exception
    • resetCache

      public void resetCache()
      Resets the simple image cache.
    • tryResolveBase64ImageSource

      protected PdfXObject tryResolveBase64ImageSource (String src)
    • tryResolveUrlImageSource

      protected PdfXObject tryResolveUrlImageSource (String uri)
    • createImageByUrl

      protected PdfXObject createImageByUrl (URL url) throws Exception
      Create a iText XObject based on the image stored at the passed location.
      Parameters:
      url - location of the Image file.
      Returns:
      PdfXObject containing the Image loaded in.
      Throws:
      Exception - thrown if error occurred during fetching or constructing the image.