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:
12: public static final Pattern pattern = Pattern.compile(
13: "\\$\\{(\\w+)\\}", Pattern.MULTILINE + Pattern.DOTALL);
14:
15: private String name = null;
16:
17: private String renderedText;
18:
19: private boolean rendered;
20:
21: public VariableWidget(ParentWidget parent, String text) {
22: super (parent);
23: Matcher match = pattern.matcher(text);
24: if (match.find()) {
25: name = match.group(1);
26: }
27: }
28:
29: public String render() throws Exception {
30: if (!rendered)
31: doRender();
32: return renderedText;
33: }
34:
35: private void doRender() throws Exception {
36: String value = parent.getVariable(name);
37: if (value != null) {
38: addChildWidgets(value);
39: renderedText = childHtml();
40: } else
41: renderedText = makeUndefinedVariableExpression(name);
42: rendered = true;
43: }
44:
45: private String makeUndefinedVariableExpression(String name)
46: throws Exception {
47: return HtmlUtil.metaText("undefined variable: " + name);
48: }
49:
50: public String asWikiText() throws Exception {
51: return "${" + name + "}";
52: }
53: }
|