View Javadoc

1   /*
2    * $Header: /cvsroot/jdbforms/dbforms/src/org/dbforms/config/ConfigLoader.java,v 1.9 2006/01/26 13:07:16 hkollmann Exp $
3    * $Revision: 1.9 $
4    * $Date: 2006/01/26 13:07:16 $
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;
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       // 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       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       // parse "Table" - object + add it to parent
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       // parse "Field" - object + add it to parent (which is "Table")
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       // parse "calc" - object + add it to parent (which is "Table")
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       // parse "Foreign-Key" - object + add it to parent (which is "Table")
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       // parse "Reference" - object + add it to parent (which is "ForeignKey")
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       // parse "GrantedPrivileges" - object + add it to parent (which is "Table")
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       // parse "Interceptor" - object + add it to parent (which is "Table")
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       // J.Peer 03/2004: parse interceptor "param"s + add it to parent (which is "Interceptor")
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       // register custom database or navigation events (parent is "Table");
385       // 1) for every "events" element, instance a new TableEvents object;
386       // 2) set the TableEvents reference into the Table object
387       // 3) for every "event" element, instance a new EventInfo object and set its properties ("type" and "id")
388       // 4) register the EventInfo object into the TableEvents via TableEvents.addEventInfo()
389       // 5) for every event's property attribute, instance a new Property object
390       //    and and set its properties ("name" and "value")
391       // 6) register the Property object into the EventInfo object
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       // parse "Query" - object + add it to parent
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       // parse "Field" - object + add it to parent (which is "Query")
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       // parse "calc" - object + add it to parent (which is "Table")
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       // parse "search" - object + add it to parent (which is "Query")
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       // parse "Foreign-Key" - object + add it to parent (which is "Table")
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       // parse "Reference" - object + add it to parent (which is "ForeignKey")
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       // parse "GrantedPrivileges" - object + add it to parent (which is "Query")
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       // parse "Condition" - object + add it to parent (which is "Query")
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       // J.Peer 03/2004: parse interceptor "param"s + add it to parent (which is "Interceptor")
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       // register custom database or navigation events (parent is "Query");
461       // 1) for every "events" element, instance a new TableEvents object;
462       // 2) set the TableEvents reference into the Table object
463       // 3) for every "event" element, instance a new EventInfo object and set its properties ("type" and "id")
464       // 4) register the EventInfo object into the TableEvents via TableEvents.addEventInfo()
465       // 5) for every event's property attribute, instance a new Property object
466       //    and and set its properties ("name" and "value")
467       // 6) register the Property object into the EventInfo object
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       // parse "DbConnecion" - object
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       // parse "property" - object + add it to parent (which is "DbConnection")
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       // parse "pool-property" - object + add it to parent (which is "DbConnection")
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       // parse "Interceptor" - object + add it to parent (which is "DbConfig")
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       // J.Peer 03/2004: parse interceptor "param"s + add it to parent (which is "Interceptor")
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       // parse "database-events/database-event" - object and register them into the DatabaseEventsFactory
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       // parse "database-events/navigation-event" - object and register them into the NavigationEventsFactory
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       // Initialize a new Digester instance
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       // Configure the processing rules
554       // parse "Error" - object
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       // parse "Message" - object + add it to parent (which is "Error")
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 }