| /* | ||
| * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved. | ||
| * | ||
| * This software is open source. | ||
| * See the bottom of this file for the licence. | ||
| * | ||
| * $Id: Mode.java,v 1.7 2004/06/25 08:03:39 maartenc Exp $ | ||
| */ | ||
| package org.dom4j.rule; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
| import org.dom4j.Attribute; | ||
| import org.dom4j.Document; | ||
| import org.dom4j.Element; | ||
| import org.dom4j.Node; | ||
| /** <p><code>Mode</code> manages a number of RuleSet instances | ||
| * for the mode in a stylesheet. | ||
| * It is responsible for finding the correct rule for a given DOM4J Node | ||
| * using the XSLT processing model uses the smallest possible RuleSet to | ||
| * reduce the number of Rule evaluations.</p> | ||
| * | ||
| * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a> | ||
| * @version $Revision: 1.7 $ | ||
| */ | ||
| public class Mode { | ||
| 2x | private RuleSet[] ruleSets = new RuleSet[ Pattern.NUMBER_OF_TYPES ]; | |
| /** Map of exact (local) element names to RuleSet instances */ | ||
| private Map elementNameRuleSets; | ||
| /** Map of exact (local) attribute names to RuleSet instances */ | ||
| private Map attributeNameRuleSets; | ||
| 2x | public Mode() { | |
| 2x | } | |
| /** Runs the actions associated with the given node | ||
| */ | ||
| public void fireRule( Node node ) throws Exception { | ||
| 1/2 28x | if ( node != null ) { | |
| 28x | Rule rule = getMatchingRule( node ); | |
| 1/2 28x | if ( rule != null ) { | |
| 28x | Action action = rule.getAction(); | |
| 1/2 28x | if ( action != null ) { | |
| 28x | action.run( node ); | |
| } | ||
| } | ||
| } | ||
| 28x | } | |
| public void applyTemplates( Element element ) throws Exception { | ||
| 2/2 18x | for ( int i = 0, size = element.attributeCount(); i < size; i++ ) { | |
| 8x | Attribute attribute = element.attribute(i); | |
| 8x | fireRule( attribute ); | |
| } | ||
| 2/2 26x | for ( int i = 0, size = element.nodeCount(); i < size; i++ ) { | |
| 16x | Node node = element.node(i); | |
| 16x | fireRule( node ); | |
| } | ||
| 10x | } | |
| public void applyTemplates( Document document ) throws Exception { | ||
| 2/2 4x | for ( int i = 0, size = document.nodeCount(); i < size; i++ ) { | |
| 2x | Node node = document.node(i); | |
| 2x | fireRule( node ); | |
| } | ||
| 2x | } | |
| public void addRule(Rule rule) { | ||
| 26x | int matchType = rule.getMatchType(); | |
| 26x | String name = rule.getMatchesNodeName(); | |
| 1/2 26x | if ( name != null ) { | |
| 0/2 0x | if ( matchType == Node.ELEMENT_NODE ) { | |
| 0x | elementNameRuleSets = addToNameMap( | |
| elementNameRuleSets, name, rule | ||
| ); | ||
| } | ||
| 0/2 0x | else if ( matchType == Node.ATTRIBUTE_NODE ) { | |
| 0x | attributeNameRuleSets = addToNameMap( | |
| attributeNameRuleSets, name, rule | ||
| ); | ||
| } | ||
| } | ||
| 1/2 26x | if ( matchType >= Pattern.NUMBER_OF_TYPES ) { | |
| 0x | matchType = Pattern.ANY_NODE; | |
| } | ||
| 1/2 26x | if ( matchType == Pattern.ANY_NODE ) { | |
| // add rule to all other RuleSets if they exist | ||
| 0/2 0x | for ( int i = 1, size = ruleSets.length; i < size; i++ ) { | |
| 0x | RuleSet ruleSet = ruleSets[i]; | |
| 0/2 0x | if ( ruleSet != null ) { | |
| 0x | ruleSet.addRule( rule ); | |
| } | ||
| } | ||
| } | ||
| 26x | getRuleSet( matchType ).addRule( rule ); | |
| 26x | } | |
| public void removeRule(Rule rule) { | ||
| 0x | int matchType = rule.getMatchType(); | |
| 0x | String name = rule.getMatchesNodeName(); | |
| 0/2 0x | if ( name != null ) { | |
| 0/2 0x | if ( matchType == Node.ELEMENT_NODE ) { | |
| 0x | removeFromNameMap( elementNameRuleSets, name, rule ); | |
| } | ||
| 0/2 0x | else if ( matchType == Node.ATTRIBUTE_NODE ) { | |
| 0x | removeFromNameMap( attributeNameRuleSets, name, rule ); | |
| } | ||
| } | ||
| 0/2 0x | if ( matchType >= Pattern.NUMBER_OF_TYPES ) { | |
| 0x | matchType = Pattern.ANY_NODE; | |
| } | ||
| 0x | getRuleSet( matchType ).removeRule( rule ); | |
| 0/2 0x | if ( matchType != Pattern.ANY_NODE ) { | |
| 0x | getRuleSet( Pattern.ANY_NODE ).removeRule( rule ); | |
| } | ||
| 0x | } | |
| /** Performs an XSLT processing model match for the rule | ||
| * which matches the given Node the best. | ||
| * | ||
| * @param node is the DOM4J Node to match against | ||
| * @return the matching Rule or no rule if none matched | ||
| */ | ||
| public Rule getMatchingRule(Node node) { | ||
| 28x | int matchType = node.getNodeType(); | |
| 2/2 28x | if ( matchType == Node.ELEMENT_NODE ) { | |
| 1/2 10x | if ( elementNameRuleSets != null ) { | |
| 0x | String name = node.getName(); | |
| 0x | RuleSet ruleSet = (RuleSet) elementNameRuleSets.get( name ); | |
| 0/2 0x | if ( ruleSet != null ) { | |
| 0x | Rule answer = ruleSet.getMatchingRule( node ); | |
| 0/2 0x | if ( answer != null ) { | |
| 0x | return answer; | |
| } | ||
| } | ||
| } | ||
| } | ||
| 2/2 18x | else if ( matchType == Node.ATTRIBUTE_NODE ) { | |
| 1/2 8x | if ( attributeNameRuleSets != null ) { | |
| 0x | String name = node.getName(); | |
| 0x | RuleSet ruleSet = (RuleSet) attributeNameRuleSets.get( name ); | |
| 0/2 0x | if ( ruleSet != null ) { | |
| 0x | Rule answer = ruleSet.getMatchingRule( node ); | |
| 0/2 0x | if ( answer != null ) { | |
| 0x | return answer; | |
| } | ||
| } | ||
| } | ||
| } | ||
| 2/4 28x | if ( matchType < 0 || matchType >= ruleSets.length ) { | |
| 0x | matchType = Pattern.ANY_NODE; | |
| } | ||
| 28x | Rule answer = null; | |
| 28x | RuleSet ruleSet = ruleSets[ matchType ]; | |
| 1/2 28x | if ( ruleSet != null ) { | |
| // try rules that match this kind of node first | ||
| 28x | answer = ruleSet.getMatchingRule( node ); | |
| } | ||
| 1/4 28x | if ( answer == null && matchType != Pattern.ANY_NODE ) { | |
| // try general rules that match any kind of node | ||
| 0x | ruleSet = ruleSets[ Pattern.ANY_NODE ]; | |
| 0/2 0x | if ( ruleSet != null ) { | |
| 0x | answer = ruleSet.getMatchingRule( node ); | |
| } | ||
| } | ||
| 28x | return answer; | |
| } | ||
| /** @return the RuleSet for the given matching type. | ||
| * This method will never return null, a new instance will be created. | ||
| */ | ||
| protected RuleSet getRuleSet( int matchType ) { | ||
| 26x | RuleSet ruleSet = ruleSets[ matchType ]; | |
| 2/2 26x | if ( ruleSet == null ) { | |
| 8x | ruleSet = new RuleSet(); | |
| 8x | ruleSets[ matchType ] = ruleSet; | |
| // add the patterns that match any node | ||
| 1/2 8x | if ( matchType != Pattern.ANY_NODE ) { | |
| 8x | RuleSet allRules = ruleSets[ Pattern.ANY_NODE ]; | |
| 1/2 8x | if ( allRules != null ) { | |
| 0x | ruleSet.addAll( allRules ); | |
| } | ||
| } | ||
| } | ||
| 26x | return ruleSet; | |
| } | ||
| /** Adds the Rule to a RuleSet for the given name. | ||
| * @return the Map (which will be created if the given map was null | ||
| */ | ||
| protected Map addToNameMap( Map map, String name, Rule rule ) { | ||
| 0/2 0x | if ( map == null ) { | |
| 0x | map = new HashMap(); | |
| } | ||
| 0x | RuleSet ruleSet = (RuleSet) map.get( name ); | |
| 0/2 0x | if ( ruleSet == null ) { | |
| 0x | ruleSet = new RuleSet(); | |
| 0x | map.put( name, ruleSet ); | |
| } | ||
| 0x | ruleSet.addRule( rule ); | |
| 0x | return map; | |
| } | ||
| protected void removeFromNameMap( Map map, String name, Rule rule ) { | ||
| 0/2 0x | if ( map != null ) { | |
| 0x | RuleSet ruleSet = (RuleSet) map.get( name ); | |
| 0/2 0x | if ( ruleSet != null ) { | |
| 0x | ruleSet.removeRule( rule ); | |
| } | ||
| } | ||
| 0x | } | |
| } | ||
| /* | ||
| * 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: Mode.java,v 1.7 2004/06/25 08:03:39 maartenc Exp $ | ||
| */ |