001: // Copyright (C) 2003,2004,2005 by Object Mentor, Inc. All rights reserved.
002: // Released under the terms of the GNU General Public License version 2 or later.
003: package fitnesse.wikitext;
004:
005: import fitnesse.wikitext.widgets.*;
006: import java.lang.reflect.*;
007: import java.util.regex.*;
008:
009: public class WidgetBuilder {
010: public static final Class[] htmlWidgetClasses = new Class[] { //
011: CommentWidget.class, LiteralWidget.class, WikiWordWidget.class,
012: BoldWidget.class, ItalicWidget.class,
013: PreformattedWidget.class, HruleWidget.class,
014: HeaderWidget.class, CenterWidget.class, NoteWidget.class,
015: TableWidget.class, ListWidget.class, ClasspathWidget.class,
016: LineBreakWidget.class, ImageWidget.class, LinkWidget.class,
017: ContentsWidget.class, AliasLinkWidget.class,
018: VirtualWikiWidget.class, StrikeWidget.class,
019: FixtureWidget.class, XRefWidget.class, MetaWidget.class,
020: EmailWidget.class, AnchorDeclarationWidget.class,
021: AnchorMarkerWidget.class, CollapsableWidget.class,
022: IncludeWidget.class, VariableDefinitionWidget.class,
023: VariableWidget.class, SuiteWidget.class,
024: CommentAggregatorWidget.class, TagSuiteWidget.class,
025: TagWidget.class, ContentsTreeWidget.class,
026: RandomVariableWidget.class, ComponentsWidget.class,
027: EmbedWidget.class
028: // LastModifiedWidget.class,
029: };
030:
031: public static WidgetBuilder htmlWidgetBuilder = new WidgetBuilder(
032: htmlWidgetClasses);
033:
034: public static WidgetBuilder literalAndVariableWidgetBuilder = new WidgetBuilder(
035: new Class[] { LiteralWidget.class, VariableWidget.class });
036:
037: public static WidgetBuilder variableWidgetBuilder = new WidgetBuilder(
038: new Class[] { VariableWidget.class });
039:
040: private Class[] widgetClasses;
041:
042: private Pattern widgetPattern;
043:
044: public WidgetBuilder(Class[] widgetClasses) {
045: this .widgetClasses = widgetClasses;
046: widgetPattern = buildCompositeWidgetPattern();
047: }
048:
049: private Pattern buildCompositeWidgetPattern() {
050: StringBuffer pattern = new StringBuffer();
051: for (int i = 0; i < widgetClasses.length; i++) {
052: Class widgetClass = widgetClasses[i];
053: String regexp = getRegexpFromWidgetClass(widgetClass);
054: pattern.append("(").append(regexp).append(")");
055: if (i != (widgetClasses.length - 1))
056: pattern.append("|");
057: }
058: return Pattern.compile(pattern.toString(), Pattern.DOTALL
059: | Pattern.MULTILINE);
060: }
061:
062: private static String getRegexpFromWidgetClass(Class widgetClass) {
063: String regexp = null;
064: try {
065: Field f = widgetClass.getField("REGEXP");
066: regexp = (String) f.get(widgetClass);
067: } catch (Exception e) {
068: e.printStackTrace();
069: }
070: return regexp;
071: }
072:
073: public WikiWidget makeWidget(ParentWidget parent, Matcher matcher)
074: throws Exception {
075: int group = getGroupMatched(matcher);
076: Class widgetClass = widgetClasses[group - 1];
077: return constructWidget(widgetClass, parent, matcher.group());
078: }
079:
080: private WikiWidget constructWidget(Class widgetClass,
081: ParentWidget parent, String text) throws Exception {
082: try {
083: Constructor widgetConstructor = widgetClass
084: .getConstructor(new Class[] { ParentWidget.class,
085: String.class });
086: WikiWidget widget = (WikiWidget) widgetConstructor
087: .newInstance(new Object[] { parent, text });
088: return widget;
089: } catch (Exception e) {
090: e.printStackTrace();
091: Exception exception = new Exception(
092: "Widget Construction failed for "
093: + widgetClass.getName() + "\n"
094: + e.getMessage());
095: exception.setStackTrace(e.getStackTrace());
096: throw exception;
097: }
098: }
099:
100: public int getGroupMatched(Matcher matcher) {
101: for (int i = 1; i <= matcher.groupCount(); i++) {
102: if (matcher.group(i) != null)
103: return i;
104: }
105: return -1;
106: }
107:
108: public Pattern getWidgetPattern() {
109: return widgetPattern;
110: }
111: }
|