1
2
3
4
5
6
7
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
94
95
96
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
109
110
111
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
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
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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228