KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > mmbase > util > transformers > LinkFinder


1 /*
2
3 This software is OSI Certified Open Source Software.
4 OSI Certified is a certification mark of the Open Source Initiative.
5
6 The license (Mozilla version 1.0) can be read at the MMBase site.
7 See http://www.MMBase.org/license
8
9 */

10 package org.mmbase.util.transformers;
11
12 import java.util.*;
13 import java.util.regex.*;
14 import org.mmbase.util.Entry;
15
16 /**
17  * Finds links in the Character String, and makes them 'clickable' for HTML (using a-tags). This
18  * implementation is very simple and straightforward. It contains a list of regular expression which
19  * are matched on all 'words'. It ignores existing XML markup, and also avoids trailing dots and
20  * comments and surrounding quotes and parentheses.
21  *
22  * @author Michiel Meeuwissen
23  * @since MMBase-1.7
24  */

25
26 public class LinkFinder extends RegexpReplacer {
27
28     protected static Collection urlPatterns = new ArrayList();
29
30     static {
31         new LinkFinder().readPatterns(urlPatterns);
32     }
33
34     public LinkFinder() {
35         super(XMLTEXT_WORDS);
36     }
37
38
39     protected String JavaDoc getConfigFile() {
40         return "linkfinder.xml";
41     }
42
43     protected Collection getPatterns() {
44         return urlPatterns;
45     }
46
47
48     protected void readDefaultPatterns(Collection patterns) {
49
50         patterns.add(new Entry(Pattern.compile(".+@.+"), "<a HREF=\"mailto:$0\">$0</a>"));
51         patterns.add(new Entry(Pattern.compile("http://.+"), "<a HREF=\"$0\">$0</a>"));
52         patterns.add(new Entry(Pattern.compile("https://.+"), "<a HREF=\"$0\">$0</a>"));
53         patterns.add(new Entry(Pattern.compile("ftp://.+"), "<a HREF=\"$0\">$0</a>"));
54         patterns.add(new Entry(Pattern.compile("www\\..+"), "<a HREF=\"http://$0\">$0</a>"));
55         return;
56     }
57
58
59     public String JavaDoc toString() {
60         return "LINKFINDER";
61     }
62
63 }
64
Popular Tags