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: Stylesheet.java,v 1.4 2001/08/09 08:22:59 jstrachan Exp $
008 */
009
010package org.dom4j.rule;
011
012import java.util.Iterator;
013import java.util.List;
014
015import org.dom4j.Attribute;
016import org.dom4j.Branch;
017import org.dom4j.Document;
018import org.dom4j.Element;
019import org.dom4j.Node;
020import org.dom4j.XPath;
021
022
023/** <p><code>Stylesheet</code> implements an XSLT stylesheet
024  * such that rules can be added to the stylesheet and the 
025  * stylesheet can be applied to a source document or node.</p>
026  *
027  * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a>
028  * @version $Revision: 1.4 $
029  */
030public class Stylesheet {
031
032    private RuleManager ruleManager = new RuleManager();
033    
034    /** Holds value of property mode. */
035    private String modeName;    
036
037    
038    public Stylesheet() {
039    }
040
041    public void addRule( Rule rule ) {
042        ruleManager.addRule( rule );
043    }
044    
045    public void removeRule( Rule rule ) {
046        ruleManager.addRule( rule );
047    }
048
049    /** Runs this stylesheet on the given input which should be 
050      * either a Node or a List of Node objects.
051      */
052    public void run( Object input ) throws Exception {
053        if ( input instanceof Node ) {
054            run ( (Node) input );
055        }
056        else if ( input instanceof List ) {
057            run( (List) input );
058        }
059    }
060    
061    public void run( List list ) throws Exception {
062        for ( int i = 0, size = list.size(); i < size; i++ ) {
063            Object object = list.get(i);
064            if ( object instanceof Node ) {
065                run( (Node) object );
066            }
067        }
068    }
069    
070    public void run( Node node ) throws Exception {
071        Mode mode = getMode();
072        if ( mode != null ) {
073            mode.fireRule( node );
074        }
075    }
076    
077    
078    public void applyTemplates( Object input, XPath xpath ) throws Exception {
079        List list = xpath.selectNodes( input );
080        for ( int i = 0, size = list.size(); i < size; i++ ) {
081            Object object = list.get(i);
082            if ( object != input && object instanceof Node ) {
083                run( (Node) object );
084            }
085        }
086    }
087    
088    public void applyTemplates( Object input ) throws Exception {
089        // iterate through all children
090        Mode mode = getMode();
091        if ( mode != null ) {
092            if ( input instanceof Element ) {
093                mode.applyTemplates( (Element) input );
094            }
095            else if ( input instanceof Document ) { 
096                mode.applyTemplates( (Document) input );
097            }
098            else if ( input instanceof List ) {
099                List list = (List) input;
100                for ( int i = 0, size = list.size(); i < size; i++ ) {
101                    Object object = list.get(i);
102                    if ( object != input ) {
103                        if ( object instanceof Element ) {
104                            mode.applyTemplates( (Element) object );
105                        }
106                        else if ( object instanceof Document ) { 
107                            mode.applyTemplates( (Document) object );
108                        }
109                    }
110                }
111            }
112        }
113    }
114
115    public void clear() {
116        ruleManager.clear();
117    }
118
119    
120    // Properties
121    //-------------------------------------------------------------------------                
122    
123    /** @return the name of the mode the stylesheet uses by default
124      */
125    public String getModeName() {
126        return modeName;
127    }
128    
129    /** Sets the name of the mode that the stylesheet uses by default.
130      */
131    public void setModeName(String modeName) {
132        this.modeName = modeName;
133    }
134    
135    /** @return the default value-of action which is used 
136     * in the default rules for the pattern "text()|@*"
137     */
138    public Action getValueOfAction() {
139        return ruleManager.getValueOfAction();
140    }
141    
142    /** Sets the default value-of action which is used 
143     * in the default rules for the pattern "text()|@*"
144     */
145    public void setValueOfAction(Action valueOfAction) {
146        ruleManager.setValueOfAction( valueOfAction );
147    }
148    
149
150    // Implementation methods
151    //------------------------------------------------------------------------- 
152    protected Mode getMode() {
153        return ruleManager.getMode( modeName );
154    }
155    
156    
157}
158
159
160
161
162/*
163 * Redistribution and use of this software and associated documentation
164 * ("Software"), with or without modification, are permitted provided
165 * that the following conditions are met:
166 *
167 * 1. Redistributions of source code must retain copyright
168 *    statements and notices.  Redistributions must also contain a
169 *    copy of this document.
170 *
171 * 2. Redistributions in binary form must reproduce the
172 *    above copyright notice, this list of conditions and the
173 *    following disclaimer in the documentation and/or other
174 *    materials provided with the distribution.
175 *
176 * 3. The name "DOM4J" must not be used to endorse or promote
177 *    products derived from this Software without prior written
178 *    permission of MetaStuff, Ltd.  For written permission,
179 *    please contact dom4j-info@metastuff.com.
180 *
181 * 4. Products derived from this Software may not be called "DOM4J"
182 *    nor may "DOM4J" appear in their names without prior written
183 *    permission of MetaStuff, Ltd. DOM4J is a registered
184 *    trademark of MetaStuff, Ltd.
185 *
186 * 5. Due credit should be given to the DOM4J Project
187 *    (http://dom4j.org/).
188 *
189 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
190 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
191 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
192 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
193 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
194 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
195 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
196 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
197 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
198 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
199 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
200 * OF THE POSSIBILITY OF SUCH DAMAGE.
201 *
202 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved.
203 *
204 * $Id: Stylesheet.java,v 1.4 2001/08/09 08:22:59 jstrachan Exp $
205 */