Here are the examples of the java api class bboss.org.apache.velocity.app.VelocityEngine taken from open source projects.
1. TexenTask#execute()
Project: bboss
File: TexenTask.java
File: TexenTask.java
/** * Execute the input script with Velocity * * @throws BuildException * BuildExceptions are thrown when required attributes are missing. * Exceptions thrown by Velocity are rethrown as BuildExceptions. */ public void execute() throws BuildException { // Make sure the template path is set. if (templatePath == null && useClasspath == false) { throw new BuildException("The template path needs to be defined if you are not using " + "the classpath for locating templates!"); } // Make sure the control template is set. if (controlTemplate == null) { throw new BuildException("The control template needs to be defined!"); } // Make sure the output directory is set. if (outputDirectory == null) { throw new BuildException("The output directory needs to be defined!"); } // Make sure there is an output file. if (outputFile == null) { throw new BuildException("The output file needs to be defined!"); } VelocityEngine ve = new VelocityEngine(); try { // Setup the Velocity Runtime. if (templatePath != null) { log("Using templatePath: " + templatePath, Project.MSG_VERBOSE); ve.setProperty(RuntimeConstants.FILE_RESOURCE_LOADER_PATH, templatePath); } if (useClasspath) { log("Using classpath"); ve.addProperty(VelocityEngine.RESOURCE_LOADER, "classpath"); ve.setProperty("classpath." + VelocityEngine.RESOURCE_LOADER + ".class", "bboss.org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"); ve.setProperty("classpath." + VelocityEngine.RESOURCE_LOADER + ".cache", useResourceLoaderCache); ve.setProperty("classpath." + VelocityEngine.RESOURCE_LOADER + ".modificationCheckInterval", resourceLoaderModificationCheckInterval); } if (this.logFile != null) { ve.setProperty(RuntimeConstants.RUNTIME_LOG, this.logFile); } ve.init(); // Create the text generator. Generator generator = Generator.getInstance(); generator.setVelocityEngine(ve); generator.setOutputPath(outputDirectory); generator.setInputEncoding(inputEncoding); generator.setOutputEncoding(outputEncoding); if (templatePath != null) { generator.setTemplatePath(templatePath); } // Make sure the output directory exists, if it doesn't // then create it. File file = new File(outputDirectory); if (!file.exists()) { file.mkdirs(); } String path = outputDirectory + File.separator + outputFile; log("Generating to file " + path, Project.MSG_INFO); Writer writer = generator.getWriter(path, outputEncoding); // The generator and the output path should // be placed in the init context here and // not in the generator class itself. Context c = initControlContext(); // Everything in the generator class should be // pulled out and placed in here. What the generator // class does can probably be added to the Velocity // class and the generator class can probably // be removed all together. populateInitialContext(c); // in the control/worker templates. if (contextProperties != null) { Iterator i = contextProperties.getKeys(); while (i.hasNext()) { String property = (String) i.next(); String value = StringUtils.nullTrim(contextProperties.getString(property)); // into the context as an Integer. try { c.put(property, new Integer(value)); } catch (NumberFormatException nfe) { String booleanString = contextProperties.testBoolean(value); if (booleanString != null) { c.put(property, Boolean.valueOf(booleanString)); } else { if (property.endsWith("file.contents")) { value = StringUtils.fileContentsToString(project.resolveFile(value).getCanonicalPath()); property = property.substring(0, property.indexOf("file.contents") - 1); } c.put(property, value); } } } } writer.write(generator.parse(controlTemplate, c)); writer.flush(); writer.close(); generator.shutdown(); cleanup(); } catch (BuildException e) { throw e; } catch (MethodInvocationException e) { throw new BuildException("Exception thrown by '" + e.getReferenceName() + "." + e.getMethodName() + "'" + ERR_MSG_FRAGMENT, e.getWrappedThrowable()); } catch (ParseErrorException e) { throw new BuildException("Velocity syntax error" + ERR_MSG_FRAGMENT, e); } catch (ResourceNotFoundException e) { throw new BuildException("Resource not found" + ERR_MSG_FRAGMENT, e); } catch (Exception e) { throw new BuildException("Generation failed" + ERR_MSG_FRAGMENT, e); } }