01: /*
02:
03: This software is OSI Certified Open Source Software.
04: OSI Certified is a certification mark of the Open Source Initiative.
05:
06: The license (Mozilla version 1.0) can be read at the MMBase site.
07: See http://www.MMBase.org/license
08:
09: */
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<Entry<Pattern, String>> urlPatterns = new ArrayList<Entry<Pattern, String>>();
29:
30: static {
31: new LinkFinder().readPatterns(urlPatterns);
32: }
33:
34: public LinkFinder() {
35: super (XMLTEXT_WORDS);
36: }
37:
38: protected String getConfigFile() {
39: return "linkfinder.xml";
40: }
41:
42: protected Collection<Entry<Pattern, String>> getPatterns() {
43: return urlPatterns;
44: }
45:
46: protected void readDefaultPatterns(
47: Collection<Entry<Pattern, String>> patterns) {
48:
49: patterns.add(new Entry<Pattern, String>(Pattern
50: .compile(".+@.+"), "<a href=\"mailto:$0\">$0</a>"));
51: patterns.add(new Entry<Pattern, String>(Pattern
52: .compile("http://.+"), "<a href=\"$0\">$0</a>"));
53: patterns.add(new Entry<Pattern, String>(Pattern
54: .compile("https://.+"), "<a href=\"$0\">$0</a>"));
55: patterns.add(new Entry<Pattern, String>(Pattern
56: .compile("ftp://.+"), "<a href=\"$0\">$0</a>"));
57: patterns.add(new Entry<Pattern, String>(Pattern
58: .compile("www\\..+"), "<a href=\"http://$0\">$0</a>"));
59: return;
60: }
61:
62: public String toString() {
63: return "LINKFINDER";
64: }
65:
66: }
|