1
2
3
4
5
6
7
8
9
10 package org.dom4j.rule;
11
12 import org.dom4j.Node;
13
14 /*** <p><code>Rule</code> matches against DOM4J Node so that some action
15 * can be performed such as in the XSLT processing model.</p>
16 *
17 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a>
18 * @version $Revision: 1.5 $
19 */
20 public class Rule implements Comparable {
21
22 /*** Holds value of property mode. */
23 private String mode;
24
25 /*** Holds value of property importPrecedence. */
26 private int importPrecedence;
27
28 /*** Holds value of property priority. */
29 private double priority;
30
31 /*** Holds value of property appearenceCount. */
32 private int appearenceCount;
33
34 /*** Holds value of property pattern. */
35 private Pattern pattern;
36
37 /*** Holds value of property action. */
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 /*** Constructs a new Rule with the same instance data
55 * as the given rule but a different pattern.
56 */
57 public Rule(Rule that, Pattern pattern) {
58 this.mode = that.mode;
59 this.importPrecedence = that.importPrecedence;
60 this.priority = that.priority;
61 this.appearenceCount = that.appearenceCount;
62 this.action = that.action;
63 this.pattern = pattern;
64 }
65
66 public boolean equals(Object that) {
67 if ( that instanceof Rule ) {
68 return compareTo( (Rule) that ) == 0;
69 }
70 return false;
71 }
72
73 public int compareTo(Object that) {
74 if ( that instanceof Rule ) {
75 return compareTo((Rule) that);
76 }
77 return getClass().getName().compareTo( that.getClass().getName() );
78 }
79
80 /*** Compares two rules in XSLT processing model order
81 * assuming that the modes are equal.
82 */
83 public int compareTo(Rule that) {
84 int answer = this.importPrecedence - that.importPrecedence;
85 if ( answer == 0 ) {
86 answer = (int) Math.round( this.priority - that.priority );
87 if ( answer == 0 ) {
88 answer = this.appearenceCount - that.appearenceCount;
89 }
90 }
91 return answer;
92 }
93
94 public String toString() {
95 return super.toString() + "[ pattern: " + getPattern() + " action: " + getAction() + " ]";
96 }
97
98
99
100 /*** @return true if the pattern matches the given
101 * DOM4J node.
102 */
103 public final boolean matches( Node node ) {
104 return pattern.matches( node );
105 }
106
107 /*** If this rule contains a union pattern then this
108 * method should return an array of Rules which
109 * describe the union rule, which should contain more than one rule.
110 * Otherwise this method should return null.
111 *
112 * @return an array of the rules which make up this union rule
113 * or null if this rule is not a union rule
114 */
115 public Rule[] getUnionRules() {
116 Pattern[] patterns = pattern.getUnionPatterns();
117 if ( patterns == null ) {
118 return null;
119 }
120 int size = patterns.length;
121 Rule[] answer = new Rule[ size ];
122 for ( int i = 0; i < size; i++ ) {
123 answer[i] = new Rule( this, patterns[i] );
124 }
125 return answer;
126 }
127
128
129 /*** @return the type of node the pattern matches
130 * which by default should return ANY_NODE if it can
131 * match any kind of node.
132 */
133 public final short getMatchType() {
134 return pattern.getMatchType();
135 }
136
137
138 /*** For patterns which only match an ATTRIBUTE_NODE or an
139 * ELEMENT_NODE then this pattern may return the name of the
140 * element or attribute it matches. This allows a more efficient
141 * rule matching algorithm to be performed, rather than a brute
142 * force approach of evaluating every pattern for a given Node.
143 *
144 * @return the name of the element or attribute this pattern matches
145 * or null if this pattern matches any or more than one name.
146 */
147 public final String getMatchesNodeName() {
148 return pattern.getMatchesNodeName();
149 }
150
151
152
153
154
155 /*** Getter for property mode.
156 * @return Value of property mode.
157 */
158 public String getMode() {
159 return mode;
160 }
161
162 /*** Setter for property mode.
163 * @param mode New value of property mode.
164 */
165 public void setMode(String mode) {
166 this.mode = mode;
167 }
168
169 /*** Getter for property importPrecedence.
170 * @return Value of property importPrecedence.
171 */
172 public int getImportPrecedence() {
173 return importPrecedence;
174 }
175
176 /*** Setter for property importPrecedence.
177 * @param importPrecedence New value of property importPrecedence.
178 */
179 public void setImportPrecedence(int importPrecedence) {
180 this.importPrecedence = importPrecedence;
181 }
182
183 /*** Getter for property priority.
184 * @return Value of property priority.
185 */
186 public double getPriority() {
187 return priority;
188 }
189
190 /*** Setter for property priority.
191 * @param priority New value of property priority.
192 */
193 public void setPriority(double priority) {
194 this.priority = priority;
195 }
196
197 /*** Getter for property appearenceCount.
198 * @return Value of property appearenceCount.
199 */
200 public int getAppearenceCount() {
201 return appearenceCount;
202 }
203
204 /*** Setter for property appearenceCount.
205 * @param appearenceCount New value of property appearenceCount.
206 */
207 public void setAppearenceCount(int appearenceCount) {
208 this.appearenceCount = appearenceCount;
209 }
210
211 /*** Getter for property pattern.
212 * @return Value of property pattern.
213 */
214 public Pattern getPattern() {
215 return pattern;
216 }
217
218 /*** Setter for property pattern.
219 * @param pattern New value of property pattern.
220 */
221 public void setPattern(Pattern pattern) {
222 this.pattern = pattern;
223 }
224
225 /*** Getter for property action.
226 * @return Value of property action.
227 */
228 public Action getAction() {
229 return action;
230 }
231
232 /*** Setter for property action.
233 * @param action New value of property action.
234 */
235 public void setAction(Action action) {
236 this.action = action;
237 }
238
239 }
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287