Example #1
0
  /**
   * gets the named resource as a stream, parses and inits
   *
   * @return true if successful
   * @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 some other problem, should only be from initialization of the template AST.
   */
  public boolean process() throws ResourceNotFoundException, ParseErrorException, Exception {
    data = null;
    InputStream is = null;
    errorCondition = null;

    /*
     *  first, try to get the stream from the loader
     */
    try {
      is = resourceLoader.getResourceStream(name);
    } catch (ResourceNotFoundException rnfe) {
      /*
       *  remember and re-throw
       */

      errorCondition = rnfe;
      throw rnfe;
    }

    /*
     *  if that worked, lets protect in case a loader impl
     *  forgets to throw a proper exception
     */

    if (is != null) {
      /*
       *  now parse the template
       */

      try {
        BufferedReader br = new BufferedReader(new InputStreamReader(is, encoding));

        data = rsvc.parse(br, name);
        initDocument();
        return true;
      } catch (UnsupportedEncodingException uce) {
        String msg =
            "Template.process : Unsupported input encoding : " + encoding + " for template " + name;

        errorCondition = new ParseErrorException(msg);
        throw errorCondition;
      } catch (ParseException pex) {
        /*
         *  remember the error and convert
         */

        errorCondition = new ParseErrorException(pex.getMessage());
        throw errorCondition;
      } catch (Exception e) {
        /*
         *  who knows?  Something from initDocument()
         */

        errorCondition = e;
        throw e;
      } finally {
        /*
         *  Make sure to close the inputstream when we are done.
         */
        is.close();
      }
    } else {
      /*
       *  is == null, therefore we have some kind of file issue
       */

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