Ejemplo n.º 1
0
  /**
   * Get an InputStream so that the Runtime can build a template with it.
   *
   * @param source name of template to get
   * @return InputStream containing the template
   * @throws ResourceNotFoundException if template not found in the file template path.
   */
  public InputStream getResourceAsInputStream(String source) throws ResourceNotFoundException {
    InputStream results = null;

    if (source == null || source.length() == 0) {
      throw new ResourceNotFoundException("Need to have a resource!");
    }

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

      getLogger().error("JarResourceLoader : " + msg);

      throw new ResourceNotFoundException(msg);
    }

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

    if (entryDirectory.containsKey(source)) {
      String jarurl = (String) entryDirectory.get(source);

      if (jarfiles.containsKey(jarurl)) {
        JarHolder holder = (JarHolder) jarfiles.get(jarurl);

        results = holder.getResource(source);

        return results;
      }
    }

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