001/* 002 * (C) Copyright 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 * 016 * Contributors: 017 * bstefanescu 018 */ 019package org.nuxeo.ecm.automation.core.rendering; 020 021import java.io.File; 022import java.io.IOException; 023import java.io.StringReader; 024import java.io.StringWriter; 025import java.io.Writer; 026import java.net.MalformedURLException; 027import java.net.URL; 028import java.util.Map; 029 030import org.nuxeo.ecm.platform.rendering.api.RenderingException; 031import org.nuxeo.ecm.platform.rendering.api.ResourceLocator; 032import org.nuxeo.ecm.platform.rendering.fm.FreemarkerEngine; 033import org.nuxeo.runtime.api.Framework; 034import org.nuxeo.runtime.services.resource.ResourceService; 035 036import freemarker.core.Environment; 037import freemarker.template.Template; 038import freemarker.template.TemplateException; 039 040/** 041 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 042 */ 043public class FreemarkerRender extends FreemarkerEngine implements Renderer { 044 045 public FreemarkerRender() { 046 setResourceLocator(new ResourceLocator() { 047 @Override 048 public URL getResourceURL(String key) { 049 try { 050 if (key.startsWith(Renderer.TEMPLATE_PREFIX)) { 051 return Framework.getService(ResourceService.class).getResource( 052 key.substring(Renderer.TEMPLATE_PREFIX.length())); 053 } else { 054 return new URL(key); 055 } 056 } catch (MalformedURLException e) { 057 return null; 058 } 059 } 060 061 @Override 062 public File getResourceFile(String key) { 063 return null; 064 } 065 }); 066 } 067 068 public void renderContent(String content, Object ctx, Writer writer) throws IOException, TemplateException { 069 StringReader reader = new StringReader(content); 070 Template tpl = new Template("@inline", reader, getConfiguration(), "UTF-8"); 071 Environment env = tpl.createProcessingEnvironment(ctx, writer, getObjectWrapper()); 072 env.process(); 073 } 074 075 @Override 076 public String render(String uriOrContent, Map<String, Object> root) throws RenderingException, TemplateException, 077 IOException { 078 if (root.get("Document") != null) { 079 // mvel wrapper not supported in freemarker 080 root.put("Document", root.get("This")); 081 } 082 StringWriter result = new StringWriter(); 083 if (uriOrContent.startsWith(Renderer.TEMPLATE_PREFIX)) { 084 render(uriOrContent, root, result); 085 } else { 086 renderContent(uriOrContent, root, result); 087 } 088 return result.getBuffer().toString(); 089 } 090}