Search in sources :

Example 6 with VelocityEngine

use of org.apache.velocity.app.VelocityEngine in project bazel by bazelbuild.

the class TemplateEngine method newPage.

/**
   * Returns a new {@link Page} using the given .vm template file path. The template file
   * path must be the resource path for the .vm file in the JAR since the VelocityEngine
   * is configured to load .vm files from JAR resources.
   */
public static Page newPage(String template) {
    VelocityEngine engine = new VelocityEngine();
    engine.setProperty("resource.loader", "classpath, jar");
    engine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
    engine.setProperty("jar.resource.loader.class", JarResourceLoader.class.getName());
    engine.setProperty("input.encoding", "UTF-8");
    engine.setProperty("output.encoding", "UTF-8");
    engine.setProperty("directive.set.null.allowed", true);
    engine.setProperty("parser.pool.size", 3);
    engine.setProperty("runtime.references.strict", true);
    return new Page(engine, template);
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) JarResourceLoader(org.apache.velocity.runtime.resource.loader.JarResourceLoader) ClasspathResourceLoader(org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader)

Example 7 with VelocityEngine

use of org.apache.velocity.app.VelocityEngine in project gocd by gocd.

the class VelocityFixture method renderMacro.

public String renderMacro(String template, HashMap model) throws Exception {
    String path = new File("../server/webapp/WEB-INF/vm").getCanonicalPath();
    Properties properties = new Properties();
    properties.setProperty(Velocity.RESOURCE_LOADER, "file");
    properties.setProperty(Velocity.FILE_RESOURCE_LOADER_PATH, path);
    properties.setProperty(Velocity.FILE_RESOURCE_LOADER_CACHE, "false");
    VelocityEngine engine = new VelocityEngine();
    engine.init(properties);
    Template t = engine.getTemplate(template + ".vm");
    VelocityContext ctx = new VelocityContext();
    for (Object key : model.keySet()) {
        ctx.put((String) key, model.get(key));
    }
    ctx.put("req", new FakeRequest());
    Writer writer = new StringWriter();
    t.merge(ctx, writer);
    return writer.toString();
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) Properties(java.util.Properties) File(java.io.File) StringWriter(java.io.StringWriter) Writer(java.io.Writer) Template(org.apache.velocity.Template)

Example 8 with VelocityEngine

use of org.apache.velocity.app.VelocityEngine in project midpoint by Evolveum.

the class SchemaDocMojo method execute.

public void execute() throws MojoExecutionException, MojoFailureException {
    getLog().info("SchemaDoc plugin started");
    PrismContext prismContext = createInitializedPrismContext();
    File outDir = initializeOutDir();
    PathGenerator pathGenerator = new PathGenerator(outDir);
    VelocityEngine velocityEngine = createVelocityEngine();
    SchemaRegistry schemaRegistry = prismContext.getSchemaRegistry();
    try {
        renderSchemaIndex(schemaRegistry, prismContext, velocityEngine, pathGenerator);
    } catch (IOException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }
    for (PrismSchema schema : schemaRegistry.getSchemas()) {
        try {
            renderSchema(schema, prismContext, velocityEngine, pathGenerator);
        } catch (IOException e) {
            throw new MojoExecutionException(e.getMessage(), e);
        }
    }
    try {
        copyResources(outDir);
    } catch (IOException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }
    File archiveFile = null;
    try {
        archiveFile = generateArchive(outDir, finalName + "-schemadoc.zip");
    } catch (IOException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    } catch (ArchiverException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    }
    projectHelper.attachArtifact(project, "zip", "schemadoc", archiveFile);
    getLog().info("SchemaDoc plugin finished");
}
Also used : PrismSchema(com.evolveum.midpoint.prism.schema.PrismSchema) VelocityEngine(org.apache.velocity.app.VelocityEngine) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) ArchiverException(org.codehaus.plexus.archiver.ArchiverException) PrismContext(com.evolveum.midpoint.prism.PrismContext) SchemaRegistry(com.evolveum.midpoint.prism.schema.SchemaRegistry)

