Package org.apache.velocity

Examples of org.apache.velocity.Template


   */
  private String merge(String template, Context c, String theme) {
    StringWriter wOut = new StringWriter(10000);
   
    try {
      Template vtemplate = null;
      if (isLogDebugEnabled()) logDebug("Merging template::" + template + " for theme::" + theme, null);

      if (theme != null) {
        // try the theme first, if resource not found exception, fallback to normal resource.
        // e.g. try /_accessibility/index.html first, if not found, try /index.html.
        // this allows for themes to only provide the delta to the default templates
       
        // todo we could avoid those string operations, if the performance gain is measureable
        int latestSlash = template.lastIndexOf('/');
        StringBuilder sb = new StringBuilder(template.substring(0,latestSlash));
        sb.append("/_").append(theme).append("/").append(template.substring(latestSlash+1));
        String themedTemplatePath = sb.toString();

        // check cache
        boolean notFound;
        synchronized (resourcesNotFound) { //o_clusterOK by:fj
          notFound = resourcesNotFound.contains(themedTemplatePath);
        }
       
        if (!notFound) {
          // never tried before -> try to load it
          if (!ve.templateExists(themedTemplatePath)) {
            // remember not found (since velocity doesn't) then try fallback.
            // this will happen once for each theme when a resource does not exist in its themed variant but only in the default theme.
            if (!Settings.isDebuging()) {
              synchronized (resourcesNotFound) { //o_clusterOK by:fj
                resourcesNotFound.add(themedTemplatePath);
              }
            } // for debugging, allow introduction of themed files without restarting the application
          } else {
            // template exists -> load it
            vtemplate = ve.getTemplate(themedTemplatePath, VelocityModule.getInputEncoding());
          }
        }
        // if not found, fallback to standard
        if (vtemplate == null) {
          vtemplate = ve.getTemplate(template, VelocityModule.getInputEncoding());
        }
      } else {
        // no theme, load the standard template
        vtemplate = ve.getTemplate(template, VelocityModule.getInputEncoding());
      }
     
      vtemplate.merge(c, wOut);     
    } catch (MethodInvocationException me) {
      throw new OLATRuntimeException(VelocityHelper.class, "MethodInvocationException occured while merging template: methName:"
          + me.getMethodName() + ", refName:" + me.getReferenceName(), me.getWrappedThrowable());
    } catch (Exception e) {
      throw new OLATRuntimeException(VelocityHelper.class, "exception occured while merging template: " + e.getMessage(), e);
View Full Code Here


            LinkedHashMap<String,String> allFonts = new LinkedHashMap<String,String>();
            allFonts.put("TimesNewRoman", "TimesNewRoman");
            allFonts.put("Verdana", "Verdana");
            allFonts.put("Arial", "Arial");
             
            Template tpl = super.getTemplate ("install_step1_"+lang+".vm");
            ctx.put("allLanguages", allLanguages);
            ctx.put("allFonts", allFonts);
            StringWriter writer = new StringWriter();
            tpl.merge(ctx, writer);
           
            return tpl;
          } else {
            return getVelocityEngine().getTemplate("install_step2_"+lang+".vm");
          }
        }
       
      } else if (command.equals("step1")) {
       
        int i = InstallationDocumentHandler.getInstance().getCurrentStepNumber(working_dir);
        if (i == 0){
         
          log.error("do init installation");
         
          //update to next step
          //InstallationDocumentHandler.getInstance().createDocument(working_dir+InstallationDocumentHandler.installFileName,1);
         
          String filePath = getServletContext().getRealPath("/")+ImportInitvalues.languageFolderName;
          LinkedHashMap<Integer,LinkedHashMap<String,Object>> allLanguagesAll = ImportInitvalues.getInstance().getLanguageFiles(filePath);
          LinkedHashMap<Integer,String> allLanguages = new LinkedHashMap<Integer,String>();
          for (Iterator<Integer> iter=allLanguagesAll.keySet().iterator();iter.hasNext();) {
            Integer key = iter.next();
            String langName = (String) allLanguagesAll.get(key).get("name");
            allLanguages.put(key, langName);
          }
         
          LinkedHashMap<String,String> allFonts = new LinkedHashMap<String,String>();
          allFonts.put("TimesNewRoman", "TimesNewRoman");
          allFonts.put("Verdana", "Verdana");
          allFonts.put("Arial", "Arial");
         
          LinkedHashMap<String,String> allTimeZones = new LinkedHashMap<String,String>();
          List<OmTimeZone> omTimeZoneList = ImportInitvalues.getInstance().getTimeZones(filePath);
          log.debug("omTimeZoneList :: "+omTimeZoneList.size());
          for (OmTimeZone omTimeZone : omTimeZoneList) {
            String labelName = omTimeZone.getJname() + " (" + omTimeZone.getLabel() + ")";
            log.debug("labelName :: "+labelName);
            allTimeZones.put(omTimeZone.getJname(), labelName);
          }
         
          Template tpl = super.getTemplate ("install_step1_"+lang+".vm");
          ctx.put("allLanguages", allLanguages);
          ctx.put("allFonts", allFonts);
          ctx.put("allTimeZones", allTimeZones);
          StringWriter writer = new StringWriter();
          tpl.merge(ctx, writer);
         
          return tpl;
         
        } else {
          ctx.put("error", "This Step of the installation has already been done. continue with step 2 <A HREF='?command=step2'>continue with step 2</A>");
View Full Code Here

      ve.setProperty("file.resource.loader.path", daemonTemplate.getParent());
      ve.setProperty("runtime.log.logsystem.class", VelocityLog.class.getCanonicalName());
      // TODO find a way to set a non static logger
      VelocityLog.setLogger(_logger);
      ve.init();
      Template t = ve.getTemplate(daemonTemplate.getName());
      VelocityContext context = new VelocityContext();
      context.put("w_name", _name);
      context.put("w_long_name", _displayName);
      context.put("w_start_cmd", toStrCommand(_startCmd));
      context.put("w_stop_cmd", toStrCommand(_stopCmd));
      context.put("w_status_cmd", toStrCommand(_statusCmd));
      context.put("w_description", _description);
      context.put("w_conf_file", _confFile);
      context.put("w_app_pid_file", _appPidFile);
      context.put("w_wrapper_pid_file", _wrapperPidFile);
      FileWriter writer = new FileWriter(_daemonScript);

      t.merge(context, writer);
      writer.flush();
      writer.close();
      File daemonScript = new File(_daemonScript);
      if (daemonScript.exists())
      {
View Full Code Here

      VelocityEngine ve = new VelocityEngine();
      ve.setProperty(VelocityEngine.RESOURCE_LOADER, "file");
      ve.setProperty("file.resource.loader.path", daemonTemplate.getParent());
      ve.setProperty("runtime.log.logsystem.class", VelocityLog.class.getCanonicalName());
      ve.init();
      Template t = ve.getTemplate(daemonTemplate.getName());
      VelocityContext context = new VelocityContext();
      context.put("name", _plistName);
      context.put("command", Arrays.asList(_execCmd.split(" ")));
      context.put("autoStart", "AUTOMATIC".equals(_config.getString("wrapper.ntservice.starttype", DEFAULT_SERVICE_START_TYPE)));
      FileWriter writer = new FileWriter(_plistFile);

      t.merge(context, writer);
      writer.flush();
      writer.close();
      _utils.osCommand("launchctl load " + _plistFile, 5000);

    }
View Full Code Here

      VelocityEngine ve = new VelocityEngine();
      ve.setProperty(VelocityEngine.RESOURCE_LOADER, "file");
      ve.setProperty("file.resource.loader.path", daemonTemplate.getParent());
      ve.setProperty("runtime.log.logsystem.class", VelocityLog.class.getCanonicalName());
      ve.init();
      Template t = ve.getTemplate(daemonTemplate.getName());
      VelocityContext context = new VelocityContext();
      context.put("name", _plistName);
      context.put("command", Arrays.asList(_execCmd.split(" ")));
      context.put("autoStart", "AUTOMATIC".equals(_config.getString("wrapper.ntservice.starttype", DEFAULT_SERVICE_START_TYPE)));
      FileWriter writer = new FileWriter(_plistFile);

      t.merge(context, writer);
      writer.flush();
      writer.close();
      _utils.osCommand("launchctl load " + _plistFile, 5000);

    }
View Full Code Here

    private void processPlan(VelocityEngine velocity, File plan) throws Exception {
        // Get the plan name, which is basically plan.getAbsolutePath() - sourceDir.getAbsolutePath()
        String planName = extractPlanName(sourceDir, plan);

        // load the template
        Template template = velocity.getTemplate(planName);

        // determine the output file
        File outputFile = new File(targetDir, planName);

        // create the output directory
        File outputDir = outputFile.getParentFile();
        if (!outputDir.exists()) {
            if (!outputDir.mkdirs()) {
                throw new IllegalArgumentException("Could not create outputDir: " + outputDir.getAbsolutePath());
            }
        }
        if (!outputDir.isDirectory()) {
            throw new IllegalArgumentException("outputDir is not a directory: " + outputDir.getAbsolutePath());
        }

        if (force || outputFile.lastModified() < plan.lastModified()) {
            System.out.println("    Preprocessing " + planName);

            // process the plan
            PrintStream out = null;
            FileReader templateReader = null;
            try {
                out = new PrintStream(new FileOutputStream(outputFile));

                PrintWriter writer = new PrintWriter(out);
                template.merge(context, writer);
                writer.flush();
            } finally {
                close(out);
                close(templateReader);
            }
View Full Code Here

            HttpServletResponse res,
            VelocityContext velocityContext,
            String templateName) {

        try {
            Template template = getVelocityEngine().getTemplate(templateName);
            template.merge(velocityContext, res.getWriter());
            return true;
        } catch (Exception e) {
            log("Error rendering template: " + templateName, e);
        }
View Full Code Here

            AppProperties properties)
            throws ServletException, IOException {

        VelocityEngine engine = (VelocityEngine)properties.getProperty(CONFIG_PROPERTY);
       
        Template template = engine.getTemplate(templateName);
       
        try {
           
            template.merge(new VelocityContext(attributes), response.getWriter());
           
        } catch (Exception e) {
            throw new RuntimeException("error processing template : " + templateName, e);
        }
    }
View Full Code Here

            List<Feature> featureList = (List<Feature>) pairs.getValue();

            for (Feature feature : featureList) {
                VelocityEngine ve = new VelocityEngine();
                ve.init(getProperties());
                Template featureResult = ve.getTemplate("templates/featureReport.vm");
                VelocityContextMap contextMap = VelocityContextMap.of(new VelocityContext());
                contextMap.putAll(getGeneralParameters());
                contextMap.put("parallel", ReportBuilder.isParallel());
                contextMap.put("feature", feature);
                contextMap.put("report_status_colour", ri.getReportStatusColour(feature));
View Full Code Here

    }

    private void generateFeatureOverview() throws Exception {
        VelocityEngine ve = new VelocityEngine();
        ve.init(getProperties());
        Template featureOverview = ve.getTemplate("templates/featureOverview.vm");
        VelocityContextMap contextMap = VelocityContextMap.of(new VelocityContext());
        contextMap.putAll(getGeneralParameters());
        contextMap.put("features", ri.getFeatures());
        contextMap.put("parallel", ReportBuilder.isParallel());
        contextMap.put("total_features", ri.getTotalNumberOfFeatures());
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.