Class ImportSupport

  • Direct Known Subclasses:
    JsonTool

    @InvalidScope({"application","session","request"})
    public class ImportSupport
    extends SafeConfig

    Provides methods to import arbitrary local or remote resources as strings, generic version.

    Based on ImportSupport from the JSTL taglib by Shawn Bayern

    Since:
    VelocityTools 3.0
    Version:
    $$
    Author:
    Marino A. Jonsson, Claude Brisson
    • Constructor Summary

      Constructors 
      Constructor Description
      ImportSupport()  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected java.io.Reader acquireLocalURLReader​(java.lang.String url)
      Acquire a reader to a local URL - non applicable to the generic version of ImportSupport
      protected java.lang.String acquireLocalURLString​(java.lang.String url)
      Aquire the content of a local URL.
      java.io.Reader acquireReader​(java.lang.String url)
      Acquire a reader to an URL
      protected java.io.Reader acquireRemoteURLReader​(java.lang.String url)
      Acquire a reader to a remote URL
      protected java.lang.String acquireRemoteURLString​(java.lang.String url)
      Aquire the content of a remote URL.
      java.lang.String acquireString​(java.lang.String url)  
      protected void configure​(ValueParser values)
      Configure import support
      protected java.net.URL getClasspathResource​(java.lang.String resource)
      Classpath entry URL builder
      static java.lang.String getContentTypeAttribute​(java.lang.String input, java.lang.String name)
      Get the value associated with a content-type attribute.
      protected java.net.URL getFileResource​(java.lang.String resource)
      Overridable local file URL builder.
      static java.lang.String getProtocol​(java.lang.String url)
      Returns protocol, or null for a local URL
      java.io.Reader getResourceReader​(java.lang.String resource)
      Get a reader of a local resource, first trying with a file (or a webapp resource for the view flavor) then with a classpath entry.
      java.lang.String getResourceString​(java.lang.String resource)
      Fetch a local resource, first trying with a file (or a webapp resource for the view flavor) then with a classpath entry.
      static boolean isRemoteURL​(java.lang.String url)
      Returns whether an URL is remote or local
      void setSafeMode​(boolean safe)
      Sets or clears safe mode
      • Methods inherited from class java.lang.Object

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

      • VALID_SCHEME_CHARS

        protected static final java.lang.String VALID_SCHEME_CHARS
        See Also:
        Constant Field Values
      • RESOURCE_KEY

        public static final java.lang.String RESOURCE_KEY
        Configuration key for XmlTool and JsonTool, used to specify a local resource
        See Also:
        Constant Field Values
      • URL_KEY

        public static final java.lang.String URL_KEY
        Configuration key for ImportTool, XmlTool and JsonTool, used to specify a local or remote source URL
        See Also:
        Constant Field Values
    • Constructor Detail

      • ImportSupport

        public ImportSupport()
    • Method Detail

      • configure

        protected void configure​(ValueParser values)
        Configure import support
        Overrides:
        configure in class SafeConfig
        Parameters:
        values - configuration values
      • setSafeMode

        public void setSafeMode​(boolean safe)
        Sets or clears safe mode
        Overrides:
        setSafeMode in class SafeConfig
        Parameters:
        safe - flag value
      • acquireString

        public java.lang.String acquireString​(java.lang.String url)
                                       throws java.io.IOException
        Parameters:
        url - the URL resource to return as string
        Returns:
        the URL resource as string
        Throws:
        java.io.IOException - if operation failed
      • acquireRemoteURLString

        protected java.lang.String acquireRemoteURLString​(java.lang.String url)
                                                   throws java.io.IOException
        Aquire the content of a remote URL.
        Parameters:
        url - remote URL
        Returns:
        the URL resource as string
        Throws:
        java.io.IOException - if operation failed
      • acquireLocalURLString

        protected java.lang.String acquireLocalURLString​(java.lang.String url)
                                                  throws java.io.IOException
        Aquire the content of a local URL.
        Parameters:
        url - local URL
        Returns:
        the URL resource as string
        Throws:
        java.io.IOException - if operation failed
      • acquireReader

        public java.io.Reader acquireReader​(java.lang.String url)
                                     throws java.io.IOException
        Acquire a reader to an URL
        Parameters:
        url - the URL to read
        Returns:
        a Reader for the InputStream created from the supplied URL
        Throws:
        java.io.IOException - if operation failed
      • acquireRemoteURLReader

        protected java.io.Reader acquireRemoteURLReader​(java.lang.String url)
                                                 throws java.io.IOException
        Acquire a reader to a remote URL
        Parameters:
        url - the URL to read
        Returns:
        a Reader for the InputStream created from the supplied URL
        Throws:
        java.io.IOException - if operation failed
      • acquireLocalURLReader

        protected java.io.Reader acquireLocalURLReader​(java.lang.String url)
                                                throws java.io.IOException
        Acquire a reader to a local URL - non applicable to the generic version of ImportSupport
        Parameters:
        url - the URL to read
        Returns:
        a Reader for the InputStream created from the supplied URL
        Throws:
        java.io.IOException - if operation failed
      • isRemoteURL

        public static boolean isRemoteURL​(java.lang.String url)
        Returns whether an URL is remote or local
        Parameters:
        url - the url to check out
        Returns:
        wether the URL is remote
      • getProtocol

        public static java.lang.String getProtocol​(java.lang.String url)
        Returns protocol, or null for a local URL
        Parameters:
        url - the url to check out
        Returns:
        found protocol or null for a local URL
      • getContentTypeAttribute

        public static java.lang.String getContentTypeAttribute​(java.lang.String input,
                                                               java.lang.String name)
        Get the value associated with a content-type attribute. Syntax defined in RFC 2045, section 5.1.
        Parameters:
        input - the string containing the attributes
        name - the name of the content-type attribute
        Returns:
        the value associated with a content-type attribute
      • getResourceString

        public java.lang.String getResourceString​(java.lang.String resource)
        Fetch a local resource, first trying with a file (or a webapp resource for the view flavor) then with a classpath entry.
        Parameters:
        resource - the resource to read
        Returns:
        the content of the resource
      • getResourceReader

        public java.io.Reader getResourceReader​(java.lang.String resource)
        Get a reader of a local resource, first trying with a file (or a webapp resource for the view flavor) then with a classpath entry.
        Parameters:
        resource - the resource to read
        Returns:
        a reader of the resource
      • getFileResource

        protected java.net.URL getFileResource​(java.lang.String resource)
                                        throws java.lang.Exception
        Overridable local file URL builder.
        Parameters:
        resource - the resource to read
        Returns:
        the content of the resource
        Throws:
        java.lang.Exception - if operation failed
      • getClasspathResource

        protected java.net.URL getClasspathResource​(java.lang.String resource)
                                             throws java.lang.Exception
        Classpath entry URL builder
        Parameters:
        resource - the resource to read
        Returns:
        the content of the resource
        Throws:
        java.lang.Exception - if operation failed