Search in sources :

Example 1 with VelocityContext

use of org.apache.velocity.VelocityContext in project eweb4j-framework by laiweiwei.

the class VelocityRendererImpl method render.

public synchronized void render(Writer writer, Map<String, Object> datas) {
    VelocityContext context = new VelocityContext();
    if (datas != null) {
        for (Iterator<Entry<String, Object>> it = datas.entrySet().iterator(); it.hasNext(); ) {
            Entry<String, Object> e = it.next();
            context.put(e.getKey(), e.getValue());
        }
    }
    String tplPath = paths.get(MVCConfigConstant.LAYOUT_SCREEN_CONTENT_KEY);
    // 将环境变量和输出部分结合
    if (this.layout != null) {
        for (Iterator<Entry<String, String>> it = this.paths.entrySet().iterator(); it.hasNext(); ) {
            Entry<String, String> e = it.next();
            String paramName = e.getKey();
            String path = e.getValue();
            StringWriter w = new StringWriter();
            ve.getTemplate(path).merge(context, w);
            String screenContent = w.toString();
            context.put(paramName, screenContent);
        }
        tplPath = layout;
    }
    ve.getTemplate(tplPath).merge(context, writer);
}
Also used : Entry(java.util.Map.Entry) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext)

Example 2 with VelocityContext

use of org.apache.velocity.VelocityContext in project hive by apache.

the class TestScripts method getTemplateResult.

protected static String getTemplateResult(String command, Map<String, String> keyValues) throws IOException {
    VelocityContext context = new VelocityContext();
    for (String key : keyValues.keySet()) {
        context.put(key, keyValues.get(key));
    }
    StringWriter writer = new StringWriter();
    if (!Velocity.evaluate(context, writer, command, command)) {
        throw new IOException("Unable to render " + command + " with " + keyValues);
    }
    writer.close();
    return writer.toString();
}
Also used : StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) IOException(java.io.IOException)

Example 3 with VelocityContext

use of org.apache.velocity.VelocityContext 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 4 with VelocityContext

use of org.apache.velocity.VelocityContext in project opennms by OpenNMS.

the class GraphConfigGenerator method generateSnmpGraphInternal.

private String generateSnmpGraphInternal(Collection<Report> reports, String graphTemplate) {
    Velocity.init();
    VelocityContext context = new VelocityContext();
    context.put("reportsList", reports.iterator());
    context.put("reportsBody", reports.iterator());
    Template template = Velocity.getTemplate(graphTemplate);
    StringWriter sw = new StringWriter();
    if (template != null) {
        template.merge(context, sw);
    }
    return sw.toString();
}
Also used : StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) Template(org.apache.velocity.Template)

Example 5 with VelocityContext

use of org.apache.velocity.VelocityContext 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

VelocityContext (org.apache.velocity.VelocityContext)473 StringWriter (java.io.StringWriter)142 Template (org.apache.velocity.Template)119 Test (org.junit.Test)72 IOException (java.io.IOException)60 VelocityEngine (org.apache.velocity.app.VelocityEngine)52 File (java.io.File)46 ArrayList (java.util.ArrayList)39 Identity (org.olat.core.id.Identity)36 Map (java.util.Map)35 HashMap (java.util.HashMap)34 Context (org.apache.velocity.context.Context)32 MailTemplate (org.olat.core.util.mail.MailTemplate)28 Writer (java.io.Writer)22 Properties (java.util.Properties)19 ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)19 ParseErrorException (org.apache.velocity.exception.ParseErrorException)16 ClasspathResourceLoader (org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader)16 FileWriter (java.io.FileWriter)15 OutputStreamWriter (java.io.OutputStreamWriter)14