1 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 50 public class ConfigLoader { 51 52 public static final String BASE_CLASS_TABLE = "org.dbforms.config.Table"; 53 54 55 public static final String BASE_CLASS_FIELD = "org.dbforms.config.Field"; 56 57 58 public static final String BASE_CLASS_QUERY = "org.dbforms.config.Query"; 59 60 61 public static final String BASE_CLASS_FOREIGNKEY = "org.dbforms.config.ForeignKey"; 62 63 64 public static final String BASE_CLASS_REFERENCE = "org.dbforms.config.Reference"; 65 66 67 public static final String DEFAULT_CONFIG = "/WEB-INF/dbforms-config.xml"; 68 69 70 public static final String DEFAULT_ERRORS = "/WEB-INF/dbforms-errors.xml"; 71 72 73 public static final String DEFAULT_VALIDATION = "/WEB-INF/validation.xml"; 74 75 76 public static final String DEFAULT_RULES = "/WEB-INF/validator-rules.xml"; 77 private static Log logCat = LogFactory.getLog(ConfigLoader.class); 78 79 80 private String config = DEFAULT_CONFIG; 81 82 83 private String errors = DEFAULT_ERRORS; 84 85 86 private String fieldClassName = BASE_CLASS_FIELD; 87 88 89 private String foreignKeyClassName = BASE_CLASS_FOREIGNKEY; 90 91 92 private String queryClassName = BASE_CLASS_QUERY; 93 94 95 private String referenceClassName = BASE_CLASS_REFERENCE; 96 97 98 private String tableClassName = BASE_CLASS_TABLE; 99 100 101 private String validation = DEFAULT_VALIDATION; 102 103 104 private String validator_rules = DEFAULT_RULES; 105 106 109 public ConfigLoader() { 110 } 111 112 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 131 public String getConfig() { 132 return config; 133 } 134 135 136 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 155 public String getErrors() { 156 return errors; 157 } 158 159 160 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 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 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 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 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 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 249 public String getValidation() { 250 return validation; 251 } 252 253 254 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 273 public String getValidatorRules() { 274 return validator_rules; 275 } 276 277 278 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 298 public void loadErrors(InputStream input, 299 DbFormsErrors dbFormsErrors) 300 throws IOException , SAXException { 301 Digester digester = initErrorsDigester(dbFormsErrors); 302 303 digester.parse(input); 305 } 306 307 308 316 protected Digester initDigester(DbFormsConfig dbFormsConfig) { 317 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 digester.addCallMethod("dbforms-config/DOMFactoryClass", 327 "setDOMFactoryClass", 0); 328 digester.addCallMethod("dbforms-config/DefaultEscaperClass", 329 "setDefaultEscaperClass", 0); 330 331 digester.addObjectCreate("dbforms-config/table", tableClassName); 333 digester.addSetProperties("dbforms-config/table"); 334 digester.addSetNext("dbforms-config/table", "addTable", BASE_CLASS_TABLE); 335 336 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 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 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 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 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 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 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 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 digester.addObjectCreate("dbforms-config/query", queryClassName); 409 digester.addSetProperties("dbforms-config/query"); 410 digester.addSetNext("dbforms-config/query", "addTable", BASE_CLASS_TABLE); 411 412 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 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 559 protected Digester initErrorsDigester(DbFormsErrors dbFormsErrors) { 560 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 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 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 |