01: /*
02: JSPWiki - a JSP-based WikiWiki clone.
03:
04: Copyright (C) 2001-2002 Janne Jalkanen (Janne.Jalkanen@iki.fi)
05:
06: This program is free software; you can redistribute it and/or modify
07: it under the terms of the GNU Lesser General Public License as published by
08: the Free Software Foundation; either version 2.1 of the License, or
09: (at your option) any later version.
10:
11: This program is distributed in the hope that it will be useful,
12: but WITHOUT ANY WARRANTY; without even the implied warranty of
13: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14: GNU Lesser General Public License for more details.
15:
16: You should have received a copy of the GNU Lesser General Public License
17: along with this program; if not, write to the Free Software
18: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19: */
20: package com.ecyrd.jspwiki.tags;
21:
22: import java.io.IOException;
23: import javax.servlet.jsp.JspWriter;
24: import javax.servlet.jsp.JspException;
25:
26: import com.ecyrd.jspwiki.WikiEngine;
27: import com.ecyrd.jspwiki.NoSuchVariableException;
28:
29: /**
30: * Returns the value of an Wiki variable.
31: *
32: * <P><B>Attributes</B></P>
33: * <UL>
34: * <LI>var - Name of the variable. Required.
35: * <LI>default - Revert to this value, if the value of "var" is null.
36: * If left out, this tag will produce a concise error message
37: * if the named variable is not found. Set to empty (default="")
38: * to hide the message.
39: * </UL>
40: *
41: * <P>A default value implies <I>failmode='quiet'</I>.
42: *
43: * @author Janne Jalkanen
44: * @since 2.0
45: */
46: public class VariableTag extends WikiTagBase {
47: private String m_var = null;
48: private String m_default = null;
49:
50: public String getVar() {
51: return m_var;
52: }
53:
54: public void setVar(String arg) {
55: m_var = arg;
56: }
57:
58: public void setDefault(String arg) {
59: m_default = arg;
60: }
61:
62: public final int doWikiStartTag() throws JspException, IOException {
63: WikiEngine engine = m_wikiContext.getEngine();
64: JspWriter out = pageContext.getOut();
65: String msg = null;
66: String value = null;
67:
68: try {
69: value = engine.getVariableManager().getValue(m_wikiContext,
70: getVar());
71: } catch (NoSuchVariableException e) {
72: msg = m_wikiContext.translate("msg.no_such_variable") + " "
73: + e.getMessage();
74: } catch (IllegalArgumentException e) {
75: msg = m_wikiContext
76: .translate("msg.incorrect_variable_name")
77: + " " + e.getMessage();
78: }
79:
80: if (value == null) {
81: value = m_default;
82: }
83:
84: if (value == null) {
85: value = msg;
86: }
87: out.write(value);
88: return (SKIP_BODY);
89: }
90: }
|