Ejemplo n.º 1
0
        /// <summary>  Determines is a template exists, and returns name of the loader that
        /// provides it.  This is a slightly less hokey way to support
        /// the Velocity.templateExists() utility method, which was broken
        /// when per-template encoding was introduced.  We can revisit this.
        /// *
        /// </summary>
        /// <param name="resourceName">Name of template or content resource
        /// </param>
        /// <returns>class name of loader than can provide it
        ///
        /// </returns>
        public virtual System.String getLoaderNameForResource(System.String resourceName)
        {
            ResourceLoader resourceLoader = null;

            /*
             *  loop through our loaders...
             */
            for (int i = 0; i < resourceLoaders.Count; i++)
            {
                resourceLoader = (ResourceLoader)resourceLoaders[i];

                System.IO.Stream is_Renamed = null;

                /*
                 *  if we find one that can provide the resource,
                 *  return the name of the loaders's Class
                 */
                try {
                    is_Renamed = resourceLoader.getResourceStream(resourceName);

                    if (is_Renamed != null)
                    {
                        return(resourceLoader.GetType().ToString());
                    }
                } catch (ResourceNotFoundException e) {
                    /*
                     * this isn't a problem.  keep going
                     */
                } finally {
                    /*
                     *  if we did find one, clean up because we were
                     *  returned an open stream
                     */
                    if (is_Renamed != null)
                    {
                        try {
                            is_Renamed.Close();
                        } catch (System.IO.IOException ioe) {}
                    }
                }
            }

            return(null);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Loads a resource from the current set of resource loaders
        /// </summary>
        /// <param name="resourceName">The name of the resource to retrieve.</param>
        /// <param name="resourceType">The type of resource (<code>RESOURCE_TEMPLATE</code>,
        /// <code>RESOURCE_CONTENT</code>, etc.).
        /// </param>
        /// <param name="encoding"> The character encoding to use.</param>
        /// <returns>Resource with the template parsed and ready.
        /// @throws ResourceNotFoundException if template not found
        /// from any available source.
        /// @throws ParseErrorException if template cannot be parsed due
        /// to syntax (or other) error.
        /// @throws Exception if a problem in parse
        /// </returns>
        protected internal virtual Resource loadResource(System.String resourceName, int resourceType, System.String encoding)
        {
            Resource resource = ResourceFactory.getResource(resourceName, resourceType);

            resource.RuntimeServices = rsvc;

            resource.Name     = resourceName;
            resource.Encoding = encoding;

            /*
             * Now we have to try to find the appropriate
             * loader for this resource. We have to cycle through
             * the list of available resource loaders and see
             * which one gives us a stream that we can use to
             * make a resource with.
             */

            long howOldItWas = 0; // Initialize to avoid warnings

            ResourceLoader resourceLoader = null;

            for (int i = 0; i < resourceLoaders.Count; i++)
            {
                resourceLoader          = (ResourceLoader)resourceLoaders[i];
                resource.ResourceLoader = resourceLoader;

                /*
                 *  catch the ResourceNotFound exception
                 *  as that is ok in our new multi-loader environment
                 */

                try {
                    if (resource.Process())
                    {
                        /*
                         *  FIXME  (gmj)
                         *  moved in here - technically still
                         *  a problem - but the resource needs to be
                         *  processed before the loader can figure
                         *  it out due to to the new
                         *  multi-path support - will revisit and fix
                         */

                        if (logWhenFound)
                        {
                            rsvc.info("ResourceManager : found " + resourceName + " with loader " + resourceLoader.ClassName);
                        }

                        howOldItWas = resourceLoader.getLastModified(resource);
                        break;
                    }
                } catch (ResourceNotFoundException rnfe) {
                    /*
                     *  that's ok - it's possible to fail in
                     *  multi-loader environment
                     */
                }
            }

            /*
             * Return null if we can't find a resource.
             */
            if (resource.Data == null)
            {
                throw new ResourceNotFoundException("Unable to find resource '" + resourceName + "'");
            }

            /*
             *  some final cleanup
             */

            resource.LastModified = howOldItWas;

            resource.ModificationCheckInterval = resourceLoader.ModificationCheckInterval;

            resource.Touch();

            return(resource);
        }