public Reader getResourceReader()

in velocity-engine-core/src/main/java/org/apache/velocity/runtime/resource/loader/JarResourceLoader.java [179:242]


    public Reader getResourceReader(String source, String encoding )
            throws ResourceNotFoundException
    {
        Reader result = null;

        if (org.apache.commons.lang3.StringUtils.isEmpty(source))
        {
            throw new ResourceNotFoundException("Need to have a resource!");
        }

        String normalizedPath = FilenameUtils.normalize( source, true );

        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.";

            log.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  = entryDirectory.get( normalizedPath );

            if ( jarfiles.containsKey( jarurl ) )
            {
                JarHolder holder = (JarHolder)jarfiles.get( jarurl );
                InputStream rawStream = holder.getResource( normalizedPath );
                try
                {
                    return buildReader(rawStream, encoding);
                }
                catch (Exception e)
                {
                    if (rawStream != null)
                    {
                        try
                        {
                            rawStream.close();
                        }
                        catch (IOException ioe) {}
                    }
                    String msg = "JAR resource error: Exception while loading " + source;
                    log.error(msg, e);
                    throw new VelocityException(msg, e, rsvc.getLogContext().getStackTrace());
                }
            }
        }

        throw new ResourceNotFoundException( "JarResourceLoader Error: cannot find resource " +
                source );

    }