Package org.apache.velocity

Examples of org.apache.velocity.Template


    public void testGenerateException5() throws Exception {
        directory.delete();
        directory.mkdir();
        TemplateSuite suite = createMock(TemplateSuite.class);
        TemplateClass clazz = createMock(TemplateClass.class);
        Template template = createMock(Template.class);
        @SuppressWarnings("unchecked")
        Map<String, String> parameters = createMock(Map.class);
        String packageName = "org.apache.tiles.autotag.test";
        String runtimeClass = "org.apache.tiles.autotag.test.DoStuffRuntime";
        String requestClass = "org.apache.tiles.autotag.test.DoStuffRequest";

        expect(generator.getDirectoryName(directory, packageName, suite, clazz, parameters, runtimeClass, requestClass)).andReturn("mydir");
        File mydir = new File(directory, "mydir");
        expect(generator.getFilename(mydir , packageName, suite, clazz, parameters, runtimeClass, requestClass)).andReturn("myfile.txt");
        String sampleVmPath = "/sample.vm";
        expect(generator.getTemplatePath(mydir, packageName, suite, clazz, parameters, runtimeClass, requestClass)).andReturn(sampleVmPath);
        expect(velocityEngine.getTemplate("/sample.vm")).andReturn(template);
        template.merge(isA(VelocityContext.class), isA(FileWriter.class));
        expectLastCall().andThrow(new ClassParseException());

        replay(velocityEngine, generator, suite, clazz, template, parameters);
        generator.generate(directory, packageName, suite, clazz, parameters, runtimeClass, requestClass);
        verify(velocityEngine, generator, suite, clazz, template, parameters);
View Full Code Here


        // Don't spit out any logs
        velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM_CLASS, "org.apache.velocity.runtime.log.NullLogSystem");
        velocity.init();

        Template template = velocity.getTemplate(planFileName);
        StringWriter writer = new StringWriter();
        template.merge(context, writer);

        String plan = writer.toString();

        XmlObject doc = XmlObject.Factory.parse(plan);
        XmlCursor xmlCursor = doc.newCursor();
View Full Code Here

      VelocityContext context) {
    String templatePath = config.getTemplateFolder()+ System.getProperty("file.separator") +template;
    Writer writer = null;
    try {
      log.info("Writing " + file);
      Template velocityTemplate = engine.getTemplate(templatePath);
      writer = new FileWriter(file);
      velocityTemplate.merge(context, writer);
    } catch (IOException e) {
      log.error(e.getMessage(), e);
    } catch (ResourceNotFoundException e) {
      log.error(e.getMessage(), e);
    } catch (ParseErrorException e) {
View Full Code Here

        Velocity.setProperty(Velocity.RUNTIME_LOG_LOGSYSTEM, new Log4JLogChute());
        Velocity.setProperty(Velocity.RESOURCE_LOADER, "class");
        Velocity.setProperty("class.resource.loader.description", "Velocity Classpath Resource Loader");
        Velocity.setProperty("class.resource.loader.class", ClasspathResourceLoader.class.getName());
        Velocity.init();
        Template template = Velocity.getTemplate("/org/apache/openejb/tomee/configs/" + filename);
        VelocityContext context = new VelocityContext();
        context.put("tomcatHttpPort", Integer.toString(configuration.getHttpPort()));
        context.put("tomcatShutdownPort", Integer.toString(configuration.getStopPort()));
        Writer writer = new FileWriter(new File(targetDir, filename));
        template.merge(context, writer);
        writer.flush();
        writer.close();
    }
View Full Code Here

//  private static final Logger log = LoggerFactory.getLogger(VelocityResponseWriter.class);

  public void write(Writer writer, SolrQueryRequest request, SolrQueryResponse response) throws IOException {
    VelocityEngine engine = getEngine(request)// TODO: have HTTP headers available for configuring engine

    Template template = getTemplate(engine, request);

    VelocityContext context = new VelocityContext();

    context.put("request", request);

    // 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.
    // TODO: 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
      context.put("page", new PageTool(request, response))// page tool only makes sense for a SearchHandler request... *sigh*
    } catch (ClassCastException e) {
      // known edge case where QueryResponse's extraction assumes "response" is a SolrDocumentList
      // (AnalysisRequestHandler emits a "response")
      e.printStackTrace();
      rsp = new SolrResponseBase();
      rsp.setResponse(parsedResponse);
    }
    context.put("response", rsp);

    // Velocity context tools - TODO: make these pluggable
    context.put("esc", new EscapeTool());
    context.put("date", new ComparisonDateTool());
    context.put("list", new ListTool());
    context.put("math", new MathTool());
    context.put("number", new NumberTool());
    context.put("sort", new SortTool());

    context.put("engine", engine)// for $engine.resourceExists(...)

    String layout_template = request.getParams().get("v.layout");
    String json_wrapper = request.getParams().get("v.json");
    boolean wrap_response = (layout_template != null) || (json_wrapper != null);

    // create output, optionally wrap it into a json object
    if (wrap_response) {
      StringWriter stringWriter = new StringWriter();
      template.merge(context, stringWriter);

      if (layout_template != null) {
        context.put("content", stringWriter.toString());
        stringWriter = new StringWriter();
        try {
          engine.getTemplate(layout_template + ".vm").merge(context, stringWriter);
        } catch (Exception e) {
          throw new IOException(e.getMessage());
        }
      }

      if (json_wrapper != null) {
        writer.write(request.getParams().get("v.json") + "(");
        writer.write(getJSONWrap(stringWriter.toString()));
        writer.write(')');
      } else // using a layout, but not JSON wrapping
        writer.write(stringWriter.toString());
      }
    } else {
      template.merge(context, writer);
    }
  }
