Search in sources :

Example 26 with VelocityContext

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

the class SimpleVelocityServlet method process.

private void process(HttpServletRequest req, HttpServletResponse resp) {
    try {
        resp.setContentType("text/html");
        resp.setCharacterEncoding("UTF-8");
        Writer out = resp.getWriter();
        String path = path(req);
        VelocityContext vc = SimpleVelocity.createContext(datamodel);
        vc.put("request", req);
        SimpleVelocity.process(docbase, path, out, vc);
    } catch (IOException ex) {
        vlog.warn("IOException", ex);
    }
}
Also used : VelocityContext(org.apache.velocity.VelocityContext) IOException(java.io.IOException) Writer(java.io.Writer)

Example 27 with VelocityContext

use of org.apache.velocity.VelocityContext in project logging-log4j2 by apache.

the class VelocityTest method testVelocity.

@Test
public void testVelocity() {
    Velocity.init();
    final VelocityContext vContext = new VelocityContext();
    vContext.put("name", new String("Velocity"));
    final Template template = Velocity.getTemplate("target/test-classes/hello.vm");
    final StringWriter sw = new StringWriter();
    template.merge(vContext, sw);
}
Also used : StringWriter(java.io.StringWriter) VelocityContext(org.apache.velocity.VelocityContext) Template(org.apache.velocity.Template) Test(org.junit.Test)

Example 28 with VelocityContext

use of org.apache.velocity.VelocityContext in project maven-plugins by apache.

the class DefaultCheckstyleRssGenerator method generateRSS.

@Override
public void generateRSS(CheckstyleResults results, CheckstyleRssGeneratorRequest checkstyleRssGeneratorRequest) throws MavenReportException {
    VelocityTemplate vtemplate = new VelocityTemplate(velocityComponent, CheckstyleReport.PLUGIN_RESOURCES);
    vtemplate.setLog(checkstyleRssGeneratorRequest.getLog());
    Context context = new VelocityContext();
    context.put("results", results);
    context.put("project", checkstyleRssGeneratorRequest.getMavenProject());
    context.put("copyright", checkstyleRssGeneratorRequest.getCopyright());
    context.put("levelInfo", SeverityLevel.INFO);
    context.put("levelWarning", SeverityLevel.WARNING);
    context.put("levelError", SeverityLevel.ERROR);
    context.put("stringutils", new StringUtils());
    try {
        vtemplate.generate(checkstyleRssGeneratorRequest.getOutputDirectory().getPath() + "/checkstyle.rss", "checkstyle-rss.vm", context);
    } catch (ResourceNotFoundException e) {
        throw new MavenReportException("Unable to find checkstyle-rss.vm resource.", e);
    } catch (MojoExecutionException | IOException | VelocityException e) {
        throw new MavenReportException("Unable to generate checkstyle.rss.", e);
    }
}
Also used : VelocityContext(org.apache.velocity.VelocityContext) Context(org.apache.velocity.context.Context) MojoExecutionException(org.apache.maven.plugin.MojoExecutionException) VelocityContext(org.apache.velocity.VelocityContext) StringUtils(org.codehaus.plexus.util.StringUtils) VelocityException(org.apache.velocity.exception.VelocityException) IOException(java.io.IOException) ResourceNotFoundException(org.apache.velocity.exception.ResourceNotFoundException) MavenReportException(org.apache.maven.reporting.MavenReportException)

Example 29 with VelocityContext

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

the class VelocityResponseWriter method createContext.

