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.Head; 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 NXThemesHeadDirective implements TemplateDirectiveModel { 046 047 public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) 048 throws TemplateException, IOException { 049 050 if (!params.isEmpty()) { 051 throw new TemplateModelException("This directive doesn't allow parameters."); 052 } 053 if (loopVars.length != 0) { 054 throw new TemplateModelException("This directive doesn't allow loop variables."); 055 } 056 if (body != null) { 057 throw new TemplateModelException("Didn't expect a body"); 058 } 059 060 Writer writer = env.getOut(); 061 062 WebContext context = WebEngine.getActiveContext(); 063 HttpServletRequest request = context.getRequest(); 064 final URL themeUrl = (URL) request.getAttribute("org.nuxeo.theme.url"); 065 066 String baseUrl = context.head().getURL(); 067 if (!baseUrl.endsWith("/")) { 068 baseUrl += "/"; 069 } 070 071 Map<String, String> attributes = new HashMap<String, String>(); 072 attributes.put("themeName", ThemeManager.getThemeNameByUrl(themeUrl)); 073 attributes.put("path", context.getModulePath()); 074 attributes.put("baseUrl", baseUrl); 075 writer.write(Head.render(attributes)); 076 } 077}