1 16 package org.apache.commons.betwixt.io.read; 17 18 import java.beans.IntrospectionException ; 19 import java.util.HashMap ; 20 21 import org.apache.commons.betwixt.AttributeDescriptor; 22 import org.apache.commons.betwixt.BindingConfiguration; 23 import org.apache.commons.betwixt.ElementDescriptor; 24 import org.apache.commons.betwixt.XMLBeanInfo; 25 import org.apache.commons.betwixt.XMLIntrospector; 26 import org.apache.commons.betwixt.expression.Context; 27 import org.apache.commons.betwixt.expression.Updater; 28 import org.apache.commons.betwixt.strategy.ActionMappingStrategy; 29 import org.apache.commons.collections.ArrayStack; 30 import org.apache.commons.logging.Log; 31 import org.apache.commons.logging.LogFactory; 32 import org.xml.sax.Attributes ; 33 34 51 public class ReadContext extends Context { 52 53 54 private HashMap beansById = new HashMap (); 55 56 private ClassLoader classLoader; 57 58 private ReadConfiguration readConfiguration; 59 60 private ArrayStack elementMappingStack = new ArrayStack(); 61 62 private ArrayStack actionMappingStack = new ArrayStack(); 63 64 private ArrayStack objectStack = new ArrayStack(); 65 66 private ArrayStack descriptorStack = new ArrayStack(); 67 68 private ArrayStack updaterStack = new ArrayStack(); 69 70 private Class rootClass; 71 72 private XMLIntrospector xmlIntrospector; 73 74 80 public ReadContext(Context context, ReadConfiguration readConfiguration) { 81 super(context); 82 this.readConfiguration = readConfiguration; 83 } 84 85 90 public ReadContext( 91 BindingConfiguration bindingConfiguration, 92 ReadConfiguration readConfiguration) { 93 this( 94 LogFactory.getLog(ReadContext.class), 95 bindingConfiguration, 96 readConfiguration); 97 } 98 99 105 public ReadContext( 106 Log log, 107 BindingConfiguration bindingConfiguration, 108 ReadConfiguration readConfiguration) { 109 super(null, log, bindingConfiguration); 110 this.readConfiguration = readConfiguration; 111 } 112 113 118 public ReadContext(ReadContext readContext) { 119 super(readContext); 120 beansById = readContext.beansById; 121 classLoader = readContext.classLoader; 122 readConfiguration = readContext.readConfiguration; 123 } 124 125 131 public void putBean(String id, Object bean) { 132 beansById.put(id, bean); 133 } 134 135 141 public Object getBean(String id) { 142 return beansById.get(id); 143 } 144 145 148 public void clearBeans() { 149 beansById.clear(); 150 } 151 152 156 public ClassLoader getClassLoader() { 157 return classLoader; 158 } 159 160 164 public void setClassLoader(ClassLoader classLoader) { 165 this.classLoader = classLoader; 166 } 167 168 173 public BeanCreationChain getBeanCreationChain() { 174 return readConfiguration.getBeanCreationChain(); 175 } 176 177 182 public ActionMappingStrategy getActionMappingStrategy() { 183 return readConfiguration.getActionMappingStrategy(); 184 } 185 186 194 public String popElement() { 195 if (!descriptorStack.isEmpty()) { 198 descriptorStack.pop(); 199 } 200 201 if (!updaterStack.isEmpty()) { 202 updaterStack.pop(); 203 } 204 205 Object top = null; 206 if (!elementMappingStack.isEmpty()) { 207 top = elementMappingStack.pop(); 208 if (top != null) { 209 if (!(top instanceof String )) { 210 return popElement(); 211 } 212 } 213 } 214 215 return (String ) top; 216 } 217 218 223 public String getCurrentElement() { 224 return (String ) elementMappingStack.peek(); 225 } 226 227 233 public Class getLastMappedClass() { 234 Class lastMapped = null; 235 for (int i = 0, size = elementMappingStack.size(); 236 i < size; 237 i++) { 238 Object entry = elementMappingStack.peek(i); 239 if (entry instanceof Class ) { 240 lastMapped = (Class ) entry; 241 break; 242 } 243 } 244 return lastMapped; 245 } 246 247 private ElementDescriptor getParentDescriptor() throws IntrospectionException { 248 ElementDescriptor result = null; 249 if (descriptorStack.size() > 1) { 250 result = (ElementDescriptor) descriptorStack.peek(1); 251 } 252 return result; 253 } 254 255 256 262 public void pushElement(String elementName) throws Exception { 263 264 elementMappingStack.push(elementName); 265 ElementDescriptor nextDescriptor = null; 268 if (elementMappingStack.size() == 1 && rootClass != null) { 269 markClassMap(rootClass); 270 XMLBeanInfo rootClassInfo 271 = getXMLIntrospector().introspect(rootClass); 272 nextDescriptor = rootClassInfo.getElementDescriptor(); 273 } else { 274 ElementDescriptor currentDescriptor = getCurrentDescriptor(); 275 if (currentDescriptor != null) { 276 nextDescriptor = currentDescriptor.getElementDescriptor(elementName); 277 } 278 } 279 Updater updater = null; 280 if (nextDescriptor != null) { 281 updater = nextDescriptor.getUpdater(); 282 } 283 updaterStack.push(updater); 284 descriptorStack.push(nextDescriptor); 285 } 286 287 293 public void markClassMap(Class mappedClazz) throws IntrospectionException { 294 if (mappedClazz.isArray()) { 295 mappedClazz = mappedClazz.getComponentType(); 296 } 297 elementMappingStack.push(mappedClazz); 298 299 XMLBeanInfo mappedClassInfo = getXMLIntrospector().introspect(mappedClazz); 300 ElementDescriptor mappedElementDescriptor = mappedClassInfo.getElementDescriptor(); 301 descriptorStack.push(mappedElementDescriptor); 302 303 Updater updater = mappedElementDescriptor.getUpdater(); 304 updaterStack.push(updater); 305 } 306 307 311 public MappingAction popMappingAction() { 312 return (MappingAction) actionMappingStack.pop(); 313 } 314 315 319 public void pushMappingAction(MappingAction mappingAction) { 320 actionMappingStack.push(mappingAction); 321 } 322 323 327 public MappingAction currentMappingAction() { 328 if (actionMappingStack.size() == 0) 329 { 330 return null; 331 } 332 return (MappingAction) actionMappingStack.peek(); 333 } 334 335 public Object getBean() { 336 return objectStack.peek(); 337 } 338 339 public void setBean(Object bean) { 340 } 344 345 350 public Object popBean() { 351 return objectStack.pop(); 352 } 353 354 358 public void pushBean(Object bean) { 359 objectStack.push(bean); 360 } 361 362 367 public XMLIntrospector getXMLIntrospector() { 368 if (xmlIntrospector == null) { 371 xmlIntrospector = new XMLIntrospector(); 372 } 373 return xmlIntrospector; 374 } 375 376 381 public void setXMLIntrospector(XMLIntrospector xmlIntrospector) { 382 this.xmlIntrospector = xmlIntrospector; 383 } 384 385 public Class getRootClass() { 386 return rootClass; 387 } 388 389 public void setRootClass(Class rootClass) { 390 this.rootClass = rootClass; 391 } 392 393 400 public ElementDescriptor getCurrentDescriptor() throws Exception { 401 ElementDescriptor result = null; 402 if (!descriptorStack.empty()) { 403 result = (ElementDescriptor) descriptorStack.peek(); 404 } 405 return result; 406 } 407 408 414 public void populateAttributes( 415 AttributeDescriptor[] attributeDescriptors, 416 Attributes attributes) { 417 418 Log log = getLog(); 419 if (attributeDescriptors != null) { 420 for (int i = 0, size = attributeDescriptors.length; 421 i < size; 422 i++) { 423 AttributeDescriptor attributeDescriptor = 424 attributeDescriptors[i]; 425 426 String value = 431 attributes.getValue( 432 attributeDescriptor.getURI(), 433 attributeDescriptor.getLocalName()); 434 435 if (value == null) { 436 value = 437 attributes.getValue( 438 attributeDescriptor.getQualifiedName()); 439 } 440 441 if (log.isTraceEnabled()) { 442 log.trace("Attr URL:" + attributeDescriptor.getURI()); 443 log.trace( 444 "Attr LocalName:" + attributeDescriptor.getLocalName()); 445 log.trace(value); 446 } 447 448 Updater updater = attributeDescriptor.getUpdater(); 449 log.trace(updater); 450 if (updater != null && value != null) { 451 updater.update(this, value); 452 } 453 } 454 } 455 } 456 457 472 public void pushUpdater(Updater updater) { 473 updaterStack.push(updater); 474 } 475 476 485 public Updater popUpdater() { 486 return (Updater) updaterStack.pop(); 487 } 488 489 500 public Updater getCurrentUpdater() { 501 Updater result = null; 507 if (!updaterStack.empty()) { 508 result = (Updater) updaterStack.peek(); 509 if ( result == null && updaterStack.size() >1 ) { 510 result = (Updater) updaterStack.peek(1); 511 } 512 } 513 return result; 514 } 515 516 } 517 | Popular Tags |