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: RuleManager.java,v 1.3 2001/02/15 12:06:49 jstrachan Exp $
008 */
009
010package org.dom4j.rule;
011
012import java.util.HashMap;
013
014import org.dom4j.Attribute;
015import org.dom4j.Document;
016import org.dom4j.Element;
017import org.dom4j.Node;
018import org.dom4j.rule.pattern.NodeTypePattern;
019
020
021/** <p><code>RuleManager</code> manages a set of rules such that a rule
022  * can be found for a given DOM4J Node using the XSLT processing model.</p>
023  *
024  * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a>
025  * @version $Revision: 1.3 $
026  */
027public class RuleManager {
028
029    /** Map of modes indexed by mode */
030    private HashMap modes = new HashMap();
031    
032    /** A counter so that rules can be ordered by the order in which they 
033     * were added to the rule base 
034     */
035    private int appearenceCount;
036    
037    /** Holds value of property valueOfAction. */
038    private Action valueOfAction;    
039    
040    
041    public RuleManager() {
042    }
043
044    /** @return the Mode instance for the given mode name. If one does not exist
045     * then it will be created.
046     */
047    public Mode getMode( String modeName ) {
048        Mode mode = (Mode) modes.get(modeName);
049        if ( mode == null ) {
050            mode = createMode();
051            modes.put(modeName, mode);
052        }
053        return mode;
054    }
055    
056    public void addRule(Rule rule) {
057        rule.setAppearenceCount( ++appearenceCount );
058        
059        Mode mode = getMode( rule.getMode() );
060        Rule[] childRules = rule.getUnionRules();
061        if ( childRules != null ) {
062            for ( int i = 0, size = childRules.length; i < size; i++ ) {
063                mode.addRule( childRules[i] );
064            }
065        }
066        else {
067            mode.addRule( rule );
068        }
069    }
070    
071    public void removeRule(Rule rule) {
072        Mode mode = getMode( rule.getMode() );
073        Rule[] childRules = rule.getUnionRules();
074        if ( childRules != null ) {
075            for ( int i = 0, size = childRules.length; i < size; i++ ) {
076                mode.removeRule( childRules[i] );
077            }
078        }
079        else {
080            mode.removeRule( rule );
081        }
082    }
083
084    /** Performs an XSLT processing model match for the rule
085      * which matches the given Node the best.
086      *
087      * @param modeName is the name of the mode associated with the rule if any
088      * @param node is the DOM4J Node to match against
089      * @return the matching Rule or no rule if none matched
090      */
091    public Rule getMatchingRule(String modeName, Node node) {
092        Mode mode = (Mode) modes.get(modeName);
093        if ( mode != null ) {
094            return mode.getMatchingRule( node );
095        }
096        else {
097            System.out.println( "Warning: No Mode for mode: " + mode );
098            return null;
099        }
100    }
101    
102    public void clear() {
103        modes.clear();
104        appearenceCount = 0;
105    }
106
107    
108    // Properties
109    //-------------------------------------------------------------------------                
110    
111    /** @return the default value-of action which is used 
112     * in the default rules for the pattern "text()|@*"
113     */
114    public Action getValueOfAction() {
115        return valueOfAction;
116    }
117    
118    /** Sets the default value-of action which is used 
119     * in the default rules for the pattern "text()|@*"
120     */
121    public void setValueOfAction(Action valueOfAction) {
122        this.valueOfAction = valueOfAction;
123    }
124    
125    // Implementation methods
126    //-------------------------------------------------------------------------                
127    
128    /** A factory method to return a new {@link Mode} instance
129     * which should add the necessary default rules
130     */
131    protected Mode createMode() {
132        Mode mode = new Mode();
133        addDefaultRules( mode );
134        return mode;
135    }
136    
137    /** Adds the default stylesheet rules to the given 
138     * {@link Mode} instance
139     */
140    protected void addDefaultRules(final Mode mode) {
141        // add an apply templates rule
142        Action applyTemplates = new Action() {
143            public void run( Node node ) throws Exception {
144                if ( node instanceof Element ) {
145                    mode.applyTemplates( (Element) node );
146                }
147                else if ( node instanceof Document ) {
148                    mode.applyTemplates( (Document) node );
149                }
150            }
151        };
152
153        Action valueOfAction = getValueOfAction();
154        
155        addDefaultRule( mode, NodeTypePattern.ANY_DOCUMENT, applyTemplates );
156        addDefaultRule( mode, NodeTypePattern.ANY_ELEMENT, applyTemplates );
157        
158        if ( valueOfAction != null ) {
159            addDefaultRule( mode, NodeTypePattern.ANY_ATTRIBUTE, valueOfAction );
160            addDefaultRule( mode, NodeTypePattern.ANY_TEXT, valueOfAction );
161        }
162    }
163    
164    protected void addDefaultRule( Mode mode, Pattern pattern, Action action ) {
165        Rule rule = createDefaultRule( pattern, action );
166        mode.addRule( rule );
167    }
168    
169    protected Rule createDefaultRule( Pattern pattern, Action action ) {
170        Rule rule = new Rule( pattern, action );
171        rule.setImportPrecedence( -1 );
172        return rule;
173    }
174    
175}
176
177
178
179
180/*
181 * Redistribution and use of this software and associated documentation
182 * ("Software"), with or without modification, are permitted provided
183 * that the following conditions are met:
184 *
185 * 1. Redistributions of source code must retain copyright
186 *    statements and notices.  Redistributions must also contain a
187 *    copy of this document.
188 *
189 * 2. Redistributions in binary form must reproduce the
190 *    above copyright notice, this list of conditions and the
191 *    following disclaimer in the documentation and/or other
192 *    materials provided with the distribution.
193 *
194 * 3. The name "DOM4J" must not be used to endorse or promote
195 *    products derived from this Software without prior written
196 *    permission of MetaStuff, Ltd.  For written permission,
197 *    please contact dom4j-info@metastuff.com.
198 *
199 * 4. Products derived from this Software may not be called "DOM4J"
200 *    nor may "DOM4J" appear in their names without prior written
201 *    permission of MetaStuff, Ltd. DOM4J is a registered
202 *    trademark of MetaStuff, Ltd.
203 *
204 * 5. Due credit should be given to the DOM4J Project
205 *    (http://dom4j.org/).
206 *
207 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
208 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
209 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
210 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
211 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
212 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
213 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
214 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
215 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
216 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
217 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
218 * OF THE POSSIBILITY OF SUCH DAMAGE.
219 *
220 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
221 *
222 * $Id: RuleManager.java,v 1.3 2001/02/15 12:06:49 jstrachan Exp $
223 */