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 fitnesse.wiki.*;
06: import fitnesse.wikitext.WidgetVisitor;
07:
08: import java.util.regex.Matcher;
09: import java.util.regex.Pattern;
10:
11: public class AliasLinkWidget extends ParentWidget {
12: public static final String REGEXP = "\\[\\[[^\n\r\\]]+\\]\\[[^\n\r\\]]+\\]\\]";
13:
14: public static final Pattern pattern = Pattern
15: .compile("\\[\\[([^\n\r\\]]+)\\]\\[([^\n\r\\]]+)\\]\\]");
16:
17: private String tag;
18:
19: private String href;
20:
21: WikiPage parentPage;
22:
23: public AliasLinkWidget(ParentWidget parent, String text)
24: throws Exception {
25: super (parent);
26: parentPage = getWikiPage().getParent();
27: Matcher match = pattern.matcher(text);
28: if (match.find()) {
29: tag = match.group(1);
30: href = match.group(2);
31: addChildWidgets(tag);
32: }
33: }
34:
35: public String render() throws Exception {
36: if (WikiWordWidget.isWikiWord(href)) {
37: WikiWordWidget www = new WikiWordWidget(
38: new BlankParentWidget(this , ""), href);
39: String theWord = www.getWikiWord();
40: WikiPagePath wikiWordPath = PathParser.parse(theWord);
41: WikiPagePath fullPathOfWikiWord = parentPage
42: .getPageCrawler().getFullPathOfChild(parentPage,
43: wikiWordPath);
44: String qualifiedName = PathParser
45: .render(fullPathOfWikiWord);
46: if (parentPage.getPageCrawler().pageExists(parentPage,
47: PathParser.parse(theWord)))
48: return ("<a href=\"" + qualifiedName + "\">"
49: + childHtml() + "</a>");
50: else if (getWikiPage() instanceof ProxyPage)
51: return makeAliasLinkToNonExistentRemotePage(theWord);
52: else
53: return (childHtml() + "<a href=\"" + qualifiedName
54: + "?edit\">"
55: + WikiWordWidget.NONEXISTENT_PAGE_TOKEN + "</a>");
56: } else
57: return ("<a href=\"" + href + "\">" + childHtml() + "</a>");
58: }
59:
60: private String makeAliasLinkToNonExistentRemotePage(String theWord)
61: throws Exception {
62: ProxyPage proxy = (ProxyPage) getWikiPage();
63: String remoteURLOfPage = proxy.getThisPageUrl();
64: String nameOfThisPage = proxy.getName();
65: int startOfThisPageName = remoteURLOfPage
66: .lastIndexOf(nameOfThisPage);
67: String remoteURLOfParent = remoteURLOfPage.substring(0,
68: startOfThisPageName);
69: return childHtml() + "<a href=\"" + remoteURLOfParent + theWord
70: + "?edit\"" + " target=\"" + theWord + "\"" + ">"
71: + WikiWordWidget.NONEXISTENT_PAGE_TOKEN + "</a>";
72: }
73:
74: public String asWikiText() throws Exception {
75: return "[[" + childWikiText() + "][" + href + "]]";
76: }
77:
78: public void acceptVisitor(WidgetVisitor visitor) throws Exception {
79: visitor.visit(this );
80: }
81:
82: public void renamePageIfReferenced(WikiPage pageToRename,
83: String newName) throws Exception {
84: if (WikiWordWidget.isWikiWord(href)) {
85: WikiWordWidget www = new WikiWordWidget(
86: new BlankParentWidget(this , ""), href);
87: www.renamePageIfReferenced(pageToRename, newName);
88: href = www.getText();
89: }
90: }
91: }
|