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.wikitext.WidgetBuilder;
08:
09: public class LinkWidget extends ParentWidget {
10: public static final String REGEXP = "https?://[^\\s]+[^\\s.)]+";
11: private static final Pattern pattern = Pattern
12: .compile("https?://([^/\\s]*)(\\S*)?");
13:
14: public LinkWidget(ParentWidget parent, String text)
15: throws Exception {
16: super (parent);
17: addChildWidgets(text);
18: }
19:
20: public String render() throws Exception {
21: String linkText = childHtml();
22: String usableURL = makeUrlUsable(linkText);
23: StringBuffer html = new StringBuffer("<a href=\"");
24: html.append(usableURL);
25: html.append("\">");
26: html.append(linkText);
27: html.append("</a>");
28:
29: return html.toString();
30: }
31:
32: public static String makeUrlUsable(String url) {
33: String usableUrl = url;
34: Matcher match = pattern.matcher(url);
35: if (match.find()) {
36: String host = match.group(1);
37: String resource = match.group(2);
38: if ("files".equals(host))
39: usableUrl = "/files" + resource;
40: }
41:
42: return usableUrl;
43: }
44:
45: public WidgetBuilder getBuilder() {
46: return WidgetBuilder.variableWidgetBuilder;
47: }
48:
49: public String asWikiText() throws Exception {
50: return childWikiText();
51: }
52: }
|