Package org.apache.velocity.exception

Examples of org.apache.velocity.exception.ResourceNotFoundException


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

        /*
         *  some final cleanup
View Full Code Here


    public synchronized InputStream getResourceStream(String name)
        throws ResourceNotFoundException
    {
        if (name == null || name.length() == 0)
        {
            throw new ResourceNotFoundException("URLResourceLoader : No template name provided");
        }

        InputStream inputStream = null;
        Exception exception = null;
        for(int i=0; i < roots.length; i++)
        {
            try
            {
                URL u = new URL(roots[i] + name);
                inputStream = u.openStream();
               
                if (inputStream != null)
                {
                    if (log.isDebugEnabled()) log.debug("URLResourceLoader: Found '"+name+"' at '"+roots[i]+"'");

                    // save this root for later re-use
                    templateRoots.put(name, roots[i]);
                    break;
                }
            }
            catch(IOException ioe)
            {
                if (log.isDebugEnabled()) log.debug("URLResourceLoader: Exception when looking for '"+name+"' at '"+roots[i]+"'", ioe);

                // only save the first one for later throwing
                if (exception == null)
                {
                    exception = ioe;
                }
            }
        }

        // if we never found the template
        if (inputStream == null)
        {
            String msg;
            if (exception == null)
            {
                msg = "URLResourceLoader : Resource '" + name + "' not found.";
            }
            else
            {
                msg = exception.getMessage();
            }
            // convert to a general Velocity ResourceNotFoundException
            throw new ResourceNotFoundException(msg);
        }

        return inputStream;
    }
View Full Code Here

        {
            /*
             *  is == null, therefore we have some kind of file issue
             */

            errorCondition = new ResourceNotFoundException("Unknown resource error for resource " + name );
            throw errorCondition;
        }
    }
View Full Code Here

            /*
             * If we don't get a properly formed templateName then
             * there's not much we can do. So we'll forget about
             * trying to search any more paths for the template.
             */
            throw new ResourceNotFoundException( "Need to specify a file name or file path!" );
        }

        String template = StringUtils.normalizePath( templateName );
        if ( template == null || template.length() == 0 )
        {
            String msg = "Project Resource loader error : argument " + template
                + " contains .. and may be trying to access " + "content outside of template root.  Rejected.";

            rsvc.getLog().error( "ProjectResourceLoader : " + msg );

            throw new ResourceNotFoundException( msg );
        }

        /*
         *  if a / leads off, then just nip that :)
         */
        if ( template.startsWith( "/" ) )
        {
            template = template.substring( 1 );
        }
       
        // MCHANGES-118 adding the basedir path
        paths.add( (String) rsvc.getApplicationAttribute( "baseDirectory" ) );

        for ( String path : paths )
        {
            InputStream inputStream = findTemplate( path, template );

            if ( inputStream != null )
            {
                /*
                 * Store the path that this template came
                 * from so that we can check its modification
                 * time.
                 */

                templatePaths.put( templateName, path );
                return inputStream;
            }
        }

        /*
         * We have now searched all the paths for
         * templates and we didn't find anything so
         * throw an exception.
         */
        String msg = "ProjectResourceLoader Error: cannot find resource " + template;

        throw new ResourceNotFoundException( msg );
    }
View Full Code Here

            getLog().info( "Created template " + f );
        }

        catch ( ResourceNotFoundException rnfe )
        {
            throw new ResourceNotFoundException( "Template not found. ( " + templateDirectory + "/" + template + " )" );
        }
        catch ( VelocityException ve )
        {
            throw new VelocityException( ve.toString() );
        }
View Full Code Here

  {
    try
    {
      String template = templates.get(name);
      if (template == null)
        throw new ResourceNotFoundException("Resource " + name + " not found.");

      return new ByteArrayInputStream(template.getBytes("UTF-8"));
    }
    catch (UnsupportedEncodingException x)
    {
      throw new ResourceNotFoundException("Resource " + name
              + " has unsupported encoding. We support only utf-8.");
    }
  }
