KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dom4j > rule > Rule


1 /*
2  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
3  *
4  * This software is open source.
5  * See the bottom of this file for the licence.
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 JavaDoc {
22     /** Holds value of property mode. */
23     private String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc getMatchesNodeName() {
178         return pattern.getMatchesNodeName();
179     }
180
181     /**
182      * Getter for property mode.
183      *
184      * @return Value of property mode.
185      */

186     public String JavaDoc 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 JavaDoc 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  * Redistribution and use of this software and associated documentation
298  * ("Software"), with or without modification, are permitted provided that the
299  * following conditions are met:
300  *
301  * 1. Redistributions of source code must retain copyright statements and
302  * notices. Redistributions must also contain a copy of this document.
303  *
304  * 2. Redistributions in binary form must reproduce the above copyright notice,
305  * this list of conditions and the following disclaimer in the documentation
306  * and/or other materials provided with the distribution.
307  *
308  * 3. The name "DOM4J" must not be used to endorse or promote products derived
309  * from this Software without prior written permission of MetaStuff, Ltd. For
310  * written permission, please contact dom4j-info@metastuff.com.
311  *
312  * 4. Products derived from this Software may not be called "DOM4J" nor may
313  * "DOM4J" appear in their names without prior written permission of MetaStuff,
314  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
315  *
316  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
317  *
318  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
319  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
320  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
321  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
322  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
323  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
324  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
325  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
326  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
327  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
328  * POSSIBILITY OF SUCH DAMAGE.
329  *
330  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
331  */

332
Popular Tags