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