001/*
002 * This library is part of OpenCms -
003 * the Open Source Content Management System
004 *
005 * Copyright (c) Alkacon Software GmbH & Co. KG (http://www.alkacon.com)
006 *
007 * This library is free software; you can redistribute it and/or
008 * modify it under the terms of the GNU Lesser General Public
009 * License as published by the Free Software Foundation; either
010 * version 2.1 of the License, or (at your option) any later version.
011 *
012 * This library is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015 * Lesser General Public License for more details.
016 *
017 * For further information about Alkacon Software GmbH & Co. KG, please see the
018 * company website: http://www.alkacon.com
019 *
020 * For further information about OpenCms, please see the
021 * project website: http://www.opencms.org
022 *
023 * You should have received a copy of the GNU Lesser General Public
024 * License along with this library; if not, write to the Free Software
025 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
026 */
027
028package org.opencms.jsp;
029
030import java.util.HashMap;
031import java.util.Map;
032
033import javax.servlet.jsp.JspException;
034import javax.servlet.jsp.tagext.BodyTagSupport;
035
036/**
037 * Builds a <i>java.util.Map</i> isntance with string keys and values from nested param tags, then stores it in a page context variable whose name is supplied by the user.
038 */
039public class CmsJspTagMap extends BodyTagSupport implements I_CmsJspTagParamParent {
040
041    /** Serial version id. */
042    private static final long serialVersionUID = 3547998166985921533L;
043
044    /** Map to save parameters to the include in. */
045    private Map<String, String> m_content;
046
047    /** The variable name used to store the map. */
048    private String m_var;
049
050    /**
051     * Empty constructor, required for attribute value initialization.<p>
052     */
053    public CmsJspTagMap() {
054
055        super();
056    }
057
058    /**
059     * @see org.opencms.jsp.I_CmsJspTagParamParent#addParameter(java.lang.String, java.lang.String)
060     */
061    public void addParameter(String name, String value) {
062
063        m_content.put(name, value);
064    }
065
066    /**
067     * @return <code>EVAL_PAGE</code>
068     *
069     * @see javax.servlet.jsp.tagext.Tag#doEndTag()
070     *
071     * @throws JspException by interface default
072     */
073    @Override
074    public int doEndTag() throws JspException {
075
076        pageContext.setAttribute(m_var, m_content);
077        return EVAL_PAGE;
078    }
079
080    /**
081     * Returns <code>{@link #EVAL_BODY_BUFFERED}</code>.<p>
082     *
083     * @return <code>{@link #EVAL_BODY_BUFFERED}</code>
084     *
085     * @see javax.servlet.jsp.tagext.Tag#doStartTag()
086     */
087    @Override
088    public int doStartTag() {
089
090        m_content = new HashMap<String, String>();
091        return EVAL_BODY_BUFFERED;
092    }
093
094    /**
095     * Sets the variable name in which the map should be stored.<p>
096     *
097     * @param var the name of the variable
098     */
099    public void setVar(String var) {
100
101        m_var = var;
102    }
103
104}