Project dom4j 1.5.2 [5/2/05 10:13 PM]
 
Coverage - org/dom4j/rule/RuleManager.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: RuleManager.java,v 1.7 2004/06/25 08:03:39 maartenc Exp $
   */
 
  package org.dom4j.rule;
 
  import java.util.HashMap;
 
  import org.dom4j.Document;
  import org.dom4j.Element;
  import org.dom4j.Node;
  import org.dom4j.rule.pattern.NodeTypePattern;
 
 
  /** <p><code>RuleManager</code> manages a set of rules such that a rule
    * can be found for a given DOM4J Node using the XSLT processing model.</p>
    *
    * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a>
    * @version $Revision: 1.7 $
    */
  public class RuleManager {
 
      /** Map of modes indexed by mode */
4x       private HashMap modes = new HashMap();
      
      /** A counter so that rules can be ordered by the order in which they 
       * were added to the rule base 
       */
      private int appearenceCount;
      
      /** Holds value of property valueOfAction. */
      private Action valueOfAction;    
      
      
4x       public RuleManager() {
4x       }
 
      /** @return the Mode instance for the given mode name. If one does not exist
       * then it will be created.
       */
      public Mode getMode( String modeName ) {
36x           Mode mode = (Mode) modes.get(modeName);
2/2 36x           if ( mode == null ) {
2x               mode = createMode();
2x               modes.put(modeName, mode);
          }
36x           return mode;
      }
      
      public void addRule(Rule rule) {
18x           rule.setAppearenceCount( ++appearenceCount );
          
18x           Mode mode = getMode( rule.getMode() );
18x           Rule[] childRules = rule.getUnionRules();
1/2 18x           if ( childRules != null ) {
0/2 0x               for ( int i = 0, size = childRules.length; i < size; i++ ) {
0x                   mode.addRule( childRules[i] );
              }
          }
          else {
18x               mode.addRule( rule );
          }
18x       }
      
      public void removeRule(Rule rule) {
0x           Mode mode = getMode( rule.getMode() );
0x           Rule[] childRules = rule.getUnionRules();
0/2 0x           if ( childRules != null ) {
0/2 0x               for ( int i = 0, size = childRules.length; i < size; i++ ) {
0x                   mode.removeRule( childRules[i] );
              }
          }
          else {
0x               mode.removeRule( rule );
          }
0x       }
 
      /** Performs an XSLT processing model match for the rule
        * which matches the given Node the best.
        *
        * @param modeName is the name of the mode associated with the rule if any
        * @param node is the DOM4J Node to match against
        * @return the matching Rule or no rule if none matched
        */
      public Rule getMatchingRule(String modeName, Node node) {
0x           Mode mode = (Mode) modes.get(modeName);
0/2 0x           if ( mode != null ) {
0x               return mode.getMatchingRule( node );
          }
          else {
0x               System.out.println( "Warning: No Mode for mode: " + mode );
0x               return null;
          }
      }
      
      public void clear() {
0x           modes.clear();
0x           appearenceCount = 0;
0x       }
 
      
      // Properties
      //-------------------------------------------------------------------------                
      
      /** @return the default value-of action which is used 
       * in the default rules for the pattern "text()|@*"
       */
      public Action getValueOfAction() {
2x           return valueOfAction;
      }
      
      /** Sets the default value-of action which is used 
       * in the default rules for the pattern "text()|@*"
       */
      public void setValueOfAction(Action valueOfAction) {
4x           this.valueOfAction = valueOfAction;
4x       }
      
      // Implementation methods
      //-------------------------------------------------------------------------                
      
      /** A factory method to return a new {@link Mode} instance
       * which should add the necessary default rules
       */
      protected Mode createMode() {
2x           Mode mode = new Mode();
2x           addDefaultRules( mode );
2x           return mode;
      }
      
      /** Adds the default stylesheet rules to the given 
       * {@link Mode} instance
       */
      protected void addDefaultRules(final Mode mode) {
          // add an apply templates rule
2x           Action applyTemplates = new Action() {
              public void run( Node node ) throws Exception {
                  if ( node instanceof Element ) {
                      mode.applyTemplates( (Element) node );
                  }
                  else if ( node instanceof Document ) {
                      mode.applyTemplates( (Document) node );
                  }
              }
          };
 
2x           Action valueOfAction = getValueOfAction();
          
2x           addDefaultRule( mode, NodeTypePattern.ANY_DOCUMENT, applyTemplates );
2x           addDefaultRule( mode, NodeTypePattern.ANY_ELEMENT, applyTemplates );
          
1/2 2x           if ( valueOfAction != null ) {
2x               addDefaultRule( mode, NodeTypePattern.ANY_ATTRIBUTE, valueOfAction );
2x               addDefaultRule( mode, NodeTypePattern.ANY_TEXT, valueOfAction );
          }
2x       }
      
      protected void addDefaultRule( Mode mode, Pattern pattern, Action action ) {
8x           Rule rule = createDefaultRule( pattern, action );
8x           mode.addRule( rule );
8x       }
      
      protected Rule createDefaultRule( Pattern pattern, Action action ) {
8x           Rule rule = new Rule( pattern, action );
8x           rule.setImportPrecedence( -1 );
8x           return rule;
      }
      
  }
 
 
 
 
  /*
   * 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: RuleManager.java,v 1.7 2004/06/25 08:03:39 maartenc Exp $
   */