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.Matcher;
06: import java.util.regex.Pattern;
07: import fitnesse.html.HtmlUtil;
08:
09: public class VariableDefinitionWidget extends ParentWidget {
10: public static final String REGEXP = "^!define \\w+ (?:(?:\\{[^}]*\\})|(?:\\([^)]*\\)))";
11: private static final Pattern pattern = Pattern.compile(
12: "^!define (\\w+) ([\\{\\(])(.*)[\\}\\)]", Pattern.DOTALL
13: + Pattern.MULTILINE);
14: public String name;
15: public String value;
16:
17: public VariableDefinitionWidget(ParentWidget parent, String text)
18: throws Exception {
19: super (parent);
20: Matcher match = pattern.matcher(text);
21: if (match.find()) {
22: name = match.group(1);
23: value = match.group(3);
24: }
25: }
26:
27: public String render() throws Exception {
28: this .parent.addVariable(name, value);
29: return HtmlUtil.metaText("variable defined: " + name + "="
30: + value);
31: }
32:
33: public String asWikiText() throws Exception {
34: String text = "!define " + name + " ";
35: if (value.indexOf("{") == -1)
36: text += "{" + value + "}";
37: else
38: text += "(" + value + ")";
39: return text;
40: }
41: }
|