Class ResourceLoader


  • public class ResourceLoader
    extends java.lang.Object
    The class provides a static method for loading resources from a URL. This method follows HTTP 301 and 302 redirects.
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static int MAX_REDIRECTS
      The maximum number of redirects to follow before throwing an IOException.
    • Constructor Summary

      Constructors 
      Constructor Description
      ResourceLoader()  
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      static java.io.BufferedReader getReaderFromStream​(java.io.InputStream inputStream, java.lang.String resourceEncoding)
      Get a reader corresponding to a binary input stream and an encoding.
      static javax.xml.transform.stream.StreamSource typedStreamSource​(Configuration config, java.lang.String url)  
      static java.net.URLConnection urlConnection​(java.net.URL url)
      Open a URLConnection to the resource identified by the URI.
      static java.io.Reader urlReader​(Configuration config, java.lang.String url, java.lang.String requestedEncoding)
      Open a reader to retrieve the content identified by the URI.
      static java.io.InputStream urlStream​(Configuration config, java.lang.String url)
      Open a stream to retrieve the content identified by the URI.
      • Methods inherited from class java.lang.Object

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

      • MAX_REDIRECTS

        public static int MAX_REDIRECTS
        The maximum number of redirects to follow before throwing an IOException. If you allow the underlying Java URL class to follow redirects, it gives up after 20 hops.
    • Constructor Detail

      • ResourceLoader

        public ResourceLoader()
    • Method Detail

      • urlConnection

        public static java.net.URLConnection urlConnection​(java.net.URL url)
                                                    throws java.io.IOException
        Open a URLConnection to the resource identified by the URI. For HTTP URIs, this method will follow up to MAX_REDIRECTS redirects or until it detects a loop; the connection returned in this case is to the first resource that did not return a 301 or 302 response code.
        Parameters:
        url - The URL to retrieve.
        Returns:
        An InputStream for the resource content.
        Throws:
        java.io.IOException - If more than MAX_REDIRECTS are occur or if a loop is detected.
      • urlStream

        public static java.io.InputStream urlStream​(Configuration config,
                                                    java.lang.String url)
                                             throws java.io.IOException
        Open a stream to retrieve the content identified by the URI. If the URI is a classpath: URI, then it will be retrieved with the configuration's dynamic loader. For HTTP URIs, this method will follow up to MAX_REDIRECTS redirects or until it detects a loop. This method automatically accepts and decompresses gzip encoded responses.
        Parameters:
        config - The current configuration.
        url - The URL to retrieve.
        Returns:
        An InputStream for the resource content.
        Throws:
        java.io.IOException - If more than MAX_REDIRECTS are occur or if a loop is detected.
      • typedStreamSource

        public static javax.xml.transform.stream.StreamSource typedStreamSource​(Configuration config,
                                                                                java.lang.String url)
                                                                         throws java.io.IOException
        Throws:
        java.io.IOException
      • urlReader

        public static java.io.Reader urlReader​(Configuration config,
                                               java.lang.String url,
                                               java.lang.String requestedEncoding)
                                        throws java.io.IOException
        Open a reader to retrieve the content identified by the URI. This handles HTTP redirects in the same way as urlStream(net.sf.saxon.Configuration, java.lang.String), but then it also wraps the stream in a Reader, using the logic prescribed for the fn:unparsed-text function.
        Parameters:
        url - The URL to retrieve.
        requestedEncoding - The requested encoding. This is used only as a fallback, following the rules of the fn:unparsed-text specification
        Returns:
        A Reader for the resource content.
        Throws:
        java.io.IOException - If more than MAX_REDIRECTS are occur or if a loop is detected.
      • getReaderFromStream

        public static java.io.BufferedReader getReaderFromStream​(java.io.InputStream inputStream,
                                                                 java.lang.String resourceEncoding)
                                                          throws java.io.UnsupportedEncodingException
        Get a reader corresponding to a binary input stream and an encoding. The mapping is such that any encoding errors that are detected lead to a fatal error, rather than being repaired or ignored
        Parameters:
        inputStream - the input stream. Non-null.
        resourceEncoding - the encoding. Non-null
        Returns:
        a corresponding reader.
        Throws:
        java.io.UnsupportedEncodingException - if there's a problem with the encoding