Package org.apache.velocity.app

Examples of org.apache.velocity.app.VelocityEngine


  // TODO: maybe pass this Logger to the template for logging from there?
//  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());
        }
      }

View Full Code Here


      template.merge(context, writer);
    }
  }

  private VelocityEngine getEngine(SolrQueryRequest request) {
    VelocityEngine engine = new VelocityEngine();
    String template_root = request.getParams().get("v.base_dir");
    File baseDir = new File(request.getCore().getResourceLoader().getConfigDir(), "velocity");
    if (template_root != null) {
      baseDir = new File(template_root);
    }
    engine.setProperty(VelocityEngine.FILE_RESOURCE_LOADER_PATH, baseDir.getAbsolutePath());
    engine.setProperty("params.resource.loader.instance", new SolrParamResourceLoader(request));
    SolrVelocityResourceLoader resourceLoader =
        new SolrVelocityResourceLoader(request.getCore().getSolrConfig().getResourceLoader());
    engine.setProperty("solr.resource.loader.instance", resourceLoader);

    // TODO: Externalize Velocity properties
    engine.setProperty(VelocityEngine.RESOURCE_LOADER, "params,file,solr");
    String propFile = request.getParams().get("v.properties");
    try {
      if (propFile == null)
        engine.init();
      else {
        InputStream is = null;
        try {
          is = resourceLoader.getResourceStream(propFile);
          Properties props = new Properties();
          props.load(is);
          engine.init(props);
        }
        finally {
          if (is != null) is.close();
        }
      }
View Full Code Here

    private VelocityEngine ve = null;

    public void init(ServletContext context) throws ViewHandlerException {
        try {
            Debug.logInfo("[VelocityViewHandler.init] : Loading...", module);
            ve = new VelocityEngine();
            // set the properties
            // use log4j for logging
            // use classpath template loading (file loading will not work in WAR)
            ve.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS,
                "org.apache.velocity.runtime.log.Log4JLogSystem");
View Full Code Here

      }
      if (siteDomainLanguage.getSiteName() != null) {
        siteName = siteDomainLanguage.getSiteName();
      }
    }
    engine = new VelocityEngine();
    engine.setProperty(RuntimeConstants.RUNTIME_LOG_LOGSYSTEM_CLASS, "org.apache.velocity.runtime.log.Log4JLogChute" );

      engine.setProperty("runtime.log.logsystem.log4j.logger", "loggername");

    initResourceLoader();
View Full Code Here

      }
      if (siteDomainLanguage.getSiteName() != null) {
        siteName = siteDomainLanguage.getSiteName();
      }
    }
    engine = new VelocityEngine();
    initResourceLoader();
    dataApi = DataApi.getInstance();
    api = new ContentApi(request);
    siteInfo = api.getSite();
  }
View Full Code Here

      }
    }
  }
 
  public void velocity() throws Exception {
    VelocityEngine engine = new VelocityEngine();
      engine.setProperty("resource.loader", "file, file1");
      engine.setProperty("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.FileResourceLoader");
      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

    }

    public static void main(String[] args) {
        try {
            // set up
            VelocityEngine ve = new VelocityEngine();
            ve.setProperty(VelocityEngine.RESOURCE_LOADER, "class");
            ve.setProperty("class.resource.loader.class", ClasspathResourceLoader.class.getName());

            ve.init();
            new File("build/generated").delete();
            new File("build/generated/example/stories").mkdirs();
            // read template
            StoryDetails story = buildStory();
            VelocityContext context = new VelocityContext();
View Full Code Here

  private static void fillTemplates(String sourceDir, Expression root) throws Exception {
    // intialising template engine (ie. Velocity)
    Properties props = new Properties();
    props.setProperty("file.resource.loader.path", sourceDir);
    VelocityEngine ve = new VelocityEngine(props);
    ve.init();

    // Create output-files.xml
    createOutputFileList(ve);
    LOG.info("DONE.\n");
View Full Code Here

    public VelocityTemplateProcessor(String templateResourcePath, String macroLibrary, Class referenceInsertionClass) {
        this.context = new VelocityContext();
        Properties config = createVelocityConfiguration(macroLibrary, referenceInsertionClass);
        try
        {
            VelocityEngine engine = new VelocityEngine(config);
            this.template = engine.getTemplate(templateResourcePath);
        }
        catch (Exception e)
        {
            throw new RuntimeException(e);
        }
View Full Code Here

        this.setProperty(CLASSPATH_RESOURCE_LOADER_CACHE, "true");
        this.setProperty(CLASSPATH_RESOURCE_LOADER_CHECKINT, CHECK_INTERVAL);
    }

    private VelocityEngine createEngine() throws Exception{
        final VelocityEngine ve = new VelocityEngine();
       
        // set properties
        final Set<String> names = _properties.keySet();
        for (final String name:names){
            ve.setProperty(name, _properties.get(name));
        }
       
        ve.init();
        return ve;
    }
View Full Code Here

TOP

Related Classes of org.apache.velocity.app.VelocityEngine

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.