1 17 18 19 package org.apache.commons.digester; 20 21 22 import java.util.ArrayList ; 23 import java.util.HashMap ; 24 import java.util.Iterator ; 25 import java.util.List ; 26 27 28 51 52 public class RulesBase implements Rules { 53 54 55 57 58 63 protected HashMap cache = new HashMap (); 64 65 66 69 protected Digester digester = null; 70 71 72 77 protected String namespaceURI = null; 78 79 80 84 protected ArrayList rules = new ArrayList (); 85 86 87 89 90 94 public Digester getDigester() { 95 96 return (this.digester); 97 98 } 99 100 101 106 public void setDigester(Digester digester) { 107 108 this.digester = digester; 109 Iterator items = rules.iterator(); 110 while (items.hasNext()) { 111 Rule item = (Rule) items.next(); 112 item.setDigester(digester); 113 } 114 115 } 116 117 118 122 public String getNamespaceURI() { 123 124 return (this.namespaceURI); 125 126 } 127 128 129 137 public void setNamespaceURI(String namespaceURI) { 138 139 this.namespaceURI = namespaceURI; 140 141 } 142 143 144 146 147 153 public void add(String pattern, Rule rule) { 154 int patternLength = pattern.length(); 156 if (patternLength>1 && pattern.endsWith("/")) { 157 pattern = pattern.substring(0, patternLength-1); 158 } 159 160 161 List list = (List ) cache.get(pattern); 162 if (list == null) { 163 list = new ArrayList (); 164 cache.put(pattern, list); 165 } 166 list.add(rule); 167 rules.add(rule); 168 if (this.digester != null) { 169 rule.setDigester(this.digester); 170 } 171 if (this.namespaceURI != null) { 172 rule.setNamespaceURI(this.namespaceURI); 173 } 174 175 } 176 177 178 181 public void clear() { 182 183 cache.clear(); 184 rules.clear(); 185 186 } 187 188 189 200 public List match(String pattern) { 201 202 return (match(null, pattern)); 203 204 } 205 206 207 218 public List match(String namespaceURI, String pattern) { 219 220 List rulesList = lookup(namespaceURI, pattern); 222 if ((rulesList == null) || (rulesList.size() < 1)) { 223 String longKey = ""; 225 Iterator keys = this.cache.keySet().iterator(); 226 while (keys.hasNext()) { 227 String key = (String ) keys.next(); 228 if (key.startsWith("*/")) { 229 if (pattern.equals(key.substring(2)) || 230 pattern.endsWith(key.substring(1))) { 231 if (key.length() > longKey.length()) { 232 rulesList = lookup(namespaceURI, key); 234 longKey = key; 235 } 236 } 237 } 238 } 239 } 240 if (rulesList == null) { 241 rulesList = new ArrayList (); 242 } 243 return (rulesList); 244 245 } 246 247 248 255 public List rules() { 256 257 return (this.rules); 258 259 } 260 261 262 264 265 274 protected List lookup(String namespaceURI, String pattern) { 275 276 List list = (List ) this.cache.get(pattern); 278 if (list == null) { 279 return (null); 280 } 281 if ((namespaceURI == null) || (namespaceURI.length() == 0)) { 282 return (list); 283 } 284 285 ArrayList results = new ArrayList (); 287 Iterator items = list.iterator(); 288 while (items.hasNext()) { 289 Rule item = (Rule) items.next(); 290 if ((namespaceURI.equals(item.getNamespaceURI())) || 291 (item.getNamespaceURI() == null)) { 292 results.add(item); 293 } 294 } 295 return (results); 296 297 } 298 299 300 } 301 | Popular Tags |