1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package org.apache.struts.tiles.xmlDefinition;
23
24 import java.io.BufferedInputStream;
25 import java.io.FileInputStream;
26 import java.io.IOException;
27 import java.io.InputStream;
28 import java.net.URL;
29
30 import org.apache.commons.digester.Digester;
31 import org.xml.sax.SAXException;
32
33
34
35
36 public class XmlParser
37 {
38
39
40 protected Digester digester;
41
42
43
44
45 protected boolean validating = false;
46
47
48
49
50
51 protected String registrations[] = {
52 "-//Apache Software Foundation//DTD Tiles Configuration 1.1//EN",
53 "/org/apache/struts/resources/tiles-config_1_1.dtd",
54 "-//Apache Software Foundation//DTD Tiles Configuration 1.3//EN",
55 "/org/apache/struts/resources/tiles-config_1_3.dtd",
56 "-//Apache Software Foundation//DTD Tiles Configuration 1.4//EN",
57 "/org/apache/struts/resources/tiles-config_1_4.dtd"
58 };
59
60
61
62
63
64 public XmlParser()
65 {
66 digester = new Digester();
67 digester.setValidating(validating);
68 digester.setNamespaceAware(true);
69 digester.setUseContextClassLoader(true);
70
71 for (int i = 0; i < registrations.length; i += 2) {
72 URL url = this.getClass().getResource(registrations[i+1]);
73 if (url != null)
74 {
75 digester.register(registrations[i], url.toString());
76 }
77 }
78
79 initDigester( digester );
80 }
81
82
83
84
85 public void setValidating( boolean validating )
86 {
87 digester.setValidating( validating);
88 }
89
90
91
92
93
94
95
96 private void initDigesterForComponentsDefinitionsSyntax( Digester digester )
97 {
98
99 String PACKAGE_NAME = "org.apache.struts.tiles.xmlDefinition";
100 String DEFINITION_TAG = "component-definitions/definition";
101 String definitionHandlerClass = PACKAGE_NAME + ".XmlDefinition";
102
103 String PUT_TAG = DEFINITION_TAG + "/put";
104 String putAttributeHandlerClass = PACKAGE_NAME + ".XmlAttribute";
105
106 String LIST_TAG = DEFINITION_TAG + "/putList";
107 String listHandlerClass = PACKAGE_NAME + ".XmlListAttribute";
108
109 String ADD_LIST_ELE_TAG = LIST_TAG + "/add";
110
111
112 digester.addObjectCreate( DEFINITION_TAG, definitionHandlerClass );
113 digester.addSetProperties( DEFINITION_TAG);
114 digester.addSetNext( DEFINITION_TAG, "putDefinition", definitionHandlerClass);
115
116 digester.addObjectCreate( PUT_TAG, putAttributeHandlerClass);
117 digester.addSetNext( PUT_TAG, "addAttribute", putAttributeHandlerClass);
118 digester.addSetProperties( PUT_TAG);
119 digester.addCallMethod( PUT_TAG, "setBody", 0);
120
121 digester.addObjectCreate( LIST_TAG, listHandlerClass);
122 digester.addSetProperties( LIST_TAG);
123 digester.addSetNext( LIST_TAG, "addAttribute", putAttributeHandlerClass);
124
125
126
127 digester.addObjectCreate( ADD_LIST_ELE_TAG, putAttributeHandlerClass);
128 digester.addSetNext( ADD_LIST_ELE_TAG, "add", putAttributeHandlerClass);
129 digester.addSetProperties( ADD_LIST_ELE_TAG);
130 digester.addCallMethod( ADD_LIST_ELE_TAG, "setBody", 0);
131 }
132
133
134
135
136
137
138 private void initDigesterForTilesDefinitionsSyntax( Digester digester )
139 {
140
141 String PACKAGE_NAME = "org.apache.struts.tiles.xmlDefinition";
142 String DEFINITION_TAG = "tiles-definitions/definition";
143 String definitionHandlerClass = PACKAGE_NAME + ".XmlDefinition";
144
145 String PUT_TAG = DEFINITION_TAG + "/put";
146 String putAttributeHandlerClass = PACKAGE_NAME + ".XmlAttribute";
147
148
149
150 String LIST_TAG = "putList";
151 String DEF_LIST_TAG = DEFINITION_TAG + "/" + LIST_TAG;
152 String listHandlerClass = PACKAGE_NAME + ".XmlListAttribute";
153
154 String ADD_LIST_ELE_TAG = "*/" + LIST_TAG + "/add";
155
156
157 digester.addObjectCreate( DEFINITION_TAG, definitionHandlerClass );
158 digester.addSetProperties( DEFINITION_TAG);
159 digester.addSetNext( DEFINITION_TAG, "putDefinition", definitionHandlerClass);
160
161
162
163
164
165 digester.addObjectCreate( PUT_TAG, putAttributeHandlerClass);
166 digester.addSetNext( PUT_TAG, "addAttribute", putAttributeHandlerClass);
167 digester.addSetProperties( PUT_TAG);
168 digester.addCallMethod( PUT_TAG, "setBody", 0);
169
170
171 digester.addObjectCreate( DEF_LIST_TAG, listHandlerClass);
172 digester.addSetProperties( DEF_LIST_TAG);
173 digester.addSetNext( DEF_LIST_TAG, "addAttribute", putAttributeHandlerClass);
174
175
176
177 digester.addObjectCreate( ADD_LIST_ELE_TAG, putAttributeHandlerClass);
178 digester.addSetNext( ADD_LIST_ELE_TAG, "add", putAttributeHandlerClass);
179 digester.addSetProperties( ADD_LIST_ELE_TAG);
180 digester.addCallMethod( ADD_LIST_ELE_TAG, "setBody", 0);
181
182
183
184 String NESTED_LIST = "*/" + LIST_TAG + "/" + LIST_TAG;
185 digester.addObjectCreate( NESTED_LIST, listHandlerClass);
186 digester.addSetProperties( NESTED_LIST);
187 digester.addSetNext( NESTED_LIST, "add", putAttributeHandlerClass);
188
189
190
191
192
193
194 String ADD_WILDCARD = "*/item";
195 String menuItemDefaultClass = "org.apache.struts.tiles.beans.SimpleMenuItem";
196 digester.addObjectCreate( ADD_WILDCARD, menuItemDefaultClass, "classtype");
197 digester.addSetNext( ADD_WILDCARD, "add", "java.lang.Object");
198 digester.addSetProperties( ADD_WILDCARD);
199
200
201 String BEAN_TAG = "*/bean";
202 String beanDefaultClass = "org.apache.struts.tiles.beans.SimpleMenuItem";
203 digester.addObjectCreate( BEAN_TAG, beanDefaultClass, "classtype");
204 digester.addSetNext( BEAN_TAG, "add", "java.lang.Object");
205 digester.addSetProperties( BEAN_TAG);
206
207
208 digester.addSetProperty(BEAN_TAG+ "/set-property", "property", "value");
209 }
210
211
212
213
214
215
216
217 private void initDigesterForInstancesSyntax( Digester digester )
218 {
219
220 String PACKAGE_NAME = "org.apache.struts.tiles.xmlDefinition";
221 String INSTANCE_TAG = "component-instances/instance";
222 String instanceHandlerClass = PACKAGE_NAME + ".XmlDefinition";
223
224 String PUT_TAG = INSTANCE_TAG + "/put";
225 String PUTATTRIBUTE_TAG = INSTANCE_TAG + "/putAttribute";
226 String putAttributeHandlerClass = PACKAGE_NAME + ".XmlAttribute";
227
228 String LIST_TAG = INSTANCE_TAG + "/putList";
229 String listHandlerClass = PACKAGE_NAME + ".XmlListAttribute";
230
231 String ADD_LIST_ELE_TAG = LIST_TAG + "/add";
232
233
234 digester.addObjectCreate( INSTANCE_TAG, instanceHandlerClass );
235 digester.addSetProperties( INSTANCE_TAG);
236 digester.addSetNext( INSTANCE_TAG, "putDefinition", instanceHandlerClass);
237
238 digester.addObjectCreate( PUTATTRIBUTE_TAG, putAttributeHandlerClass);
239 digester.addSetProperties( PUTATTRIBUTE_TAG);
240 digester.addSetNext( PUTATTRIBUTE_TAG, "addAttribute", putAttributeHandlerClass);
241
242 digester.addObjectCreate( PUT_TAG, putAttributeHandlerClass);
243 digester.addSetProperties( PUT_TAG);
244 digester.addSetNext( PUT_TAG, "addAttribute", putAttributeHandlerClass);
245
246 digester.addObjectCreate( LIST_TAG, listHandlerClass);
247 digester.addSetProperties( LIST_TAG);
248 digester.addSetNext( LIST_TAG, "addAttribute", putAttributeHandlerClass);
249
250
251
252 digester.addObjectCreate( ADD_LIST_ELE_TAG, putAttributeHandlerClass);
253 digester.addSetProperties( ADD_LIST_ELE_TAG);
254 digester.addSetNext( ADD_LIST_ELE_TAG, "add", putAttributeHandlerClass);
255 }
256
257
258
259
260
261 protected void initDigester( Digester digester )
262 {
263 initDigesterForTilesDefinitionsSyntax( digester );
264 initDigesterForComponentsDefinitionsSyntax( digester );
265 initDigesterForInstancesSyntax( digester );
266 }
267
268
269
270
271
272
273
274
275 public void parse( InputStream in, XmlDefinitionsSet definitions ) throws IOException, SAXException
276 {
277 try
278 {
279
280
281 digester.push(definitions);
282
283 digester.parse(in);
284 in.close();
285 }
286 catch (SAXException e)
287 {
288
289 throw e;
290 }
291
292 }
293
294
295
296
297 public static void main(String[] args)
298 {
299
300 String filename = "E:/programs/jakarta-tomcat-4.0.3/webapps/wtiles-struts/WEB-INF/tiles-examples-defs.xml";
301
302
303
304
305
306 if( args.length > 1 )
307 {
308 filename = args[1];
309 }
310
311 System.out.println( "Read file '" + filename +"'" );
312
313 InputStream input = null;
314
315
316 try
317 {
318 input = new BufferedInputStream(
319 new FileInputStream( filename) );
320
321
322 }
323 catch( IOException ex )
324 {
325 System.out.println( "can't open file '" + filename + "' : " + ex.getMessage() );
326 }
327
328 try
329 {
330 XmlParser parser = new XmlParser();
331 parser.setValidating(true);
332 XmlDefinitionsSet definitions = new XmlDefinitionsSet();
333 System.out.println( " Parse file" );
334 parser.parse( input, definitions);
335
336
337 System.out.println( " done." );
338 System.out.println( " Result : " + definitions.toString() );
339 }
340 catch( Exception ex )
341 {
342 System.out.println( "Error during parsing '" + filename + "' : " + ex.getMessage() );
343 ex.printStackTrace();
344 }
345 }
346
347 }