RuleSet xref

View Javadoc

    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 java.util.ArrayList;
    11  import java.util.Collections;
    12
    13  import org.dom4j.Node;
    14
    15  /***
    16   * <p>
    17   * <code>RuleSet</code> manages a set of rules which are sorted in order of
    18   * relevance according to the XSLT defined conflict resolution policy. This
    19   * makes finding the correct rule for a DOM4J Node using the XSLT processing
    20   * model efficient as the rules can be evaluated in order of priority.
    21   * </p>
    22   *
    23   * @author <a href="mailto:james.strachan@metastuff.com">James Strachan </a>
    24   * @version $Revision: 1.10 $
    25   */
    26  public class RuleSet {
    27      /*** An unordered list of Rule objects */
    28      private ArrayList rules = new ArrayList();
    29
    30      /*** A lazily evaluated and cached array of rules sorted */
    31      private Rule[] ruleArray;
    32
    33      public RuleSet() {
    34      }
    35
    36      public String toString() {
    37          return super.toString() + " [RuleSet: " + rules + " ]";
    38      }
    39
    40      /***
    41       * Performs an XSLT processing model match for the rule which matches the
    42       * given Node the best.
    43       *
    44       * @param node
    45       *            is the DOM4J Node to match against
    46       *
    47       * @return the matching Rule or no rule if none matched
    48       */
    49      public Rule getMatchingRule(Node node) {
    50          Rule[] matches = getRuleArray();
    51
    52          for (int i = matches.length - 1; i >= 0; i--) {
    53              Rule rule = matches[i];
    54
    55              if (rule.matches(node)) {
    56                  return rule;
    57              }
    58          }
    59
    60          return null;
    61      }
    62
    63      public void addRule(Rule rule) {
    64          rules.add(rule);
    65          ruleArray = null;
    66      }
    67
    68      public void removeRule(Rule rule) {
    69          rules.remove(rule);
    70          ruleArray = null;
    71      }
    72
    73      /***
    74       * Adds all the rules to this RuleSet from the given other rule set.
    75       *
    76       * @param that
    77       *            DOCUMENT ME!
    78       */
    79      public void addAll(RuleSet that) {
    80          rules.addAll(that.rules);
    81          ruleArray = null;
    82      }
    83
    84      /***
    85       * Returns an array of sorted rules.
    86       *
    87       * @return the rules as a sorted array in ascending precendence so that the
    88       *         rules at the end of the array should be used first
    89       */
    90      protected Rule[] getRuleArray() {
    91          if (ruleArray == null) {
    92              Collections.sort(rules);
    93
    94              int size = rules.size();
    95              ruleArray = new Rule[size];
    96              rules.toArray(ruleArray);
    97          }
    98
    99          return ruleArray;
    100     }
    101 }
    102
    103 /*
    104  * Redistribution and use of this software and associated documentation
    105  * ("Software"), with or without modification, are permitted provided that the
    106  * following conditions are met:
    107  *
    108  * 1. Redistributions of source code must retain copyright statements and
    109  * notices. Redistributions must also contain a copy of this document.
    110  *
    111  * 2. Redistributions in binary form must reproduce the above copyright notice,
    112  * this list of conditions and the following disclaimer in the documentation
    113  * and/or other materials provided with the distribution.
    114  *
    115  * 3. The name "DOM4J" must not be used to endorse or promote products derived
    116  * from this Software without prior written permission of MetaStuff, Ltd. For
    117  * written permission, please contact dom4j-info@metastuff.com.
    118  *
    119  * 4. Products derived from this Software may not be called "DOM4J" nor may
    120  * "DOM4J" appear in their names without prior written permission of MetaStuff,
    121  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
    122  *
    123  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
    124  *
    125  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
    126  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    127  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    128  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
    129  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    130  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    131  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    132  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    133  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    134  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    135  * POSSIBILITY OF SUCH DAMAGE.
    136  *
    137  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
    138  */