001/*
002 * (C) Copyright 2006-2009 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Jean-Marc Orliaguet, Chalmers
016 *
017 * $Id$
018 */
019
020package org.nuxeo.theme.webengine.fm.extensions;
021
022import java.io.BufferedReader;
023import java.io.IOException;
024import java.io.StringReader;
025import java.net.URL;
026import java.util.Map;
027
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030import org.nuxeo.ecm.webengine.WebEngine;
031import org.nuxeo.ecm.webengine.model.WebContext;
032import org.nuxeo.theme.models.InfoPool;
033import org.nuxeo.theme.themes.ThemeException;
034import org.nuxeo.theme.themes.ThemeManager;
035
036import freemarker.core.Environment;
037import freemarker.ext.beans.BeansWrapper;
038import freemarker.template.Template;
039import freemarker.template.TemplateDirectiveBody;
040import freemarker.template.TemplateDirectiveModel;
041import freemarker.template.TemplateException;
042import freemarker.template.TemplateModel;
043import freemarker.template.TemplateModelException;
044
045/**
046 * @author <a href="mailto:jmo@chalmers.se">Jean-Marc Orliaguet</a>
047 */
048public class NXThemesFragmentDirective implements TemplateDirectiveModel {
049
050    private static final Log log = LogFactory.getLog(NXThemesFragmentDirective.class);
051
052    final String templateEngine = "freemarker";
053
054    @SuppressWarnings("unchecked")
055    public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body)
056            throws TemplateException, IOException {
057
058        if (loopVars.length != 0) {
059            throw new TemplateModelException("This directive doesn't allow loop variables.");
060        }
061        if (body != null) {
062            throw new TemplateModelException("Didn't expect a body");
063        }
064
065        WebContext ctx = WebEngine.getActiveContext();
066        if (ctx == null) {
067            throw new IllegalStateException("Not In a Web Context");
068        }
069
070        env.setVariable("nxthemesInfo", BeansWrapper.getDefaultInstance().wrap(InfoPool.getInfoMap()));
071
072        Map<String, String> attributes = Utils.getTemplateDirectiveParameters(params);
073        final URL elementUrl = new URL(String.format("nxtheme://element/%s/%s/%s/%s", attributes.get("engine"),
074                attributes.get("mode"), templateEngine, attributes.get("uid")));
075
076        String rendered = "";
077        try {
078            rendered = ThemeManager.renderElement(elementUrl);
079        } catch (ThemeException e) {
080            log.error("Element rendering failed: " + e.getMessage());
081            return;
082        }
083        StringReader sr = new StringReader(rendered);
084        BufferedReader reader = new BufferedReader(sr);
085        Template tpl = new Template(elementUrl.toString(), reader, env.getConfiguration(),
086                env.getTemplate().getEncoding());
087
088        try {
089            env.include(tpl);
090        } catch (TemplateException | IOException e) {
091            log.error("Rendering of Freemarker template failed: \n" + rendered, e);
092        } finally {
093            reader.close();
094            sr.close();
095        }
096
097    }
098
099}