View Javadoc

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: Stylesheet.java,v 1.11 2004/08/22 12:16:33 maartenc Exp $
8    */
9   
10  package org.dom4j.rule;
11  
12  import java.util.List;
13  
14  import org.dom4j.Document;
15  import org.dom4j.Element;
16  import org.dom4j.Node;
17  import org.dom4j.XPath;
18  
19  
20  /*** <p><code>Stylesheet</code> implements an XSLT stylesheet
21    * such that rules can be added to the stylesheet and the 
22    * stylesheet can be applied to a source document or node.</p>
23    *
24    * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a>
25    * @version $Revision: 1.11 $
26    */
27  public class Stylesheet {
28  
29      private RuleManager ruleManager = new RuleManager();
30      
31      /*** Holds value of property mode. */
32      private String modeName;    
33  
34      
35      public Stylesheet() {
36      }
37  
38      public void addRule( Rule rule ) {
39          ruleManager.addRule( rule );
40      }
41      
42      public void removeRule( Rule rule ) {
43          ruleManager.removeRule( rule );
44      }
45  
46      /*** Runs this stylesheet on the given input which should be 
47        * either a Node or a List of Node objects.
48        */
49      public void run(Object input) throws Exception {
50          run(input, this.modeName);
51      }
52      
53      public void run(Object input, String mode) throws Exception {
54          if (input instanceof Node) {
55              run ((Node) input, mode);
56          }
57          else if (input instanceof List) {
58              run((List) input, mode);
59          }
60      }
61      
62      public void run(List list) throws Exception {
63          run(list, this.modeName);
64      }
65      
66      public void run(List list, String mode) throws Exception {
67          for (int i = 0, size = list.size(); i < size; i++) {
68              Object object = list.get(i);
69              if (object instanceof Node) {
70                  run((Node) object, mode);
71              }
72          }
73      }
74      
75      public void run(Node node) throws Exception {
76          run(node, this.modeName);
77      }
78      
79      public void run(Node node, String mode) throws Exception {
80          Mode mod = ruleManager.getMode(mode);
81          mod.fireRule(node);
82      }
83      
84      
85      public void applyTemplates(Object input, XPath xpath) throws Exception {
86          applyTemplates(input, xpath, this.modeName);
87      }
88      
89      public void applyTemplates(Object input, XPath xpath, String mode) throws Exception {
90          List list = xpath.selectNodes(input);
91          list.remove(input);
92          applyTemplates(list, mode);
93  //        for ( int i = 0, size = list.size(); i < size; i++ ) {
94  //            Object object = list.get(i);
95  //            if ( object != input && object instanceof Node ) {
96  //                run( (Node) object );
97  //            }
98  //        }
99      }
100     
101     public void applyTemplates(Object input, org.jaxen.XPath xpath) throws Exception {
102         applyTemplates(input, xpath, this.modeName);
103     }
104     
105     public void applyTemplates(Object input, org.jaxen.XPath xpath, String mode) throws Exception {
106         List list = xpath.selectNodes(input);
107         applyTemplates(list, mode);
108 //        for ( int i = 0, size = list.size(); i < size; i++ ) {
109 //            Object object = list.get(i);
110 //            if ( object != input && object instanceof Node ) {
111 //                run( (Node) object );
112 //            }
113 //        }
114     }
115     
116     public void applyTemplates(Object input) throws Exception {
117         applyTemplates(input, this.modeName);
118     }
119     
120     public void applyTemplates(Object input, String mode) throws Exception {
121         // iterate through all children
122         Mode mod = ruleManager.getMode(mode);
123 
124         if ( input instanceof Element ) {
125             mod.applyTemplates( (Element) input );
126         }
127         else if ( input instanceof Document ) { 
128             mod.applyTemplates( (Document) input );
129         }
130         else if ( input instanceof List ) {
131             List list = (List) input;
132             for ( int i = 0, size = list.size(); i < size; i++ ) {
133                 Object object = list.get(i);
134                 if ( object != input ) {
135                     if ( object instanceof Element ) {
136                         mod.applyTemplates( (Element) object );
137                     }
138                     else if ( object instanceof Document ) { 
139                         mod.applyTemplates( (Document) object );
140                     }
141                 }
142             }
143         }
144     }
145 
146     public void clear() {
147         ruleManager.clear();
148     }
149 
150     
151     // Properties
152     //-------------------------------------------------------------------------                
153     
154     /*** @return the name of the mode the stylesheet uses by default
155       */
156     public String getModeName() {
157         return modeName;
158     }
159     
160     /*** Sets the name of the mode that the stylesheet uses by default.
161       */
162     public void setModeName(String modeName) {
163         this.modeName = modeName;
164     }
165     
166     /*** @return the default value-of action which is used 
167      * in the default rules for the pattern "text()|@*"
168      */
169     public Action getValueOfAction() {
170         return ruleManager.getValueOfAction();
171     }
172     
173     /*** Sets the default value-of action which is used 
174      * in the default rules for the pattern "text()|@*"
175      */
176     public void setValueOfAction(Action valueOfAction) {
177         ruleManager.setValueOfAction( valueOfAction );
178     }
179     
180 }
181 
182 
183 
184 
185 /*
186  * Redistribution and use of this software and associated documentation
187  * ("Software"), with or without modification, are permitted provided
188  * that the following conditions are met:
189  *
190  * 1. Redistributions of source code must retain copyright
191  *    statements and notices.  Redistributions must also contain a
192  *    copy of this document.
193  *
194  * 2. Redistributions in binary form must reproduce the
195  *    above copyright notice, this list of conditions and the
196  *    following disclaimer in the documentation and/or other
197  *    materials provided with the distribution.
198  *
199  * 3. The name "DOM4J" must not be used to endorse or promote
200  *    products derived from this Software without prior written
201  *    permission of MetaStuff, Ltd.  For written permission,
202  *    please contact dom4j-info@metastuff.com.
203  *
204  * 4. Products derived from this Software may not be called "DOM4J"
205  *    nor may "DOM4J" appear in their names without prior written
206  *    permission of MetaStuff, Ltd. DOM4J is a registered
207  *    trademark of MetaStuff, Ltd.
208  *
209  * 5. Due credit should be given to the DOM4J Project - 
210  *    http://www.dom4j.org
211  *
212  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS
213  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
214  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
215  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
216  * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
217  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
218  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
219  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
220  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
221  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
222  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
223  * OF THE POSSIBILITY OF SUCH DAMAGE.
224  *
225  * Copyright 2001-2004 (C) MetaStuff, Ltd. All Rights Reserved.
226  *
227  * $Id: Stylesheet.java,v 1.11 2004/08/22 12:16:33 maartenc Exp $
228  */