Search in sources :

Example 71 with Template

use of org.apache.velocity.Template in project selenium-tests by Wikia.

the class VelocityWrapper method fillLogRowWithLink.

public static String fillLogRowWithLink(String link, String label) {
    StringBuilder builder = new StringBuilder();
    Template t = velocityEngine.getTemplate(LOG_ROW_WITH_LINK_TEMPLATE_PATH);
    VelocityContext context = new VelocityContext();
    context.put("link", link);
    context.put("label", label);
    StringWriter writer = new StringWriter();
    t.merge(context, writer);
    builder.append(writer.toString());
    return builder.toString();
}
Also used : StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) Template(org.apache.velocity.Template)

Example 72 with Template

use of org.apache.velocity.Template in project selenium-tests by Wikia.

the class VelocityWrapper method fillButton.

static String fillButton(String id, String label) {
    StringBuilder builder = new StringBuilder();
    Template t = velocityEngine.getTemplate(BUTTON_TEMPLATE_PATH);
    VelocityContext context = new VelocityContext();
    context.put("id", id);
    context.put("label", label);
    StringWriter writer = new StringWriter();
    t.merge(context, writer);
    builder.append(writer.toString());
    return builder.toString();
}
Also used : StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) Template(org.apache.velocity.Template)

Example 73 with Template

use of org.apache.velocity.Template in project camel by apache.

the class AbstractGeneratorMojo method mergeTemplate.

protected void mergeTemplate(VelocityContext context, File outFile, String templateName) throws MojoExecutionException {
    // ensure parent directories exist
    final File outDir = outFile.getParentFile();
    if (!outDir.isDirectory() && !outDir.mkdirs()) {
        throw new MojoExecutionException("Error creating directory " + outDir);
    }
    // add generated date
    context.put("generatedDate", new Date().toString());
    // add output package
    context.put("packageName", outPackage);
    context.put("newLine", "\n");
    // load velocity template
    final Template template = getEngine().getTemplate(templateName, "UTF-8");
    // generate file
    BufferedWriter writer = null;
    try {
        writer = new BufferedWriter(new FileWriter(outFile));
        template.merge(context, writer);
    } catch (IOException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    } catch (VelocityException e) {
        throw new MojoExecutionException(e.getMessage(), e);
    } finally {
        if (writer != null) {
            try {
                writer.close();
            } catch (IOException ignore) {
            }
        }
    }
}
Also used : MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) FileWriter(java.io.FileWriter) VelocityException(org.apache.velocity.exception.VelocityException) IOException(java.io.IOException) File(java.io.File) Date(java.util.Date) Template(org.apache.velocity.Template) BufferedWriter(java.io.BufferedWriter)

Example 74 with Template

use of org.apache.velocity.Template in project midpoint by Evolveum.

the class SchemaDocMojo method renderSchema.

private void renderSchema(PrismSchema schema, PrismContext prismContext, VelocityEngine velocityEngine, PathGenerator pathGenerator) throws IOException {
    getLog().info("Processing schema: " + schema);
    VelocityContext velocityContext = new VelocityContext();
    populateVelocityContextBase(velocityContext, prismContext, pathGenerator, schema, "..");
    Template template = velocityEngine.getTemplate(TEMPLATE_SCHEMA_NAME);
    Writer writer = new FileWriter(pathGenerator.prepareSchemaOutputFile(schema));
    template.merge(velocityContext, writer);
    writer.close();
    // Object Definitions
    for (PrismObjectDefinition objectDefinition : schema.getObjectDefinitions()) {
        renderObjectDefinition(objectDefinition, schema, prismContext, velocityEngine, pathGenerator);
    }
    // Types
    for (ComplexTypeDefinition typeDefinition : schema.getComplexTypeDefinitions()) {
        renderComplexTypeDefinition(typeDefinition, schema, prismContext, velocityEngine, pathGenerator);
    }
}
Also used : VelocityContext(org.apache.velocity.VelocityContext) PrismObjectDefinition(com.evolveum.midpoint.prism.PrismObjectDefinition) ComplexTypeDefinition(com.evolveum.midpoint.prism.ComplexTypeDefinition) Template(org.apache.velocity.Template)

Example 75 with Template

use of org.apache.velocity.Template in project lucene-solr by apache.

the class VelocityResponseWriter method write.

@Override
public void write(Writer writer, SolrQueryRequest request, SolrQueryResponse response) throws IOException {
    // TODO: have HTTP headers available for configuring engine
    VelocityEngine engine = createEngine(request);
    Template template = getTemplate(engine, request);
    VelocityContext context = createContext(request, response);
    // for $engine.resourceExists(...)
    context.put("engine", engine);
    String layoutTemplate = request.getParams().get(LAYOUT);
    boolean layoutEnabled = request.getParams().getBool(LAYOUT_ENABLED, true) && layoutTemplate != null;
    String jsonWrapper = request.getParams().get(JSON);
    boolean wrapResponse = layoutEnabled || jsonWrapper != null;
    // create output
    if (!wrapResponse) {
        // straight-forward template/context merge to output
        template.merge(context, writer);
    } else {
        // merge to a string buffer, then wrap with layout and finally as JSON
        StringWriter stringWriter = new StringWriter();
        template.merge(context, stringWriter);
        if (layoutEnabled) {
            context.put("content", stringWriter.toString());
            stringWriter = new StringWriter();
            try {
                engine.getTemplate(layoutTemplate + TEMPLATE_EXTENSION).merge(context, stringWriter);
            } catch (Exception e) {
                throw new IOException(e.getMessage());
            }
        }
        if (jsonWrapper != null) {
            writer.write(jsonWrapper + "(");
            writer.write(getJSONWrap(stringWriter.toString()));
            writer.write(')');
        } else {
            // using a layout, but not JSON wrapping
            writer.write(stringWriter.toString());
        }
    }
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) IOException(java.io.IOException) IOException(java.io.IOException) Template(org.apache.velocity.Template)

Aggregations

Template (org.apache.velocity.Template)160 VelocityContext (org.apache.velocity.VelocityContext)118 StringWriter (java.io.StringWriter)76 VelocityEngine (org.apache.velocity.app.VelocityEngine)39 Test (org.junit.Test)33 File (java.io.File)21 IOException (java.io.IOException)19 ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)17 Writer (java.io.Writer)14 FileWriter (java.io.FileWriter)12 ClasspathResourceLoader (org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader)12 ParseErrorException (org.apache.velocity.exception.ParseErrorException)11 MethodInvocationException (org.apache.velocity.exception.MethodInvocationException)9 APITemplateException (org.wso2.carbon.apimgt.impl.template.APITemplateException)9 FileOutputStream (java.io.FileOutputStream)8 PrintWriter (java.io.PrintWriter)8 Map (java.util.Map)8 Properties (java.util.Properties)8 VelocityException (org.apache.velocity.exception.VelocityException)7 OutputStreamWriter (java.io.OutputStreamWriter)6