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 java.io.IOException;
022
023import javax.servlet.jsp.JspWriter;
024import javax.servlet.jsp.JspException;
025
026import org.apache.wiki.WikiEngine;
027import org.apache.wiki.api.exceptions.NoSuchVariableException;
028import org.apache.wiki.util.TextUtil;
029
030/**
031 *  Returns the value of an Wiki variable.
032 *
033 *  <P><B>Attributes</B></P>
034 *  <UL>
035 *    <LI>var - Name of the variable.  Required.
036 *    <LI>default - Revert to this value, if the value of "var" is null.
037 *                  If left out, this tag will produce a concise error message
038 *                  if the named variable is not found. Set to empty (default="")
039 *                  to hide the message.
040 *  </UL>
041 *
042 *  <P>A default value implies <I>failmode='quiet'</I>.
043 *
044 *  @since 2.0
045 */
046public class VariableTag
047    extends WikiTagBase
048{
049    private static final long serialVersionUID = 0L;
050
051    private String m_var      = null;
052    private String m_default  = null;
053
054    public void initTag()
055    {
056        super.initTag();
057        m_var = m_default = null;
058    }
059
060    public String getVar()
061    {
062        return m_var;
063    }
064
065    public void setVar( String arg )
066    {
067        m_var = arg;
068    }
069
070    public void setDefault( String arg )
071    {
072        m_default = arg;
073    }
074
075    public final int doWikiStartTag()
076        throws JspException,
077               IOException
078    {
079        WikiEngine engine   = m_wikiContext.getEngine();
080        JspWriter out = pageContext.getOut();
081        String msg = null;
082        String value = null;
083
084        try
085        {
086            value = engine.getVariableManager().getValue( m_wikiContext,
087                                                          getVar() );
088        }
089        catch( NoSuchVariableException e )
090        {
091            msg = "No such variable: "+e.getMessage();
092        }
093        catch( IllegalArgumentException e )
094        {
095            msg = "Incorrect variable name: "+e.getMessage();
096        }
097
098        if( value == null )
099        {
100            value = m_default;
101        }
102
103        if( value == null )
104        {
105            value = msg;
106        }
107        out.write( TextUtil.replaceEntities(value) );
108        return SKIP_BODY;
109    }
110}