Search in sources :

Example 36 with VelocityContext

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

the class BenchmarkServlet method getVelocityContext.

private VelocityContext getVelocityContext(HttpServletRequest request, String pageTitle) {
    final VelocityContext ctx = new VelocityContext();
    ctx.put("title", getServletInfo() + " - " + pageTitle);
    ctx.put("contextPath", request.getContextPath());
    ctx.put("cssPath", request.getContextPath() + mountPath + "/benchmark.css");
    ctx.put("esc", new EscapeTool());
    return ctx;
}
Also used : VelocityContext(org.apache.velocity.VelocityContext) EscapeTool(org.apache.velocity.tools.generic.EscapeTool)

Example 37 with VelocityContext

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

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

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

the class NgModelSourceLink method getVelocityContext.

public VelocityContext getVelocityContext() {
    VelocityContext vc = new VelocityContext();
    vc.put("simpleClassName", modelName);
    vc.put("className", packageName.fold(new Option.OptionCB<String, String>() {

        @Override
        public String absent() {
            return modelName;
        }

        @Override
        public String present(String pack) {
            return pack + '.' + modelName;
        }
    }));
    vc.put("packageName", packageName.getOrElse(""));
    vc.put("fullName", fullName);
    vc.put("isInterface", isInterface);
    vc.put("fields", fields);
    return vc;
}
Also used : VelocityContext(org.apache.velocity.VelocityContext)

Example 40 with VelocityContext

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

the class ScopeSourceLink method getVelocityContext.

@Override
public VelocityContext getVelocityContext() {
    VelocityContext vc = new VelocityContext();
    vc.put("scope", scope);
    vc.put("className", className);
    vc.put("package", pack);
    vc.put("packageName", packageName);
    return vc;
}
Also used : VelocityContext(org.apache.velocity.VelocityContext)

Aggregations

VelocityContext (org.apache.velocity.VelocityContext)146 StringWriter (java.io.StringWriter)43 Template (org.apache.velocity.Template)41 VelocityEngine (org.apache.velocity.app.VelocityEngine)23 IOException (java.io.IOException)19 Test (org.junit.Test)15 HashMap (java.util.HashMap)13 Map (java.util.Map)12 ObjEntity (org.apache.cayenne.map.ObjEntity)12 File (java.io.File)11 ArrayList (java.util.ArrayList)10 ResourceNotFoundException (org.apache.velocity.exception.ResourceNotFoundException)10 Writer (java.io.Writer)8 ExecuteResult (com.ctrip.platform.dal.daogen.entity.ExecuteResult)7 Progress (com.ctrip.platform.dal.daogen.entity.Progress)7 Callable (java.util.concurrent.Callable)7 ParseErrorException (org.apache.velocity.exception.ParseErrorException)7 JavaCodeGenContext (com.ctrip.platform.dal.daogen.generator.java.JavaCodeGenContext)6 OutputStreamWriter (java.io.OutputStreamWriter)6 PrintWriter (java.io.PrintWriter)6