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.widgets;
004:
005: import fitnesse.components.PageReferencer;
006: import fitnesse.wiki.*;
007: import java.util.regex.*;
008:
009: public class IncludeWidget extends ParentWidget implements
010: PageReferencer {
011: public static final String REGEXP = "^!include(?: +-setup| +-teardown| +-seamless)? "
012: + WikiWordWidget.REGEXP + LineBreakWidget.REGEXP + "?";
013:
014: static final Pattern pattern = Pattern
015: .compile("^!include *(-setup|-teardown|-seamless)? (.*)");
016:
017: protected String pageName;
018:
019: protected WikiPage includingPage;
020:
021: protected WikiPage parentPage;
022:
023: public IncludeWidget(ParentWidget parent, String text)
024: throws Exception {
025: super (parent);
026: Matcher matcher = pattern.matcher(text);
027: if (matcher.find()) {
028: pageName = parsePageName(matcher);
029: includingPage = parent.getWikiPage();
030: parentPage = includingPage.getParent();
031: handleDisplayForOption(parseOption(matcher));
032: }
033: }
034:
035: private String parseOption(Matcher match) {
036: return match.group(1);
037: }
038:
039: private String parsePageName(Matcher match) {
040: return match.group(2);
041: }
042:
043: // TODO MDM I know this is bad... But it seems better then creatting two new
044: // widgets.
045: private void handleDisplayForOption(String option) throws Exception {
046: String widgetText = processLiterals(getIncludedPageContent());
047: if ("-seamless".equals(option)) {
048: addChildWidgets(widgetText + "\n");
049: } else if ("-setup".equals(option)) {
050: new CollapsableWidget(this , "Set Up: " + pageName,
051: widgetText, "setup");
052: } else if ("-teardown".equals(option)) {
053: new CollapsableWidget(this , "Tear Down: " + pageName,
054: widgetText, "teardown");
055: } else {
056: new CollapsableWidget(this , pageName, widgetText,
057: "included");
058: }
059: }
060:
061: public String render() throws Exception {
062: return childHtml();
063: }
064:
065: public WikiPage getReferencedPage() throws Exception {
066: return getParentPage().getPageCrawler().getPage(
067: getParentPage(), PathParser.parse(pageName));
068: }
069:
070: protected String getIncludedPageContent() throws Exception {
071: PageCrawler crawler = parentPage.getPageCrawler();
072: crawler.setDeadEndStrategy(new VirtualEnabledPageCrawler());
073: WikiPagePath pagePath = PathParser.parse(pageName);
074: if (crawler.pageExists(parentPage, pagePath)) {
075: WikiPage page = getIncludedPage();
076: return page.getData().getContent();
077: } else if (includingPage instanceof ProxyPage) {
078: ProxyPage proxy = (ProxyPage) includingPage;
079: String host = proxy.getHost();
080: int port = proxy.getHostPort();
081: try {
082: ProxyPage remoteIncludedPage = new ProxyPage(
083: "RemoteIncludedPage", null, host, port,
084: pagePath);
085: return remoteIncludedPage.getData().getContent();
086: } catch (Exception e) {
087: return "!meta '''Remote page " + host + ":" + port
088: + "/" + pageName + " does not exist.'''";
089: }
090: } else {
091: return "!meta '''Page include failed because the page "
092: + pageName + " does not exist.'''";
093: }
094: }
095:
096: protected WikiPage getIncludedPage() throws Exception {
097: PageCrawler crawler = parentPage.getPageCrawler();
098: crawler.setDeadEndStrategy(new VirtualEnabledPageCrawler());
099: return crawler.getPage(parentPage, PathParser.parse(pageName));
100: }
101:
102: protected WikiPage getParentPage() throws Exception {
103: return parent.getWikiPage().getParent();
104: }
105: }
|