Example #1
0
  /**
   * Parse the control template and merge it with the control context. This is the starting point in
   * texen.
   *
   * @param String control template
   * @param Context control context
   * @return String generated output
   */
  public String parse(String controlTemplate, Context controlContext) throws Exception {
    this.controlContext = controlContext;
    fillContextDefaults(this.controlContext);
    fillContextProperties(this.controlContext);

    Template template = getTemplate(controlTemplate, inputEncoding);
    StringWriter sw = new StringWriter();
    template.merge(controlContext, sw);

    return sw.toString();
  }
Example #2
0
  /**
   * Parse an input and write the output to an output file. If the output file parameter is null or
   * an empty string the result is returned as a string object. Otherwise an empty string is
   * returned. You can add objects to the context with the objs Hashtable.
   *
   * @param String input template
   * @param String inputEncoding template encoding
   * @param String output file
   * @param String outputEncoding encoding of output file
   * @param String id for object to be placed in the control context
   * @param String object to be placed in the context
   * @return String generated output from velocity
   */
  public String parse(
      String inputTemplate,
      String intputEncoding,
      String outputFile,
      String outputEncoding,
      String objectID,
      Object object)
      throws Exception {
    if (objectID != null && object != null) {
      controlContext.put(objectID, object);
    }

    Template template =
        getTemplate(inputTemplate, inputEncoding != null ? inputEncoding : this.inputEncoding);

    if (outputFile == null || outputFile.equals("")) {
      StringWriter sw = new StringWriter();
      template.merge(controlContext, sw);
      return sw.toString();
    } else {
      Writer writer = null;

      if (writers.get(outputFile) == null) {
        /*
         * We have never seen this file before so create
         * a new file writer for it.
         */
        writer =
            getWriter(
                getOutputPath() + File.separator + outputFile,
                outputEncoding != null ? outputEncoding : this.outputEncoding);

        /*
         * Place the file writer in our collection
         * of file writers.
         */
        writers.put(outputFile, writer);
      } else {
        writer = (Writer) writers.get(outputFile);
      }

      VelocityContext vc = new VelocityContext(controlContext);
      template.merge(vc, writer);

      // commented because it is closed in shutdown();
      // fw.close();

      return "";
    }
  }