1 package org.javabb.transaction; 2 3 import java.util.Iterator ; 4 import java.util.List ; 5 import java.util.regex.Pattern ; 6 7 import org.javabb.dao.entity.IBadWordDAO; 8 import org.javabb.vo.BadWord; 9 10 25 26 27 32 public class BadWordTransaction { 33 37 private IBadWordDAO _badWordDAO = null; 38 39 42 public void setBadWordDAO(IBadWordDAO dao) { 43 this._badWordDAO = dao; 44 } 45 46 public static List badwords; 47 48 51 public List listAll() { 52 try { 53 if (badwords == null) { 54 badwords = _badWordDAO.findAll(); 55 } 56 return badwords; 57 } catch (Exception e) { 58 throw new RuntimeException (e); } 60 } 61 62 66 public String verifyBadWords(String post) { 67 if (badwords == null) { 69 badwords = listAll(); 70 } 71 72 for (Iterator it = badwords.iterator(); it.hasNext();) { 73 BadWord bw = (BadWord) it.next(); 74 75 try { 76 String badword = bw.getWord(); 77 String reBadWord = "(?:^|\\b)" + badword + "s?(?:^|\\b)"; 78 Pattern 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 e) { 93 post = pattern.matcher(post) 94 .replaceAll(firstLetter + "***"); 95 } 96 } catch (Exception e) { 97 e.printStackTrace(); 99 } 100 } 101 102 return post; 103 } 104 105 109 public BadWord getBadWord(Long id) { 110 return _badWordDAO.load(id); 111 } 112 113 116 public void delete(BadWord badword) { 117 _badWordDAO.delete(badword); 118 badwords = null; 119 } 120 121 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 134 public void save(BadWord badword) { 135 _badWordDAO.save(badword); 136 badwords = null; 137 } 138 } | Popular Tags |