/*
This library is part of octools
Copyright (c) 2000-2007 Valtech A/S (http://www.valtech.dk)
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
Alkacon OpenCms and the OpenCms logo are registered trademarks of
Alkacon Software GmbH in Germany, the USA and other countries
For further information about Alkacon Software GmbH, please see the
company website: http://www.alkacon.com
For further information about OpenCms, please see the
project website: http://www.opencms.org
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package dk.valtech.octools.jsp;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import org.apache.commons.beanutils.BeanUtils;
import org.opencms.file.CmsObject;
import org.opencms.flex.CmsFlexController;
import org.opencms.jsp.CmsJspNavElement;
import dk.valtech.octools.navigation.FlatRecursiveNavigationIterator;
import dk.valtech.octools.navigation.NavigationIterationStatus;
import dk.valtech.octools.navigation.NavigationIterator;
import dk.valtech.octools.navigation.TreeNavigationIterator;
public class CmsJspTagNavigation extends BodyTagSupport {
private static final long serialVersionUID = 1L;
private String var;
private String status;
private NavigationIterator navigationIterator;
private String recursive;
private Iterator<CmsJspNavElement> iterator;
private HashMap<String, Object> attributes;
public CmsJspTagNavigation() {
attributes = new HashMap<String, Object>();
}
public void setRecursive(String recursive) {
this.recursive = recursive;
}
public void setStatus(String status) {
this.status = status;
}
public void setVar(String var) {
this.var = var;
}
@Override
public int doStartTag() throws JspException {
CmsFlexController m_controller = CmsFlexController.getController(pageContext.getRequest());
CmsObject cms = m_controller.getCmsObject();
if (recursive == null || "tree".equalsIgnoreCase(recursive)) {
navigationIterator = new TreeNavigationIterator(cms);
} else if ("flat".equalsIgnoreCase(recursive)) {
navigationIterator = new FlatRecursiveNavigationIterator(cms);
} else {
throw new UnsupportedOperationException("Only two valid arguments for recursive is tree|flat");
}
try {
BeanUtils.populate(navigationIterator, attributes);
} catch (Exception e) {
throw new JspException("Unable to populate " + navigationIterator + " with " + attributes, e);
}
iterator = navigationIterator.iterator();
if (status != null) {
pageContext.setAttribute(status, navigationIterator.getStatus());
}
int action = SKIP_BODY;
if (iterator.hasNext()) {
action = EVAL_BODY_INCLUDE;
doAfterBody();
}
return action;
}
@Override
public int doAfterBody() throws JspException {
if (iterator.hasNext()) {
pageContext.setAttribute(var, iterator.next());
return EVAL_BODY_AGAIN;
} else {
try {
navigationIterator.close();
} catch (IOException e) {
throw new JspException("Unable to clote the iteration process properly", e);
}
}
return SKIP_BODY;
}
public CmsJspNavElement getCurrentElement() {
return (CmsJspNavElement) pageContext.getAttribute(var);
}
@Override
public int doEndTag() throws JspException {
return EVAL_PAGE;
}
public void setFolder(String folder) {
attributes.put("folder", folder);
}
public void setLevel(String level) {
attributes.put("level", Integer.parseInt(level));
}
public void setEndLevel(String endLevel) {
attributes.put("endLevel", Integer.parseInt(endLevel));
}
public void setExplodeFrom(String explodeFrom) {
attributes.put("explodeFrom", Integer.parseInt(explodeFrom));
}
public void setExplodeTo(String explodeTo) {
attributes.put("explodeTo", Integer.parseInt(explodeTo));
}
public void setIncludeNonNavigation(boolean includeNonNavigation) {
attributes.put("includingNonNavigation", includeNonNavigation);
}
public NavigationIterator getNavigationIterator() {
return navigationIterator;
}
public NavigationIterationStatus getNavigationIterationStatus() {
return navigationIterator.getStatus();
}
}