KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > roller > util > RegexUtil


1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. The ASF licenses this file to You
4 * under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License. For additional information regarding
15 * copyright in this work, please see the NOTICE file in the top level
16 * directory of this distribution.
17 */

18 /*
19  * Created on Nov 8, 2003
20  *
21  */

22 package org.apache.roller.util;
23 import org.apache.commons.codec.binary.Hex;
24
25 import java.io.UnsupportedEncodingException JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.regex.Matcher JavaDoc;
28 import java.util.regex.Pattern JavaDoc;
29
30
31 /**
32  * @author lance
33  */

34 public class RegexUtil
35 {
36     public static final Pattern JavaDoc mailtoPattern = Pattern.compile("mailto:([a-zA-Z0-9\\.]+@[a-zA-Z0-9\\.]+\\.[a-zA-Z0-9]+)");
37     public static final Pattern JavaDoc emailPattern = Pattern.compile("\\b[a-zA-Z0-9\\.]+(@)([a-zA-Z0-9\\.]+)(\\.)([a-zA-Z0-9]+)\\b");
38     
39     public static String JavaDoc encodeEmail(String JavaDoc str)
40     {
41         // obfuscate mailto's: turns them into hex encoded,
42
// so that browsers can still understand the mailto link
43
Matcher JavaDoc mailtoMatch = mailtoPattern.matcher(str);
44         while (mailtoMatch.find())
45         {
46             String JavaDoc email = mailtoMatch.group(1);
47             //System.out.println("email=" + email);
48
String JavaDoc hexed = encode(email);
49             str = str.replaceFirst("mailto:"+email, "mailto:"+hexed);
50         }
51         
52         return obfuscateEmail(str);
53     }
54
55     /**
56      * obfuscate plaintext emails: makes them
57      * "human-readable" - still too easy for
58      * machines to parse however.
59      *
60      * @param str
61      * @return
62      */

63     public static String JavaDoc obfuscateEmail(String JavaDoc str)
64     {
65         Matcher JavaDoc emailMatch = emailPattern.matcher(str);
66         while (emailMatch.find())
67         {
68             String JavaDoc at = emailMatch.group(1);
69             //System.out.println("at=" + at);
70
str = str.replaceFirst(at, "-AT-");
71             
72             String JavaDoc dot = emailMatch.group(2) + emailMatch.group(3) + emailMatch.group(4);
73             String JavaDoc newDot = emailMatch.group(2) + "-DOT-" + emailMatch.group(4);
74             //System.out.println("dot=" + dot);
75
str = str.replaceFirst(dot, newDot);
76         }
77         return str;
78     }
79     
80     /**
81      * Return the specified match "groups" from the pattern.
82      * For each group matched a String will be entered in the ArrayList.
83      *
84      * @param pattern The Pattern to use.
85      * @param match The String to match against.
86      * @param group The group number to return in case of a match.
87      * @return
88      */

89     public static ArrayList JavaDoc getMatches(Pattern JavaDoc pattern, String JavaDoc match, int group)
90     {
91         ArrayList JavaDoc matches = new ArrayList JavaDoc();
92         Matcher JavaDoc matcher = pattern.matcher(match);
93         while (matcher.find())
94         {
95             matches.add( matcher.group(group) );
96         }
97         return matches;
98     }
99
100     /**
101      * Thanks to the folks at Blojsom (http://sf.net/projects/blojsom)
102      * for showing me what I was doing wrong with the Hex class.
103      *
104      * @param email
105      * @return
106      */

107     public static String JavaDoc encode(String JavaDoc email)
108     {
109         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
110         try {
111             char[] hexString = Hex.encodeHex(email.getBytes("UTF-8"));
112             for (int i = 0; i < hexString.length; i++) {
113                 if (i % 2 == 0) {
114                     result.append("%");
115                 }
116                 result.append(hexString[i]);
117             }
118         } catch (UnsupportedEncodingException JavaDoc e) {
119             return email;
120         }
121
122         return result.toString();
123     }
124 }
125
Popular Tags