Project dom4j 1.5.2 [5/2/05 10:13 PM]
 
Coverage - org/dom4j/rule/Rule.java
  /*
   * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
   * 
   * This software is open source. 
   * See the bottom of this file for the licence.
   * 
   * $Id: Rule.java,v 1.5 2004/06/25 08:03:39 maartenc Exp $
   */
 
  package org.dom4j.rule;
 
  import org.dom4j.Node;
 
  /** <p><code>Rule</code> matches against DOM4J Node so that some action
    * can be performed such as in the XSLT processing model.</p>
    *
    * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a>
    * @version $Revision: 1.5 $
    */
  public class Rule implements Comparable {
 
      /** Holds value of property mode. */
      private String mode;
      
      /** Holds value of property importPrecedence. */
      private int importPrecedence;
      
      /** Holds value of property priority. */
      private double priority;
      
      /** Holds value of property appearenceCount. */
      private int appearenceCount;
      
      /** Holds value of property pattern. */
      private Pattern pattern;    
      
      /** Holds value of property action. */
      private Action action;
      
0x       public Rule() {
0x           this.priority = Pattern.DEFAULT_PRIORITY;
0x       }
 
34x       public Rule( Pattern pattern ) {
34x           this.pattern = pattern;
34x           this.priority = pattern.getPriority();
34x       }
 
      public Rule( Pattern pattern, Action action ) {
26x           this( pattern );
26x           this.action = action;
26x       }
 
      /** Constructs a new Rule with the same instance data
        * as the given rule but a different pattern.
        */
0x       public Rule(Rule that, Pattern pattern) {
0x           this.mode = that.mode;
0x           this.importPrecedence = that.importPrecedence;
0x           this.priority = that.priority;
0x           this.appearenceCount = that.appearenceCount;
0x           this.action = that.action;
0x           this.pattern = pattern;
0x       }
 
      public boolean equals(Object that) {
0/2 0x           if ( that instanceof Rule ) {
0x               return compareTo( (Rule) that ) == 0;
          }
0x           return false;
      }
      
      public int compareTo(Object that) {
1/2 22x           if ( that instanceof Rule ) {
22x               return compareTo((Rule) that);
          }
0x           return getClass().getName().compareTo( that.getClass().getName() );
      }
      
      /** Compares two rules in XSLT processing model order
        * assuming that the modes are equal.
        */
      public int compareTo(Rule that) {
24x           int answer = this.importPrecedence - that.importPrecedence;
2/2 24x           if ( answer == 0 ) {
18x               answer = (int) Math.round( this.priority - that.priority );
2/2 18x               if ( answer == 0 ) {
14x                   answer = this.appearenceCount - that.appearenceCount;
              }
          }
24x           return answer;
      }
      
      public String toString() {
4x           return super.toString() + "[ pattern: " + getPattern() + " action: " + getAction() + " ]";
      }
      
      
      
      /** @return true if the pattern matches the given 
        * DOM4J node.
        */
      public final boolean matches( Node node ) {
80x           return pattern.matches( node );
      }
      
      /** If this rule contains a union pattern then this
        * method should return an array of Rules which
        * describe the union rule, which should contain more than one rule.
        * Otherwise this method should return null.
        *
        * @return an array of the rules which make up this union rule
        * or null if this rule is not a union rule
        */
      public Rule[] getUnionRules() {
18x           Pattern[] patterns = pattern.getUnionPatterns();
1/2 18x           if ( patterns == null ) {
18x               return null;
          }
0x           int size = patterns.length;
0x           Rule[] answer = new Rule[ size ];
0/2 0x           for ( int i = 0; i < size; i++ ) {
0x               answer[i] = new Rule( this, patterns[i] );
          }
0x           return answer;
      }
 
      
      /** @return the type of node the pattern matches
        * which by default should return ANY_NODE if it can
        * match any kind of node.
        */
      public final short getMatchType() {
26x           return pattern.getMatchType();
      }
 
 
      /** For patterns which only match an ATTRIBUTE_NODE or an 
        * ELEMENT_NODE then this pattern may return the name of the
        * element or attribute it matches. This allows a more efficient
        * rule matching algorithm to be performed, rather than a brute 
        * force approach of evaluating every pattern for a given Node.
        *
        * @return the name of the element or attribute this pattern matches
        * or null if this pattern matches any or more than one name.
        */
      public final String getMatchesNodeName() {
26x           return pattern.getMatchesNodeName();
      }
      
      
      
      
      
      /** Getter for property mode.
       * @return Value of property mode.
       */
      public String getMode() {
18x           return mode;
      }
      
      /** Setter for property mode.
       * @param mode New value of property mode.
       */
      public void setMode(String mode) {
0x           this.mode = mode;
0x       }
      
      /** Getter for property importPrecedence.
       * @return Value of property importPrecedence.
       */
      public int getImportPrecedence() {
0x           return importPrecedence;
      }
      
      /** Setter for property importPrecedence.
       * @param importPrecedence New value of property importPrecedence.
       */
      public void setImportPrecedence(int importPrecedence) {
8x           this.importPrecedence = importPrecedence;
8x       }
      
      /** Getter for property priority.
       * @return Value of property priority.
       */
      public double getPriority() {
0x           return priority;
      }
      
      /** Setter for property priority.
       * @param priority New value of property priority.
       */
      public void setPriority(double priority) {
0x           this.priority = priority;
0x       }
      
      /** Getter for property appearenceCount.
       * @return Value of property appearenceCount.
       */
      public int getAppearenceCount() {
0x           return appearenceCount;
      }
      
      /** Setter for property appearenceCount.
       * @param appearenceCount New value of property appearenceCount.
       */
      public void setAppearenceCount(int appearenceCount) {
18x           this.appearenceCount = appearenceCount;
18x       }
      
      /** Getter for property pattern.
       * @return Value of property pattern.
       */
      public Pattern getPattern() {
4x           return pattern;
      }
      
      /** Setter for property pattern.
       * @param pattern New value of property pattern.
       */
      public void setPattern(Pattern pattern) {
0x           this.pattern = pattern;
0x       }
      
      /** Getter for property action.
       * @return Value of property action.
       */
      public Action getAction() {
32x           return action;
      }
      
      /** Setter for property action.
       * @param action New value of property action.
       */
      public void setAction(Action action) {
0x           this.action = action;
0x       }
      
  }
 
 
 
 
  /*
   * Redistribution and use of this software and associated documentation
   * ("Software"), with or without modification, are permitted provided
   * that the following conditions are met:
   *
   * 1. Redistributions of source code must retain copyright
   *    statements and notices.  Redistributions must also contain a
   *    copy of this document.
   *
   * 2. Redistributions in binary form must reproduce the
   *    above copyright notice, this list of conditions and the
   *    following disclaimer in the documentation and/or other
   *    materials provided with the distribution.
   *
   * 3. The name "DOM4J" must not be used to endorse or promote
   *    products derived from this Software without prior written
   *    permission of MetaStuff, Ltd.  For written permission,
   *    please contact dom4j-info@metastuff.com.
   *
   * 4. Products derived from this Software may not be called "DOM4J"
   *    nor may "DOM4J" appear in their names without prior written
   *    permission of MetaStuff, Ltd. DOM4J is a registered
   *    trademark of MetaStuff, Ltd.
   *
   * 5. Due credit should be given to the DOM4J Project - 
   *    http://www.dom4j.org
   *
   * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
   * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
   * OF THE POSSIBILITY OF SUCH DAMAGE.
   *
   * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
   *
   * $Id: Rule.java,v 1.5 2004/06/25 08:03:39 maartenc Exp $
   */