1 17 18 package org.apache.commons.digester; 19 20 import java.util.ArrayList ; 21 import java.util.Iterator ; 22 import java.util.List ; 23 24 48 49 public class WithDefaultsRulesWrapper implements Rules { 50 51 53 54 private Rules wrappedRules; 55 56 private List defaultRules = new ArrayList (); 57 58 private List allRules = new ArrayList (); 59 60 62 68 public WithDefaultsRulesWrapper(Rules wrappedRules) { 69 if (wrappedRules == null) { 70 throw new IllegalArgumentException ("Wrapped rules must not be null"); 71 } 72 this.wrappedRules = wrappedRules; 73 } 74 75 77 78 public Digester getDigester() { 79 return wrappedRules.getDigester(); 80 } 81 82 83 public void setDigester(Digester digester) { 84 wrappedRules.setDigester(digester); 85 Iterator it = defaultRules.iterator(); 86 while (it.hasNext()) { 87 Rule rule = (Rule) it.next(); 88 rule.setDigester(digester); 89 } 90 } 91 92 93 public String getNamespaceURI() { 94 return wrappedRules.getNamespaceURI(); 95 } 96 97 98 public void setNamespaceURI(String namespaceURI) { 99 wrappedRules.setNamespaceURI(namespaceURI); 100 } 101 102 103 public List getDefaults() { 104 return defaultRules; 105 } 106 107 109 public List match(String pattern) { 110 return match("", pattern); 111 } 112 113 118 public List match(String namespaceURI, String pattern) { 119 List matches = wrappedRules.match(namespaceURI, pattern); 120 if (matches == null || matches.isEmpty()) { 121 return new ArrayList (defaultRules); 123 } 124 return matches; 126 } 127 128 129 public void addDefault(Rule rule) { 130 if (wrappedRules.getDigester() != null) { 132 rule.setDigester(wrappedRules.getDigester()); 133 } 134 135 if (wrappedRules.getNamespaceURI() != null) { 136 rule.setNamespaceURI(wrappedRules.getNamespaceURI()); 137 } 138 139 defaultRules.add(rule); 140 allRules.add(rule); 141 } 142 143 144 public List rules() { 145 return allRules; 146 } 147 148 149 public void clear() { 150 wrappedRules.clear(); 151 allRules.clear(); 152 defaultRules.clear(); 153 } 154 155 159 public void add(String pattern, Rule rule) { 160 wrappedRules.add(pattern, rule); 161 allRules.add(rule); 162 } 163 } 164 | Popular Tags |