KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jivesoftware > messenger > plugin > ContentFilter


1 /**
2  * $RCSfile: ContentFilter.java,v $
3  * $Revision: 1.1 $
4  * $Date: 2005/07/04 17:08:41 $
5  *
6  * Copyright (C) 2004 Jive Software. All rights reserved.
7  *
8  * This software is published under the terms of the GNU Public License (GPL),
9  * a copy of which is included in this distribution.
10  */

11
12 package org.jivesoftware.messenger.plugin;
13
14 import org.xmpp.packet.Message;
15
16 import java.util.ArrayList JavaDoc;
17 import java.util.Collection JavaDoc;
18 import java.util.regex.Matcher JavaDoc;
19 import java.util.regex.Pattern JavaDoc;
20
21 /**
22  * Filters message content using regular expressions. If a content mask is
23  * provided message content will be altered.
24  *
25  * @author Conor Hayes
26  */

27 public class ContentFilter {
28
29     private String JavaDoc patterns;
30
31     private Collection JavaDoc<Pattern JavaDoc> compiledPatterns = new ArrayList JavaDoc<Pattern JavaDoc>();
32
33     private String JavaDoc mask;
34
35
36     /**
37      * A default instance will allow all message content.
38      *
39      * @see #setPatterns(String)
40      * @see #setMask(String)
41      */

42     public ContentFilter() {
43     }
44
45     /**
46      * Set the patterns to use for searching content.
47      *
48      * @param regExps a comma separated String of regular expressions
49      */

50     public void setPatterns(String JavaDoc patterns) {
51         if (patterns != null) {
52             this.patterns = patterns;
53             String JavaDoc[] data = patterns.split(",");
54
55             compiledPatterns.clear();
56
57             for (int i = 0; i < data.length; i++) {
58                 compiledPatterns.add(Pattern.compile(data[i]));
59             }
60         }
61         else {
62             clearPatterns();
63         }
64
65     }
66
67     public String JavaDoc getPatterns() {
68         return this.patterns;
69     }
70
71     /**
72      * Clears all patterns. Calling this method means that all message content
73      * will be allowed.
74      */

75     public void clearPatterns() {
76         patterns = null;
77         compiledPatterns.clear();
78     }
79
80     /**
81      * Set the content replacement mask.
82      *
83      * @param mask the mask to use when replacing content
84      */

85     public void setMask(String JavaDoc mask) {
86         this.mask = mask;
87     }
88
89     /**
90      * @return the current mask or null if none has been set
91      */

92     public String JavaDoc getMask() {
93         return mask;
94     }
95
96     /**
97      * Clears the content mask.
98      *
99      * @see #filter(Message)
100      */

101     public void clearMask() {
102         mask = null;
103     }
104
105
106     /**
107      * @return true if the filter is currently masking content, false otherwise
108      */

109     public boolean isMaskingContent() {
110         return mask != null;
111     }
112
113     /**
114      * Filters message content.
115      *
116      * @param msg the message to filter, its subject/body may be altered if there
117      * are content matches and a content mask is set
118      * @return true if the msg content matched up, false otherwise
119      */

120     public boolean filter(Message msg) {
121         boolean hasMatch = false;
122
123         if (msg.getSubject() != null) {
124             if (hasMatch(msg.getSubject())) {
125                 hasMatch = true;
126                 if (isMaskingContent()) {
127                     String JavaDoc newSubject = replaceContent(msg.getSubject());
128                     msg.setSubject(newSubject);
129                 }
130             }
131         }
132
133         if (msg.getBody() != null) {
134             if (hasMatch(msg.getBody())) {
135                 hasMatch = true;
136                 if (isMaskingContent()) {
137                     String JavaDoc newBody = replaceContent(msg.getBody());
138                     msg.setBody(newBody);
139                 }
140             }
141         }
142
143         return hasMatch;
144     }
145
146     private String JavaDoc replaceContent(String JavaDoc content) {
147         for (Pattern JavaDoc pattern : compiledPatterns) {
148             Matcher JavaDoc m = pattern.matcher(content);
149             content = m.replaceAll(mask);
150         }
151
152         return content;
153     }
154
155     /**
156      * Performs sequential search for any pattern match.
157      *
158      * @param content the content to search against
159      * @return true if a match is found, false otherwise
160      */

161     private boolean hasMatch(String JavaDoc content) {
162         boolean hasMatch = false;
163
164         for (Pattern JavaDoc pattern : compiledPatterns) {
165             Matcher JavaDoc m = pattern.matcher(content);
166
167             if (m.find()) {
168                 hasMatch = true;
169                 break;
170             }
171         }
172
173         return hasMatch;
174     }
175 }
Popular Tags