示例#1
0
  /**
   * @param va
   * @param fw
   * @throws Exception
   * @throws IOException
   */
  public void generate(VelocityAction va, Writer fw) throws Exception, IOException {
    Assert.isNotNull(va.getContext(), "velocity context must be given");
    Assert.isNotNull(va.getTemplatePath(), "velocity template must be given");

    // get Template
    VelocityContext context = va.getContext();
    String templatePath = va.getTemplatePath();
    InputStream template = null;
    File templateFile = new File(templatePath);
    if (templateFile.exists()) {
      // the template is located on File System
      template = new FileInputStream(templateFile);
    } else {
      // resource may be acceded in classPath
      template = getClass().getResourceAsStream(templatePath);
    }

    Assert.isNotNull(template, "velocity template resource can't be loaded");
    // execute
    Velocity.init();
    String s = IOUtils.toString(template);
    Velocity.evaluate(context, fw, "log", s);

    // close streams
    template.close();
    fw.flush();
    fw.close();
  }
示例#2
0
  public void generateAll() throws Exception {
    for (VelocityAction va : vActions) {
      Assert.isNotNull(
          va.getOutputFilePath(),
          "this velocity generator must have an output stream where to write");
      // the output is a path where it's possible to obtain a stream
      String out = va.getOutputFilePath();
      File f = new File(out);
      Writer fw = new FileWriter(f);

      generate(va, fw);
    }
  }