View Javadoc

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