001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     bstefanescu
011 *
012 * $Id$
013 */
014
015package org.nuxeo.ecm.platform.rendering.fm.extensions;
016
017import java.io.IOException;
018import java.util.Map;
019
020import freemarker.core.Environment;
021import freemarker.template.SimpleScalar;
022import freemarker.template.Template;
023import freemarker.template.TemplateDirectiveBody;
024import freemarker.template.TemplateDirectiveModel;
025import freemarker.template.TemplateException;
026import freemarker.template.TemplateModel;
027import freemarker.template.TemplateModelException;
028
029/**
030 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
031 */
032public class ExtendsDirective implements TemplateDirectiveModel {
033
034    @Override
035    @SuppressWarnings("rawtypes")
036    public void execute(Environment env, Map params, TemplateModel[] loopVars, TemplateDirectiveBody body)
037            throws TemplateException, IOException {
038
039        if (body == null) {
040            throw new TemplateModelException("Expecting a body");
041        }
042
043        String src = null;
044        SimpleScalar scalar = (SimpleScalar) params.get("src");
045        if (scalar != null) {
046            src = scalar.getAsString();
047        } else {
048            throw new TemplateModelException("src attribute is not defined");
049        }
050
051        BlockWriter writer = (BlockWriter) env.getOut();
052        writer.suppressOutput = true;
053        body.render(writer);
054        writer.suppressOutput = false;
055
056        // now we should go into the base template and render it
057        // String oldPath = writer.reg.path;
058        // writer.reg.path = src;
059        Template temp = env.getConfiguration().getTemplate(src);
060        env.include(temp);
061        // writer.reg.path = oldPath;
062    }
063
064}