Package groovy.text

Examples of groovy.text.Template


        }

        //
        // Get the template source file handle.
        //
        Template template;
        long getMillis;
        String name;
       
        File file = super.getScriptUriAsFile(request);
        if (file != null) {
            name = file.getName();
            if (!file.exists()) {
                response.sendError(HttpServletResponse.SC_NOT_FOUND);
                return; // throw new IOException(file.getAbsolutePath());
            }
            if (!file.canRead()) {
                response.sendError(HttpServletResponse.SC_FORBIDDEN, "Can not read \"" + name + "\"!");
                return; // throw new IOException(file.getAbsolutePath());
            }
            getMillis = System.currentTimeMillis();
            template = getTemplate(file);
            getMillis = System.currentTimeMillis() - getMillis;
        } else {
            name = super.getScriptUri(request);
            URL url = servletContext.getResource(name);
            getMillis = System.currentTimeMillis();
            template = getTemplate(url);
            getMillis = System.currentTimeMillis() - getMillis;
        }

        //
        // Create new binding for the current request.
        //
        ServletBinding binding = new ServletBinding(request, response, servletContext);
        setVariables(binding);

        //
        // Prepare the response buffer content type _before_ getting the writer.
        // and set status code to ok
        //
        response.setContentType(CONTENT_TYPE_TEXT_HTML + "; charset=" + encoding);
        response.setStatus(HttpServletResponse.SC_OK);

        //
        // Get the output stream writer from the binding.
        //
        Writer out = (Writer) binding.getVariable("out");
        if (out == null) {
            out = response.getWriter();
        }

        //
        // Evaluate the template.
        //
        if (verbose) {
            log("Making template \"" + name + "\"...");
        }
        // String made = template.make(binding.getVariables()).toString();
        // log(" = " + made);
        long makeMillis = System.currentTimeMillis();
        template.make(binding.getVariables()).writeTo(out);
        makeMillis = System.currentTimeMillis() - makeMillis;

        if (generateBy) {
            StringBuilder sb = new StringBuilder(100);
            sb.append("\n<!-- Generated by Groovy TemplateServlet [create/get=");
View Full Code Here


    String applyClassTemplates(GroovyClassDoc classDoc) {
        String templatePath = classTemplatePaths.get(0); // todo (iterate)
        String templateWithBindingApplied = "";
        try {
            Template t = classTemplates.get(templatePath);
            if (t == null) {
                t = engine.createTemplate(resourceManager.getReader(templatePath));
                classTemplates.put(templatePath, t);
            }
            Map<String, Object> binding = new HashMap<String, Object>();
            binding.put("classDoc", classDoc);
            binding.put("props", properties);
            templateWithBindingApplied = t.make(binding).toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return templateWithBindingApplied;
    }
View Full Code Here

    }

    String applyPackageTemplate(String template, GroovyPackageDoc packageDoc) {
        String templateWithBindingApplied = "";
        try {
            Template t = packageTemplates.get(template);
            if (t == null) {
                t = engine.createTemplate(resourceManager.getReader(template));
                packageTemplates.put(template, t);
            }
            Map<String, Object> binding = new HashMap<String, Object>();
            binding.put("packageDoc", packageDoc);
            binding.put("props", properties);
            templateWithBindingApplied = t.make(binding).toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return templateWithBindingApplied;
    }
View Full Code Here

    }

    String applyRootDocTemplate(String template, GroovyRootDoc rootDoc) {
        String templateWithBindingApplied = "";
        try {
            Template t = docTemplates.get(template);
            if (t == null) {
                t = engine.createTemplate(resourceManager.getReader(template));
                docTemplates.put(template, t);
            }
            Map<String, Object> binding = new HashMap<String, Object>();
            binding.put("rootDoc", rootDoc);
            binding.put("props", properties);
            templateWithBindingApplied = t.make(binding).toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return templateWithBindingApplied;
    }
View Full Code Here

     * @return this template instance
     * @throws IOException
     * @throws ClassNotFoundException
     */
    public Object fragment(Map model, String templateText) throws IOException, ClassNotFoundException {
        Template template = cachedFragments.get(templateText);
        if (template==null) {
            template = engine.createTemplate(new StringReader(templateText));
            cachedFragments.put(templateText, template);
        }
        template.make(model).writeTo(out);
        return this;
    }
View Full Code Here

    securityChecks(template);
    if (template == null) {
      return null;
    }
    try {
      final Template templateObject = templateEngine.createTemplate(template);
      final Writable writable = templateObject.make(variables);
      final StringWriter writer = new StringWriter();
      writable.writeTo(writer);
      writer.flush();
      if (log.isDebugEnabled() == true) {
        log.debug(writer.toString());
View Full Code Here

    }

    @Override
    public void renderDocument(final Map<String, Object> model, final String templateName, final Writer writer) throws RenderingException {
        try {
            Template template = findTemplate(templateName);
            Writable writable = template.make(wrap(model));
            writable.writeTo(writer);
        } catch (Exception e) {
            throw new RenderingException(e);
        }
    }
View Full Code Here

    }

    private Template findTemplate(final String templateName) throws SAXException, ParserConfigurationException, ClassNotFoundException, IOException {
        TemplateEngine ste = templateName.endsWith(".gxml") ? new XmlTemplateEngine() : new SimpleTemplateEngine();
        File sourceTemplate = new File(templatesPath, templateName);
        Template template = cachedTemplates.get(templateName);
        if (template == null) {
            template = ste.createTemplate(new InputStreamReader(new BufferedInputStream(new FileInputStream(sourceTemplate)), config.getString(Keys.TEMPLATE_ENCODING)));
            cachedTemplates.put(templateName, template);
        }
        return template;
View Full Code Here

  public void render(Context context, MarkupTemplate template) throws Exception {
    String contentType = template.getContentType();
    contentType = contentType == null ? context.get(MimeTypes.class).getContentType(template.getName()) : contentType;
    try {

      Template compiledTemplate = engine.createTemplateByPath(template.getName());
      Writable boundTemplate = compiledTemplate.make(template.getModel());
      context.getResponse().send(contentType, boundTemplate.toString());
    } catch (IOException e) {
      context.error(e);
    }
  }
View Full Code Here

    binding.put("looker", looker);
    TemplateEngine engine = new IncludeTemplateEngine();
    try {
      // auto import some utils
      String header = "<%import static org.groovymud.utils.WordUtils.*;\nimport static org.groovymud.utils.NumberToWordConverter.*%>";
      Template template = engine.createTemplate(header + text);

      stream.writeln(colorizeText(template.make(binding).toString(), BOLD));
    } catch (CompilationFailedException e) {
      logger.error(e, e);
    } catch (ClassNotFoundException e) {
      logger.error(e, e);
    } catch (IOException e) {
View Full Code Here

TOP

Related Classes of groovy.text.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.