|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
Rule.java | 50% | 51,1% | 58,3% | 53% |
|
1 | /* | |
2 | * Copyright 2001-2004 (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 | * $Id: Rule.java,v 1.5 2004/06/25 08:03:39 maartenc Exp $ | |
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 | 0 | public Rule() { |
41 | 0 | this.priority = Pattern.DEFAULT_PRIORITY; |
42 | } | |
43 | ||
44 | 50 | public Rule( Pattern pattern ) { |
45 | 50 | this.pattern = pattern; |
46 | 50 | this.priority = pattern.getPriority(); |
47 | } | |
48 | ||
49 | 42 | public Rule( Pattern pattern, Action action ) { |
50 | 42 | this( pattern ); |
51 | 42 | 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 | 0 | public Rule(Rule that, Pattern pattern) { |
58 | 0 | this.mode = that.mode; |
59 | 0 | this.importPrecedence = that.importPrecedence; |
60 | 0 | this.priority = that.priority; |
61 | 0 | this.appearenceCount = that.appearenceCount; |
62 | 0 | this.action = that.action; |
63 | 0 | this.pattern = pattern; |
64 | } | |
65 | ||
66 | 0 | public boolean equals(Object that) { |
67 | 0 | if ( that instanceof Rule ) { |
68 | 0 | return compareTo( (Rule) that ) == 0; |
69 | } | |
70 | 0 | return false; |
71 | } | |
72 | ||
73 | 34 | public int compareTo(Object that) { |
74 | 34 | if ( that instanceof Rule ) { |
75 | 34 | return compareTo((Rule) that); |
76 | } | |
77 | 0 | 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 | 36 | public int compareTo(Rule that) { |
84 | 36 | int answer = this.importPrecedence - that.importPrecedence; |
85 | 36 | if ( answer == 0 ) { |
86 | 24 | answer = (int) Math.round( this.priority - that.priority ); |
87 | 24 | if ( answer == 0 ) { |
88 | 16 | answer = this.appearenceCount - that.appearenceCount; |
89 | } | |
90 | } | |
91 | 36 | return answer; |
92 | } | |
93 | ||
94 | 4 | public String toString() { |
95 | 4 | 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 | 122 | public final boolean matches( Node node ) { |
104 | 122 | 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 | 26 | public Rule[] getUnionRules() { |
116 | 26 | Pattern[] patterns = pattern.getUnionPatterns(); |
117 | 26 | if ( patterns == null ) { |
118 | 26 | return null; |
119 | } | |
120 | 0 | int size = patterns.length; |
121 | 0 | Rule[] answer = new Rule[ size ]; |
122 | 0 | for ( int i = 0; i < size; i++ ) { |
123 | 0 | answer[i] = new Rule( this, patterns[i] ); |
124 | } | |
125 | 0 | 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 | 42 | public final short getMatchType() { |
134 | 42 | 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 | 42 | public final String getMatchesNodeName() { |
148 | 42 | return pattern.getMatchesNodeName(); |
149 | } | |
150 | ||
151 | ||
152 | ||
153 | ||
154 | ||
155 | /** Getter for property mode. | |
156 | * @return Value of property mode. | |
157 | */ | |
158 | 26 | public String getMode() { |
159 | 26 | return mode; |
160 | } | |
161 | ||
162 | /** Setter for property mode. | |
163 | * @param mode New value of property mode. | |
164 | */ | |
165 | 0 | public void setMode(String mode) { |
166 | 0 | this.mode = mode; |
167 | } | |
168 | ||
169 | /** Getter for property importPrecedence. | |
170 | * @return Value of property importPrecedence. | |
171 | */ | |
172 | 0 | public int getImportPrecedence() { |
173 | 0 | return importPrecedence; |
174 | } | |
175 | ||
176 | /** Setter for property importPrecedence. | |
177 | * @param importPrecedence New value of property importPrecedence. | |
178 | */ | |
179 | 16 | public void setImportPrecedence(int importPrecedence) { |
180 | 16 | this.importPrecedence = importPrecedence; |
181 | } | |
182 | ||
183 | /** Getter for property priority. | |
184 | * @return Value of property priority. | |
185 | */ | |
186 | 0 | public double getPriority() { |
187 | 0 | return priority; |
188 | } | |
189 | ||
190 | /** Setter for property priority. | |
191 | * @param priority New value of property priority. | |
192 | */ | |
193 | 0 | public void setPriority(double priority) { |
194 | 0 | this.priority = priority; |
195 | } | |
196 | ||
197 | /** Getter for property appearenceCount. | |
198 | * @return Value of property appearenceCount. | |
199 | */ | |
200 | 0 | public int getAppearenceCount() { |
201 | 0 | return appearenceCount; |
202 | } | |
203 | ||
204 | /** Setter for property appearenceCount. | |
205 | * @param appearenceCount New value of property appearenceCount. | |
206 | */ | |
207 | 26 | public void setAppearenceCount(int appearenceCount) { |
208 | 26 | this.appearenceCount = appearenceCount; |
209 | } | |
210 | ||
211 | /** Getter for property pattern. | |
212 | * @return Value of property pattern. | |
213 | */ | |
214 | 4 | public Pattern getPattern() { |
215 | 4 | return pattern; |
216 | } | |
217 | ||
218 | /** Setter for property pattern. | |
219 | * @param pattern New value of property pattern. | |
220 | */ | |
221 | 0 | public void setPattern(Pattern pattern) { |
222 | 0 | this.pattern = pattern; |
223 | } | |
224 | ||
225 | /** Getter for property action. | |
226 | * @return Value of property action. | |
227 | */ | |
228 | 64 | public Action getAction() { |
229 | 64 | return action; |
230 | } | |
231 | ||
232 | /** Setter for property action. | |
233 | * @param action New value of property action. | |
234 | */ | |
235 | 0 | public void setAction(Action action) { |
236 | 0 | this.action = action; |
237 | } | |
238 | ||
239 | } | |
240 | ||
241 | ||
242 | ||
243 | ||
244 | /* | |
245 | * Redistribution and use of this software and associated documentation | |
246 | * ("Software"), with or without modification, are permitted provided | |
247 | * that the following conditions are met: | |
248 | * | |
249 | * 1. Redistributions of source code must retain copyright | |
250 | * statements and notices. Redistributions must also contain a | |
251 | * copy of this document. | |
252 | * | |
253 | * 2. Redistributions in binary form must reproduce the | |
254 | * above copyright notice, this list of conditions and the | |
255 | * following disclaimer in the documentation and/or other | |
256 | * materials provided with the distribution. | |
257 | * | |
258 | * 3. The name "DOM4J" must not be used to endorse or promote | |
259 | * products derived from this Software without prior written | |
260 | * permission of MetaStuff, Ltd. For written permission, | |
261 | * please contact dom4j-info@metastuff.com. | |
262 | * | |
263 | * 4. Products derived from this Software may not be called "DOM4J" | |
264 | * nor may "DOM4J" appear in their names without prior written | |
265 | * permission of MetaStuff, Ltd. DOM4J is a registered | |
266 | * trademark of MetaStuff, Ltd. | |
267 | * | |
268 | * 5. Due credit should be given to the DOM4J Project - | |
269 | * http://www.dom4j.org | |
270 | * | |
271 | * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS | |
272 | * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT | |
273 | * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
274 | * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL | |
275 | * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, | |
276 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES | |
277 | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR | |
278 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) | |
279 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, | |
280 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | |
281 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED | |
282 | * OF THE POSSIBILITY OF SUCH DAMAGE. | |
283 | * | |
284 | * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved. | |
285 | * | |
286 | * $Id: Rule.java,v 1.5 2004/06/25 08:03:39 maartenc Exp $ | |
287 | */ |
|