|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
Stylesheet.java | 32,1% | 44,4% | 45% | 41,2% |
|
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; | |
11 | import java.util.List; | |
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 modeName; | |
33 | ||
34 | /** | |
35 | * Creates a new empty stylesheet. | |
36 | */ | |
37 | 5 | public Stylesheet() { |
38 | } | |
39 | ||
40 | /** | |
41 | * Add a rule to this stylesheet. | |
42 | * | |
43 | * @param rule | |
44 | * the rule to add | |
45 | */ | |
46 | 14 | public void addRule(Rule rule) { |
47 | 14 | 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 | 0 | public void removeRule(Rule rule) { |
57 | 0 | 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 | 0 | public void run(Object input) throws Exception { |
71 | 0 | run(input, this.modeName); |
72 | } | |
73 | ||
74 | 0 | public void run(Object input, String mode) throws Exception { |
75 | 0 | if (input instanceof Node) { |
76 | 0 | run((Node) input, mode); |
77 | 0 | } else if (input instanceof List) { |
78 | 0 | run((List) input, mode); |
79 | } | |
80 | } | |
81 | ||
82 | 0 | public void run(List list) throws Exception { |
83 | 0 | run(list, this.modeName); |
84 | } | |
85 | ||
86 | 0 | public void run(List list, String mode) throws Exception { |
87 | 0 | for (int i = 0, size = list.size(); i < size; i++) { |
88 | 0 | Object object = list.get(i); |
89 | ||
90 | 0 | if (object instanceof Node) { |
91 | 0 | run((Node) object, mode); |
92 | } | |
93 | } | |
94 | } | |
95 | ||
96 | 2 | public void run(Node node) throws Exception { |
97 | 2 | run(node, this.modeName); |
98 | } | |
99 | ||
100 | 2 | public void run(Node node, String mode) throws Exception { |
101 | 2 | Mode mod = ruleManager.getMode(mode); |
102 | 2 | 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 | 1 | public void applyTemplates(Object input, XPath xpath) throws Exception { |
117 | 1 | 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 | 1 | public void applyTemplates(Object input, XPath xpath, String mode) |
134 | throws Exception { | |
135 | 1 | Mode mod = ruleManager.getMode(mode); |
136 | ||
137 | 1 | List list = xpath.selectNodes(input); |
138 | 1 | Iterator it = list.iterator(); |
139 | 1 | while (it.hasNext()) { |
140 | 2 | Node current = (Node) it.next(); |
141 | 2 | 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 | 0 | public void applyTemplates(Object input, org.jaxen.XPath xpath) |
158 | throws Exception { | |
159 | 0 | 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 | 0 | public void applyTemplates(Object input, org.jaxen.XPath xpath, String mode) |
178 | throws Exception { | |
179 | 0 | Mode mod = ruleManager.getMode(mode); |
180 | ||
181 | 0 | List list = xpath.selectNodes(input); |
182 | 0 | Iterator it = list.iterator(); |
183 | 0 | while (it.hasNext()) { |
184 | 0 | Node current = (Node) it.next(); |
185 | 0 | 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 | 8 | public void applyTemplates(Object input) throws Exception { |
202 | 8 | 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 | 8 | public void applyTemplates(Object input, String mode) throws Exception { |
220 | 8 | Mode mod = ruleManager.getMode(mode); |
221 | ||
222 | 8 | if (input instanceof Element) { |
223 | // iterate through all children | |
224 | 7 | Element element = (Element) input; |
225 | 7 | for (int i = 0, size = element.nodeCount(); i < size; i++) { |
226 | 10 | Node node = element.node(i); |
227 | 10 | mod.fireRule(node); |
228 | } | |
229 | 1 | } else if (input instanceof Document) { |
230 | // iterate through all children | |
231 | 1 | Document document = (Document) input; |
232 | 1 | for (int i = 0, size = document.nodeCount(); i < size; i++) { |
233 | 1 | Node node = document.node(i); |
234 | 1 | mod.fireRule(node); |
235 | } | |
236 | 0 | } else if (input instanceof List) { |
237 | 0 | List list = (List) input; |
238 | ||
239 | 0 | for (int i = 0, size = list.size(); i < size; i++) { |
240 | 0 | Object object = list.get(i); |
241 | ||
242 | 0 | if (object instanceof Element) { |
243 | 0 | applyTemplates((Element) object, mode); |
244 | 0 | } else if (object instanceof Document) { |
245 | 0 | applyTemplates((Document) object, mode); |
246 | } | |
247 | } | |
248 | } | |
249 | } | |
250 | ||
251 | 0 | public void clear() { |
252 | 0 | 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 | 0 | public String getModeName() { |
264 | 0 | 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 | 0 | public void setModeName(String modeName) { |
274 | 0 | 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()|@" | |
282 | */ | |
283 | 0 | public Action getValueOfAction() { |
284 | 0 | 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()|@" | |
290 | * | |
291 | * @param valueOfAction | |
292 | * DOCUMENT ME! | |
293 | */ | |
294 | 4 | public void setValueOfAction(Action valueOfAction) { |
295 | 4 | 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 | */ |
|