Search in sources :

Example 61 with Template

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

the class GoVelocityView method getContentAsString.

public String getContentAsString() {
    try {
        Template template = getTemplate();
        StringWriter writer = new StringWriter();
        template.merge(null, writer);
        return writer.toString();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : StringWriter(java.io.StringWriter) Template(org.apache.velocity.Template)

Example 62 with Template

use of org.apache.velocity.Template in project ngAndroid by davityle.

the class SourceCreator method createSourceFiles.

public void createSourceFiles() {
    Properties props = new Properties();
    props.setProperty("runtime.log.logsystem.class", "org.apache.velocity.runtime.log.SystemLogChute");
    props.setProperty("resource.loader", "classpath");
    props.setProperty("classpath.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
    VelocityEngine ve = new VelocityEngine(props);
    ve.init();
    Template vtModel = ve.getTemplate("templates/ngmodel.vm");
    Template vtScope = ve.getTemplate("templates/scope.vm");
    Template vtLayout = ve.getTemplate("templates/layout.vm");
    for (NgModelSourceLink ms : modelSourceLinks) {
        try {
            JavaFileObject jfo = filer.createSourceFile(ms.getSourceFileName(), ms.getElements());
            Writer writer = jfo.openWriter();
            vtModel.merge(ms.getVelocityContext(), writer);
            writer.flush();
            writer.close();
        } catch (IOException e) {
            messageUtils.error(Option.of(ms.getElements()[0]), e.getMessage());
        }
    }
    for (ScopeSourceLink ss : scopeSourceLinks) {
        try {
            JavaFileObject jfo = filer.createSourceFile(ss.getSourceFileName(), ss.getElements());
            Writer writer = jfo.openWriter();
            vtScope.merge(ss.getVelocityContext(), writer);
            writer.flush();
            writer.close();
        } catch (IOException e) {
            messageUtils.error(Option.of(ss.getElements()[0]), e.getMessage());
        }
    }
    for (LayoutSourceLink lsl : layoutSourceLinks) {
        try {
            JavaFileObject jfo = filer.createSourceFile(lsl.getSourceFileName(), lsl.getElements());
            Writer writer = jfo.openWriter();
            vtLayout.merge(lsl.getVelocityContext(), writer);
            writer.flush();
            writer.close();
        } catch (IOException e) {
            messageUtils.error(Option.<Element>absent(), e.getMessage());
        }
    }
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) JavaFileObject(javax.tools.JavaFileObject) ScopeSourceLink(com.github.davityle.ngprocessor.source.links.ScopeSourceLink) Element(javax.lang.model.element.Element) LayoutSourceLink(com.github.davityle.ngprocessor.source.links.LayoutSourceLink) IOException(java.io.IOException) Properties(java.util.Properties) Writer(java.io.Writer) Template(org.apache.velocity.Template) NgModelSourceLink(com.github.davityle.ngprocessor.source.links.NgModelSourceLink)

Example 63 with Template

use of org.apache.velocity.Template in project scoold by Erudika.

the class VelocityLayoutView method renderScreenContent.

/**
 * The resulting context contains any mappings from render, plus screen content.
 */
private void renderScreenContent(Context velocityContext) throws Exception {
    if (logger.isDebugEnabled()) {
        logger.debug("Rendering screen content template [" + getUrl() + "]");
    }
    StringWriter sw = new StringWriter();
    Template screenContentTemplate = getTemplate(getUrl());
    screenContentTemplate.merge(velocityContext, sw);
    // Put rendered content into Velocity context.
    velocityContext.put(this.screenContentKey, sw.toString());
}
Also used : StringWriter(java.io.StringWriter) Template(org.apache.velocity.Template)

Example 64 with Template

use of org.apache.velocity.Template in project openolat by klemens.

the class VelocityTemplateTest method testTemplates.

private void testTemplates(String dir, File file, List<Exception> exs) {
    String name = file.getName();
    if ("_content".equals(name)) {
        File[] templates = file.listFiles();
        for (File template : templates) {
            String templateName = template.getName();
            if (templateName.endsWith(".html")) {
                try {
                    String path = dir + templateName;
                    StringWriter writer = new StringWriter();
                    Context context = new VelocityContext();
                    Template veloTemplate = engine.getTemplate(path);
                    veloTemplate.merge(context, writer);
                    count++;
                } catch (Exception e) {
                    exs.add(e);
                }
            }
        }
    } else if (file.isDirectory()) {
        File[] files = file.listFiles();
        for (File child : files) {
            String subDir = dir + child.getName() + "/";
            testTemplates(subDir, child, exs);
        }
    }
}
Also used : VelocityContext(org.apache.velocity.VelocityContext) Context(org.apache.velocity.context.Context) StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) File(java.io.File) Template(org.apache.velocity.Template)

Example 65 with Template

use of org.apache.velocity.Template in project openolat by klemens.

the class VelocityHelper method merge.

/**
 * @param template e.g. org/olat/demo/_content/index.html
 * @param c the context
 * @param theme the theme e.g. "accessibility" or "printing". may be null if the default theme ("") should be taken
 * @return String the rendered template
 */
private void merge(String template, Context c, Writer wOut, String theme) {
    try {
        Template vtemplate = null;
        if (isLogDebugEnabled())
            logDebug("Merging template::" + template + " for theme::" + theme, null);
        if (theme != null) {
            // try the theme first, if resource not found exception, fallback to normal resource.
            // e.g. try /_accessibility/index.html first, if not found, try /index.html.
            // this allows for themes to only provide the delta to the default templates
            // todo we could avoid those string operations, if the performance gain is measureable
            int latestSlash = template.lastIndexOf('/');
            StringBuilder sb = new StringBuilder(template.substring(0, latestSlash));
            sb.append("/_").append(theme).append("/").append(template.substring(latestSlash + 1));
            String themedTemplatePath = sb.toString();
            // check cache
            boolean notFound = resourcesNotFound.contains(themedTemplatePath);
            if (!notFound) {
                // never tried before -> try to load it
                if (!ve.resourceExists(themedTemplatePath)) {
                    // this will happen once for each theme when a resource does not exist in its themed variant but only in the default theme.
                    if (!Settings.isDebuging()) {
                        resourcesNotFound.add(themedTemplatePath);
                    }
                // for debugging, allow introduction of themed files without restarting the application
                } else {
                    // template exists -> load it
                    vtemplate = ve.getTemplate(themedTemplatePath, VelocityModule.getInputEncoding());
                }
            }
            // if not found, fallback to standard
            if (vtemplate == null) {
                vtemplate = ve.getTemplate(template, VelocityModule.getInputEncoding());
            }
        } else {
            // no theme, load the standard template
            vtemplate = ve.getTemplate(template, VelocityModule.getInputEncoding());
        }
        vtemplate.merge(c, wOut);
    } catch (MethodInvocationException me) {
        throw new OLATRuntimeException(VelocityHelper.class, "MethodInvocationException occured while merging template: methName:" + me.getMethodName() + ", refName:" + me.getReferenceName(), me.getWrappedThrowable());
    } catch (Exception e) {
        throw new OLATRuntimeException(VelocityHelper.class, "exception occured while merging template: " + e.getMessage(), e);
    }
}
Also used : OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) AssertException(org.olat.core.logging.AssertException) MethodInvocationException(org.apache.velocity.exception.MethodInvocationException) OLATRuntimeException(org.olat.core.logging.OLATRuntimeException) 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