FreemarkerPipeline implements Pipeline { public Writer pipeline(String pipeTemplates[],Object rootMap,Writer writer) throws PipeException { try { Map globalContext = new HashMap(); for(int i = 0; i < pipeTemplates.length; i++) { String templateName = pipeTemplates[i]; boolean isLastTemplate = i == pipeTemplates.length - 1; try { Template template = conf.getTemplate(templateName); if(isLastTemplate) { Environment env = template.createProcessingEnvironment(rootMap, writer); env.getCurrentNamespace().putAll(globalContext); env.process(); }else { Writer tempOutput = new StringWriter(bufferSize); Environment env = template.createProcessingEnvironment(rootMap, tempOutput); env.getCurrentNamespace().putAll(globalContext); env.process(); globalContext.putAll(env.getCurrentNamespace().toMap()); globalContext.put(Pipeline.PIPELINE_CONTENT_VAR_NAME, tempOutput.toString()); } }catch(Exception e){ handleException(e,templateName,isLastTemplate); } } return writer; }catch(Exception e) { throw new PipeException("process FreeMarker template occer exception,pipeTemplates:"+StringUtils.join(pipeTemplates," | "),e); } } FreemarkerPipeline();  FreemarkerPipeline(Configuration conf); Configuration getConfiguration(); void setConfiguration(Configuration conf); int getBufferSize(); void setBufferSize(int bufferSize); Writer pipeline(String pipeTemplates[],Object rootMap,Writer writer); void handleException(Exception e, String templateName,boolean isLastTemplate); Writer pipeline(String[] pipeTemplates, Map model, Writer writer); Writer pipeline(String pipeTemplates,Object rootMap,Writer writer); Writer pipeline(String pipeTemplates, Map model, Writer writer);  }
@Test public void testFreemarker() throws Exception { StringWriter out = new StringWriter(); HashMap model = new HashMap(); model.put("name", "badqiu"); pipeline.pipeline("first.flt|second.flt | three.flt", model, out); System.out.println(out.toString()); String expected = "<html><head>override_by_first</head><body>override_by_second</body><three><second>first:badqiu</second></three></html>"; Assert.assertEquals(expected,out.toString().replaceAll("\\s+", "")); }
@Test public void testOtherMethod() { pipeline.pipeline("first.flt|second.flt | three.flt", new Object(), new StringWriter()); }