1 18 19 20 package org.apache.struts.config; 21 22 23 import org.apache.commons.digester.AbstractObjectCreationFactory; 24 import org.apache.commons.digester.Digester; 25 import org.apache.commons.digester.Rule; 26 import org.apache.commons.digester.RuleSetBase; 27 import org.apache.struts.util.RequestUtils; 28 import org.xml.sax.Attributes ; 29 30 31 38 39 public class ConfigRuleSet extends RuleSetBase { 40 41 42 44 45 56 public void addRuleInstances(Digester digester) { 57 58 digester.addObjectCreate 59 ("struts-config/data-sources/data-source", 60 "org.apache.struts.config.DataSourceConfig", 61 "className"); 62 digester.addSetProperties 63 ("struts-config/data-sources/data-source"); 64 digester.addSetNext 65 ("struts-config/data-sources/data-source", 66 "addDataSourceConfig", 67 "org.apache.struts.config.DataSourceConfig"); 68 69 digester.addRule 70 ("struts-config/data-sources/data-source/set-property", 71 new AddDataSourcePropertyRule()); 72 73 digester.addRule 74 ("struts-config/action-mappings", 75 new SetActionMappingClassRule()); 76 77 digester.addFactoryCreate 78 ("struts-config/action-mappings/action", 79 new ActionMappingFactory()); 80 digester.addSetProperties 81 ("struts-config/action-mappings/action"); 82 digester.addSetNext 83 ("struts-config/action-mappings/action", 84 "addActionConfig", 85 "org.apache.struts.config.ActionConfig"); 86 87 digester.addSetProperty 88 ("struts-config/action-mappings/action/set-property", 89 "property", "value"); 90 91 digester.addObjectCreate 92 ("struts-config/action-mappings/action/exception", 93 "org.apache.struts.config.ExceptionConfig", 94 "className"); 95 digester.addSetProperties 96 ("struts-config/action-mappings/action/exception"); 97 digester.addSetNext 98 ("struts-config/action-mappings/action/exception", 99 "addExceptionConfig", 100 "org.apache.struts.config.ExceptionConfig"); 101 102 digester.addSetProperty 103 ("struts-config/action-mappings/action/exception/set-property", 104 "property", "value"); 105 106 digester.addFactoryCreate 107 ("struts-config/action-mappings/action/forward", 108 new ActionForwardFactory()); 109 digester.addSetProperties 110 ("struts-config/action-mappings/action/forward"); 111 digester.addSetNext 112 ("struts-config/action-mappings/action/forward", 113 "addForwardConfig", 114 "org.apache.struts.config.ForwardConfig"); 115 116 digester.addSetProperty 117 ("struts-config/action-mappings/action/forward/set-property", 118 "property", "value"); 119 120 digester.addObjectCreate 121 ("struts-config/controller", 122 "org.apache.struts.config.ControllerConfig", 123 "className"); 124 digester.addSetProperties 125 ("struts-config/controller"); 126 digester.addSetNext 127 ("struts-config/controller", 128 "setControllerConfig", 129 "org.apache.struts.config.ControllerConfig"); 130 131 digester.addSetProperty 132 ("struts-config/controller/set-property", 133 "property", "value"); 134 135 digester.addRule 136 ("struts-config/form-beans", 137 new SetActionFormBeanClassRule()); 138 139 digester.addFactoryCreate 140 ("struts-config/form-beans/form-bean", 141 new ActionFormBeanFactory()); 142 digester.addSetProperties 143 ("struts-config/form-beans/form-bean"); 144 digester.addSetNext 145 ("struts-config/form-beans/form-bean", 146 "addFormBeanConfig", 147 "org.apache.struts.config.FormBeanConfig"); 148 149 digester.addObjectCreate 150 ("struts-config/form-beans/form-bean/form-property", 151 "org.apache.struts.config.FormPropertyConfig", 152 "className"); 153 digester.addSetProperties 154 ("struts-config/form-beans/form-bean/form-property"); 155 digester.addSetNext 156 ("struts-config/form-beans/form-bean/form-property", 157 "addFormPropertyConfig", 158 "org.apache.struts.config.FormPropertyConfig"); 159 160 digester.addSetProperty 161 ("struts-config/form-beans/form-bean/form-property/set-property", 162 "property", "value"); 163 164 digester.addSetProperty 165 ("struts-config/form-beans/form-bean/set-property", 166 "property", "value"); 167 168 digester.addObjectCreate 169 ("struts-config/global-exceptions/exception", 170 "org.apache.struts.config.ExceptionConfig", 171 "className"); 172 digester.addSetProperties 173 ("struts-config/global-exceptions/exception"); 174 digester.addSetNext 175 ("struts-config/global-exceptions/exception", 176 "addExceptionConfig", 177 "org.apache.struts.config.ExceptionConfig"); 178 179 digester.addSetProperty 180 ("struts-config/global-exceptions/exception/set-property", 181 "property", "value"); 182 183 digester.addRule 184 ("struts-config/global-forwards", 185 new SetActionForwardClassRule()); 186 187 digester.addFactoryCreate 188 ("struts-config/global-forwards/forward", 189 new GlobalForwardFactory()); 190 digester.addSetProperties 191 ("struts-config/global-forwards/forward"); 192 digester.addSetNext 193 ("struts-config/global-forwards/forward", 194 "addForwardConfig", 195 "org.apache.struts.config.ForwardConfig"); 196 197 digester.addSetProperty 198 ("struts-config/global-forwards/forward/set-property", 199 "property", "value"); 200 201 digester.addObjectCreate 202 ("struts-config/message-resources", 203 "org.apache.struts.config.MessageResourcesConfig", 204 "className"); 205 digester.addSetProperties 206 ("struts-config/message-resources"); 207 digester.addSetNext 208 ("struts-config/message-resources", 209 "addMessageResourcesConfig", 210 "org.apache.struts.config.MessageResourcesConfig"); 211 212 digester.addSetProperty 213 ("struts-config/message-resources/set-property", 214 "property", "value"); 215 216 digester.addObjectCreate 217 ("struts-config/plug-in", 218 "org.apache.struts.config.PlugInConfig"); 219 digester.addSetProperties 220 ("struts-config/plug-in"); 221 digester.addSetNext 222 ("struts-config/plug-in", 223 "addPlugInConfig", 224 "org.apache.struts.config.PlugInConfig"); 225 226 digester.addRule 227 ("struts-config/plug-in/set-property", 228 new PlugInSetPropertyRule()); 229 230 } 231 232 } 233 234 235 240 241 final class AddDataSourcePropertyRule extends Rule { 242 243 public AddDataSourcePropertyRule() { 244 super(); 245 } 246 247 public void begin(String namespace, String name, Attributes attributes) throws Exception { 248 DataSourceConfig dsc = (DataSourceConfig) digester.peek(); 249 dsc.addProperty(attributes.getValue("property"), 250 attributes.getValue("value")); 251 } 252 253 } 254 255 256 260 261 final class PlugInSetPropertyRule extends Rule { 262 263 public PlugInSetPropertyRule() { 264 super(); 265 } 266 267 public void begin(String namespace, String names, Attributes attributes) throws Exception { 268 PlugInConfig plugInConfig = (PlugInConfig) digester.peek(); 269 plugInConfig.addProperty(attributes.getValue("property"), 270 attributes.getValue("value")); 271 } 272 273 } 274 275 276 281 final class SetActionFormBeanClassRule extends Rule { 282 283 public SetActionFormBeanClassRule() { 284 super(); 285 } 286 287 public void begin(String namespace, String name, Attributes attributes) throws Exception { 288 String className = attributes.getValue("type"); 289 if (className != null) { 290 ModuleConfig mc = (ModuleConfig) digester.peek(); 291 mc.setActionFormBeanClass(className); 292 } 293 } 294 295 } 296 297 298 305 final class ActionFormBeanFactory extends AbstractObjectCreationFactory { 306 307 public Object createObject(Attributes attributes) { 308 309 String className = attributes.getValue("className"); 311 if (className == null) { 312 ModuleConfig mc = (ModuleConfig) digester.peek(); 313 className = mc.getActionFormBeanClass(); 314 } 315 316 Object actionFormBean = null; 318 try { 319 actionFormBean = 320 RequestUtils.applicationInstance(className); 321 } catch (Exception e) { 322 digester.getLogger().error( 323 "ActionFormBeanFactory.createObject: ", e); 324 } 325 326 return actionFormBean; 327 } 328 329 } 330 331 332 337 final class SetActionMappingClassRule extends Rule { 338 339 public SetActionMappingClassRule() { 340 super(); 341 } 342 343 public void begin(String namespace, String name, Attributes attributes) throws Exception { 344 String className = attributes.getValue("type"); 345 if (className != null) { 346 ModuleConfig mc = (ModuleConfig) digester.peek(); 347 mc.setActionMappingClass(className); 348 } 349 } 350 351 } 352 353 354 361 final class ActionMappingFactory extends AbstractObjectCreationFactory { 362 363 public Object createObject(Attributes attributes) { 364 365 String className = attributes.getValue("className"); 367 if (className == null) { 368 ModuleConfig mc = (ModuleConfig) digester.peek(); 369 className = mc.getActionMappingClass(); 370 } 371 372 Object actionMapping = null; 374 try { 375 actionMapping = 376 RequestUtils.applicationInstance(className); 377 } catch (Exception e) { 378 digester.getLogger().error( 379 "ActionMappingFactory.createObject: ", e); 380 } 381 382 return actionMapping; 383 } 384 385 } 386 387 388 393 final class SetActionForwardClassRule extends Rule { 394 395 public SetActionForwardClassRule() { 396 super(); 397 } 398 399 public void begin(String namespace, String name, Attributes attributes) throws Exception { 400 String className = attributes.getValue("type"); 401 if (className != null) { 402 ModuleConfig mc = (ModuleConfig) digester.peek(); 403 mc.setActionForwardClass(className); 404 } 405 } 406 407 } 408 409 410 417 final class GlobalForwardFactory extends AbstractObjectCreationFactory { 418 419 public Object createObject(Attributes attributes) { 420 421 String className = attributes.getValue("className"); 423 if (className == null) { 424 ModuleConfig mc = (ModuleConfig) digester.peek(); 425 className = mc.getActionForwardClass(); 426 } 427 428 Object globalForward = null; 430 try { 431 globalForward = 432 RequestUtils.applicationInstance(className); 433 } catch (Exception e) { 434 digester.getLogger().error( 435 "GlobalForwardFactory.createObject: ", e); 436 } 437 438 return globalForward; 439 } 440 441 } 442 443 444 451 final class ActionForwardFactory extends AbstractObjectCreationFactory { 452 453 public Object createObject(Attributes attributes) { 454 455 String className = attributes.getValue("className"); 457 if (className == null) { 458 ModuleConfig mc = (ModuleConfig) digester.peek(1); 459 className = mc.getActionForwardClass(); 460 } 461 462 Object actionForward = null; 464 try { 465 actionForward = 466 RequestUtils.applicationInstance(className); 467 } catch (Exception e) { 468 digester.getLogger().error( 469 "ActionForwardFactory.createObject: ", e); 470 } 471 472 return actionForward; 473 } 474 475 } 476 | Popular Tags |