|
|||||||||||||||||||
| Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
| Rule.java | 50% | 52,1% | 60% | 54,1% |
|
||||||||||||||
| 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 { | |
| 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 | 32 | public Rule(Pattern pattern) { |
| 45 | 32 | this.pattern = pattern; |
| 46 | 32 | this.priority = pattern.getPriority(); |
| 47 | } | |
| 48 | ||
| 49 | 28 | public Rule(Pattern pattern, Action action) { |
| 50 | 28 | this(pattern); |
| 51 | 28 | 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 | 0 | public Rule(Rule that, Pattern pattern) { |
| 64 | 0 | this.mode = that.mode; |
| 65 | 0 | this.importPrecedence = that.importPrecedence; |
| 66 | 0 | this.priority = that.priority; |
| 67 | 0 | this.appearenceCount = that.appearenceCount; |
| 68 | 0 | this.action = that.action; |
| 69 | 0 | this.pattern = pattern; |
| 70 | } | |
| 71 | ||
| 72 | 0 | public boolean equals(Object that) { |
| 73 | 0 | if (that instanceof Rule) { |
| 74 | 0 | return compareTo((Rule) that) == 0; |
| 75 | } | |
| 76 | ||
| 77 | 0 | return false; |
| 78 | } | |
| 79 | ||
| 80 | 2 | public int hashCode() { |
| 81 | 2 | return importPrecedence + appearenceCount; |
| 82 | } | |
| 83 | ||
| 84 | 11 | public int compareTo(Object that) { |
| 85 | 11 | if (that instanceof Rule) { |
| 86 | 11 | return compareTo((Rule) that); |
| 87 | } | |
| 88 | ||
| 89 | 0 | 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 | 12 | public int compareTo(Rule that) { |
| 102 | 12 | int answer = this.importPrecedence - that.importPrecedence; |
| 103 | ||
| 104 | 12 | if (answer == 0) { |
| 105 | 9 | answer = (int) Math.round(this.priority - that.priority); |
| 106 | ||
| 107 | 9 | if (answer == 0) { |
| 108 | 7 | answer = this.appearenceCount - that.appearenceCount; |
| 109 | } | |
| 110 | } | |
| 111 | ||
| 112 | 12 | return answer; |
| 113 | } | |
| 114 | ||
| 115 | 2 | public String toString() { |
| 116 | 2 | 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 | 52 | public final boolean matches(Node node) { |
| 129 | 52 | 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 | 14 | public Rule[] getUnionRules() { |
| 141 | 14 | Pattern[] patterns = pattern.getUnionPatterns(); |
| 142 | ||
| 143 | 14 | if (patterns == null) { |
| 144 | 14 | return null; |
| 145 | } | |
| 146 | ||
| 147 | 0 | int size = patterns.length; |
| 148 | 0 | Rule[] answer = new Rule[size]; |
| 149 | ||
| 150 | 0 | for (int i = 0; i < size; i++) { |
| 151 | 0 | answer[i] = new Rule(this, patterns[i]); |
| 152 | } | |
| 153 | ||
| 154 | 0 | 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 | 28 | public final short getMatchType() { |
| 164 | 28 | 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 | 28 | public final String getMatchesNodeName() { |
| 178 | 28 | return pattern.getMatchesNodeName(); |
| 179 | } | |
| 180 | ||
| 181 | /** | |
| 182 | * Getter for property mode. | |
| 183 | * | |
| 184 | * @return Value of property mode. | |
| 185 | */ | |
| 186 | 14 | public String getMode() { |
| 187 | 14 | return mode; |
| 188 | } | |
| 189 | ||
| 190 | /** | |
| 191 | * Setter for property mode. | |
| 192 | * | |
| 193 | * @param mode | |
| 194 | * New value of property mode. | |
| 195 | */ | |
| 196 | 0 | public void setMode(String mode) { |
| 197 | 0 | this.mode = mode; |
| 198 | } | |
| 199 | ||
| 200 | /** | |
| 201 | * Getter for property importPrecedence. | |
| 202 | * | |
| 203 | * @return Value of property importPrecedence. | |
| 204 | */ | |
| 205 | 0 | public int getImportPrecedence() { |
| 206 | 0 | return importPrecedence; |
| 207 | } | |
| 208 | ||
| 209 | /** | |
| 210 | * Setter for property importPrecedence. | |
| 211 | * | |
| 212 | * @param importPrecedence | |
| 213 | * New value of property importPrecedence. | |
| 214 | */ | |
| 215 | 14 | public void setImportPrecedence(int importPrecedence) { |
| 216 | 14 | this.importPrecedence = importPrecedence; |
| 217 | } | |
| 218 | ||
| 219 | /** | |
| 220 | * Getter for property priority. | |
| 221 | * | |
| 222 | * @return Value of property priority. | |
| 223 | */ | |
| 224 | 0 | public double getPriority() { |
| 225 | 0 | return priority; |
| 226 | } | |
| 227 | ||
| 228 | /** | |
| 229 | * Setter for property priority. | |
| 230 | * | |
| 231 | * @param priority | |
| 232 | * New value of property priority. | |
| 233 | */ | |
| 234 | 0 | public void setPriority(double priority) { |
| 235 | 0 | this.priority = priority; |
| 236 | } | |
| 237 | ||
| 238 | /** | |
| 239 | * Getter for property appearenceCount. | |
| 240 | * | |
| 241 | * @return Value of property appearenceCount. | |
| 242 | */ | |
| 243 | 0 | public int getAppearenceCount() { |
| 244 | 0 | return appearenceCount; |
| 245 | } | |
| 246 | ||
| 247 | /** | |
| 248 | * Setter for property appearenceCount. | |
| 249 | * | |
| 250 | * @param appearenceCount | |
| 251 | * New value of property appearenceCount. | |
| 252 | */ | |
| 253 | 14 | public void setAppearenceCount(int appearenceCount) { |
| 254 | 14 | this.appearenceCount = appearenceCount; |
| 255 | } | |
| 256 | ||
| 257 | /** | |
| 258 | * Getter for property pattern. | |
| 259 | * | |
| 260 | * @return Value of property pattern. | |
| 261 | */ | |
| 262 | 2 | public Pattern getPattern() { |
| 263 | 2 | return pattern; |
| 264 | } | |
| 265 | ||
| 266 | /** | |
| 267 | * Setter for property pattern. | |
| 268 | * | |
| 269 | * @param pattern | |
| 270 | * New value of property pattern. | |
| 271 | */ | |
| 272 | 0 | public void setPattern(Pattern pattern) { |
| 273 | 0 | this.pattern = pattern; |
| 274 | } | |
| 275 | ||
| 276 | /** | |
| 277 | * Getter for property action. | |
| 278 | * | |
| 279 | * @return Value of property action. | |
| 280 | */ | |
| 281 | 30 | public Action getAction() { |
| 282 | 30 | return action; |
| 283 | } | |
| 284 | ||
| 285 | /** | |
| 286 | * Setter for property action. | |
| 287 | * | |
| 288 | * @param action | |
| 289 | * New value of property action. | |
| 290 | */ | |
| 291 | 0 | public void setAction(Action action) { |
| 292 | 0 | 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 | */ |
|
||||||||||