    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 org.apache.wiki.api.core.Engine;
    022import org.apache.wiki.api.exceptions.NoSuchVariableException;
    023import org.apache.wiki.util.TextUtil;
    024import org.apache.wiki.variables.VariableManager;
    025
    026import javax.servlet.jsp.JspException;
    027import javax.servlet.jsp.JspWriter;
    028import java.io.IOException;
    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 extends WikiTagBase {
    047
    048    private static final long serialVersionUID = 0L;
    049
    050    private String m_var;
    051    private String m_default;
    052
    053    @Override public void initTag() {
    054        super.initTag();
    055        m_var = m_default = null;
    056    }
    057
    058    public String getVar()
    059    {
    060        return m_var;
    061    }
    062
    063    public void setVar( final String arg )
    064    {
    065        m_var = arg;
    066    }
    067
    068    public void setDefault( final String arg )
    069    {
    070        m_default = arg;
    071    }
    072
    073    @Override
    074    public final int doWikiStartTag() throws JspException, IOException {
    075        final Engine engine = m_wikiContext.getEngine();
    076        final JspWriter out = pageContext.getOut();
    077        String msg = null;
    078        String value = null;
    079
    080        try {
    081            value = engine.getManager( VariableManager.class ).getValue( m_wikiContext, getVar() );
    082        } catch( final NoSuchVariableException e ) {
    083            msg = "No such variable: " + e.getMessage();
    084        } catch( final IllegalArgumentException e ) {
    085            msg = "Incorrect variable name: " + e.getMessage();
    086        }
    087
    088        if( value == null ) {
    089            value = m_default;
    090        }
    091
    092        if( value == null ) {
    093            value = msg;
    094        }
    095        out.write( TextUtil.replaceEntities(value) );
    096        return SKIP_BODY;
    097    }
    098
    099}