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.util.Map;
025
026import org.nuxeo.theme.html.ui.MVCElement;
027
028import freemarker.core.Environment;
029import freemarker.template.TemplateDirectiveBody;
030import freemarker.template.TemplateDirectiveModel;
031import freemarker.template.TemplateException;
032import freemarker.template.TemplateModel;
033import freemarker.template.TemplateModelException;
034
035/**
036 * @author <a href="mailto:jmo@chalmers.se">Jean-Marc Orliaguet</a>
037 */
038public abstract class NXThemesBaseMVC implements TemplateDirectiveModel {
039
040    public abstract String getClassName();
041
042    @SuppressWarnings("unchecked")
043    public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body)
044            throws TemplateException, IOException {
045
046        if (loopVars.length != 0) {
047            throw new TemplateModelException("This directive doesn't allow loop variables.");
048        }
049        if (body != null) {
050            throw new TemplateModelException("Didn't expect a body");
051        }
052
053        Writer writer = env.getOut();
054        Map<String, String> attributes = Utils.getTemplateDirectiveParameters(params);
055        attributes.put("className", getClassName());
056        writer.write(MVCElement.render(attributes));
057    }
058
059}