1   
2   
3   
4   
5   
6   
7   
8   package org.dom4j.rule;
9   
10  import org.dom4j.Node;
11  
12  /***
13   * <p>
14   * <code>Rule</code> matches against DOM4J Node so that some action can be
15   * performed such as in the XSLT processing model.
16   * </p>
17   * 
18   * @author <a href="mailto:james.strachan@metastuff.com">James Strachan </a>
19   * @version $Revision: 1.7 $
20   */
21  public class Rule implements Comparable {
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      /***
55       * Constructs a new Rule with the same instance data as the given rule but a
56       * different pattern.
57       * 
58       * @param that
59       *            DOCUMENT ME!
60       * @param pattern
61       *            DOCUMENT ME!
62       */
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      /***
93       * Compares two rules in XSLT processing model order assuming that the modes
94       * are equal.
95       * 
96       * @param that
97       *            DOCUMENT ME!
98       * 
99       * @return DOCUMENT ME!
100      */
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     /***
121      * DOCUMENT ME!
122      * 
123      * @param node
124      *            DOCUMENT ME!
125      * 
126      * @return true if the pattern matches the given DOM4J node.
127      */
128     public final boolean matches(Node node) {
129         return pattern.matches(node);
130     }
131 
132     /***
133      * If this rule contains a union pattern then this method should return an
134      * array of Rules which describe the union rule, which should contain more
135      * than one rule. Otherwise this method should return null.
136      * 
137      * @return an array of the rules which make up this union rule or null if
138      *         this rule is not a union rule
139      */
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     /***
158      * DOCUMENT ME!
159      * 
160      * @return the type of node the pattern matches which by default should
161      *         return ANY_NODE if it can match any kind of node.
162      */
163     public final short getMatchType() {
164         return pattern.getMatchType();
165     }
166 
167     /***
168      * For patterns which only match an ATTRIBUTE_NODE or an ELEMENT_NODE then
169      * this pattern may return the name of the element or attribute it matches.
170      * This allows a more efficient rule matching algorithm to be performed,
171      * rather than a brute force approach of evaluating every pattern for a
172      * given Node.
173      * 
174      * @return the name of the element or attribute this pattern matches or null
175      *         if this pattern matches any or more than one name.
176      */
177     public final String getMatchesNodeName() {
178         return pattern.getMatchesNodeName();
179     }
180 
181     /***
182      * Getter for property mode.
183      * 
184      * @return Value of property mode.
185      */
186     public String getMode() {
187         return mode;
188     }
189 
190     /***
191      * Setter for property mode.
192      * 
193      * @param mode
194      *            New value of property mode.
195      */
196     public void setMode(String mode) {
197         this.mode = mode;
198     }
199 
200     /***
201      * Getter for property importPrecedence.
202      * 
203      * @return Value of property importPrecedence.
204      */
205     public int getImportPrecedence() {
206         return importPrecedence;
207     }
208 
209     /***
210      * Setter for property importPrecedence.
211      * 
212      * @param importPrecedence
213      *            New value of property importPrecedence.
214      */
215     public void setImportPrecedence(int importPrecedence) {
216         this.importPrecedence = importPrecedence;
217     }
218 
219     /***
220      * Getter for property priority.
221      * 
222      * @return Value of property priority.
223      */
224     public double getPriority() {
225         return priority;
226     }
227 
228     /***
229      * Setter for property priority.
230      * 
231      * @param priority
232      *            New value of property priority.
233      */
234     public void setPriority(double priority) {
235         this.priority = priority;
236     }
237 
238     /***
239      * Getter for property appearenceCount.
240      * 
241      * @return Value of property appearenceCount.
242      */
243     public int getAppearenceCount() {
244         return appearenceCount;
245     }
246 
247     /***
248      * Setter for property appearenceCount.
249      * 
250      * @param appearenceCount
251      *            New value of property appearenceCount.
252      */
253     public void setAppearenceCount(int appearenceCount) {
254         this.appearenceCount = appearenceCount;
255     }
256 
257     /***
258      * Getter for property pattern.
259      * 
260      * @return Value of property pattern.
261      */
262     public Pattern getPattern() {
263         return pattern;
264     }
265 
266     /***
267      * Setter for property pattern.
268      * 
269      * @param pattern
270      *            New value of property pattern.
271      */
272     public void setPattern(Pattern pattern) {
273         this.pattern = pattern;
274     }
275 
276     /***
277      * Getter for property action.
278      * 
279      * @return Value of property action.
280      */
281     public Action getAction() {
282         return action;
283     }
284 
285     /***
286      * Setter for property action.
287      * 
288      * @param action
289      *            New value of property action.
290      */
291     public void setAction(Action action) {
292         this.action = action;
293     }
294 }
295 
296 
297 
298 
299 
300 
301 
302 
303 
304 
305 
306 
307 
308 
309 
310 
311 
312 
313 
314 
315 
316 
317 
318 
319 
320 
321 
322 
323 
324 
325 
326 
327 
328 
329 
330 
331