Example #1
0
 /**
  * Add all the contents of a Hashtable to the context.
  *
  * @param Context context to fill with objects
  * @param Hashtable source of objects
  */
 protected void fillContextHash(Context context, Hashtable objs) {
   Enumeration enm = objs.keys();
   while (enm.hasMoreElements()) {
     String key = enm.nextElement().toString();
     context.put(key, objs.get(key));
   }
 }
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 "";
    }
  }
Example #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 enm = props.propertyNames();

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

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

        try {
          Class cls = Class.forName(contextObj);
          Object o = cls.newInstance();
          context.put(contextName, o);
        } catch (Exception e) {
          e.printStackTrace();
          // TO DO: Log Something Here
        }
      }
    }
  }
Example #4
0
 /**
  * Add properties that will aways be in the context by default
  *
  * @param Context control context to fill with default values.
  */
 protected void fillContextDefaults(Context context) {
   context.put("generator", instance);
   context.put("outputDirectory", getOutputPath());
 }