1 64 65 package com.jcorporate.expresso.core.security.filters; 66 67 import com.jcorporate.expresso.core.misc.ReusableChar; 68 import com.jcorporate.expresso.kernel.util.FastStringBuffer; 69 70 71 79 public class FilterTree { 80 protected FilterTreeNode root; 81 82 public FilterTree() { 83 root = new FilterTreeNode(); 84 } 85 86 93 public void addFilterString(String specialString, String replacementString) 94 throws Exception { 95 FilterTreeNode insertNode = root; 96 97 for (int i = 0; i < specialString.length(); i++) { 98 99 ReusableChar c = new ReusableChar(ReusableChar.toLowerCase(specialString.charAt(i))); 105 ReusableChar uc = new ReusableChar(ReusableChar.toUpperCase(c.charValue())); 106 107 if (insertNode.subnodeExists(c) || insertNode.subnodeExists(uc)) { 108 insertNode = insertNode.getSubnode(c); 109 } else { 110 FilterTreeNode newNode = new FilterTreeNode(); 111 insertNode.setSubnode(c, newNode); 112 insertNode.setSubnode(uc, newNode); 113 insertNode = newNode; 114 } 115 } 116 117 insertNode.setReplacementString(replacementString); 120 } 121 122 public FilterTreeNode getRootNode() { 123 return root; 124 } 125 126 133 public String replaceFilter(char[] input) { 134 FastStringBuffer fsb = FastStringBuffer.getInstance(); 135 String returnValue = null; 136 try { 137 int mark = -1; 138 ReusableChar c = new ReusableChar('a'); FilterTreeNode insertNode = root; 140 FilterTreeNode lastMatch = null; 141 String s = null; 142 143 if (input.length == 0) { 144 return (""); 145 } 146 for (int i = 0; i < input.length; i++) { 147 c.setCharValue(input[i]); 148 149 FilterTreeNode n = pushOntoParseTree(c, insertNode); 150 151 if (n == null) { 152 153 if (insertNode == root) { 155 fsb.append(c.charValue()); 156 157 } else if (s != null) { 159 fsb.append(s); 160 i--; 161 162 } else if (lastMatch != null) { 164 fsb.append(lastMatch.getReplacementString()); 165 i = mark; 167 } else { 171 i = mark; fsb.append(input, mark, i - mark + 1); 173 } 174 175 insertNode = root; 176 mark = -1; 177 lastMatch = null; 178 s = null; 179 } else { 180 insertNode = n; 181 182 s = insertNode.getReplacementString(); 184 185 if (s != null) { 186 lastMatch = insertNode; 187 mark = i; 188 } else if (mark == -1) { 189 mark = i; 190 } 191 } 192 } 193 if (s != null) { 196 fsb.append(s); 197 198 } else if (lastMatch != null) { 201 fsb.append(lastMatch.getReplacementString()); 202 203 if (mark < input.length && mark != -1) { 204 fsb.append(input, mark + 1, input.length - (mark + 1)); 205 } 206 207 } else { 209 if (mark != -1) { 210 fsb.append(input, mark, input.length - mark); 211 } 212 } 213 214 returnValue = fsb.toString(); 215 } finally { 216 fsb.release(); 217 fsb = null; 218 } 219 220 return returnValue; 221 } 222 223 230 private FilterTreeNode pushOntoParseTree(ReusableChar c, 231 FilterTreeNode insertNode) { 232 if (insertNode.subnodeExists(c)) { 233 return insertNode.getSubnode(c); 234 } else { 235 return null; 236 } 237 } 238 } | Popular Tags |