001/* 002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others. 003 * 004 * All rights reserved. This program and the accompanying materials 005 * are made available under the terms of the Eclipse Public License v1.0 006 * which accompanies this distribution, and is available at 007 * http://www.eclipse.org/legal/epl-v10.html 008 * 009 * Contributors: 010 * bstefanescu 011 */ 012package org.nuxeo.ecm.automation.core.rendering; 013 014import java.io.IOException; 015import java.util.Map; 016 017import org.nuxeo.ecm.automation.OperationContext; 018import org.nuxeo.ecm.automation.OperationException; 019import org.nuxeo.ecm.automation.core.scripting.Scripting; 020import org.nuxeo.ecm.platform.rendering.api.RenderingException; 021 022import freemarker.template.TemplateException; 023 024/** 025 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a> 026 */ 027public class RenderingService { 028 029 // TODO use a runtime service 030 private static RenderingService instance = new RenderingService(); 031 032 public static RenderingService getInstance() { 033 return instance; 034 } 035 036 protected MvelRender mvel = new MvelRender(); 037 038 protected FreemarkerRender ftl = new FreemarkerRender(); 039 040 public String render(String type, String uriOrContent, OperationContext ctx) throws OperationException, 041 RenderingException, TemplateException, IOException { 042 Map<String, Object> map = Scripting.initBindings(ctx); 043 // map.put("DocUrl", MailTemplateHelper.getDocumentUrl(doc, viewId)); 044 return getRenderer(type).render(uriOrContent, map); 045 } 046 047 public Renderer getRenderer(String type) { 048 if ("mvel".equals(type)) { 049 return mvel; 050 } else { 051 return ftl; 052 } 053 } 054 055}