/*
* Copyright 2004, 2005, 2006 Odysseus Software GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package de.odysseus.calyxo.panels.taglib;
import java.util.ArrayList;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import de.odysseus.calyxo.panels.PanelsContext;
import de.odysseus.calyxo.panels.PanelsSupport;
import de.odysseus.calyxo.panels.conf.PanelConfig;
import de.odysseus.calyxo.panels.misc.DynamicParamConfig;
/**
* Perform a jsp include. Take the path to be included
* from the subpanel named by the <code>name</code> property
* in the current panels context scope.
* <p/>
* The panel tag may contain nested param tags to set dynamic parameters.
*
* @author Christoph Beck
*/
public class PanelTag extends BodyTagSupport {
private static final Log log = LogFactory.getLog(PanelTag.class);
private String name;
private PanelConfig panel;
private ArrayList params;
private PanelsContext context;
/**
* Get panels context stack from request scope.
* Lookup a panel named <code>name</code> within the context,
* Then, push that panel, include its <code>path</code> and
* pop it again.
*/
public int doStartTag() throws JspException {
if (log.isTraceEnabled())
log.trace("panel " + name);
PanelsSupport support = PanelsSupport.getInstance(pageContext);
context = support.getContext(pageContext);
panel = context.lookupPanelConfig(name);
if (panel == null) {
throw new JspException("Could not get panel " + name);
}
return EVAL_BODY_BUFFERED;
}
public int doEndTag() throws JspException {
if (log.isTraceEnabled())
log.trace("panel " + name);
String template = panel.findTemplate(context.getLocale());
if (template == null) {
throw new JspException("Could not get dispatch path for panel " + panel.toInlineString());
}
if (log.isDebugEnabled())
log.debug("include " + template);
// log.debug("push " + name);
context.push(panel);
if (params != null) {
for (int i = 0; i < params.size(); i++) {
context.addParamConfig((DynamicParamConfig)params.get(i));
}
}
try {
pageContext.include(template);
} catch (Exception e) {
throw new JspException("Failed to include template " + template, e);
} finally {
// log.debug("pop " + name);
context.pop();
release();
}
return EVAL_PAGE;
}
public void release() {
name = null;
panel = null;
context = null;
}
/**
* Get panel name
*/
public String getName() {
return name;
}
/**
* Set panel name
*/
public void setName(String string) {
name = string;
}
/**
* Called by nested param tags.
*/
void add(DynamicParamConfig param) {
if (params == null) {
params = new ArrayList(4);
}
params.add(param);
}
}