Class SchemaResolver


  • public class SchemaResolver
    extends Object
    XML Schema resolver that maps absolute URLs to local URL resources.

    Resources are sought, in order:

    1. In an OASIS Catalog (with URI resolution semantics), which maps URLs to arbitrary filesystem locations.
    2. On the classpath, where resources are located by their Simple HTTP Resource Path (see getSimpleHttpResourcePath(URI)).
    3. In a cache, with optional downloading support.
    Author:
    Ben Caradoc-Davies (CSIRO Earth Science and Resource Engineering)
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      SchemaCache getCache()
      Cache used by this Schema Resolver.
      static URL getClasspathResourceUrl​(String location)
      Return the URL for a resource found on the classpath at the Simple HTTP Resource Path.
      static String getSimpleHttpResourcePath​(String location)
      Return the Simple HTTP Resource Path for an absolute http/https URL.
      static String getSimpleHttpResourcePath​(String location, boolean keepQuery)
      Return the Simple HTTP Resource Path for an absolute http/https URL.
      static String getSimpleHttpResourcePath​(URI location)
      Return the Simple HTTP Resource Path for an absolute http/https URL.
      static String getSimpleHttpResourcePath​(URI location, boolean keepQuery)
      Return the Simple HTTP Resource Path for an absolute http/https URL.
      String resolve​(String location)
      Resolve an absolute URL to a local file or jar URL.
      String resolve​(String location, String context)
      Resolve an absolute or relative URL to a local file or jar URL.
      static String resolveClasspathLocation​(String location)
      Return the string representation of URL for a resource found on the classpath at the Simple HTTP Resource Path.
      • Methods inherited from class Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • SchemaResolver

        public SchemaResolver​(SchemaCatalog catalog,
                              boolean classpath,
                              SchemaCache cache)
        Constructor.
        Parameters:
        classpath - whether schemas can be located on the classpath
      • SchemaResolver

        public SchemaResolver()
        Convenience constructor for a resolver with neither catalog nor cache (just classpath).
      • SchemaResolver

        public SchemaResolver​(SchemaCatalog catalog)
        Convenience constructor for a resolver with no cache.
      • SchemaResolver

        public SchemaResolver​(SchemaCache cache)
        Convenience constructor for a resolver with no catalog.
    • Method Detail

      • resolve

        public String resolve​(String location,
                              String context)
        Resolve an absolute or relative URL to a local file or jar URL. Relative URLs are resolved against a context schema URL if provided.
        Parameters:
        location - an absolute or relative URL for a schema
        context - an absolute URL specifying the context schema of a relative location, or null if none
        Returns:
        the string representation of a file or jar URL
        Throws:
        RuntimeException - if a local resource could not be found
      • getCache

        public SchemaCache getCache()
        Cache used by this Schema Resolver. Could be null if not set.
      • resolve

        public String resolve​(String location)
        Resolve an absolute URL to a local file or jar URL.
        Parameters:
        location - an absolute URL
        Returns:
        the string representation of a file or jar URL
        Throws:
        RuntimeException - if a local resource could not be found
      • getSimpleHttpResourcePath

        public static String getSimpleHttpResourcePath​(String location)
        Return the Simple HTTP Resource Path for an absolute http/https URL. Does not include query components in the path.
        Parameters:
        location - not null
        Returns:
        the resource path with a leading slash
        See Also:
        getSimpleHttpResourcePath(URI)
      • getSimpleHttpResourcePath

        public static String getSimpleHttpResourcePath​(String location,
                                                       boolean keepQuery)
        Return the Simple HTTP Resource Path for an absolute http/https URL.
        Parameters:
        location - not null
        keepQuery - indicates whether or not the query components should be included in the path. If this is set to true then the query portion is converted to an MD5 message digest and that string is used to identify the file in the cache.
        Returns:
        the resource path with a leading slash
        See Also:
        getSimpleHttpResourcePath(URI, boolean)
      • getSimpleHttpResourcePath

        public static String getSimpleHttpResourcePath​(URI location)
        Return the Simple HTTP Resource Path for an absolute http/https URL.

        The Simple HTTP Resource Path maps an HTTP or HTTPS URL to a path on the classpath or relative to some other root. To form the Simple HTTP Resource Path from an http/https URL:

        1. Protocol, port, fragment, and query are ignored.
        2. Take the host name, split it into its components, reverse their order, prepend a forward slash to each, and concatenate them.
        3. Append the path component of the URL.
        For example http://schemas.example.org/exampleml/exml.xsd becomes /org/example/schemas/exampleml/exml.xsd .

        The Simple HTTP Resource Path always starts with a forward slash (if not null). Does not include query components in the path.

        Parameters:
        location - not null
        Returns:
        the Simple HTTP Resource Path as a string, or null if the URI is not an absolute HTTP/HTTPS URL.
      • getSimpleHttpResourcePath

        public static String getSimpleHttpResourcePath​(URI location,
                                                       boolean keepQuery)
        Return the Simple HTTP Resource Path for an absolute http/https URL.

        The Simple HTTP Resource Path maps an HTTP or HTTPS URL to a path on the classpath or relative to some other root. To form the Simple HTTP Resource Path from an http/https URL:

        1. Protocol, port, fragment, and query are ignored.
        2. Take the host name, split it into its components, reverse their order, prepend a forward slash to each, and concatenate them.
        3. Append the path component of the URL.
        For example http://schemas.example.org/exampleml/exml.xsd becomes /org/example/schemas/exampleml/exml.xsd .

        The Simple HTTP Resource Path always starts with a forward slash (if not null). Does not include query components in the path.

        Parameters:
        location - not null
        keepQuery - indicates whether or not the query components should be included in the path. If this is set to true then the query portion is converted to an MD5 message digest and that string is used to identify the file in the cache.
        Returns:
        the Simple HTTP Resource Path as a string, or null if the URI is not an absolute HTTP/HTTPS URL.
      • getClasspathResourceUrl

        public static URL getClasspathResourceUrl​(String location)
        Return the URL for a resource found on the classpath at the Simple HTTP Resource Path. This allows (for example) schema documents in jar files to be loaded from the classpath using their canonical HTTP URLs.
        Returns:
        the URL or null if not found
      • resolveClasspathLocation

        public static String resolveClasspathLocation​(String location)
        Return the string representation of URL for a resource found on the classpath at the Simple HTTP Resource Path. This allows (for example) schema documents in jar files to be loaded from the classpath using their canonical HTTP URLs.
        Returns:
        the string representation of a classpath URL, or null if not found