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 * This file is based on:
028 * org.apache.taglibs.standard.tag.common.core.ParamSupport
029 * from the Apache JSTL 1.0 implmentation.
030 *
031 * The Apache Software License, Version 1.1
032 *
033 * Copyright (c) 1999 The Apache Software Foundation.  All rights
034 * reserved.
035 *
036 * Redistribution and use in source and binary forms, with or without
037 * modification, are permitted provided that the following conditions
038 * are met:
039 *
040 * 1. Redistributions of source code must retain the above copyright
041 *    notice, this list of conditions and the following disclaimer.
042 *
043 * 2. Redistributions in binary form must reproduce the above copyright
044 *    notice, this list of conditions and the following disclaimer in
045 *    the documentation and/or other materials provided with the
046 *    distribution.
047 *
048 * 3. The end-user documentation included with the redistribution, if
049 *    any, must include the following acknowlegement:
050 *       "This product includes software developed by the
051 *        Apache Software Foundation (http://www.apache.org/)."
052 *    Alternately, this acknowlegement may appear in the software itself,
053 *    if and wherever such third-party acknowlegements normally appear.
054 *
055 * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
056 *    Foundation" must not be used to endorse or promote products derived
057 *    from this software without prior written permission. For written
058 *    permission, please contact apache@apache.org.
059 *
060 * 5. Products derived from this software may not be called "Apache"
061 *    nor may "Apache" appear in their names without prior written
062 *    permission of the Apache Group.
063 *
064 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
065 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
066 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
067 * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
068 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
069 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
070 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
071 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
072 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
073 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
074 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
075 * SUCH DAMAGE.
076 * ====================================================================
077 *
078 * This software consists of voluntary contributions made by many
079 * individuals on behalf of the Apache Software Foundation.  For more
080 * information on the Apache Software Foundation, please see
081 * <http://www.apache.org/>.
082 *
083 */
084
085package org.opencms.jsp;
086
087import org.opencms.i18n.CmsEncoder;
088import org.opencms.main.OpenCms;
089import org.opencms.util.CmsStringUtil;
090
091import javax.servlet.jsp.JspException;
092import javax.servlet.jsp.JspTagException;
093import javax.servlet.jsp.tagext.BodyTagSupport;
094import javax.servlet.jsp.tagext.Tag;
095
096/**
097 * A handler for &lt;param&gt; that accepts attributes as Strings
098 * and evaluates them as expressions at runtime.<p>
099 *
100 * @since 6.0.0
101 */
102public class CmsJspTagParam extends BodyTagSupport {
103
104    /** Serial version UID required for safe serialization. */
105    private static final long serialVersionUID = -1057768160264355211L;
106
107    /**
108     * There used to be an 'encode' attribute; I've left this as a
109     * vestige in case custom subclasses want to use our functionality
110     * but NOT encode parameters.
111     */
112    protected boolean m_encode;
113
114    /** The name of the parameter. */
115    protected String m_name;
116
117    /** The value of the parameter. */
118    protected String m_value;
119
120    /**
121     * Public constructor.<p>
122     */
123    public CmsJspTagParam() {
124
125        super();
126        init();
127    }
128
129    /**
130     * Simply send our name and value to our appropriate ancestor.<p>
131     *
132     * @throws JspException (never thrown, required by interface)
133     * @return EVAL_PAGE
134     */
135    @Override
136    public int doEndTag() throws JspException {
137
138        Tag t = findAncestorWithClass(this, I_CmsJspTagParamParent.class);
139        if (t == null) {
140            throw new JspTagException(Messages.get().getBundle(pageContext.getRequest().getLocale()).key(
141                Messages.ERR_PARENTLESS_TAG_1,
142                new Object[] {"param"}));
143        }
144        // take no action for null or empty names
145        if (CmsStringUtil.isEmpty(m_name)) {
146            return EVAL_PAGE;
147        }
148
149        // send the parameter to the appropriate ancestor
150        I_CmsJspTagParamParent parent = (I_CmsJspTagParamParent)t;
151        String value = m_value;
152        if (value == null) {
153            if ((bodyContent == null) || (bodyContent.getString() == null)) {
154                value = "";
155            } else {
156                value = bodyContent.getString().trim();
157            }
158        }
159        if (m_encode) {
160            parent.addParameter(
161                CmsEncoder.encode(m_name, OpenCms.getSystemInfo().getDefaultEncoding()),
162                CmsEncoder.encode(value, OpenCms.getSystemInfo().getDefaultEncoding()));
163        } else {
164            parent.addParameter(m_name, value);
165        }
166
167        return EVAL_PAGE;
168    }
169
170    /**
171     * Releases any resources we may have (or inherit).<p>
172     */
173    @Override
174    public void release() {
175
176        init();
177    }
178
179    /**
180     * Sets the attribute name.<p>
181     *
182     * @param name the name to set
183     */
184    public void setName(String name) {
185
186        m_name = name;
187    }
188
189    /**
190     * Sets the attribute value.<p>
191     *
192     * @param value the name to set
193     */
194    public void setValue(String value) {
195
196        m_value = value;
197    }
198
199    /**
200     * Initializes the internal values.<p>
201     */
202    private void init() {
203
204        m_name = null;
205        m_value = null;
206    }
207}