1 7 8 package org.dom4j.rule; 9 10 import org.dom4j.Node; 11 12 21 public class Rule implements Comparable { 22 23 private String mode; 24 25 26 private int importPrecedence; 27 28 29 private double priority; 30 31 32 private int appearenceCount; 33 34 35 private Pattern pattern; 36 37 38 private Action action; 39 40 public Rule() { 41 this.priority = Pattern.DEFAULT_PRIORITY; 42 } 43 44 public Rule(Pattern pattern) { 45 this.pattern = pattern; 46 this.priority = pattern.getPriority(); 47 } 48 49 public Rule(Pattern pattern, Action action) { 50 this(pattern); 51 this.action = action; 52 } 53 54 63 public Rule(Rule that, Pattern pattern) { 64 this.mode = that.mode; 65 this.importPrecedence = that.importPrecedence; 66 this.priority = that.priority; 67 this.appearenceCount = that.appearenceCount; 68 this.action = that.action; 69 this.pattern = pattern; 70 } 71 72 public boolean equals(Object that) { 73 if (that instanceof Rule) { 74 return compareTo((Rule) that) == 0; 75 } 76 77 return false; 78 } 79 80 public int hashCode() { 81 return importPrecedence + appearenceCount; 82 } 83 84 public int compareTo(Object that) { 85 if (that instanceof Rule) { 86 return compareTo((Rule) that); 87 } 88 89 return getClass().getName().compareTo(that.getClass().getName()); 90 } 91 92 101 public int compareTo(Rule that) { 102 int answer = this.importPrecedence - that.importPrecedence; 103 104 if (answer == 0) { 105 answer = (int) Math.round(this.priority - that.priority); 106 107 if (answer == 0) { 108 answer = this.appearenceCount - that.appearenceCount; 109 } 110 } 111 112 return answer; 113 } 114 115 public String toString() { 116 return super.toString() + "[ pattern: " + getPattern() + " action: " 117 + getAction() + " ]"; 118 } 119 120 128 public final boolean matches(Node node) { 129 return pattern.matches(node); 130 } 131 132 140 public Rule[] getUnionRules() { 141 Pattern[] patterns = pattern.getUnionPatterns(); 142 143 if (patterns == null) { 144 return null; 145 } 146 147 int size = patterns.length; 148 Rule[] answer = new Rule[size]; 149 150 for (int i = 0; i < size; i++) { 151 answer[i] = new Rule(this, patterns[i]); 152 } 153 154 return answer; 155 } 156 157 163 public final short getMatchType() { 164 return pattern.getMatchType(); 165 } 166 167 177 public final String getMatchesNodeName() { 178 return pattern.getMatchesNodeName(); 179 } 180 181 186 public String getMode() { 187 return mode; 188 } 189 190 196 public void setMode(String mode) { 197 this.mode = mode; 198 } 199 200 205 public int getImportPrecedence() { 206 return importPrecedence; 207 } 208 209 215 public void setImportPrecedence(int importPrecedence) { 216 this.importPrecedence = importPrecedence; 217 } 218 219 224 public double getPriority() { 225 return priority; 226 } 227 228 234 public void setPriority(double priority) { 235 this.priority = priority; 236 } 237 238 243 public int getAppearenceCount() { 244 return appearenceCount; 245 } 246 247 253 public void setAppearenceCount(int appearenceCount) { 254 this.appearenceCount = appearenceCount; 255 } 256 257 262 public Pattern getPattern() { 263 return pattern; 264 } 265 266 272 public void setPattern(Pattern pattern) { 273 this.pattern = pattern; 274 } 275 276 281 public Action getAction() { 282 return action; 283 } 284 285 291 public void setAction(Action action) { 292 this.action = action; 293 } 294 } 295 296 332 | Popular Tags |