KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > dbforms > config > ConfigLoader


1 /*
2  * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/config/ConfigLoader.java,v 1.6 2004/10/20 10:51:26 hkollmann Exp $
3  * $Revision: 1.6 $
4  * $Date: 2004/10/20 10:51:26 $
5  *
6  * DbForms - a Rapid Application Development Framework
7  * Copyright (C) 2001 Joachim Peer <joepeer@excite.com>
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
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 JavaDoc;
36
37 import java.io.IOException JavaDoc;
38 import java.io.InputStream JavaDoc;
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 JavaDoc BASE_CLASS_TABLE = "org.dbforms.config.Table";
53
54    /** DOCUMENT ME! */
55    public static final String JavaDoc BASE_CLASS_FIELD = "org.dbforms.config.Field";
56
57    /** DOCUMENT ME! */
58    public static final String JavaDoc BASE_CLASS_QUERY = "org.dbforms.config.Query";
59
60    /** DOCUMENT ME! */
61    public static final String JavaDoc BASE_CLASS_FOREIGNKEY = "org.dbforms.config.ForeignKey";
62
63    /** DOCUMENT ME! */
64    public static final String JavaDoc BASE_CLASS_REFERENCE = "org.dbforms.config.Reference";
65
66    /** DOCUMENT ME! */
67    public static final String JavaDoc DEFAULT_CONFIG = "/WEB-INF/dbforms-config.xml";
68
69    /** DOCUMENT ME! */
70    public static final String JavaDoc DEFAULT_ERRORS = "/WEB-INF/dbforms-errors.xml";
71
72    /** DOCUMENT ME! */
73    public static final String JavaDoc DEFAULT_VALIDATION = "/WEB-INF/validation.xml";
74
75    /** DOCUMENT ME! */
76    public static final String JavaDoc 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 JavaDoc config = DEFAULT_CONFIG;
81
82    /** DOCUMENT ME! */
83    private String JavaDoc errors = DEFAULT_ERRORS;
84
85    /** className for Field */
86    private String JavaDoc fieldClassName = BASE_CLASS_FIELD;
87
88    /** className for ForeignKey */
89    private String JavaDoc foreignKeyClassName = BASE_CLASS_FOREIGNKEY;
90
91    /** className for Query */
92    private String JavaDoc queryClassName = BASE_CLASS_QUERY;
93
94    /** className for Reference */
95    private String JavaDoc referenceClassName = BASE_CLASS_REFERENCE;
96
97    /** className for Table */
98    private String JavaDoc tableClassName = BASE_CLASS_TABLE;
99
100    /** DOCUMENT ME! */
101    private String JavaDoc validation = DEFAULT_VALIDATION;
102
103    /** DOCUMENT ME! */
104    private String JavaDoc 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 JavaDoc 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 JavaDoc getConfig() {
132       return config;
133    }
134
135
136    /**
137     * DOCUMENT ME!
138     *
139     * @param errors DOCUMENT ME!
140     */

