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:
08: public class TagWidget extends ParentWidget {
09:
10: public static final String WIDGET_NAME = "Tag";
11:
12: public static final String WIDGET_TITLE = "Tag:";
13:
14: public static final String WIDGET_ERROR_TITLE = "Tag Error:";
15:
16: public static final String ERROR_INVALID_TAG = "invalid tag characters.";
17:
18: public static final String REGEXP = "(?:^!" + WIDGET_NAME
19: + "[ \\t]+[\\d\\w \\t]+" + LineBreakWidget.REGEXP + "?)|"
20: + "(?:^!" + WIDGET_NAME.toLowerCase()
21: + "[ \\t]+[\\d\\w \\t]+" + LineBreakWidget.REGEXP + "?)";
22:
23: private static final Pattern pattern = Pattern.compile("^!("
24: + WIDGET_NAME + "|" + WIDGET_NAME.toLowerCase() + ")"
25: + "[ \\t]+([\\d\\w \\t]+)$");
26:
27: private String[] tags = null;
28:
29: public TagWidget(ParentWidget parent, String text) throws Exception {
30: super (parent);
31: Matcher match = pattern.matcher(text);
32: if (match.find()) {
33: String rawTags = match.group(2).trim();
34: setTags(rawTags);
35: }
36: }
37:
38: public String render() throws Exception {
39: return "<div class=\"tagWidgetDiv\">" + getWidgetTitleHtml()
40: + getTagsHtml() + "</div>";
41: }
42:
43: private String getWidgetTitleHtml() {
44: return "<span class=\"tagWidgetTitle\">" + WIDGET_TITLE
45: + "</span>";
46: }
47:
48: private String getTagsHtml() {
49: String tagsString = "";
50: if ((tags != null) && (tags.length > 0)) {
51: for (int i = 0; i < tags.length; i++) {
52: tagsString += "<span class=\"tagWidgetTag\">" + tags[i]
53: + "</span>";
54: }
55: } else {
56: tagsString = ERROR_INVALID_TAG;
57: }
58: return tagsString.trim();
59: }
60:
61: private void setTags(String rawTags) {
62: String squashed = rawTags.replaceAll("\\t", " ");
63: squashed = squashed.replaceAll("\\s{2,}", " ");
64: this .tags = squashed.split("\\s+");
65: }
66:
67: public String[] getTags() {
68: return tags;
69: }
70:
71: public static boolean isTagLineAndNotError(String contentLine) {
72: return isTagLine(contentLine)
73: && !(contentLine.indexOf((">"
74: + TagWidget.WIDGET_ERROR_TITLE + "</span>")) != -1);
75: }
76:
77: public static boolean isTagLine(String contentLine) {
78: return contentLine.indexOf(">" + TagWidget.WIDGET_TITLE
79: + "</span>") != -1;
80: }
81:
82: public static boolean hasAllTags(String contentLine, String[] tags) {
83: for (int j = 0; j < tags.length; j++) {
84: if (contentLine.indexOf(">" + tags[j] + "<") == -1) {
85: return false;
86: }
87: }
88: return true;
89: }
90: }
|