Example 9 with VelocityEngine

use of org.apache.velocity.app.VelocityEngine in project opennms by OpenNMS.

the class GraphConfigGenerator method generateSnmpGraphInternal.

private String generateSnmpGraphInternal(Collection<Report> reports) {
    // init VelocityEngine
    VelocityEngine velocityEngine = new VelocityEngine();
    velocityEngine.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
    velocityEngine.setProperty("classpath.resource.loader.class", ClasspathResourceLoader.class.getName());
    velocityEngine.init();
    // create reader and writer for template extraction from jar
    InputStream templateInputStream = this.getClass().getClassLoader().getResourceAsStream(INTERN_TEMPLATE_NAME);
    StringWriter templateWriter = new StringWriter();
    Reader templateReader = new InputStreamReader(templateInputStream);
    // create context
    VelocityContext context = new VelocityContext();
    context.put("reportsList", reports.iterator());
    context.put("reportsBody", reports.iterator());
    // get template
    Velocity.evaluate(context, templateWriter, INTERN_TEMPLATE_NAME, templateReader);
    return templateWriter.toString();
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) StringWriter(java.io.StringWriter) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) VelocityContext(org.apache.velocity.VelocityContext) Reader(java.io.Reader) InputStreamReader(java.io.InputStreamReader) ClasspathResourceLoader(org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader)

Example 10 with VelocityEngine

use of org.apache.velocity.app.VelocityEngine in project Ebselen by Ardesco.

the class IDEToEbselen method generateJavaFile.

/**
     * Writes Sky Selenium format test code into a Java file ready for tests to be run
     *
     * @param name - Name of the test
     * @throws Exception
     */
public void generateJavaFile(String name) throws Exception {
    Properties props = new Properties();
    props.setProperty("resource.loader", "class");
    props.setProperty("class.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
    props.setProperty("class.resource.loader.description", "Velocity Classpath Resource Loader");
    VelocityEngine ve = new VelocityEngine(props);
    VelocityContext context = new VelocityContext();
    context.put("template", name);
    context.put("templateclass", name + ".class");
    context.put("testname", name);
    context.put("testdata", testCode);
    Template ebselenTemplate = ve.getTemplate(ebselenTestTemplate);
    FileHandler convertedFile = new FileHandler(conversionLocation + File.separator + name + ".java", true);
    StringWriter writer = new StringWriter();
    ebselenTemplate.merge(context, writer);
    convertedFile.write(writer.toString());
    convertedFile.close();
    LOGGER.info("Selenium IDE test converted and saved as '" + convertedFile.getFilePath() + convertedFile.getFileName() + "'");
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) Properties(java.util.Properties) CleanerProperties(org.htmlcleaner.CleanerProperties) Template(org.apache.velocity.Template) FileHandler(com.lazerycode.ebselen.handlers.FileHandler)

Aggregations

VelocityEngine (org.apache.velocity.app.VelocityEngine)29 VelocityContext (org.apache.velocity.VelocityContext)13 StringWriter (java.io.StringWriter)10 Properties (java.util.Properties)9 Template (org.apache.velocity.Template)9 IOException (java.io.IOException)7 ClasspathResourceLoader (org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader)6 Writer (java.io.Writer)5 File (java.io.File)4 ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)4 CopyrightManager (com.intellij.copyright.CopyrightManager)3 InputStreamReader (java.io.InputStreamReader)3 Reader (java.io.Reader)3 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)3 ParseErrorException (org.apache.velocity.exception.ParseErrorException)3 FileNotFoundException (java.io.FileNotFoundException)2 InputStream (java.io.InputStream)2 OutputStreamWriter (java.io.OutputStreamWriter)2 Map (java.util.Map)2 ExtendedProperties (org.apache.commons.collections.ExtendedProperties)2