001/* 002 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. 003 * 004 * This software is open source. 005 * See the bottom of this file for the licence. 006 * 007 * $Id: RuleSet.java,v 1.3 2001/08/17 09:45:19 jstrachan Exp $ 008 */ 009 010package org.dom4j.rule; 011 012import java.util.Collections; 013import java.util.Iterator; 014import java.util.ArrayList; 015 016import org.dom4j.Node; 017 018/** <p><code>RuleSet</code> manages a set of rules which are sorted 019 * in order of relevance according to the XSLT defined conflict 020 * resolution policy. This makes finding the correct rule for 021 * a DOM4J Node using the XSLT processing model efficient as the 022 * rules can be evaluated in order of priority.</p> 023 * 024 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a> 025 * @version $Revision: 1.3 $ 026 */ 027public class RuleSet { 028 029 /** An unordered list of Rule objects */ 030 private ArrayList rules = new ArrayList(); 031 032 /** A lazily evaluated and cached array of rules sorted */ 033 private Rule[] ruleArray; 034 035 public RuleSet() { 036 } 037 038 public String toString() { 039 return super.toString() + " [RuleSet: " + rules + " ]"; 040 } 041 042 043 /** Performs an XSLT processing model match for the rule 044 * which matches the given Node the best. 045 * 046 * @param mode is the mode associated with the rule if any 047 * @param node is the DOM4J Node to match against 048 * @return the matching Rule or no rule if none matched 049 */ 050 public Rule getMatchingRule( Node node ) { 051 Rule[] rules = getRuleArray(); 052 for ( int i = rules.length - 1; i >= 0; i-- ) { 053 Rule rule = rules[i]; 054 if ( rule.matches( node ) ) { 055 return rule; 056 } 057 } 058 return null; 059 } 060 061 public void addRule(Rule rule) { 062 rules.add( rule ); 063 ruleArray = null; 064 } 065 066 public void removeRule(Rule rule) { 067 rules.remove( rule ); 068 ruleArray = null; 069 } 070 071 /** Adds all the rules to this RuleSet from the given other rule set. 072 */ 073 public void addAll(RuleSet that) { 074 rules.addAll( that.rules ); 075 ruleArray = null; 076 } 077 078 /** Returns an array of sorted rules. 079 * 080 * @return the rules as a sorted array in ascending precendence 081 * so that the rules at the end of the array should be used first 082 */ 083 protected Rule[] getRuleArray() { 084 if ( ruleArray == null ) { 085 Collections.sort( rules ); 086 int size = rules.size(); 087 ruleArray = new Rule[ size ]; 088 rules.toArray( ruleArray ); 089 } 090 return ruleArray; 091 } 092 093} 094 095 096 097 098/* 099 * Redistribution and use of this software and associated documentation 100 * ("Software"), with or without modification, are permitted provided 101 * that the following conditions are met: 102 * 103 * 1. Redistributions of source code must retain copyright 104 * statements and notices. Redistributions must also contain a 105 * copy of this document. 106 * 107 * 2. Redistributions in binary form must reproduce the 108 * above copyright notice, this list of conditions and the 109 * following disclaimer in the documentation and/or other 110 * materials provided with the distribution. 111 * 112 * 3. The name "DOM4J" must not be used to endorse or promote 113 * products derived from this Software without prior written 114 * permission of MetaStuff, Ltd. For written permission, 115 * please contact dom4j-info@metastuff.com. 116 * 117 * 4. Products derived from this Software may not be called "DOM4J" 118 * nor may "DOM4J" appear in their names without prior written 119 * permission of MetaStuff, Ltd. DOM4J is a registered 120 * trademark of MetaStuff, Ltd. 121 * 122 * 5. Due credit should be given to the DOM4J Project 123 * (http://dom4j.org/). 124 * 125 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS 126 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 127 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 128 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 129 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 130 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 131 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 132 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 133 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 134 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 135 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 136 * OF THE POSSIBILITY OF SUCH DAMAGE. 137 * 138 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. 139 * 140 * $Id: RuleSet.java,v 1.3 2001/08/17 09:45:19 jstrachan Exp $ 141 */