01: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
02: // Released under the terms of the GNU General Public License version 2 or later.
03: package fitnesse.wikitext.widgets;
04:
05: import java.util.regex.Pattern;
06: import java.util.regex.Matcher;
07: import fitnesse.html.HtmlUtil;
08:
09: public class VariableWidget extends ParentWidget {
10: public static final String REGEXP = "\\$\\{\\w+\\}";
11: public static final Pattern pattern = Pattern.compile(
12: "\\$\\{(\\w+)\\}", Pattern.MULTILINE + Pattern.DOTALL);
13: private String name = null;
14: private String renderedText;
15: private boolean rendered;
16:
17: public VariableWidget(ParentWidget parent, String text) {
18: super (parent);
19: Matcher match = pattern.matcher(text);
20: if (match.find()) {
21: name = match.group(1);
22: }
23: }
24:
25: public String render() throws Exception {
26: if (!rendered)
27: doRender();
28: return renderedText;
29: }
30:
31: private void doRender() throws Exception {
32: String value = parent.getVariable(name);
33: if (value != null) {
34: addChildWidgets(value);
35: renderedText = childHtml();
36: } else
37: renderedText = makeUndefinedVariableExpression(name);
38: rendered = true;
39: }
40:
41: private String makeUndefinedVariableExpression(String name)
42: throws Exception {
43: return HtmlUtil.metaText("undefined variable: " + name);
44: }
45:
46: public String asWikiText() throws Exception {
47: return "${" + name + "}";
48: }
49: }
|