|
|||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
Stylesheet.java | 22,7% | 28,6% | 35% | 28,6% |
|
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 | 4 | public Stylesheet() { |
36 | } | |
37 | ||
38 | 26 | public void addRule( Rule rule ) { |
39 | 26 | ruleManager.addRule( rule ); |
40 | } | |
41 | ||
42 | 0 | public void removeRule( Rule rule ) { |
43 | 0 | 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 | 0 | public void run(Object input) throws Exception { |
50 | 0 | run(input, this.modeName); |
51 | } | |
52 | ||
53 | 0 | public void run(Object input, String mode) throws Exception { |
54 | 0 | if (input instanceof Node) { |
55 | 0 | run ((Node) input, mode); |
56 | } | |
57 | 0 | else if (input instanceof List) { |
58 | 0 | run((List) input, mode); |
59 | } | |
60 | } | |
61 | ||
62 | 0 | public void run(List list) throws Exception { |
63 | 0 | run(list, this.modeName); |
64 | } | |
65 | ||
66 | 0 | public void run(List list, String mode) throws Exception { |
67 | 0 | for (int i = 0, size = list.size(); i < size; i++) { |
68 | 0 | Object object = list.get(i); |
69 | 0 | if (object instanceof Node) { |
70 | 0 | run((Node) object, mode); |
71 | } | |
72 | } | |
73 | } | |
74 | ||
75 | 4 | public void run(Node node) throws Exception { |
76 | 4 | run(node, this.modeName); |
77 | } | |
78 | ||
79 | 4 | public void run(Node node, String mode) throws Exception { |
80 | 4 | Mode mod = ruleManager.getMode(mode); |
81 | 4 | mod.fireRule(node); |
82 | } | |
83 | ||
84 | ||
85 | 0 | public void applyTemplates(Object input, XPath xpath) throws Exception { |
86 | 0 | applyTemplates(input, xpath, this.modeName); |
87 | } | |
88 | ||
89 | 0 | public void applyTemplates(Object input, XPath xpath, String mode) throws Exception { |
90 | 0 | List list = xpath.selectNodes(input); |
91 | 0 | list.remove(input); |
92 | 0 | 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 | 0 | public void applyTemplates(Object input, org.jaxen.XPath xpath) throws Exception { |
102 | 0 | applyTemplates(input, xpath, this.modeName); |
103 | } | |
104 | ||
105 | 0 | public void applyTemplates(Object input, org.jaxen.XPath xpath, String mode) throws Exception { |
106 | 0 | List list = xpath.selectNodes(input); |
107 | 0 | 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 | 28 | public void applyTemplates(Object input) throws Exception { |
117 | 28 | applyTemplates(input, this.modeName); |
118 | } | |
119 | ||
120 | 28 | public void applyTemplates(Object input, String mode) throws Exception { |
121 | // iterate through all children | |
122 | 28 | Mode mod = ruleManager.getMode(mode); |
123 | ||
124 | 28 | if ( input instanceof Element ) { |
125 | 20 | mod.applyTemplates( (Element) input ); |
126 | } | |
127 | 8 | else if ( input instanceof Document ) { |
128 | 4 | mod.applyTemplates( (Document) input ); |
129 | } | |
130 | 4 | else if ( input instanceof List ) { |
131 | 0 | List list = (List) input; |
132 | 0 | for ( int i = 0, size = list.size(); i < size; i++ ) { |
133 | 0 | Object object = list.get(i); |
134 | 0 | if ( object != input ) { |
135 | 0 | if ( object instanceof Element ) { |
136 | 0 | mod.applyTemplates( (Element) object ); |
137 | } | |
138 | 0 | else if ( object instanceof Document ) { |
139 | 0 | mod.applyTemplates( (Document) object ); |
140 | } | |
141 | } | |
142 | } | |
143 | } | |
144 | } | |
145 | ||
146 | 0 | public void clear() { |
147 | 0 | ruleManager.clear(); |
148 | } | |
149 | ||
150 | ||
151 | // Properties | |
152 | //------------------------------------------------------------------------- | |
153 | ||
154 | /** @return the name of the mode the stylesheet uses by default | |
155 | */ | |
156 | 0 | public String getModeName() { |
157 | 0 | return modeName; |
158 | } | |
159 | ||
160 | /** Sets the name of the mode that the stylesheet uses by default. | |
161 | */ | |
162 | 0 | public void setModeName(String modeName) { |
163 | 0 | 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 | 0 | public Action getValueOfAction() { |
170 | 0 | 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 | 4 | public void setValueOfAction(Action valueOfAction) { |
177 | 4 | 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 | */ |
|