public static void main(String[] args) { WebMacroTemplateEngine a = new WebMacroTemplateEngine(); Template t = new DefaultTemplate(); t.setResource("noservlet.wm"); Context c = new DefaultContext(); c.put("msg", "hello中文哦."); StringWriter writer = new StringWriter(); a.mergeTemplate(t, c, writer); System.out.println(writer.toString()); }
protected void mergeFileTemplate(Template pTemplate, Context pContext, Writer pWriter) throws MergeTemplateException { Properties props = WebMacroHelper.getDefaultProperties(); props.putAll(this.properties); props.setProperty("TemplateEncoding", pTemplate.getInputEncoding()); String path = pTemplate.getResource(); if (log.isDebugEnabled()) { log.debug("模板文件: \"" + path + "\" 采用WebMacro引擎进行合并."); log.debug("模板文件: \"" + path + "\" 输入编码方式是:" + pTemplate.getInputEncoding()); } /** * @FIXME 由于WebMacro没有提供运行时,设置模板编码方式,所以要求每次实例化Engine,这样性能有些影响,希望以后版本 * WebMacro会提供该方法。不过好在,是单机版本的,性能问题不大. */ WM wm = WebMacroHelper.getWMEngine(props); org.webmacro.Context c = new org.webmacro.Context(wm.getBroker()); Map params = pContext.getParameters(); for (Iterator i = params.keySet().iterator(); i.hasNext(); ) { String key = (String) i.next(); Object value = params.get(key); c.put(key, value); } org.webmacro.Template t = null; try { t = wm.getTemplate(pTemplate.getResource()); } catch (ResourceException e) { final String MSG = "合并模板文件 \"" + path + "\" 失败!" + e.getMessage(); if (log.isErrorEnabled()) { log.error(MSG, e); } throw new MergeTemplateException(MSG, e); } String result = null; try { // result = t.evaluateAsString(c); // } catch (PropertyException e) { } catch (Exception e) { final String MSG = "合并模板文件 \"" + path + "\" 失败!" + e.getMessage(); if (log.isErrorEnabled()) { log.error(MSG, e); } throw new MergeTemplateException(MSG, e); } try { pWriter.write(result); } catch (IOException e) { final String MSG = "合并模板文件 \"" + path + "\" 失败!" + e.getMessage(); if (log.isErrorEnabled()) { log.error(MSG, e); } throw new MergeTemplateException(MSG, e); } }