rdonkin 2004/02/21 08:34:57 Modified: betwixt/src/java/org/apache/commons/betwixt/io/read Tag: REFACTORING-BRANCH_2004-01-13 BeanBindAction.java ReadContext.java Log: Refactoring to simplify algorithm Revision Changes Path No revision No revision 1.1.2.4 +22 -143 jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/read/Attic/BeanBindAction.java Index: BeanBindAction.java =================================================================== RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/read/Attic/BeanBindAction.java,v retrieving revision 1.1.2.3 retrieving revision 1.1.2.4 diff -u -r1.1.2.3 -r1.1.2.4 --- BeanBindAction.java 26 Jan 2004 22:20:01 -0000 1.1.2.3 +++ BeanBindAction.java 21 Feb 2004 16:34:57 -0000 1.1.2.4 @@ -61,8 +61,6 @@ */ package org.apache.commons.betwixt.io.read; -import java.util.Iterator; -import java.util.List; import java.util.Map; import org.apache.commons.betwixt.AttributeDescriptor; @@ -71,7 +69,6 @@ import org.apache.commons.betwixt.XMLBeanInfo; import org.apache.commons.betwixt.XMLIntrospector; import org.apache.commons.betwixt.expression.Updater; -import org.apache.commons.collections.IteratorUtils; import org.apache.commons.logging.Log; import org.xml.sax.Attributes; @@ -122,110 +119,31 @@ update(context, instance); } - private void updateCurrentDescriptor(ReadContext context, Object value) - throws Exception { - ElementDescriptor childDescriptor = context.getCurrentDescriptor(); - if (childDescriptor != null) { - Updater updater = childDescriptor.getUpdater(); - if (updater != null) { - updater.update(context, value); - } - } - } - private void update(ReadContext context, Object value) throws Exception { Log log = context.getLog(); boolean popped = false; //TODO: add dyna-bean support! // probably refactoring needed - Iterator relativePathIterator = - context.getRelativeElementPathIterator(); - if (relativePathIterator.hasNext()) { - ElementDescriptor childDescriptor = context.getCurrentDescriptor(); - if (childDescriptor != null) { - Updater updater = childDescriptor.getUpdater(); - if (updater != null) { - updater.update(context, value); - } else { - // this is a workaround to make the tests run - // it will be replaced when proper support for maps is added - List list = - IteratorUtils.toList( - context.getRelativeElementPathIterator()); - if (list.size() > 0) { - list.remove(list.size() - 1); - XMLBeanInfo lastMappedClazzInfo = - context.getLastMappedClassXMLBeanInfo(); - childDescriptor = - lastMappedClazzInfo - .getElementDescriptor() - .getElementDescriptor( - list.iterator()); - updater = childDescriptor.getUpdater(); - if (updater != null) { - - updater.update(context, value); - - } else { - - String poppedElement = context.popElement(); - popped = true; - updateCurrentDescriptor(context, value); - } - - } else { - String poppedElement = context.popElement(); - popped = true; - updateCurrentDescriptor(context, value); - } - } + + ElementDescriptor currentDescriptor = context.getCurrentDescriptor(); + ElementDescriptor parentDescriptor = context.getParentElementDescriptor(); + Updater updater = currentDescriptor.getUpdater(); + if (updater == null) { + if (parentDescriptor != null) { + updater = parentDescriptor.getUpdater(); } - } else { - // in here when we need to get hold of the parent class - String element = context.popElement(); + String poppedElement = context.popElement(); popped = true; - XMLBeanInfo lastMappedClazzInfo = - context.getLastMappedClassXMLBeanInfo(); - if (lastMappedClazzInfo != null) { - - ElementDescriptor baseDescriptor = - lastMappedClazzInfo.getElementDescriptor(); - List list = - IteratorUtils.toList( - context.getRelativeElementPathIterator()); - list.add(element); - ElementDescriptor childDescriptor = - lastMappedClazzInfo - .getElementDescriptor() - .getElementDescriptor( - list.iterator()); - - if (childDescriptor != null) { - list.remove(element); - Iterator iterator = list.iterator(); - updateChild( - context, - value, - lastMappedClazzInfo, - childDescriptor, - iterator, - true); - } else { - childDescriptor = - context.getRelativePathElementDescriptor(); - Iterator iterator = context.getParentElementPathIterator(); - - if (childDescriptor != null) { - updateChild( - context, - value, - lastMappedClazzInfo, - childDescriptor, - iterator, - false); - } + } + + if ( updater == null ) { + if (!context.isAtRootElement() && !context.isStackEmpty()) { + if ( context.getLog().isDebugEnabled() ) { + context.getLog().debug("Cannot find updater for " + context.getCurrentElement()); } } + } else { + updater.update(context, value); } if (!popped) { @@ -233,46 +151,7 @@ } } - private void updateChild( - ReadContext context, - Object value, - XMLBeanInfo lastMappedClazzInfo, - ElementDescriptor descriptor, - Iterator iterator, - boolean useOriginal) { - - Updater updater = descriptor.getUpdater(); - if (updater != null) { - updater.update(context, value); - - } else { - - ElementDescriptor childDescriptor = - lastMappedClazzInfo - .getElementDescriptor() - .getElementDescriptor( - iterator); - if (childDescriptor == null - || childDescriptor.getUpdater() == null) { - if (useOriginal) { - - childDescriptor = - lastMappedClazzInfo.getElementDescriptor().findParent( - descriptor); - } else { - childDescriptor = - lastMappedClazzInfo.getElementDescriptor().findParent( - childDescriptor); - } - } - if (childDescriptor != null) { - updater = childDescriptor.getUpdater(); - if (updater != null) { - updater.update(context, value); - } - } - } - } + ElementDescriptor getElementDescriptor( ElementDescriptor propertyDescriptor, 1.4.2.3 +62 -6 jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/read/ReadContext.java Index: ReadContext.java =================================================================== RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/read/ReadContext.java,v retrieving revision 1.4.2.2 retrieving revision 1.4.2.3 diff -u -r1.4.2.2 -r1.4.2.3 --- ReadContext.java 15 Jan 2004 20:21:21 -0000 1.4.2.2 +++ ReadContext.java 21 Feb 2004 16:34:57 -0000 1.4.2.3 @@ -281,11 +281,12 @@ ElementDescriptor result = null; XMLBeanInfo lastMappedClazzInfo = getLastMappedClassXMLBeanInfo(); if (lastMappedClazzInfo != null) { + Iterator it = getParentElementPathIterator(); + Object test = it.next(); result = lastMappedClazzInfo .getElementDescriptor() - .getElementDescriptor( - getParentElementPathIterator()); + .getElementDescriptor(it); } return result; } @@ -312,6 +313,56 @@ } return lastMapped; } + + + private Class getParentClass() + { + Class result = null; + boolean first = true; + for (int i = 0, size = elementMappingStack.size(); + i < size; + i++) { + Object entry = elementMappingStack.peek(i); + if (entry instanceof Class) { + if (first) { + first = false; + } else { + + result = (Class) entry; + break; + } + } + } + return result; + } + + private XMLBeanInfo getParentXMLBeanInfo() throws IntrospectionException { + XMLBeanInfo result = null; + Class parentClass = getParentClass(); + if ( parentClass != null ) { + result = getXMLIntrospector().introspect(parentClass); + } + return result; + } + + + /** + * @return + */ + public ElementDescriptor getParentElementDescriptor() throws IntrospectionException { + ElementDescriptor parentDescriptor = null; + XMLBeanInfo parentInfo = getParentXMLBeanInfo(); + if ( parentInfo != null ) { + Iterator it = getParentElementPathIterator(); + parentDescriptor = + parentInfo + .getElementDescriptor() + .getElementDescriptor(it); + } + + return parentDescriptor; + } + public XMLBeanInfo getLastMappedClassXMLBeanInfo() throws IntrospectionException { @@ -348,6 +399,10 @@ return (elementMappingStack.size() == 1); } + public boolean isStackEmpty() { + return (elementMappingStack.size() == 0); + } + /** * Marks the element name stack with a class mapping. @@ -562,5 +617,6 @@ } } } + }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]