001/* 
002    Licensed to the Apache Software Foundation (ASF) under one
003    or more contributor license agreements.  See the NOTICE file
004    distributed with this work for additional information
005    regarding copyright ownership.  The ASF licenses this file
006    to you under the Apache License, Version 2.0 (the
007    "License"); you may not use this file except in compliance
008    with the License.  You may obtain a copy of the License at
009
010       http://www.apache.org/licenses/LICENSE-2.0
011
012    Unless required by applicable law or agreed to in writing,
013    software distributed under the License is distributed on an
014    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015    KIND, either express or implied.  See the License for the
016    specific language governing permissions and limitations
017    under the License.  
018 */
019package org.apache.wiki.tags;
020
021import javax.servlet.jsp.tagext.BodyContent;
022import javax.servlet.jsp.tagext.BodyTagSupport;
023import javax.servlet.jsp.tagext.Tag;
024
025/**
026 * ParamTag submits name-value pairs to the first enclosing 
027 * ParamHandler instance. Name and value are strings, and can
028 * be given as tag attributes, or alternatively the value can be 
029 * given as the body contents of this tag. 
030 * <p>
031 * The name-value pair is passed to the closest containing 
032 * ancestor tag that implements ParamHandler. 
033 */
034public class ParamTag 
035    extends BodyTagSupport
036{
037
038    private static final long serialVersionUID = -4671059568218551633L;
039    private String m_name;
040    private String m_value;
041    
042    /**
043     *  {@inheritDoc}
044     */
045    @Override
046    public void release() 
047    {
048        m_name = m_value = null;
049    }
050    
051    /**
052     *  Set the name of the parameter to transfer.
053     *  
054     *  @param s The name.
055     */
056    public void setName( String s ) 
057    {
058        m_name = s;
059    }
060    
061    /**
062     *  Set the value of the parameter to transfer.
063     *  
064     *  @param s The value.
065     */
066    public void setValue( String s ) 
067    {
068        m_value = s;
069    }
070    
071    /**
072     *  {@inheritDoc}
073     */
074    @Override
075    public int doEndTag()
076    {
077        Tag t = null;
078        do
079        {
080            t = getParent();
081        } while (t != null && !(t instanceof ParamHandler));
082
083        if( t != null )
084        {
085            String val = m_value;
086            if( val == null )
087            {
088                BodyContent bc = getBodyContent();
089                if( bc != null ) 
090                {
091                    val = bc.getString();
092                }
093            }
094            if( val != null ) 
095            {
096                ((ParamHandler)t).setContainedParameter( m_name, val );
097            }
098        }
099        
100        
101        return EVAL_PAGE;
102    }
103}