Search in sources :

Example 86 with Template

use of org.apache.velocity.Template in project nextprot-api by calipho-sib.

the class EntryTTLStreamWriter method writeHeader.

@Override
protected void writeHeader(Map<String, Object> infos) {
    int entryNum = (int) infos.get(EntryStreamWriter.getEntryCountKey());
    ReleaseInfoVersions releaseInfoVersions = (ReleaseInfoVersions) infos.get(EntryStreamWriter.getReleaseInfoKey());
    Template headerTemplate = velocityConfig.getVelocityEngine().getTemplate("turtle/prefix.ttl.vm");
    headerTemplate.merge(new NXVelocityContext(entryNum, releaseInfoVersions), getStream());
}
Also used : NXVelocityContext(org.nextprot.api.web.NXVelocityContext) ReleaseInfoVersions(org.nextprot.api.core.domain.release.ReleaseInfoVersions) Template(org.apache.velocity.Template)

Example 87 with Template

use of org.apache.velocity.Template in project nextprot-api by calipho-sib.

the class EntryXMLStreamWriter method writeHeader.

@Override
protected void writeHeader(Map<String, Object> infos) throws IOException {
    int entryNum = (int) infos.get(EntryStreamWriter.getEntryCountKey());
    ReleaseInfoVersions releaseInfoVersions = (ReleaseInfoVersions) infos.get(EntryStreamWriter.getReleaseInfoKey());
    ReleaseInfoDataSources releaseInfoDataSources = (ReleaseInfoDataSources) infos.get(EntryStreamWriter.getReleaseDataSourcesKey());
    Template headerTemplate = velocityConfig.getVelocityEngine().getTemplate("export-header.xml.vm");
    headerTemplate.merge(new NXVelocityContext(entryNum, releaseInfoVersions), getStream());
    Template releaseContentTemplate = velocityConfig.getVelocityEngine().getTemplate("release-contents.xml.vm");
    writePrettyXml(releaseContentTemplate, new NXVelocityContext(entryNum, releaseInfoVersions, releaseInfoDataSources), 2);
    getStream().write("    </header>\n");
    getStream().write("    <entry-list>\n");
}
Also used : NXVelocityContext(org.nextprot.api.web.NXVelocityContext) ReleaseInfoVersions(org.nextprot.api.core.domain.release.ReleaseInfoVersions) ReleaseInfoDataSources(org.nextprot.api.core.domain.release.ReleaseInfoDataSources) Template(org.apache.velocity.Template)

Example 88 with Template

use of org.apache.velocity.Template in project nextprot-api by calipho-sib.

the class EntryTTLValidationIntegrationTest method addTurtlePrefixes.

static void addTurtlePrefixes(Writer writer, VelocityConfig config) {
    Template headerTemplate = config.getVelocityEngine().getTemplate("turtle/prefix.ttl.vm");
    headerTemplate.merge(new NXVelocityContext(), writer);
}
Also used : NXVelocityContext(org.nextprot.api.web.NXVelocityContext) Template(org.apache.velocity.Template)

Example 89 with Template

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

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)

Example 90 with Template

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

the class VelocityUtils method create.

public static Template create(String source, String templateName) throws ParseException {
    RuntimeServices runtimeServices = RuntimeSingleton.getRuntimeServices();
    StringReader reader = new StringReader(source);
    SimpleNode node = runtimeServices.parse(reader, templateName);
    Template template = new Template();
    template.setRuntimeServices(runtimeServices);
    template.setData(node);
    template.initDocument();
    return template;
}
Also used : RuntimeServices(org.apache.velocity.runtime.RuntimeServices) StringReader(java.io.StringReader) SimpleNode(org.apache.velocity.runtime.parser.node.SimpleNode) 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