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