1 18 19 20 package org.apache.tomcat.util.digester; 21 22 23 import java.util.ArrayList ; 24 import java.util.HashMap ; 25 import java.util.Iterator ; 26 import java.util.List ; 27 28 29 45 46 public class RulesBase implements Rules { 47 48 49 51 52 57 protected HashMap cache = new HashMap (); 58 59 60 63 protected Digester digester = null; 64 65 66 71 protected String namespaceURI = null; 72 73 74 78 protected ArrayList rules = new ArrayList (); 79 80 81 83 84 88 public Digester getDigester() { 89 90 return (this.digester); 91 92 } 93 94 95 100 public void setDigester(Digester digester) { 101 102 this.digester = digester; 103 Iterator items = rules.iterator(); 104 while (items.hasNext()) { 105 Rule item = (Rule) items.next(); 106 item.setDigester(digester); 107 } 108 109 } 110 111 112 116 public String getNamespaceURI() { 117 118 return (this.namespaceURI); 119 120 } 121 122 123 131 public void setNamespaceURI(String namespaceURI) { 132 133 this.namespaceURI = namespaceURI; 134 135 } 136 137 138 140 141 147 public void add(String pattern, Rule rule) { 148 int patternLength = pattern.length(); 150 if (patternLength>1 && pattern.endsWith("/")) { 151 pattern = pattern.substring(0, patternLength-1); 152 } 153 154 155 List list = (List ) cache.get(pattern); 156 if (list == null) { 157 list = new ArrayList (); 158 cache.put(pattern, list); 159 } 160 list.add(rule); 161 rules.add(rule); 162 if (this.digester != null) { 163 rule.setDigester(this.digester); 164 } 165 if (this.namespaceURI != null) { 166 rule.setNamespaceURI(this.namespaceURI); 167 } 168 169 } 170 171 172 175 public void clear() { 176 177 cache.clear(); 178 rules.clear(); 179 180 } 181 182 183 194 public List match(String pattern) { 195 196 return (match(null, pattern)); 197 198 } 199 200 201 212 public List match(String namespaceURI, String pattern) { 213 214 List rulesList = lookup(namespaceURI, pattern); 216 if ((rulesList == null) || (rulesList.size() < 1)) { 217 String longKey = ""; 219 Iterator keys = this.cache.keySet().iterator(); 220 while (keys.hasNext()) { 221 String key = (String ) keys.next(); 222 if (key.startsWith("*/")) { 223 if (pattern.equals(key.substring(2)) || 224 pattern.endsWith(key.substring(1))) { 225 if (key.length() > longKey.length()) { 226 rulesList = lookup(namespaceURI, key); 228 longKey = key; 229 } 230 } 231 } 232 } 233 } 234 if (rulesList == null) { 235 rulesList = new ArrayList (); 236 } 237 return (rulesList); 238 239 } 240 241 242 249 public List rules() { 250 251 return (this.rules); 252 253 } 254 255 256 258 259 268 protected List lookup(String namespaceURI, String pattern) { 269 270 List list = (List ) this.cache.get(pattern); 272 if (list == null) { 273 return (null); 274 } 275 if ((namespaceURI == null) || (namespaceURI.length() == 0)) { 276 return (list); 277 } 278 279 ArrayList results = new ArrayList (); 281 Iterator items = list.iterator(); 282 while (items.hasNext()) { 283 Rule item = (Rule) items.next(); 284 if ((namespaceURI.equals(item.getNamespaceURI())) || 285 (item.getNamespaceURI() == null)) { 286 results.add(item); 287 } 288 } 289 return (results); 290 291 } 292 293 294 } 295 | Popular Tags |