01: /*
02: JSPWiki - a JSP-based WikiWiki clone.
03:
04: Copyright (C) 2001-2006 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.parser;
21:
22: import org.apache.commons.lang.StringEscapeUtils;
23: import org.jdom.Text;
24:
25: import com.ecyrd.jspwiki.NoSuchVariableException;
26: import com.ecyrd.jspwiki.WikiContext;
27: import com.ecyrd.jspwiki.render.RenderingManager;
28:
29: /**
30: * Stores the contents of a WikiVariable in a WikiDocument DOM tree.
31: * @author Janne Jalkanen
32: * @since 2.4
33: */
34: public class VariableContent extends Text {
35: private static final long serialVersionUID = 1L;
36:
37: private String m_varName;
38:
39: public VariableContent(String varName) {
40: m_varName = varName;
41: }
42:
43: /**
44: * Evaluates the variable and returns the contents.
45: */
46: public String getValue() {
47: String result = "";
48: WikiDocument root = (WikiDocument) getDocument();
49:
50: if (root == null) {
51: // See similar note in PluginContent
52: return m_varName;
53: }
54:
55: WikiContext context = root.getContext();
56:
57: if (context == null)
58: return "No WikiContext available: INTERNAL ERROR";
59:
60: Boolean wysiwygEditorMode = (Boolean) context
61: .getVariable(RenderingManager.WYSIWYG_EDITOR_MODE);
62:
63: if (wysiwygEditorMode != null
64: && wysiwygEditorMode.booleanValue()) {
65: result = "[" + m_varName + "]";
66: } else {
67: try {
68: result = context.getEngine().getVariableManager()
69: .parseAndGetValue(context, m_varName);
70: } catch (NoSuchVariableException e) {
71: result = JSPWikiMarkupParser.makeError(
72: "No such variable: " + e.getMessage())
73: .getText();
74: }
75: }
76:
77: return StringEscapeUtils.escapeXml(result);
78: }
79:
80: public String getText() {
81: return getValue();
82: }
83:
84: public String toString() {
85: return "VariableElement[\"" + m_varName + "\"]";
86: }
87: }
|