141    public void setErrors(String JavaDoc 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 JavaDoc getErrors() {
156       return errors;
157    }
158
159
160    /**
161     * DOCUMENT ME!
162     *
163     * @param className DOCUMENT ME!
164     */

165    public void setFieldClassName(String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc getValidation() {
250       return validation;
251    }
252
253
254    /**
255     * DOCUMENT ME!
256     *
257     * @param validator_rules DOCUMENT ME!
258     */

259    public void setValidatorRules(String JavaDoc 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 JavaDoc 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 JavaDoc input,
285                           DbFormsConfig dbFormsConfig)
286                    throws IOException JavaDoc, SAXException JavaDoc {
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 JavaDoc input,
299                           DbFormsErrors dbFormsErrors)
300                    throws IOException JavaDoc, SAXException JavaDoc {
301       Digester digester = initErrorsDigester(dbFormsErrors);
302
303       // Parse the input stream to configure our mappings
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       // Initialize a new Digester instance
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       // Configure the processing rules
325
// parse "DOMFactoryClass"
326
digester.addCallMethod("dbforms-config/DOMFactoryClass",
327                              "setDOMFactoryClass", 0);
328       digester.addCallMethod("dbforms-config/DefaultEscaperClass",
329                              "setDefaultEscaperClass", 0);
330
331       // parse "Table" - object + add it to parent
332
digester.addObjectCreate("dbforms-config/table", tableClassName);
333       digester.addSetProperties("dbforms-config/table");
334       digester.addSetNext("dbforms-config/table", "addTable", BASE_CLASS_TABLE);
335
336       // parse "Field" - object + add it to parent (which is "Table")
337
digester.addObjectCreate("dbforms-config/table/field", fieldClassName);
338       digester.addSetProperties("dbforms-config/table/field");
339       digester.addSetNext("dbforms-config/table/field", "addField",
340                           BASE_CLASS_FIELD);
341
342       // parse "calc" - object + add it to parent (which is "Table")
343
digester.addObjectCreate("dbforms-config/table/calc", fieldClassName);
344       digester.addSetProperties("dbforms-config/table/calc");
345       digester.addSetNext("dbforms-config/table/calc", "addCalcField",
346                           BASE_CLASS_FIELD);
347
348       // parse "Foreign-Key" - object + add it to parent (which is "Table")
349
digester.addObjectCreate("dbforms-config/table/foreign-key",
350                                foreignKeyClassName);
351       digester.addSetProperties("dbforms-config/table/foreign-key");
352       digester.addSetNext("dbforms-config/table/foreign-key", "addForeignKey",
353                           BASE_CLASS_FOREIGNKEY);
354
355       // parse "Reference" - object + add it to parent (which is "ForeignKey")
356
digester.addObjectCreate("dbforms-config/table/foreign-key/reference",
357                                referenceClassName);
358       digester.addSetProperties("dbforms-config/table/foreign-key/reference");
359       digester.addSetNext("dbforms-config/table/foreign-key/reference",
360                           "addReference", BASE_CLASS_REFERENCE);
361
362       // parse "GrantedPrivileges" - object + add it to parent (which is "Table")
363
digester.addObjectCreate("dbforms-config/table/granted-privileges",
364                                "org.dbforms.config.GrantedPrivileges");
365       digester.addSetProperties("dbforms-config/table/granted-privileges");
366       digester.addSetNext("dbforms-config/table/granted-privileges",
367                           "setGrantedPrivileges",
368                           "org.dbforms.config.GrantedPrivileges");
369
370       // parse "Interceptor" - object + add it to parent (which is "Table")
371
digester.addObjectCreate("dbforms-config/table/interceptor",
372                                "org.dbforms.config.Interceptor");
373       digester.addSetProperties("dbforms-config/table/interceptor");
374       digester.addSetNext("dbforms-config/table/interceptor", "addInterceptor",
375                           "org.dbforms.config.Interceptor");
376
377       // J.Peer 03/2004: parse interceptor "param"s + add it to parent (which is "Interceptor")
378
digester.addCallMethod("dbforms-config/table/interceptor/param",
379                              "addParam", 2);
380       digester.addCallParam("dbforms-config/table/interceptor/param", 0, "name");
381       digester.addCallParam("dbforms-config/table/interceptor/param", 1, "value");
382
383       // register custom database or navigation events (parent is "Table");
384
// 1) for every "events" element, instance a new TableEvents object;
385
// 2) set the TableEvents reference into the Table object
386
// 3) for every "event" element, instance a new EventInfo object and set its properties ("type" and "id")
387
// 4) register the EventInfo object into the TableEvents via TableEvents.addEventInfo()
388
// 5) for every event's property attribute, instance a new Property object
389
// and and set its properties ("name" and "value")
390
// 6) register the Property object into the EventInfo object
391
digester.addObjectCreate("dbforms-config/table/events",
392                                "org.dbforms.config.TableEvents");
393       digester.addSetNext("dbforms-config/table/events", "setTableEvents",
394                           "org.dbforms.config.TableEvents");
395       digester.addObjectCreate("dbforms-config/table/events/event",
396                                "org.dbforms.config.EventInfo");
397       digester.addSetProperties("dbforms-config/table/events/event");
398       digester.addSetNext("dbforms-config/table/events/event", "addEventInfo",
399                           "org.dbforms.config.EventInfo");
400       digester.addObjectCreate("dbforms-config/table/events/event/property",
401                                "org.dbforms.config.DbConnectionProperty");
402       digester.addSetProperties("dbforms-config/table/events/event/property");
403       digester.addSetNext("dbforms-config/table/events/event/property",
404                           "addProperty",
405                           "org.dbforms.config.DbConnectionProperty");
406
407       // parse "Query" - object + add it to parent
408
digester.addObjectCreate("dbforms-config/query", queryClassName);
409       digester.addSetProperties("dbforms-config/query");
410       digester.addSetNext("dbforms-config/query", "addTable", BASE_CLASS_TABLE);
411
412       // parse "Field" - object + add it to parent (which is "Query")
413
digester.addObjectCreate("dbforms-config/query/field", fieldClassName);
414       digester.addSetProperties("dbforms-config/query/field");
415       digester.addSetNext("dbforms-config/query/field", "addField",
416                           BASE_CLASS_FIELD);
417
418       // parse "calc" - object + add it to parent (which is "Table")
419
digester.addObjectCreate("dbforms-config/query/calc", fieldClassName);
420       digester.addSetProperties("dbforms-config/query/calc");
421       digester.addSetNext("dbforms-config/query/calc", "addCalcField",
422                           BASE_CLASS_FIELD);
423
424       
425       // parse "search" - object + add it to parent (which is "Query")
426
digester.addObjectCreate("dbforms-config/query/search", fieldClassName);
427       digester.addSetProperties("dbforms-config/query/search");
428       digester.addSetNext("dbforms-config/query/search", "addSearchField",
429                           BASE_CLASS_FIELD);
430
431       // parse "Foreign-Key" - object + add it to parent (which is "Table")
432
digester.addObjectCreate("dbforms-config/query/foreign-key",
433                                foreignKeyClassName);
434       digester.addSetProperties("dbforms-config/query/foreign-key");
435       digester.addSetNext("dbforms-config/query/foreign-key", "addForeignKey",
436                           BASE_CLASS_FOREIGNKEY);
437
438       // parse "Reference" - object + add it to parent (which is "ForeignKey")
439
digester.addObjectCreate("dbforms-config/query/foreign-key/reference",
440                                referenceClassName);
441       digester.addSetProperties("dbforms-config/query/foreign-key/reference");
442       digester.addSetNext("dbforms-config/query/foreign-key/reference",
443                           "addReference", BASE_CLASS_REFERENCE);
444
445       // parse "GrantedPrivileges" - object + add it to parent (which is "Query")
446
digester.addObjectCreate("dbforms-config/query/granted-privileges",
447                                "org.dbforms.config.GrantedPrivileges");
448       digester.addSetProperties("dbforms-config/query/granted-privileges");
449       digester.addSetNext("dbforms-config/query/granted-privileges",
450                           "setGrantedPrivileges",
451                           "org.dbforms.config.GrantedPrivileges");
452
453       // parse "Condition" - object + add it to parent (which is "Query")
454
digester.addObjectCreate("dbforms-config/query/interceptor",
455                                "org.dbforms.config.Interceptor");
456       digester.addSetProperties("dbforms-config/query/interceptor");
457       digester.addSetNext("dbforms-config/query/interceptor", "addInterceptor",
458                           "org.dbforms.config.Interceptor");
459
460       // J.Peer 03/2004: parse interceptor "param"s + add it to parent (which is "Interceptor")
461
digester.addCallMethod("dbforms-config/query/interceptor/param",
462                              "addParam", 2);
463       digester.addCallParam("dbforms-config/query/interceptor/param", 0, "name");
464       digester.addCallParam("dbforms-config/query/interceptor/param", 1, "value");
465
466       // register custom database or navigation events (parent is "Query");
467
// 1) for every "events" element, instance a new TableEvents object;
468
// 2) set the TableEvents reference into the Table object
469
// 3) for every "event" element, instance a new EventInfo object and set its properties ("type" and "id")
470
// 4) register the EventInfo object into the TableEvents via TableEvents.addEventInfo()
471
// 5) for every event's property attribute, instance a new Property object
472
// and and set its properties ("name" and "value")
473
// 6) register the Property object into the EventInfo object
474
digester.addObjectCreate("dbforms-config/query/events",
475                                "org.dbforms.config.TableEvents");
476       digester.addSetNext("dbforms-config/query/events", "setTableEvents",
477                           "org.dbforms.config.TableEvents");
478       digester.addObjectCreate("dbforms-config/query/events/event",
479                                "org.dbforms.config.EventInfo");
480       digester.addSetProperties("dbforms-config/query/events/event");
481       digester.addSetNext("dbforms-config/query/events/event", "addEventInfo",
482                           "org.dbforms.config.EventInfo");
483       digester.addObjectCreate("dbforms-config/query/events/event/property",
484                                "org.dbforms.config.DbConnectionProperty");
485       digester.addSetProperties("dbforms-config/query/events/event/property");
486       digester.addSetNext("dbforms-config/query/events/event/property",
487                           "addProperty",
488                           "org.dbforms.config.DbConnectionProperty");
489
490       // parse "DbConnecion" - object
491
digester.addObjectCreate("dbforms-config/dbconnection",
492                                "org.dbforms.config.DbConnection");
493       digester.addSetProperties("dbforms-config/dbconnection");
494       digester.addSetNext("dbforms-config/dbconnection", "addDbConnection",
495                           "org.dbforms.config.DbConnection");
496
497       // parse "property" - object + add it to parent (which is "DbConnection")
498
digester.addObjectCreate("dbforms-config/dbconnection/property",
499                                "org.dbforms.config.DbConnectionProperty");
500       digester.addSetProperties("dbforms-config/dbconnection/property");
501       digester.addSetNext("dbforms-config/dbconnection/property",
502                           "addProperty",
503                           "org.dbforms.config.DbConnectionProperty");
504
505       // parse "pool-property" - object + add it to parent (which is "DbConnection")
506
digester.addObjectCreate("dbforms-config/dbconnection/pool-property",
507                                "org.dbforms.config.DbConnectionProperty");
508       digester.addSetProperties("dbforms-config/dbconnection/pool-property");
509       digester.addSetNext("dbforms-config/dbconnection/pool-property",
510                           "addPoolProperty",
511                           "org.dbforms.config.DbConnectionProperty");
512
513       // parse "Interceptor" - object + add it to parent (which is "DbConfig")
514
digester.addObjectCreate("dbforms-config/interceptors/interceptor",
515                                "org.dbforms.config.Interceptor");
516       digester.addSetProperties("dbforms-config/interceptors/interceptor");
517       digester.addSetNext("dbforms-config/interceptors/interceptor",
518                           "addInterceptor", "org.dbforms.config.Interceptor");
519
520       // J.Peer 03/2004: parse interceptor "param"s + add it to parent (which is "Interceptor")
521
digester.addCallMethod("dbforms-config/interceptors/interceptor/param",
522                              "addParam", 2);
523       digester.addCallParam("dbforms-config/interceptors/interceptor/param", 0,
524                             "name");
525       digester.addCallParam("dbforms-config/interceptors/interceptor/param", 1,
526                             "value");
527
528       // parse "database-events/database-event" - object and register them into the DatabaseEventsFactory
529
digester.addFactoryCreate("dbforms-config/events/database-events",
530                                 new SingletonClassFactoryCreate(DatabaseEventFactoryImpl.class
531                                                                 .getName()));
532       digester.addObjectCreate("dbforms-config/events/database-events/database-event",
533                                "org.dbforms.config.EventInfo");
534       digester.addSetProperties("dbforms-config/events/database-events/database-event");
535       digester.addSetNext("dbforms-config/events/database-events/database-event",
536                           "addEventInfo", "org.dbforms.config.EventInfo");
537
538       // parse "database-events/navigation-event" - object and register them into the NavigationEventsFactory
539
digester.addFactoryCreate("dbforms-config/events/navigation-events",
540                                 new SingletonClassFactoryCreate(NavEventFactoryImpl.class
541                                                                 .getName()));
542       digester.addObjectCreate("dbforms-config/events/navigation-events/navigation-event",
543                                "org.dbforms.config.EventInfo");
544       digester.addSetProperties("dbforms-config/events/navigation-events/navigation-event");
545       digester.addSetNext("dbforms-config/events/navigation-events/navigation-event",
546                           "addEventInfo", "org.dbforms.config.EventInfo");
547
548       return digester;
549    }
550
551
552    /**
553     * Construct and return a digester that uses the new errors file format.
554     *
555     * @param dbFormsErrors DOCUMENT ME!
556     *
557     * @return DOCUMENT ME!
558     */

559    protected Digester initErrorsDigester(DbFormsErrors dbFormsErrors) {
560       // Initialize a new Digester instance
561
logCat.info("initialize Errors Digester.");
562
563       Digester digester = new Digester();
564       digester.setLogger(LogFactory.getLog(Digester.class));
565       digester.push(dbFormsErrors);
566       digester.setValidating(false);
567
568       // Configure the processing rules
569
// parse "Error" - object
570
digester.addObjectCreate("dbforms-errors/error",
571                                "org.dbforms.config.Error");
572       digester.addSetProperties("dbforms-errors/error");
573       digester.addSetNext("dbforms-errors/error", "addError",
574                           "org.dbforms.config.Error");
575
576       // parse "Message" - object + add it to parent (which is "Error")
577
digester.addObjectCreate("dbforms-errors/error/message",
578                                "org.dbforms.config.Message");
579       digester.addSetNext("dbforms-errors/error/message", "addMessage",
580                           "org.dbforms.config.Message");
581       digester.addSetProperties("dbforms-errors/error/message");
582       digester.addCallMethod("dbforms-errors/error/message", "setMessage", 0);
583
584       return digester;
585    }
586 }
587
Popular Tags