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.StringWriter; 024import java.net.URL; 025import java.util.Map; 026 027import javax.servlet.http.HttpServletRequest; 028 029import org.nuxeo.ecm.webengine.WebEngine; 030import org.nuxeo.ecm.webengine.model.WebContext; 031import org.nuxeo.theme.Manager; 032 033import freemarker.core.Environment; 034import freemarker.template.TemplateDirectiveBody; 035import freemarker.template.TemplateDirectiveModel; 036import freemarker.template.TemplateException; 037import freemarker.template.TemplateModel; 038import freemarker.template.TemplateModelException; 039 040/** 041 * @author <a href="mailto:jmo@chalmers.se">Jean-Marc Orliaguet</a> 042 */ 043public class NXThemesRequireDirective implements TemplateDirectiveModel { 044 045 public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body) 046 throws TemplateException, IOException { 047 048 if (!params.isEmpty()) { 049 throw new TemplateModelException("This directive doesn't allow parameters."); 050 } 051 if (loopVars.length != 0) { 052 throw new TemplateModelException("This directive doesn't allow loop variables."); 053 } 054 if (body == null) { 055 throw new TemplateModelException("This directive expects a body"); 056 } 057 058 WebContext context = WebEngine.getActiveContext(); 059 HttpServletRequest request = context.getRequest(); 060 final URL themeUrl = (URL) request.getAttribute("org.nuxeo.theme.url"); 061 062 StringWriter sw = new StringWriter(); 063 body.render(sw); 064 String resourceName = sw.getBuffer().toString(); 065 066 // Register as a local resource 067 final boolean local = true; 068 Manager.getResourceManager().addResource(resourceName, themeUrl, local); 069 } 070}