View Full Code Here

   * @throws Exception
   */
  private Template getTemplate( String[] path, String fn )
    throws Exception
  {
    ResourceNotFoundException rnfe = null;
   
    for (String p: path)
    {
      if (p == null)
        continue;
     
//      System.out.println( "trying to load template "+(p+fn) );
      try
      {
        if (Velocity.resourceExists( p+fn ))
          return Velocity.getTemplate( p+fn );
      }
      catch ( ResourceNotFoundException e )
      {
        rnfe = e;
      }
      catch ( Exception e )
      {
        System.out.println( "ignoring "+e);
      }
    }
   
    if (rnfe != null)
      throw rnfe;
   
    throw new ResourceNotFoundException("could not find resource: "+fn);
  }
View Full Code Here

    {
        InputStream results = null;

        if ( source == null || source.length() == 0)
        {
            throw new ResourceNotFoundException("Need to have a resource!");
        }
       
        String normalizedPath = StringUtils.normalizePath( source );
       
        if ( normalizedPath == null || normalizedPath.length() == 0 )
        {
            String msg = "JAR resource error : argument " + normalizedPath +
                " contains .. and may be trying to access " +
                "content outside of template root.  Rejected.";
           
            rsvc.error( "JarResourceLoader : " + msg );
           
            throw new ResourceNotFoundException ( msg );
        }
       
        /*
         *  if a / leads off, then just nip that :)
         */
        if ( normalizedPath.startsWith("/") )
        {
            normalizedPath = normalizedPath.substring(1);
        }
   
        if ( entryDirectory.containsKey( normalizedPath ) )
        {
            String jarurl  = (String)entryDirectory.get( normalizedPath );
           
            if ( jarfiles.containsKey( jarurl ) )
            {
                JarHolder holder = (JarHolder)jarfiles.get( jarurl );
                results =  holder.getResource( normalizedPath );
                return results;
            }
        }
       
        throw new ResourceNotFoundException( "JarResourceLoader Error: cannot find resource " +
          source );

    }
View Full Code Here

                 * If we don't get a properly formed templateName
                 * then there's not much we can do. So
                 * we'll forget about trying to search
                 * any more paths for the template.
                 */
                throw new ResourceNotFoundException(
                    "Need to specify a file name or file path!");
            }

            template = StringUtils.normalizePath(templateName);
            if ( template == null || template.length() == 0 )
            {
                String msg = "File resource error : argument " + template +
                    " contains .. and may be trying to access " +
                    "content outside of template root.  Rejected.";

                rsvc.error( "FileResourceLoader : " + msg );
     
                throw new ResourceNotFoundException ( msg );
            }

            /*
             *  if a / leads off, then just nip that :)
             */
            if ( template.startsWith("/") )
            {
                template = template.substring(1);
            }

            InputStream inputStream = findTemplate(path, template);
           
            if (inputStream != null)
            {
                /*
                 * Store the path that this template came
                 * from so that we can check its modification
                 * time.
                 */

                templatePaths.put(templateName, path);
                return inputStream;
            }               
        }
   
        /*
         * We have now searched all the paths for
         * templates and we didn't find anything so
         * throw an exception.
         */
         String msg = "FileResourceLoader Error: cannot find resource " +
          template;
   
         throw new ResourceNotFoundException( msg );
    }
View Full Code Here

    {
        InputStream result = null;
       
        if (name == null || name.length() == 0)
        {
            throw new ResourceNotFoundException ("No template name provided");
        }
       
        try
        {
            ClassLoader classLoader = this.getClass().getClassLoader();
            result= classLoader.getResourceAsStream( name );
        }
        catch( Exception fnfe )
        {
            /*
             *  log and convert to a general Velocity ResourceNotFoundException
             */
           
            throw new ResourceNotFoundException( fnfe.getMessage() );
        }
       
        return result;
    }
View Full Code Here

TOP

Related Classes of org.apache.velocity.exception.ResourceNotFoundException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.