Search in sources :

Example 16 with Template

use of org.apache.velocity.Template in project gerrit by GerritCodeReview.

the class OutgoingEmail method velocifyFile.

protected String velocifyFile(String name) throws EmailException {
    try {
        RuntimeInstance runtime = args.velocityRuntime;
        if (runtime.getLoaderNameForResource(name) == null) {
            name = "com/google/gerrit/server/mail/" + name;
        }
        Template template = runtime.getTemplate(name, UTF_8.name());
        StringWriter w = new StringWriter();
        template.merge(velocityContext, w);
        return w.toString();
    } catch (Exception e) {
        throw new EmailException("Cannot format velocity template " + name, e);
    }
}
Also used : StringWriter(java.io.StringWriter) EmailException(com.google.gerrit.common.errors.EmailException) RuntimeInstance(org.apache.velocity.runtime.RuntimeInstance) OrmException(com.google.gwtorm.server.OrmException) ValidationException(com.google.gerrit.server.validators.ValidationException) MalformedURLException(java.net.MalformedURLException) EmailException(com.google.gerrit.common.errors.EmailException) Template(org.apache.velocity.Template)

Example 17 with Template

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

the class BenchmarkServlet method doGet.

@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    final String path = request.getPathInfo() == null ? "" : request.getPathInfo();
    if (path.endsWith(".css")) {
        // Serve our css
        final Template t = getTemplate("/velocity/benchmark.css");
        response.setContentType("text/css");
        response.setCharacterEncoding("UTF-8");
        t.merge(getVelocityContext(request, null), response.getWriter());
    } else if (path.length() < 2) {
        // No benchmark specified -> redirect to default
        response.sendRedirect(getExampleBenchmarkPath(request, DEFAULT_BENCHMARK));
    } else {
        // Benchmark input form pre-filled with selected example
        final Template t = getTemplate("/velocity/benchmark-input.html");
        final VelocityContext ctx = getVelocityContext(request, "Benchmark Input");
        ctx.put("formAction", request.getContextPath() + mountPath);
        ctx.put("benchmarkText", getBenchmarkText(path));
        ctx.put("benchmarkPaths", getExampleBenchmarkPaths(request));
        ctx.put("currentBenchmarkPath", path);
        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");
        t.merge(ctx, response.getWriter());
    }
}
Also used : VelocityContext(org.apache.velocity.VelocityContext) Template(org.apache.velocity.Template)

Example 18 with Template

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

the class BenchmarkServlet method doPost.

@Override
protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
    final String content = request.getParameter(PARAM_CONTENT);
    if (content == null) {
        throw new ServletException("Missing " + PARAM_CONTENT + " parameter");
    }
    String chainName = request.getParameter(PARAM_CHAIN);
    final Template t = AccessController.doPrivileged(new PrivilegedAction<Template>() {

        @Override
        public Template run() {
            return getTemplate("/velocity/benchmark-results.html");
        }
    });
    final VelocityContext ctx = getVelocityContext(request, "Benchmark Results");
    ctx.put("contentItemFactory", ciFactory);
    ctx.put("jobManager", jobManager);
    List<? extends Benchmark> benchmarks = parser.parse(new StringReader(content));
    if (chainName != null && !chainName.isEmpty()) {
        Chain chain = chainManager.getChain(chainName);
        if (chain == null) {
            response.setStatus(404);
            PrintWriter w = response.getWriter();
            w.println("Unable to perform benchmark on EnhancementChain '" + StringEscapeUtils.escapeHtml(chainName) + "' because no chain with that name is active!");
            IOUtils.closeQuietly(w);
            return;
        }
        for (Benchmark benchmark : benchmarks) {
            benchmark.setChain(chain);
        }
    }
    ctx.put("benchmarks", benchmarks);
    ctx.put("graphFormatter", new GraphFormatter(graphSerializer));
    response.setContentType("text/html");
    response.setCharacterEncoding("UTF-8");
    try {
        AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {

            @Override
            public Object run() throws IOException {
                t.merge(ctx, response.getWriter());
                return null;
            }
        });
    } catch (PrivilegedActionException pae) {
        Exception e = pae.getException();
        if (e instanceof IOException) {
            throw (IOException) e;
        } else {
            throw RuntimeException.class.cast(e);
        }
    }
}
Also used : Chain(org.apache.stanbol.enhancer.servicesapi.Chain) PrivilegedActionException(java.security.PrivilegedActionException) VelocityContext(org.apache.velocity.VelocityContext) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) NamespaceException(org.osgi.service.http.NamespaceException) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Template(org.apache.velocity.Template) ServletException(javax.servlet.ServletException) StringReader(java.io.StringReader) Benchmark(org.apache.stanbol.enhancer.benchmark.Benchmark) PrintWriter(java.io.PrintWriter)

Example 19 with Template

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

the class Container method copyTemplateTo.

private void copyTemplateTo(final File targetDir, final String filename) throws Exception {
    final File file = new File(targetDir, filename);
    if (file.exists()) {
        return;
    }
    // don't break apps using Velocity facade
    final VelocityEngine engine = new VelocityEngine();
    engine.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM, new NullLogChute());
    engine.setProperty(Velocity.RESOURCE_LOADER, "class");
    engine.setProperty("class.resource.loader.description", "Velocity Classpath Resource Loader");
    engine.setProperty("class.resource.loader.class", ClasspathResourceLoader.class.getName());
    engine.init();
    final Template template = engine.getTemplate("/org/apache/tomee/configs/" + filename);
    final VelocityContext context = new VelocityContext();
    context.put("tomcatHttpPort", Integer.toString(configuration.getHttpPort()));
    context.put("tomcatShutdownPort", Integer.toString(configuration.getStopPort()));
    final Writer writer = new FileWriter(file);
    template.merge(context, writer);
    writer.flush();
    writer.close();
}
Also used : VelocityEngine(org.apache.velocity.app.VelocityEngine) NullLogChute(org.apache.velocity.runtime.log.NullLogChute) VelocityContext(org.apache.velocity.VelocityContext) FileWriter(java.io.FileWriter) ClasspathResourceLoader(org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader) File(java.io.File) Writer(java.io.Writer) FileWriter(java.io.FileWriter) Template(org.apache.velocity.Template)

Example 20 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)

Aggregations

Template (org.apache.velocity.Template)33 VelocityContext (org.apache.velocity.VelocityContext)20 StringWriter (java.io.StringWriter)11 IOException (java.io.IOException)9 VelocityEngine (org.apache.velocity.app.VelocityEngine)9 File (java.io.File)6 Writer (java.io.Writer)6 Properties (java.util.Properties)5 ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)5 FileWriter (java.io.FileWriter)4 FileOutputStream (java.io.FileOutputStream)3 PrintWriter (java.io.PrintWriter)3 ParseErrorException (org.apache.velocity.exception.ParseErrorException)3 ImmutableMap (com.google.common.collect.ImmutableMap)2 OutputStreamWriter (java.io.OutputStreamWriter)2 StringReader (java.io.StringReader)2 Map (java.util.Map)2 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)2 MethodInvocationException (org.apache.velocity.exception.MethodInvocationException)2 VelocityException (org.apache.velocity.exception.VelocityException)2