private VelocityContext createContext(SolrQueryRequest request, SolrQueryResponse response) {
    VelocityContext context = new VelocityContext();
    // Register useful Velocity "tools"
    String locale = request.getParams().get(LOCALE);
    Map toolConfig = new HashMap();
    toolConfig.put("locale", locale);
    // TODO: add test; TODO: should this be overridable with a custom "log" named tool?
    context.put("log", log);
    context.put("esc", new EscapeTool());
    context.put("date", new ComparisonDateTool());
    context.put("list", new ListTool());
    context.put(SORT, new SortTool());
    MathTool mathTool = new MathTool();
    mathTool.configure(toolConfig);
    context.put("math", mathTool);
    NumberTool numberTool = new NumberTool();
    numberTool.configure(toolConfig);
    context.put("number", numberTool);
    DisplayTool displayTool = new DisplayTool();
    displayTool.configure(toolConfig);
    context.put("display", displayTool);
    ResourceTool resourceTool = new SolrVelocityResourceTool(request.getCore().getSolrConfig().getResourceLoader().getClassLoader());
    resourceTool.configure(toolConfig);
    context.put("resource", resourceTool);
    // Custom tools can override any of the built-in tools provided above, by registering one with the same name
    for (String name : customTools.keySet()) {
        Object customTool = SolrCore.createInstance(customTools.get(name), Object.class, "VrW custom tool: " + name, request.getCore(), request.getCore().getResourceLoader());
        if (customTool instanceof LocaleConfig) {
            ((LocaleConfig) customTool).configure(toolConfig);
        }
        context.put(name, customTool);
    }
    // custom tools _cannot_ override context objects added below, like $request and $response
    // TODO: at least log a warning when one of the *fixed* tools classes in name with a custom one, currently silently ignored
    // Turn the SolrQueryResponse into a SolrResponse.
    // QueryResponse has lots of conveniences suitable for a view
    // Problem is, which SolrResponse class to use?
    // One patch to SOLR-620 solved this by passing in a class name as
    // as a parameter and using reflection and Solr's class loader to
    // create a new instance.  But for now the implementation simply
    // uses QueryResponse, and if it chokes in a known way, fall back
    // to bare bones SolrResponseBase.
    // Can this writer know what the handler class is?  With echoHandler=true it can get its string name at least
    SolrResponse rsp = new QueryResponse();
    NamedList<Object> parsedResponse = BinaryResponseWriter.getParsedResponse(request, response);
    try {
        rsp.setResponse(parsedResponse);
        // page only injected if QueryResponse works
        // page tool only makes sense for a SearchHandler request
        context.put("page", new PageTool(request, response));
        context.put("debug", ((QueryResponse) rsp).getDebugMap());
    } catch (ClassCastException e) {
        // known edge case where QueryResponse's extraction assumes "response" is a SolrDocumentList
        // (AnalysisRequestHandler emits a "response")
        rsp = new SolrResponseBase();
        rsp.setResponse(parsedResponse);
    }
    context.put("request", request);
    context.put("response", rsp);
    return context;
}
Also used : DisplayTool(org.apache.velocity.tools.generic.DisplayTool) ResourceTool(org.apache.velocity.tools.generic.ResourceTool) HashMap(java.util.HashMap) VelocityContext(org.apache.velocity.VelocityContext) LocaleConfig(org.apache.velocity.tools.generic.LocaleConfig) EscapeTool(org.apache.velocity.tools.generic.EscapeTool) MathTool(org.apache.velocity.tools.generic.MathTool) SolrResponse(org.apache.solr.client.solrj.SolrResponse) ComparisonDateTool(org.apache.velocity.tools.generic.ComparisonDateTool) NumberTool(org.apache.velocity.tools.generic.NumberTool) QueryResponse(org.apache.solr.client.solrj.response.QueryResponse) SolrResponseBase(org.apache.solr.client.solrj.response.SolrResponseBase) ListTool(org.apache.velocity.tools.generic.ListTool) HashMap(java.util.HashMap) Map(java.util.Map) SortTool(org.apache.velocity.tools.generic.SortTool)

Example 30 with VelocityContext

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

the class OutgoingEmail method setupVelocityContext.

protected void setupVelocityContext() {
    velocityContext = new VelocityContext();
    velocityContext.put("email", this);
    velocityContext.put("messageClass", messageClass);
    velocityContext.put("StringUtils", StringUtils.class);
}
Also used : VelocityContext(org.apache.velocity.VelocityContext)

Aggregations

VelocityContext (org.apache.velocity.VelocityContext)98 StringWriter (java.io.StringWriter)26 Template (org.apache.velocity.Template)20 IOException (java.io.IOException)16 VelocityEngine (org.apache.velocity.app.VelocityEngine)13 File (java.io.File)10 ArrayList (java.util.ArrayList)8 ExecuteResult (com.ctrip.platform.dal.daogen.entity.ExecuteResult)7 Progress (com.ctrip.platform.dal.daogen.entity.Progress)7 Writer (java.io.Writer)7 Map (java.util.Map)7 Callable (java.util.concurrent.Callable)7 JavaCodeGenContext (com.ctrip.platform.dal.daogen.generator.java.JavaCodeGenContext)6 OutputStreamWriter (java.io.OutputStreamWriter)6 Properties (java.util.Properties)6 PrintWriter (java.io.PrintWriter)5 HashMap (java.util.HashMap)5 CSharpCodeGenContext (com.ctrip.platform.dal.daogen.generator.csharp.CSharpCodeGenContext)4 MojoExecutionException (org.apache.maven.plugin.MojoExecutionException)4 FileOutputStream (java.io.FileOutputStream)3