Clover coverage report - dom4j - 1.5
Coverage timestamp: vr sep 3 2004 20:47:03 GMT+01:00
file stats: LOC: 222   Methods: 13
NCLOC: 99   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
RuleManager.java 22,2% 58,7% 69,2% 51,9%
coverage coverage
 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: RuleManager.java,v 1.7 2004/06/25 08:03:39 maartenc Exp $
 8    */
 9   
 10    package org.dom4j.rule;
 11   
 12    import java.util.HashMap;
 13   
 14    import org.dom4j.Document;
 15    import org.dom4j.Element;
 16    import org.dom4j.Node;
 17    import org.dom4j.rule.pattern.NodeTypePattern;
 18   
 19   
 20    /** <p><code>RuleManager</code> manages a set of rules such that a rule
 21    * can be found for a given DOM4J Node using the XSLT processing model.</p>
 22    *
 23    * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a>
 24    * @version $Revision: 1.7 $
 25    */
 26    public class RuleManager {
 27   
 28    /** Map of modes indexed by mode */
 29    private HashMap modes = new HashMap();
 30   
 31    /** A counter so that rules can be ordered by the order in which they
 32    * were added to the rule base
 33    */
 34    private int appearenceCount;
 35   
 36    /** Holds value of property valueOfAction. */
 37    private Action valueOfAction;
 38   
 39   
 40  4 public RuleManager() {
 41    }
 42   
 43    /** @return the Mode instance for the given mode name. If one does not exist
 44    * then it will be created.
 45    */
 46  58 public Mode getMode( String modeName ) {
 47  58 Mode mode = (Mode) modes.get(modeName);
 48  58 if ( mode == null ) {
 49  4 mode = createMode();
 50  4 modes.put(modeName, mode);
 51    }
 52  58 return mode;
 53    }
 54   
 55  26 public void addRule(Rule rule) {
 56  26 rule.setAppearenceCount( ++appearenceCount );
 57   
 58  26 Mode mode = getMode( rule.getMode() );
 59  26 Rule[] childRules = rule.getUnionRules();
 60  26 if ( childRules != null ) {
 61  0 for ( int i = 0, size = childRules.length; i < size; i++ ) {
 62  0 mode.addRule( childRules[i] );
 63    }
 64    }
 65    else {
 66  26 mode.addRule( rule );
 67    }
 68    }
 69   
 70  0 public void removeRule(Rule rule) {
 71  0 Mode mode = getMode( rule.getMode() );
 72  0 Rule[] childRules = rule.getUnionRules();
 73  0 if ( childRules != null ) {
 74  0 for ( int i = 0, size = childRules.length; i < size; i++ ) {
 75  0 mode.removeRule( childRules[i] );
 76    }
 77    }
 78    else {
 79  0 mode.removeRule( rule );
 80    }
 81    }
 82   
 83    /** Performs an XSLT processing model match for the rule
 84    * which matches the given Node the best.
 85    *
 86    * @param modeName is the name of the mode associated with the rule if any
 87    * @param node is the DOM4J Node to match against
 88    * @return the matching Rule or no rule if none matched
 89    */
 90  0 public Rule getMatchingRule(String modeName, Node node) {
 91  0 Mode mode = (Mode) modes.get(modeName);
 92  0 if ( mode != null ) {
 93  0 return mode.getMatchingRule( node );
 94    }
 95    else {
 96  0 System.out.println( "Warning: No Mode for mode: " + mode );
 97  0 return null;
 98    }
 99    }
 100   
 101  0 public void clear() {
 102  0 modes.clear();
 103  0 appearenceCount = 0;
 104    }
 105   
 106   
 107    // Properties
 108    //-------------------------------------------------------------------------
 109   
 110    /** @return the default value-of action which is used
 111    * in the default rules for the pattern "text()|@*"
 112    */
 113  4 public Action getValueOfAction() {
 114  4 return valueOfAction;
 115    }
 116   
 117    /** Sets the default value-of action which is used
 118    * in the default rules for the pattern "text()|@*"
 119    */
 120  4 public void setValueOfAction(Action valueOfAction) {
 121  4 this.valueOfAction = valueOfAction;
 122    }
 123   
 124    // Implementation methods
 125    //-------------------------------------------------------------------------
 126   
 127    /** A factory method to return a new {@link Mode} instance
 128    * which should add the necessary default rules
 129    */
 130  4 protected Mode createMode() {
 131  4 Mode mode = new Mode();
 132  4 addDefaultRules( mode );
 133  4 return mode;
 134    }
 135   
 136    /** Adds the default stylesheet rules to the given
 137    * {@link Mode} instance
 138    */
 139  4 protected void addDefaultRules(final Mode mode) {
 140    // add an apply templates rule
 141  4 Action applyTemplates = new Action() {
 142  0 public void run( Node node ) throws Exception {
 143  0 if ( node instanceof Element ) {
 144  0 mode.applyTemplates( (Element) node );
 145    }
 146  0 else if ( node instanceof Document ) {
 147  0 mode.applyTemplates( (Document) node );
 148    }
 149    }
 150    };
 151   
 152  4 Action valueOfAction = getValueOfAction();
 153   
 154  4 addDefaultRule( mode, NodeTypePattern.ANY_DOCUMENT, applyTemplates );
 155  4 addDefaultRule( mode, NodeTypePattern.ANY_ELEMENT, applyTemplates );
 156   
 157  4 if ( valueOfAction != null ) {
 158  4 addDefaultRule( mode, NodeTypePattern.ANY_ATTRIBUTE, valueOfAction );
 159  4 addDefaultRule( mode, NodeTypePattern.ANY_TEXT, valueOfAction );
 160    }
 161    }
 162   
 163  16 protected void addDefaultRule( Mode mode, Pattern pattern, Action action ) {
 164  16 Rule rule = createDefaultRule( pattern, action );
 165  16 mode.addRule( rule );
 166    }
 167   
 168  16 protected Rule createDefaultRule( Pattern pattern, Action action ) {
 169  16 Rule rule = new Rule( pattern, action );
 170  16 rule.setImportPrecedence( -1 );
 171  16 return rule;
 172    }
 173   
 174    }
 175   
 176   
 177   
 178   
 179    /*
 180    * Redistribution and use of this software and associated documentation
 181    * ("Software"), with or without modification, are permitted provided
 182    * that the following conditions are met:
 183    *
 184    * 1. Redistributions of source code must retain copyright
 185    * statements and notices. Redistributions must also contain a
 186    * copy of this document.
 187    *
 188    * 2. Redistributions in binary form must reproduce the
 189    * above copyright notice, this list of conditions and the
 190    * following disclaimer in the documentation and/or other
 191    * materials provided with the distribution.
 192    *
 193    * 3. The name "DOM4J" must not be used to endorse or promote
 194    * products derived from this Software without prior written
 195    * permission of MetaStuff, Ltd. For written permission,
 196    * please contact dom4j-info@metastuff.com.
 197    *
 198    * 4. Products derived from this Software may not be called "DOM4J"
 199    * nor may "DOM4J" appear in their names without prior written
 200    * permission of MetaStuff, Ltd. DOM4J is a registered
 201    * trademark of MetaStuff, Ltd.
 202    *
 203    * 5. Due credit should be given to the DOM4J Project -
 204    * http://www.dom4j.org
 205    *
 206    * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
 207    * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
 208    * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
 209    * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 210    * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
 211    * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 212    * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 213    * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 214    * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 215    * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 216    * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 217    * OF THE POSSIBILITY OF SUCH DAMAGE.
 218    *
 219    * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
 220    *
 221    * $Id: RuleManager.java,v 1.7 2004/06/25 08:03:39 maartenc Exp $
 222    */