1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.dbforms.config;
25
26 import org.apache.commons.digester.Digester;
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29
30 import org.dbforms.event.DatabaseEventFactoryImpl;
31 import org.dbforms.event.NavEventFactoryImpl;
32
33 import org.dbforms.util.SingletonClassFactoryCreate;
34
35 import org.xml.sax.SAXException;
36
37 import java.io.IOException;
38 import java.io.InputStream;
39
40
41
42 /***
43 * Loads the configuration. With this class you can load the configuration
44 * data from an standalone application, e.g.: dbConfig = new
45 * DbFormsConfig(realPath); FileInputStream fis = new FileInputStream(fn);
46 * ConfigLoader loader = new ConfigLoader() loader.loadConfig(fis, dbConfig);
47 *
48 * @author Ivan F. Martinez
49 */
50 public class ConfigLoader {
51 /*** DOCUMENT ME! */
52 public static final String BASE_CLASS_TABLE = "org.dbforms.config.Table";
53
54 /*** DOCUMENT ME! */
55 public static final String BASE_CLASS_FIELD = "org.dbforms.config.Field";
56
57 /*** DOCUMENT ME! */
58 public static final String BASE_CLASS_QUERY = "org.dbforms.config.Query";
59
60 /*** DOCUMENT ME! */
61 public static final String BASE_CLASS_FOREIGNKEY = "org.dbforms.config.ForeignKey";
62
63 /*** DOCUMENT ME! */
64 public static final String BASE_CLASS_REFERENCE = "org.dbforms.config.Reference";
65
66 /*** DOCUMENT ME! */
67 public static final String DEFAULT_CONFIG = "/WEB-INF/dbforms-config.xml";
68
69 /*** DOCUMENT ME! */
70 public static final String DEFAULT_ERRORS = "/WEB-INF/dbforms-errors.xml";
71
72 /*** DOCUMENT ME! */
73 public static final String DEFAULT_VALIDATION = "/WEB-INF/validation.xml";
74
75 /*** DOCUMENT ME! */
76 public static final String DEFAULT_RULES = "/WEB-INF/validator-rules.xml";
77 private static Log logCat = LogFactory.getLog(ConfigLoader.class);
78
79 /*** The context-relative path to our configuration and errors resources. */
80 private String config = DEFAULT_CONFIG;
81
82 /*** DOCUMENT ME! */
83 private String errors = DEFAULT_ERRORS;
84
85 /*** className for Field */
86 private String fieldClassName = BASE_CLASS_FIELD;
87
88 /*** className for ForeignKey */
89 private String foreignKeyClassName = BASE_CLASS_FOREIGNKEY;
90
91 /*** className for Query */
92 private String queryClassName = BASE_CLASS_QUERY;
93
94 /*** className for Reference */
95 private String referenceClassName = BASE_CLASS_REFERENCE;
96
97 /*** className for Table */
98 private String tableClassName = BASE_CLASS_TABLE;
99
100 /*** DOCUMENT ME! */
101 private String validation = DEFAULT_VALIDATION;
102
103 /*** DOCUMENT ME! */
104 private String validator_rules = DEFAULT_RULES;
105
106 /***
107 * Creates a new ConfigLoader object.
108 */
109 public ConfigLoader() {
110 }
111
112 /***
113 * DOCUMENT ME!
114 *
115 * @param config DOCUMENT ME!
116 */
117 public void setConfig(String config) {
118 if (config != null) {
119 this.config = config;
120 } else {
121 this.config = DEFAULT_CONFIG;
122 }
123 }
124
125
126 /***
127 * DOCUMENT ME!
128 *
129 * @return DOCUMENT ME!
130 */
131 public String getConfig() {
132 return config;
133 }
134
135
136 /***
137 * DOCUMENT ME!
138 *
139 * @param errors DOCUMENT ME!
140 */
141 public void setErrors(String errors) {
142 if (errors != null) {
143 this.errors = errors;
144 } else {
145 this.errors = DEFAULT_ERRORS;
146 }
147 }
148
149
150 /***
151 * DOCUMENT ME!
152 *
153 * @return DOCUMENT ME!
154 */
155 public String getErrors() {
156 return errors;
157 }
158
159
160 /***
161 * DOCUMENT ME!
162 *
163 * @param className DOCUMENT ME!
164 */
165 public void setFieldClassName(String className) {
166 if (className != null) {
167 fieldClassName = className;
168 } else {
169 fieldClassName = BASE_CLASS_FIELD;
170 }
171 }
172
173
174 /***
175 * DOCUMENT ME!
176 *
177 * @param className DOCUMENT ME!
178 */
179 public void setForeignKeyClassName(String className) {
180 if (className != null) {
181 foreignKeyClassName = className;
182 } else {
183 foreignKeyClassName = BASE_CLASS_FOREIGNKEY;
184 }
185 }
186
187
188 /***
189 * DOCUMENT ME!
190 *
191 * @param className DOCUMENT ME!
192 */
193 public void setQueryClassName(String className) {
194 if (className != null) {
195 queryClassName = className;
196 } else {
197 queryClassName = BASE_CLASS_QUERY;
198 }
199 }
200
201
202 /***
203 * DOCUMENT ME!
204 *
205 * @param className DOCUMENT ME!
206 */
207 public void setReferenceClassName(String className) {
208 if (className != null) {
209 referenceClassName = className;
210 } else {
211 referenceClassName = BASE_CLASS_REFERENCE;
212 }
213 }
214
215
216 /***
217 * DOCUMENT ME!
218 *
219 * @param className DOCUMENT ME!
220 */
221 public void setTableClassName(String className) {
222 if (className != null) {
223 tableClassName = className;
224 } else {
225 tableClassName = BASE_CLASS_TABLE;
226 }
227 }
228
229
230 /***
231 * DOCUMENT ME!
232 *
233 * @param validation DOCUMENT ME!
234 */
235 public void setValidation(String validation) {
236 if (validation != null) {
237 this.validation = validation;
238 } else {
239 this.validation = DEFAULT_VALIDATION;
240 }
241 }
242
243
244 /***
245 * DOCUMENT ME!
246 *
247 * @return DOCUMENT ME!
248 */
249 public String getValidation() {
250 return validation;
251 }
252
253
254 /***
255 * DOCUMENT ME!
256 *
257 * @param validator_rules DOCUMENT ME!
258 */
259 public void setValidatorRules(String validator_rules) {
260 if (validator_rules != null) {
261 this.validator_rules = validator_rules;
262 } else {
263 this.validator_rules = DEFAULT_RULES;
264 }
265 }
266
267
268 /***
269 * DOCUMENT ME!
270 *
271 * @return DOCUMENT ME!
272 */
273 public String getValidatorRules() {
274 return validator_rules;
275 }
276
277
278 /***
279 * Load Config from InputStream
280 *
281 * @param input InputStream to load Config
282 * @param dbFormsConfig object to load the Config
283 */
284 public void loadConfig(InputStream input,
285 DbFormsConfig dbFormsConfig)
286 throws IOException, SAXException {
287 Digester digester = initDigester(dbFormsConfig);
288 digester.parse(input);
289 }
290
291
292 /***
293 * Load Errors Config from InputStream
294 *
295 * @param input InputStream to load Errors Config
296 * @param dbFormsErrors object to load the Errors config
297 */
298 public void loadErrors(InputStream input,
299 DbFormsErrors dbFormsErrors)
300 throws IOException, SAXException {
301 Digester digester = initErrorsDigester(dbFormsErrors);
302
303
304 digester.parse(input);
305 }
306
307
308 /***
309 * Construct and return a digester that uses the new configuration file
310 * format.
311 *
312 * @param dbFormsConfig DOCUMENT ME!
313 *
314 * @return DOCUMENT ME!
315 */
316 protected Digester initDigester(DbFormsConfig dbFormsConfig) {
317
318 Digester digester = new Digester();
319 digester.setLogger(LogFactory.getLog(Digester.class));
320 digester.push(dbFormsConfig);
321 digester.setNamespaceAware(true);
322 digester.setValidating(false);
323
324
325
326 digester.addCallMethod("dbforms-config/DOMFactoryClass",
327 "setDOMFactoryClass", 0);
328 digester.addCallMethod("dbforms-config/DefaultEscaperClass",
329 "setDefaultEscaperClass", 0);
330
331 digester.addCallMethod("dbforms-config/param", "addParam", 2);
332 digester.addCallParam("dbforms-config/param", 0, "name");
333 digester.addCallParam("dbforms-config/param", 1, "value");
334
335
336
337 digester.addObjectCreate("dbforms-config/table", tableClassName);
338 digester.addSetProperties("dbforms-config/table");
339 digester.addSetNext("dbforms-config/table", "addTable", BASE_CLASS_TABLE);
340
341
342 digester.addObjectCreate("dbforms-config/table/field", fieldClassName);
343 digester.addSetProperties("dbforms-config/table/field");
344 digester.addSetNext("dbforms-config/table/field", "addField", BASE_CLASS_FIELD);
345
346
347 digester.addObjectCreate("dbforms-config/table/calc", fieldClassName);
348 digester.addSetProperties("dbforms-config/table/calc");
349 digester.addSetNext("dbforms-config/table/calc", "addCalcField", BASE_CLASS_FIELD);
350
351
352 digester.addObjectCreate("dbforms-config/table/foreign-key", foreignKeyClassName);
353 digester.addSetProperties("dbforms-config/table/foreign-key");
354 digester.addSetNext("dbforms-config/table/foreign-key", "addForeignKey",
355 BASE_CLASS_FOREIGNKEY);
356
357
358 digester.addObjectCreate("dbforms-config/table/foreign-key/reference",
359 referenceClassName);
360 digester.addSetProperties("dbforms-config/table/foreign-key/reference");
361 digester.addSetNext("dbforms-config/table/foreign-key/reference",
362 "addReference", BASE_CLASS_REFERENCE);
363
364
365 digester.addObjectCreate("dbforms-config/table/granted-privileges",
366 "org.dbforms.config.GrantedPrivileges");
367 digester.addSetProperties("dbforms-config/table/granted-privileges");
368 digester.addSetNext("dbforms-config/table/granted-privileges",
369 "setGrantedPrivileges",
370 "org.dbforms.config.GrantedPrivileges");
371
372
373 digester.addObjectCreate("dbforms-config/table/interceptor",
374 "org.dbforms.config.Interceptor");
375 digester.addSetProperties("dbforms-config/table/interceptor");
376 digester.addSetNext("dbforms-config/table/interceptor", "addInterceptor",
377 "org.dbforms.config.Interceptor");
378
379
380 digester.addCallMethod("dbforms-config/table/interceptor/param","addParam", 2);
381 digester.addCallParam("dbforms-config/table/interceptor/param", 0, "name");
382 digester.addCallParam("dbforms-config/table/interceptor/param", 1, "value");
383
384
385
386
387
388
389
390
391
392 digester.addObjectCreate("dbforms-config/table/events",
393 "org.dbforms.config.TableEvents");
394 digester.addSetNext("dbforms-config/table/events", "setTableEvents", "org.dbforms.config.TableEvents");
395 digester.addObjectCreate("dbforms-config/table/events/event", "org.dbforms.config.EventInfo");
396 digester.addSetProperties("dbforms-config/table/events/event");
397 digester.addSetNext("dbforms-config/table/events/event", "addEventInfo", "org.dbforms.config.EventInfo");
398 digester.addCallMethod("dbforms-config/table/events/event/property","addProperty", 2);
399 digester.addCallParam("dbforms-config/table/events/event/property", 0, "name");
400 digester.addCallParam("dbforms-config/table/events/event/property", 1, "value");
401
402
403
404 digester.addObjectCreate("dbforms-config/query", queryClassName);
405 digester.addSetProperties("dbforms-config/query");
406 digester.addSetNext("dbforms-config/query", "addTable", BASE_CLASS_TABLE);
407
408
409 digester.addObjectCreate("dbforms-config/query/field", fieldClassName);
410 digester.addSetProperties("dbforms-config/query/field");
411 digester.addSetNext("dbforms-config/query/field", "addField",
412 BASE_CLASS_FIELD);
413
414
415 digester.addObjectCreate("dbforms-config/query/calc", fieldClassName);
416 digester.addSetProperties("dbforms-config/query/calc");
417 digester.addSetNext("dbforms-config/query/calc", "addCalcField",
418 BASE_CLASS_FIELD);
419
420
421
422 digester.addObjectCreate("dbforms-config/query/search", fieldClassName);
423 digester.addSetProperties("dbforms-config/query/search");
424 digester.addSetNext("dbforms-config/query/search", "addSearchField",
425 BASE_CLASS_FIELD);
426
427
428 digester.addObjectCreate("dbforms-config/query/foreign-key",
429 foreignKeyClassName);
430 digester.addSetProperties("dbforms-config/query/foreign-key");
431 digester.addSetNext("dbforms-config/query/foreign-key", "addForeignKey",
432 BASE_CLASS_FOREIGNKEY);
433
434
435 digester.addObjectCreate("dbforms-config/query/foreign-key/reference",
436 referenceClassName);
437 digester.addSetProperties("dbforms-config/query/foreign-key/reference");
438 digester.addSetNext("dbforms-config/query/foreign-key/reference",
439 "addReference", BASE_CLASS_REFERENCE);
440
441
442 digester.addObjectCreate("dbforms-config/query/granted-privileges",
443 "org.dbforms.config.GrantedPrivileges");
444 digester.addSetProperties("dbforms-config/query/granted-privileges");
445 digester.addSetNext("dbforms-config/query/granted-privileges",
446 "setGrantedPrivileges",
447 "org.dbforms.config.GrantedPrivileges");
448
449
450 digester.addObjectCreate("dbforms-config/query/interceptor",
451 "org.dbforms.config.Interceptor");
452 digester.addSetProperties("dbforms-config/query/interceptor");
453 digester.addSetNext("dbforms-config/query/interceptor", "addInterceptor", "org.dbforms.config.Interceptor");
454
455
456 digester.addCallMethod("dbforms-config/query/interceptor/param", "addParam", 2);
457 digester.addCallParam("dbforms-config/query/interceptor/param", 0, "name");
458 digester.addCallParam("dbforms-config/query/interceptor/param", 1, "value");
459
460
461
462
463
464
465
466
467
468 digester.addObjectCreate("dbforms-config/query/events",
469 "org.dbforms.config.TableEvents");
470 digester.addSetNext("dbforms-config/query/events", "setTableEvents",
471 "org.dbforms.config.TableEvents");
472 digester.addObjectCreate("dbforms-config/query/events/event",
473 "org.dbforms.config.EventInfo");
474 digester.addSetProperties("dbforms-config/query/events/event");
475 digester.addSetNext("dbforms-config/query/events/event", "addEventInfo",
476 "org.dbforms.config.EventInfo");
477
478 digester.addCallMethod("dbforms-config/query/events/event/property","addProperty", 2);
479 digester.addCallParam("dbforms-config/query/events/event/property", 0, "name");
480 digester.addCallParam("dbforms-config/query/events/event/property", 1, "value");
481
482
483 digester.addObjectCreate("dbforms-config/dbconnection",
484 "org.dbforms.config.DbConnection");
485 digester.addSetProperties("dbforms-config/dbconnection");
486 digester.addSetNext("dbforms-config/dbconnection", "addDbConnection",
487 "org.dbforms.config.DbConnection");
488
489
490 digester.addCallMethod("dbforms-config/dbconnection/property","addProperty", 2);
491 digester.addCallParam("dbforms-config/dbconnection/property", 0, "name");
492 digester.addCallParam("dbforms-config/dbconnection/property", 1, "value");
493
494
495
496 digester.addCallMethod("dbforms-config/dbconnection/pool-property","addPoolProperty", 2);
497 digester.addCallParam("dbforms-config/dbconnection/pool-property", 0, "name");
498 digester.addCallParam("dbforms-config/dbconnection/pool-property", 1, "value");
499
500
501
502 digester.addObjectCreate("dbforms-config/interceptors/interceptor",
503 "org.dbforms.config.Interceptor");
504 digester.addSetProperties("dbforms-config/interceptors/interceptor");
505 digester.addSetNext("dbforms-config/interceptors/interceptor",
506 "addInterceptor", "org.dbforms.config.Interceptor");
507
508
509 digester.addCallMethod("dbforms-config/interceptors/interceptor/param", "addParam", 2);
510 digester.addCallParam("dbforms-config/interceptors/interceptor/param", 0, "name");
511 digester.addCallParam("dbforms-config/interceptors/interceptor/param", 1, "value");
512
513
514 digester.addFactoryCreate("dbforms-config/events/database-events",
515 new SingletonClassFactoryCreate(DatabaseEventFactoryImpl.class
516 .getName()));
517 digester.addObjectCreate("dbforms-config/events/database-events/database-event",
518 "org.dbforms.config.EventInfo");
519 digester.addSetProperties("dbforms-config/events/database-events/database-event");
520 digester.addSetNext("dbforms-config/events/database-events/database-event",
521 "addEventInfo", "org.dbforms.config.EventInfo");
522
523
524 digester.addFactoryCreate("dbforms-config/events/navigation-events",
525 new SingletonClassFactoryCreate(NavEventFactoryImpl.class
526 .getName()));
527 digester.addObjectCreate("dbforms-config/events/navigation-events/navigation-event",
528 "org.dbforms.config.EventInfo");
529 digester.addSetProperties("dbforms-config/events/navigation-events/navigation-event");
530 digester.addSetNext("dbforms-config/events/navigation-events/navigation-event",
531 "addEventInfo", "org.dbforms.config.EventInfo");
532
533 return digester;
534 }
535
536
537 /***
538 * Construct and return a digester that uses the new errors file format.
539 *
540 * @param dbFormsErrors DOCUMENT ME!
541 *
542 * @return DOCUMENT ME!
543 */
544 protected Digester initErrorsDigester(DbFormsErrors dbFormsErrors) {
545
546 logCat.info("initialize Errors Digester.");
547
548 Digester digester = new Digester();
549 digester.setLogger(LogFactory.getLog(Digester.class));
550 digester.push(dbFormsErrors);
551 digester.setValidating(false);
552
553
554
555 digester.addObjectCreate("dbforms-errors/error",
556 "org.dbforms.config.Error");
557 digester.addSetProperties("dbforms-errors/error");
558 digester.addSetNext("dbforms-errors/error", "addError",
559 "org.dbforms.config.Error");
560
561
562 digester.addObjectCreate("dbforms-errors/error/message",
563 "org.dbforms.config.Message");
564 digester.addSetNext("dbforms-errors/error/message", "addMessage",
565 "org.dbforms.config.Message");
566 digester.addSetProperties("dbforms-errors/error/message");
567 digester.addCallMethod("dbforms-errors/error/message", "setMessage", 0);
568
569 return digester;
570 }
571 }