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.IOException;
023import java.io.Writer;
024import java.net.URL;
025import java.util.HashMap;
026import java.util.Map;
027
028import javax.servlet.http.HttpServletRequest;
029
030import org.nuxeo.ecm.webengine.WebEngine;
031import org.nuxeo.ecm.webengine.model.WebContext;
032import org.nuxeo.theme.html.ui.ThemeStyles;
033import org.nuxeo.theme.themes.ThemeManager;
034
035import freemarker.core.Environment;
036import freemarker.template.TemplateDirectiveBody;
037import freemarker.template.TemplateDirectiveModel;
038import freemarker.template.TemplateException;
039import freemarker.template.TemplateModel;
040import freemarker.template.TemplateModelException;
041
042/**
043 * @author <a href="mailto:jmo@chalmers.se">Jean-Marc Orliaguet</a>
044 */
045public class NXThemesThemeStylesDirective implements TemplateDirectiveModel {
046
047    public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body)
048            throws TemplateException, IOException {
049
050        if (loopVars.length != 0) {
051            throw new TemplateModelException("This directive doesn't allow loop variables.");
052        }
053        if (body != null) {
054            throw new TemplateModelException("Didn't expect a body");
055        }
056
057        Writer writer = env.getOut();
058
059        WebContext context = WebEngine.getActiveContext();
060        HttpServletRequest request = context.getRequest();
061
062        final URL themeUrl = (URL) request.getAttribute("org.nuxeo.theme.url");
063
064        String themeName;
065        if (params.containsKey("theme")) {
066            themeName = params.get("theme").toString();
067        } else {
068            themeName = ThemeManager.getThemeNameByUrl(themeUrl);
069        }
070
071        String collectionName = ThemeManager.getCollectionNameByUrl(themeUrl);
072
073        Boolean cache = false;
074        if (params.containsKey("cache")) {
075            cache = Boolean.parseBoolean(params.get("cache").toString());
076        }
077
078        Boolean inline = false;
079        if (params.containsKey("inline")) {
080            inline = Boolean.parseBoolean(params.get("inline").toString());
081        }
082
083        Map<String, String> attributes = new HashMap<String, String>();
084        attributes.put("themeName", themeName);
085        attributes.put("path", context.getModulePath());
086        attributes.put("basepath", context.getBasePath());
087        attributes.put("collection", collectionName);
088
089        Boolean virtualHosting = Utils.isWebEngineVirtualHosting(request);
090        writer.write(ThemeStyles.render(attributes, cache, inline, virtualHosting));
091    }
092}