/**
   * Get an InputStream so that the Runtime can build a template with it.
   *
   * @param name name of template to get
   * @return InputStream containing the template
   * @throws ResourceNotFoundException if template not found in classpath.
   */
  public InputStream getResourceStream(String name) throws ResourceNotFoundException {
    InputStream result = null;

    if (StringUtils.isEmpty(name)) {
      throw new ResourceNotFoundException("No template name provided");
    }

    /**
     * look for resource in thread classloader first (e.g. WEB-INF\lib in a servlet container) then
     * fall back to the system classloader.
     */
    try {
      result = ClassUtils.getResourceAsStream(getClass(), name);
    } catch (Exception fnfe) {
      throw (ResourceNotFoundException)
          ExceptionUtils.createWithCause(
              ResourceNotFoundException.class, "problem with template: " + name, fnfe);
    }

    if (result == null) {
      String msg = "ClasspathResourceLoader Error: cannot find resource " + name;

      throw new ResourceNotFoundException(msg);
    }

    return result;
  }
  public StringResourceRepository createRepository(final String className, final String encoding) {
    if (log.isDebugEnabled()) {
      log.debug("Creating string repository using class " + className + "...");
    }

    StringResourceRepository repo;
    try {
      repo = (StringResourceRepository) ClassUtils.getNewInstance(className);
    } catch (ClassNotFoundException cnfe) {
      throw new VelocityException("Could not find '" + className + "'", cnfe);
    } catch (IllegalAccessException iae) {
      throw new VelocityException("Could not access '" + className + "'", iae);
    } catch (InstantiationException ie) {
      throw new VelocityException("Could not instantiate '" + className + "'", ie);
    }

    if (encoding != null) {
      repo.setEncoding(encoding);
    } else {
      repo.setEncoding(REPOSITORY_ENCODING_DEFAULT);
    }

    if (log.isDebugEnabled()) {
      log.debug("Default repository encoding is " + repo.getEncoding());
    }
    return repo;
  }
示例#3
0
  /**
   * Add objects to the context from the current properties.
   *
   * @param context control context to fill with objects that are specified in the
   *     default.properties file
   */
  protected void fillContextProperties(Context context) {
    Enumeration enumeration = props.propertyNames();

    while (enumeration.hasMoreElements()) {
      String nm = (String) enumeration.nextElement();
      if (nm.startsWith("context.objects.")) {

        String contextObj = props.getProperty(nm);
        int colon = nm.lastIndexOf('.');
        String contextName = nm.substring(colon + 1);

        try {
          Object o = ClassUtils.getNewInstance(contextObj);
          context.put(contextName, o);
        } catch (Exception e) {
          e.printStackTrace();
          // TO DO: Log Something Here
        }
      }
    }
  }