Package org.apache.velocity

Examples of org.apache.velocity.Template


          logger.debug("Template name: {}", templateFileName);
          logger.debug("Parameters: {}", contextParameters);
        }
        try {
          //Notice that every template file should be encoded with UTF-8
            Template template = Velocity.getTemplate(DEFAULT_TEMPLATE_PLACE + templateFileName,
                DEFAULT_ENCODING);
            VelocityContext context = new VelocityContext();
            for (String key : contextParameters.keySet()) {
                context.put(key, contextParameters.get(key));
            }
            StringWriter writer = new StringWriter();
            template.merge(context, writer);

            String result = writer.getBuffer().toString();
            if (logger.isDebugEnabled()) {
              logger.debug("Parse result is: ");
              logger.debug(result);
View Full Code Here


                uri.substring(_baseDir.length()) + _suffix;
        }
        if(!uri.endsWith(_suffix))
            uri += _suffix;
       
        Template template = null;
        try
        {
            template = _engine.getTemplate(uri);
        }
        catch(Exception e)
        {
            throw new ServletException(e);
        }
        template.merge(LocalizedVelocityContext.getCurrent().setRequest(request),
                response.getWriter());
    }
View Full Code Here

      StringResourceLoader.setRepository(StringResourceLoader.REPOSITORY_NAME_DEFAULT, rep);
    }
    rep.setEncoding(this.encoding);
    try {

      Template template = this.templates.get(templateName);
      if (template == null) {
        rep.putStringResource(templateName, templateSource);

        template = Velocity.getTemplate(templateName);
        rep.removeStringResource(templateName);
        this.templates.put(templateName, template);
      }

      VelocityContext context = new VelocityContext();
      Iterator<String> it = this.objectstoput.keySet().iterator();
      while (it.hasNext()) {
        String key = it.next();
        context.put(key, this.objectstoput.get(key));
      }
      StringWriter ret = new StringWriter();
      template.merge(context, ret);
      renderedTemplate = ret.toString();
    } catch (ResourceNotFoundException e) {
      throw new CRException(e);
    } catch (ParseErrorException e) {
      throw new CRException(e);
View Full Code Here

    } catch (CacheException e) {

      log.warn("Could not initialize Cache for Velocity templates.", e);
    }

    Template template = null;

    if (cache != null) {
      template = (Template) cache.get(name + source);
    }
View Full Code Here

        ve.setProperty(RuntimeConstants.RUNTIME_LOG, logFile);
      }

      ve.init();
      Template t = ve.getTemplate(template);

      // For each of the qFiles generate the test
      VelocityContext ctx = new VelocityContext();
      ctx.put("className", className);
      ctx.put("qfiles", qFiles);
      ctx.put("resultsDir", resultsDir);
      ctx.put("logDir", logDir);

      File outFile = new File(outDir, className + ".java");
      FileWriter writer = new FileWriter(outFile);
      t.merge(ctx, writer);
      writer.close();

      System.out.println("Generated " + outFile.getCanonicalPath() + " from template " + template);
    }
    catch(BuildException e) {
View Full Code Here

            if (encoding != null) {
                contentType = contentType + ";charset=" + encoding;
            }
            response.setContentType(contentType);
            Template t = getTemplate(stack,
                    velocityManager.getVelocityEngine(), invocation,
                    finalLocation, encoding);

            Context context = createContext(velocityManager, stack, request,
                    response, finalLocation);
            Writer writer = new OutputStreamWriter(response.getOutputStream(),
                    encoding);

            t.merge(context, writer);

            // always flush the writer (we used to only flush it if this was a
            // jspWriter, but someone asked
            // to do it all the time (WW-829). Since Velocity support is being
            // deprecated, we'll oblige :)
View Full Code Here

            String location, String encoding) throws Exception {
        if (!location.startsWith("/")) {
            location = invocation.getProxy().getNamespace() + "/" + location;
        }

        Template template = velocity.getTemplate(location, encoding);

        return template;
    }
View Full Code Here

        if (!deployedErrorTemplate && templatePath.equals(NOT_FOUND_PAGE_PATH)) {
            templatePath = "META-INF/resources" + NOT_FOUND_PAGE_PATH;
        }

        // May throw parsing error if template could not be obtained
        Template template = null;
        String charset = configService.getCharset();
        if (charset != null) {
            template = velocityEngine.getTemplate(templatePath, charset);

        } else {
            template = velocityEngine.getTemplate(templatePath);
        }

        VelocityWriter velocityWriter = null;

        try {
            velocityWriter = (VelocityWriter) writerPool.get();

            if (velocityWriter == null) {
                velocityWriter =
                    new VelocityWriter(writer, WRITER_BUFFER_SIZE, true);

            } else {
                velocityWriter.recycle(writer);
            }

            template.merge(context, velocityWriter);

        } catch (Exception error) {
            // Exception occured merging template and model. It is possible
            // that some output has already been written, so we will append the
            // error report to the previous output.
View Full Code Here

    public void renderTemplate(String templatePath, Map model, Writer writer) throws Exception {

        final VelocityContext velocityContext = new VelocityContext(model);

        // May throw parsing error if template could not be obtained
        Template template = null;
        String charset = configService.getCharset();
        if (charset != null) {
            template = velocityEngine.getTemplate(templatePath, charset);

        } else {
            template = velocityEngine.getTemplate(templatePath);
        }

        VelocityWriter velocityWriter = null;

        try {
            velocityWriter = (VelocityWriter) writerPool.get();

            if (velocityWriter == null) {
                velocityWriter =
                    new VelocityWriter(writer, WRITER_BUFFER_SIZE, true);

            } else {
                velocityWriter.recycle(writer);
            }

            template.merge(velocityContext, velocityWriter);

        } catch (Exception error) {
            // Exception occured merging template and model. It is possible
            // that some output has already been written, so we will append the
            // error report to the previous output.
View Full Code Here

    }

    public void testResourcesFoundUsingAbsolutePath() throws Exception {
        String location = "/WEB-INF/views/registration.vm";

        Template template = result.getTemplate(stack, velocity, actionInvocation, location, "UTF-8");
        assertNotNull(template);
        assertEquals("expect absolute locations to be handled as is", location, velocity.templateName);
    }
View Full Code Here

TOP

Related Classes of org.apache.velocity.Template

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.