View Full Code Here

    return engine;
  }

  private Template getTemplate(VelocityEngine engine, SolrQueryRequest request) throws IOException {
    Template template;

    String template_name = request.getParams().get("v.template");
    String qt = request.getParams().get("qt");
    String path = (String) request.getContext().get("path");
    if (template_name == null && path != null) {
View Full Code Here

        Context context = new VelocityContext();

        context.put(REQUEST, request);
        context.put(RESPONSE, response);

        Template template = null;

        try {
            template = ve.getTemplate(page);
        } catch (ResourceNotFoundException rne) {
            throw new ViewHandlerException("Invalid template source", rne);
        } catch (Exception e) {
            throw new ViewHandlerException(e.getMessage(), e);
        }

        ServletOutputStream out = null;
        VelocityWriter vw = null;

        try {
            out = response.getOutputStream();
        } catch (IOException e) {
            throw new ViewHandlerException(e.getMessage(), e);
        }

        try {
            vw = (VelocityWriter) writerPool.get();
            if (vw == null)
                vw = new VelocityWriter(new OutputStreamWriter(out, encoding), 4 * 1024, true);
            else
                vw.recycle(new OutputStreamWriter(out, encoding));

            if (vw == null)
                Debug.logWarning("[VelocityViewHandler.eval] : VelocityWriter is NULL", module);

            template.merge(context, vw);
        } catch (Exception e) {
            throw new ViewHandlerException(e.getMessage(), e);
        } finally {
            try {
                if (vw != null) {
View Full Code Here

  public Template getTemplate(String templateName) throws ResourceNotFoundException, ParseErrorException, Exception {
    return engine.getTemplate(templateName);
  }
 
  public String mergeData(String vmName) throws ResourceNotFoundException, ParseErrorException, Exception{
    Template template = engine.getTemplate(vmName);
    VelocityContext context = getDefaultContext();
    StringWriter writer = new StringWriter();
    template.merge(context, writer);
    return writer.toString();
  }
View Full Code Here

    template.merge(context, writer);
    return writer.toString();
  }
 
  public String mergeData(String vmName, String variableName, Object info) throws ResourceNotFoundException, ParseErrorException, Exception {
    Template template = engine.getTemplate(vmName);
    VelocityContext context = getDefaultContext();
    context.put("template", this);
    context.put(variableName, info);
    StringWriter writer = new StringWriter();
    template.merge(context, writer);
    return writer.toString();
  }
View Full Code Here

      engine.setProperty("file.resource.loader.path", "d:/tmp/template");
      engine.setProperty("file1.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
      engine.setProperty("file1.resource.loader.path", "d:/tmp/template1");
      engine.init();
     
      Template template = engine.getTemplate("sample.vm");
      VelocityContext context = new VelocityContext();
      StringWriter writer = new StringWriter();
      template.merge(context, writer);
      System.out.println(writer.toString());
  }
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.