KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dom4j > rule > Stylesheet


1 /*
2  * Copyright 2001-2005 (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
8 package org.dom4j.rule;
9
10 import java.util.Iterator JavaDoc;
11 import java.util.List JavaDoc;
12
13 import org.dom4j.Document;
14 import org.dom4j.Element;
15 import org.dom4j.Node;
16 import org.dom4j.XPath;
17
18 /**
19  * <p>
20  * <code>Stylesheet</code> implements an XSLT stylesheet such that rules can
21  * be added to the stylesheet and the stylesheet can be applied to a source
22  * document or node.
23  * </p>
24  *
25  * @author <a HREF="mailto:james.strachan@metastuff.com">James Strachan </a>
26  * @version $Revision: 1.14 $
27  */

28 public class Stylesheet {
29     private RuleManager ruleManager = new RuleManager();
30
31     /** Holds value of property mode. */
32     private String JavaDoc modeName;
33
34     /**
35      * Creates a new empty stylesheet.
36      */

37     public Stylesheet() {
38     }
39
40     /**
41      * Add a rule to this stylesheet.
42      *
43      * @param rule
44      * the rule to add
45      */

46     public void addRule(Rule rule) {
47         ruleManager.addRule(rule);
48     }
49
50     /**
51      * Removes the specified rule from this stylesheet.
52      *
53      * @param rule
54      * the rule to remove
55      */

56     public void removeRule(Rule rule) {
57         ruleManager.removeRule(rule);
58     }
59
60     /**
61      * Runs this stylesheet on the given input which should be either a Node or
62      * a List of Node objects.
63      *
64      * @param input
65      * the input to run this stylesheet on
66      *
67      * @throws Exception
68      * if something goes wrong
69      */

70     public void run(Object JavaDoc input) throws Exception JavaDoc {
71         run(input, this.modeName);
72     }
73
74     public void run(Object JavaDoc input, String JavaDoc mode) throws Exception JavaDoc {
75         if (input instanceof Node) {
76             run((Node) input, mode);
77         } else if (input instanceof List JavaDoc) {
78             run((List JavaDoc) input, mode);
79         }
80     }
81
82     public void run(List JavaDoc list) throws Exception JavaDoc {
83         run(list, this.modeName);
84     }
85
86     public void run(List JavaDoc list, String JavaDoc mode) throws Exception JavaDoc {
87         for (int i = 0, size = list.size(); i < size; i++) {
88             Object JavaDoc object = list.get(i);
89
90             if (object instanceof Node) {
91                 run((Node) object, mode);
92             }
93         }
94     }
95
96     public void run(Node node) throws Exception JavaDoc {
97         run(node, this.modeName);
98     }
99
100     public void run(Node node, String JavaDoc mode) throws Exception JavaDoc {
101         Mode mod = ruleManager.getMode(mode);
102         mod.fireRule(node);
103     }
104
105     /**
106      * Processes the result of the xpath expression. The xpath expression is
107      * evaluated against the provided input object.
108      *
109      * @param input
110      * the input object
111      * @param xpath
112      * the xpath expression
113      * @throws Exception
114      * if something goes wrong
115      */

116     public void applyTemplates(Object JavaDoc input, XPath xpath) throws Exception JavaDoc {
117         applyTemplates(input, xpath, this.modeName);
118     }
119
120     /**
121      * Processes the result of the xpath expression in the given mode. The xpath
122      * expression is evaluated against the provided input object.
123      *
124      * @param input
125      * the input object
126      * @param xpath
127      * the xpath expression
128      * @param mode
129      * the mode
130      * @throws Exception
131      * if something goes wrong
132      */

133     public void applyTemplates(Object JavaDoc input, XPath xpath, String JavaDoc mode)
134             throws Exception JavaDoc {
135         Mode mod = ruleManager.getMode(mode);
136
137         List JavaDoc list = xpath.selectNodes(input);
138         Iterator JavaDoc it = list.iterator();
139         while (it.hasNext()) {
140             Node current = (Node) it.next();
141             mod.fireRule(current);
142         }
143     }
144
145     /**
146      * Processes the result of the xpath expression. The xpath expression is
147      * evaluated against the provided input object.
148      *
149      * @param input
150      * the input object
151      * @param xpath
152      * the xpath expression
153      * @throws Exception
154      * if something goes wrong
155      * @deprecated Use {@link Stylesheet#applyTemplates(Object, XPath)}instead.
156      */

157     public void applyTemplates(Object JavaDoc input, org.jaxen.XPath xpath)
158             throws Exception JavaDoc {
159         applyTemplates(input, xpath, this.modeName);
160     }
161
162     /**
163      * Processes the result of the xpath expression in the given mode. The xpath
164      * expression is evaluated against the provided input object.
165      *
166      * @param input
167      * the input object
168      * @param xpath
169      * the xpath expression
170      * @param mode
171      * the mode
172      * @throws Exception
173      * if something goes wrong
174      * @deprecated Use {@link Stylesheet#applyTemplates(Object, XPath, String)}
175      * instead.
176      */

177     public void applyTemplates(Object JavaDoc input, org.jaxen.XPath xpath, String JavaDoc mode)
178             throws Exception JavaDoc {
179         Mode mod = ruleManager.getMode(mode);
180
181         List JavaDoc list = xpath.selectNodes(input);
182         Iterator JavaDoc it = list.iterator();
183         while (it.hasNext()) {
184             Node current = (Node) it.next();
185             mod.fireRule(current);
186         }
187     }
188
189     /**
190      * If input is a <code>Node</code>, this will processes all of the
191      * children of that node. If input is a <code>List</code> of
192      * <code>Nodes</code>s, these nodes will be iterated and all children of
193      * each node will be processed.
194      *
195      * @param input
196      * the input object, this can either be a <code>Node</code> or
197      * a <code>List</code>
198      * @throws Exception
199      * if something goes wrong
200      */

201     public void applyTemplates(Object JavaDoc input) throws Exception JavaDoc {
202         applyTemplates(input, this.modeName);
203     }
204
205     /**
206      * Processes the input object in the given mode. If input is a
207      * <code>Node</code>, this will processes all of the children of that
208      * node. If input is a <code>List</code> of <code>Nodes</code>s, these
209      * nodes will be iterated and all children of each node will be processed.
210      *
211      * @param input
212      * the input object, this can either be a <code>Node</code> or
213      * a <code>List</code>
214      * @param mode
215      * the mode
216      * @throws Exception
217      * if something goes wrong
218      */

219     public void applyTemplates(Object JavaDoc input, String JavaDoc mode) throws Exception JavaDoc {
220         Mode mod = ruleManager.getMode(mode);
221
222         if (input instanceof Element) {
223             // iterate through all children
224
Element element = (Element) input;
225             for (int i = 0, size = element.nodeCount(); i < size; i++) {
226                 Node node = element.node(i);
227                 mod.fireRule(node);
228             }
229         } else if (input instanceof Document) {
230             // iterate through all children
231
Document document = (Document) input;
232             for (int i = 0, size = document.nodeCount(); i < size; i++) {
233                 Node node = document.node(i);
234                 mod.fireRule(node);
235             }
236         } else if (input instanceof List JavaDoc) {
237             List JavaDoc list = (List JavaDoc) input;
238
239             for (int i = 0, size = list.size(); i < size; i++) {
240                 Object JavaDoc object = list.get(i);
241
242                 if (object instanceof Element) {
243                     applyTemplates((Element) object, mode);
244                 } else if (object instanceof Document) {
245                     applyTemplates((Document) object, mode);
246                 }
247             }
248         }
249     }
250
251     public void clear() {
252         ruleManager.clear();
253     }
254
255     // Properties
256
// -------------------------------------------------------------------------
257

258     /**
259      * DOCUMENT ME!
260      *
261      * @return the name of the mode the stylesheet uses by default
262      */

263     public String JavaDoc getModeName() {
264         return modeName;
265     }
266
267     /**
268      * Sets the name of the mode that the stylesheet uses by default.
269      *
270      * @param modeName
271      * DOCUMENT ME!
272      */

273     public void setModeName(String JavaDoc modeName) {
274         this.modeName = modeName;
275     }
276
277     /**
278      * DOCUMENT ME!
279      *
280      * @return the default value-of action which is used in the default rules
281      * for the pattern "text()|&#64;"
282      */

283     public Action getValueOfAction() {
284         return ruleManager.getValueOfAction();
285     }
286
287     /**
288      * Sets the default value-of action which is used in the default rules for
289      * the pattern "text()|&#64;"
290      *
291      * @param valueOfAction
292      * DOCUMENT ME!
293      */

294     public void setValueOfAction(Action valueOfAction) {
295         ruleManager.setValueOfAction(valueOfAction);
296     }
297 }
298
299 /*
300  * Redistribution and use of this software and associated documentation
301  * ("Software"), with or without modification, are permitted provided that the
302  * following conditions are met:
303  *
304  * 1. Redistributions of source code must retain copyright statements and
305  * notices. Redistributions must also contain a copy of this document.
306  *
307  * 2. Redistributions in binary form must reproduce the above copyright notice,
308  * this list of conditions and the following disclaimer in the documentation
309  * and/or other materials provided with the distribution.
310  *
311  * 3. The name "DOM4J" must not be used to endorse or promote products derived
312  * from this Software without prior written permission of MetaStuff, Ltd. For
313  * written permission, please contact dom4j-info@metastuff.com.
314  *
315  * 4. Products derived from this Software may not be called "DOM4J" nor may
316  * "DOM4J" appear in their names without prior written permission of MetaStuff,
317  * Ltd. DOM4J is a registered trademark of MetaStuff, Ltd.
318  *
319  * 5. Due credit should be given to the DOM4J Project - http://www.dom4j.org
320  *
321  * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS ``AS IS'' AND
322  * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
323  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
324  * ARE DISCLAIMED. IN NO EVENT SHALL METASTUFF, LTD. OR ITS CONTRIBUTORS BE
325  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
326  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
327  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
328  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
329  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
330  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
331  * POSSIBILITY OF SUCH DAMAGE.
332  *
333  * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
334  */

335
Popular Tags