KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > javabb > transaction > BadWordTransaction


1 package org.javabb.transaction;
2
3 import java.util.Iterator JavaDoc;
4 import java.util.List JavaDoc;
5 import java.util.regex.Pattern JavaDoc;
6
7 import org.javabb.dao.entity.IBadWordDAO;
8 import org.javabb.vo.BadWord;
9
10 /*
11  * Copyright 2004 JavaFree.org
12  *
13  * Licensed under the Apache License, Version 2.0 (the "License");
14  * you may not use this file except in compliance with the License.
15  * You may obtain a copy of the License at
16  *
17  * http://www.apache.org/licenses/LICENSE-2.0
18  *
19  * Unless required by applicable law or agreed to in writing, software
20  * distributed under the License is distributed on an "AS IS" BASIS,
21  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
22  * See the License for the specific language governing permissions and
23  * limitations under the License.
24  */

25
26 /* $Id: BadWordTransaction.java,v 1.16.10.2 2006/04/17 17:46:47 daltoncamargo Exp $ */
27 /**
28  * @author Dalton Camargo - <a HREF="mailto:dalton@javabb.org">dalton@javabb.org
29  * </a> <br>
30  * @author Ronald Tetsuo Miura
31  */

32 public class BadWordTransaction {
33     /**
34      * Sobrepõe o atributo dao definido na superclasse Este atributo está
35      * sobreposto para poder adquirir os métodos específicos desta entidade
36      */

37     private IBadWordDAO _badWordDAO = null;
38
39     /**
40      * @param dao
41      */

42     public void setBadWordDAO(IBadWordDAO dao) {
43         this._badWordDAO = dao;
44     }
45
46     public static List JavaDoc badwords;
47
48     /**
49      * @return list
50      */

51     public List JavaDoc listAll() {
52         try {
53             if (badwords == null) {
54                 badwords = _badWordDAO.findAll();
55             }
56             return badwords;
57         } catch (Exception JavaDoc e) {
58             throw new RuntimeException JavaDoc(e); // XXX change to another exception
59
}
60     }
61
62     /**
63      * @param post
64      * @return verified post
65      */

66     public String JavaDoc verifyBadWords(String JavaDoc post) {
67         // Verificação de badwords no post
68
if (badwords == null) {
69             badwords = listAll();
70         }
71
72         for (Iterator JavaDoc it = badwords.iterator(); it.hasNext();) {
73             BadWord bw = (BadWord) it.next();
74
75             try {
76                 String JavaDoc badword = bw.getWord();
77                 String JavaDoc reBadWord = "(?:^|\\b)" + badword + "s?(?:^|\\b)";
78                 Pattern JavaDoc pattern = Pattern.compile(reBadWord,
79                         Pattern.CASE_INSENSITIVE);
80                 char firstLetter;
81
82                 if ((badword != null) && (badword.length() > 0)
83                         && Character.isLetterOrDigit(badword.charAt(0))) {
84                     firstLetter = badword.charAt(0);
85                 } else {
86                     firstLetter = '*';
87                 }
88
89                 try {
90                     post = pattern.matcher(post).replaceAll(
91                             firstLetter + bw.getReplacement());
92                 } catch (Exception JavaDoc e) {
93                     post = pattern.matcher(post)
94                             .replaceAll(firstLetter + "***");
95                 }
96             } catch (Exception JavaDoc e) {
97                 // ignore
98
e.printStackTrace();
99             }
100         }
101
102         return post;
103     }
104
105     /**
106      * @param id
107      * @return badword
108      */

109     public BadWord getBadWord(Long JavaDoc id) {
110         return _badWordDAO.load(id);
111     }
112
113     /**
114      * @param badword
115      */

116     public void delete(BadWord badword) {
117         _badWordDAO.delete(badword);
118         badwords = null;
119     }
120
121     /**
122      * @param badword
123      */

124     public void update(BadWord badword) {
125         BadWord bdToUpdate = this.getBadWord(badword.getIdBadWord());
126         bdToUpdate.setReplacement(badword.getReplacement());
127         bdToUpdate.setWord(badword.getWord());
128         badwords = null;
129     }
130
131     /**
132      * @param badword
133      */

134     public void save(BadWord badword) {
135         _badWordDAO.save(badword);
136         badwords = null;
137     }
138 }
Popular Tags