Package org.apache.velocity

Examples of org.apache.velocity.VelocityContext


      }

      @Override
      public String renderTemplate(String templateName, Map<String, Object> context) {
        try {
          VelocityContext vcontext = new VelocityContext();
          if (context != null) {
            for (Map.Entry<String, Object> entry : context.entrySet()) {
              vcontext.put(entry.getKey(), entry.getValue());
            }
          }
            StringWriter writer = new StringWriter();
            VelocityEngine ve = new VelocityEngine();
            ve.setProperty(RuntimeConstants.RESOURCE_LOADER, "classpath");
View Full Code Here


 
  @Test
  public void testVelocityOutput() throws Exception {
    AdvancedCodeBlockMacro macro = new AdvancedCodeBlockMacro();

    VelocityContext context = new VelocityContext();
    Map<String, Object>  innercontext = new HashMap<String,Object>();
    String[] splitted = BaseAdvancedCodeblockMacro.splitMacroBodyIntoContentAndConfig(configWithContent);
    String configUnparsed = splitted[0];
    String contentUnparsed = splitted[1];
      context.put("codeblocks",macro.parseConfigWithContent(configUnparsed, contentUnparsed, "key", 1l, innercontext, true));
      for (Entry<String,Object> entry : innercontext.entrySet()) {
        context.put(entry.getKey(), entry.getValue());
      }
      StringWriter writer = new StringWriter();
     
   
      VelocityEngine ve = new VelocityEngine();
View Full Code Here

    /** Template directory used for unit tests. */
    public static final String UNITTEST_TEMPLATE_DIR = "test/com/cedarsolutions/util/gae";

    /** Test renderTemplate(). */
    @Test public void testRenderTemplate() {
        VelocityContext context = new VelocityContext();
        context.put("foo", "Velocity");
        String result = GaeVelocityUtils.getInstance().renderTemplate(UNITTEST_TEMPLATE_DIR, "test.vm", context);
        assertEquals("Hello Velocity World!", result);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("foo", "Another");
View Full Code Here

    }

    /** Test createVelocityContext(). */
    @Test public void testCreateVelocityContext() {
        Map<String, Object> map = null;
        VelocityContext context = GaeVelocityUtils.getInstance().createVelocityContext(map);
        assertEquals(0, context.getKeys().length);

        map = new HashMap<String, Object>();
        map.put("key1", "value1");
        map.put("key2", new Integer(3));
        context = GaeVelocityUtils.getInstance().createVelocityContext(map);
        assertEquals(2, context.getKeys().length);
        assertEquals("value1", context.get("key1"));
        assertEquals(new Integer(3), context.get("key2"));
    }
View Full Code Here

    public String render(String template, HashMap<String, Object> m) {

        try {

            Template t = this.ve.getTemplate(template);
            VelocityContext context = new VelocityContext();
            context.put("data", m);
            StringWriter writer = new StringWriter();
            t.merge(context, writer);
            return writer.toString();

        } catch (ResourceNotFoundException ex) {
View Full Code Here

    public String renderString(String sourceStr, HashMap<String, Object> m) {

        try {

            StringWriter w = new StringWriter();
            VelocityContext context = new VelocityContext();
            context.put("data", m);
            return "" + this.ve.evaluate(context, w, "mystring", sourceStr);

        } catch (ParseErrorException ex) {
            return ex.getMessage();
        } catch (MethodInvocationException ex) {
View Full Code Here

     * Create a VelocityContext based on an input map.
     * @param map  Input map to use as source of context
     * @return VelocityContext created based on input map.
     */
    public VelocityContext createVelocityContext(Map<String, Object> map) {
        VelocityContext context = new VelocityContext();

        if (map != null) {
            for (String key : map.keySet()) {
                context.put(key, map.get(key));
            }
        }

        return context;
    }
View Full Code Here

        // Pass along the SourceResolver to the Velocity resource loader
        this.resolverContext.put(CONTEXT_RESOLVER_KEY, resolver);
        this.resolverContext.put(CONTEXT_SOURCE_CACHE_KEY, new HashMap());

        // Initialize the Velocity context
        this.velocityContext = new VelocityContext();
        this.velocityContext.put("template", src);
        this.velocityContext.put("request", ObjectModelHelper.getRequest(objectModel));
        this.velocityContext.put("response", ObjectModelHelper.getResponse(objectModel));
        this.velocityContext.put("context", ObjectModelHelper.getContext(objectModel));
        this.velocityContext.put("parameters", params);
View Full Code Here

            validate();

            List<File> resourceBundleArtifacts = downloadBundles( resourceBundles );
            supplementModels = loadSupplements( supplementalModels );

            VelocityContext context = new VelocityContext( properties );
            configureVelocityContext( context );

            RemoteResourcesClassLoader classLoader = new RemoteResourcesClassLoader( null );

            initalizeClassloader( classLoader, resourceBundleArtifacts );
View Full Code Here

        super.render(request, response);
    }

    private Context createPortletVelocityContext(RenderRequest request, RenderResponse response)
    {
        Context ctx = new VelocityContext();
        request.setAttribute(PORTLET_BRIDGE_CONTEXT, ctx);
        // PLT.22
        ctx.put("renderRequest", request);
        ctx.put("renderResponse", response);
        ctx.put("portletConfig", getPortletConfig());
        // constants
        ctx.put("STATE_NORMAL", WindowState.NORMAL);
        ctx.put("STATE_MAX", WindowState.MAXIMIZED);
        ctx.put("STATE_MIN", WindowState.MINIMIZED);
        ctx.put("MODE_VIEW", PortletMode.VIEW);
        ctx.put("MODE_EDIT", PortletMode.EDIT);
        ctx.put("MODE_HELP", PortletMode.HELP);
        ctx.put("USER_INFO", PortletRequest.USER_INFO);
        return ctx;
    }
View Full Code Here

TOP

Related Classes of org.apache.velocity.VelocityContext

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.