1 16 17 package org.apache.commons.betwixt.io; 18 19 import org.apache.commons.betwixt.BindingConfiguration; 20 import org.apache.commons.betwixt.ElementDescriptor; 21 import org.apache.commons.betwixt.XMLIntrospector; 22 import org.apache.commons.betwixt.expression.Context; 23 import org.apache.commons.betwixt.io.read.BeanBindAction; 24 import org.apache.commons.betwixt.io.read.MappingAction; 25 import org.apache.commons.betwixt.io.read.ReadConfiguration; 26 import org.apache.commons.betwixt.io.read.ReadContext; 27 import org.apache.commons.digester.Digester; 28 import org.apache.commons.digester.Rule; 29 import org.apache.commons.digester.RuleSet; 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 import org.xml.sax.Attributes ; 33 34 40 public class BeanRuleSet implements RuleSet { 41 42 43 private static Log log = LogFactory.getLog(BeanRuleSet.class); 44 45 49 public static void setLog(Log aLog) { 50 log = aLog; 51 } 52 53 54 private String basePath; 55 56 private ElementDescriptor baseElementDescriptor; 57 59 private DigesterReadContext context; 60 61 private String classNameAttribute = "className"; 62 63 73 public BeanRuleSet( 74 XMLIntrospector introspector, 75 String basePath, 76 ElementDescriptor baseElementDescriptor, 77 Class baseBeanClass, 78 boolean matchIDs) { 79 this.basePath = basePath; 80 this.baseElementDescriptor = baseElementDescriptor; 81 BindingConfiguration bindingConfiguration = new BindingConfiguration(); 82 bindingConfiguration.setMapIDs(matchIDs); 83 context = 84 new DigesterReadContext( 85 log, 86 bindingConfiguration, 87 new ReadConfiguration()); 88 context.setRootClass(baseBeanClass); 89 context.setXMLIntrospector(introspector); 90 } 91 92 103 public BeanRuleSet( 104 XMLIntrospector introspector, 105 String basePath, 106 ElementDescriptor baseElementDescriptor, 107 Context context) { 108 109 this.basePath = basePath; 110 this.baseElementDescriptor = baseElementDescriptor; 111 this.context = 112 new DigesterReadContext(context, new ReadConfiguration()); 113 this.context.setRootClass( 114 baseElementDescriptor.getSingularPropertyType()); 115 this.context.setXMLIntrospector(introspector); 116 } 117 118 129 public BeanRuleSet( 130 XMLIntrospector introspector, 131 String basePath, 132 ElementDescriptor baseElementDescriptor, 133 Class baseBeanClass, 134 Context context) { 135 this( 136 introspector, 137 basePath, 138 baseElementDescriptor, 139 baseBeanClass, 140 new ReadContext( context, new ReadConfiguration() )); 141 } 142 143 153 public BeanRuleSet( 154 XMLIntrospector introspector, 155 String basePath, 156 ElementDescriptor baseElementDescriptor, 157 Class baseBeanClass, 158 ReadContext baseContext) { 159 this.basePath = basePath; 160 this.baseElementDescriptor = baseElementDescriptor; 161 this.context = new DigesterReadContext(baseContext); 162 this.context.setRootClass(baseBeanClass); 163 this.context.setXMLIntrospector(introspector); 164 } 165 166 174 public String getClassNameAttribute() { 175 return context.getClassNameAttribute(); 176 } 177 178 188 public void setClassNameAttribute(String classNameAttribute) { 189 context.setClassNameAttribute(classNameAttribute); 190 } 191 192 194 201 public String getNamespaceURI() { 202 return null; 203 } 204 205 210 public void addRuleInstances(Digester digester) { 211 if (log.isTraceEnabled()) { 212 log.trace("Adding rules to:" + digester); 213 } 214 215 context.setDigester(digester); 216 217 if (context.getClassLoader() == null) { 219 context.setClassLoader(digester.getClassLoader()); 220 } 221 222 digester.addRule("!" + basePath + "/*", new ActionMappingRule()); 225 } 226 227 232 private final class ActionMappingRule extends Rule { 233 234 239 public void begin(String namespace, String name, Attributes attributes) 240 throws Exception { 241 242 if (log.isTraceEnabled()) { 243 int attributesLength = attributes.getLength(); 244 if (attributesLength > 0) { 245 log.trace("Attributes:"); 246 } 247 for (int i = 0, size = attributesLength; i < size; i++) { 248 log.trace("Local:" + attributes.getLocalName(i)); 249 log.trace("URI:" + attributes.getURI(i)); 250 log.trace("QName:" + attributes.getQName(i)); 251 } 252 } 253 254 context.pushElement(name); 255 256 MappingAction nextAction = 257 nextAction(namespace, name, attributes, context); 258 259 context.pushMappingAction(nextAction); 260 } 261 262 271 private MappingAction nextAction( 272 String namespace, 273 String name, 274 Attributes attributes, 275 ReadContext context) 276 throws Exception { 277 278 MappingAction result = null; 279 MappingAction lastAction = context.currentMappingAction(); 280 if (lastAction == null) 281 { 282 result = BeanBindAction.INSTANCE; 283 } else { 284 285 result = lastAction.next(namespace, name, attributes, context); 286 } 287 return result.begin(namespace, name, attributes, context); 288 } 289 290 291 292 297 public void body(String namespace, String name, String text) 298 throws Exception { 299 300 log.trace("[BRS] Body with text " + text); 301 if (digester.getCount() > 0) { 302 MappingAction action = context.currentMappingAction(); 303 action.body(text, context); 304 } else { 305 log.trace("[BRS] ZERO COUNT"); 306 } 307 } 308 309 313 public void end(String namespace, String name) throws Exception { 314 315 MappingAction action = context.popMappingAction(); 316 action.end(context); 317 } 318 319 322 public void finish() { 323 context.clearBeans(); 327 } 328 329 } 330 331 336 private static class DigesterReadContext extends ReadContext { 337 338 private Digester digester; 339 340 344 public DigesterReadContext( 345 Context context, 346 ReadConfiguration readConfiguration) { 347 super(context, readConfiguration); 348 } 350 351 355 public DigesterReadContext( 356 BindingConfiguration bindingConfiguration, 357 ReadConfiguration readConfiguration) { 358 super(bindingConfiguration, readConfiguration); 359 } 360 361 366 public DigesterReadContext( 367 Log log, 368 BindingConfiguration bindingConfiguration, 369 ReadConfiguration readConfiguration) { 370 super(log, bindingConfiguration, readConfiguration); 371 } 372 373 378 public DigesterReadContext(ReadContext readContext) { 379 super(readContext); 380 } 381 382 public Digester getDigester() { 383 return digester; 385 } 386 387 public void setDigester(Digester digester) { 388 this.digester = digester; 390 } 391 392 395 public void pushBean(Object bean) { 396 super.pushBean(bean); 397 digester.push(bean); 398 } 399 400 403 public Object popBean() { 404 Object bean = super.popBean(); 405 Object top = digester.pop(); 406 return bean; 407 } 408 } 409 410 } 411 | Popular Tags |