rdonkin 2004/01/19 14:38:08
Modified: betwixt/src/java/org/apache/commons/betwixt Tag:
REFACTORING-BRANCH_2004-01-13 BeanProperty.java
XMLIntrospector.java
Log:
Refactored into smaller methods.
Revision Changes Path
No revision
No revision
1.4.2.3 +152 -66
jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/BeanProperty.java
Index: BeanProperty.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/BeanProperty.java,v
retrieving revision 1.4.2.2
retrieving revision 1.4.2.3
diff -u -r1.4.2.2 -r1.4.2.3
--- BeanProperty.java 19 Jan 2004 20:55:59 -0000 1.4.2.2
+++ BeanProperty.java 19 Jan 2004 22:38:08 -0000 1.4.2.3
@@ -73,6 +73,7 @@
import org.apache.commons.betwixt.expression.MethodExpression;
import org.apache.commons.betwixt.expression.MethodUpdater;
import org.apache.commons.betwixt.expression.Updater;
+import org.apache.commons.betwixt.strategy.NameMapper;
import org.apache.commons.logging.Log;
/**
@@ -196,7 +197,7 @@
+ name + " type=" + type);
}
- Descriptor descriptor = null;
+ NodeDescriptor descriptor = null;
Expression propertyExpression = getPropertyExpression();
Updater propertyUpdater = getPropertyUpdater();
@@ -221,88 +222,173 @@
}
+ //TODO this big conditional should be replaced with subclasses based
+ // on the type
+
//TODO replace with simple type support
if ( XMLIntrospectorHelper.isPrimitiveType( type ) ) {
- if (log.isTraceEnabled()) {
- log.trace( "Primitive type: " + name);
- }
- if ( configuration.isAttributesForPrimitives() ) {
- if (log.isTraceEnabled()) {
- log.trace( "Adding property as attribute: " + name );
- }
- descriptor = new AttributeDescriptor();
- } else {
- if (log.isTraceEnabled()) {
- log.trace( "Adding property as element: " + name );
- }
- descriptor = new ElementDescriptor();
- }
- descriptor.setTextExpression( propertyExpression );
- if ( propertyUpdater != null ) {
- descriptor.setUpdater( propertyUpdater );
- }
+ descriptor =
+ createDescriptorForPrimitive(
+ configuration,
+ name,
+ log,
+ propertyExpression,
+ propertyUpdater);
} else if ( XMLIntrospectorHelper.isLoopType( type ) ) {
+
if (log.isTraceEnabled()) {
log.trace("Loop type: " + name);
log.trace("Wrap in collections? " +
configuration.isWrapCollectionsInElement());
}
- ElementDescriptor loopDescriptor = new ElementDescriptor();
- loopDescriptor.setContextExpression(
- new IteratorExpression( propertyExpression )
- );
- loopDescriptor.setWrapCollectionsInElement(
configuration.isWrapCollectionsInElement() );
- // XXX: need to support some kind of 'add' or handle arrays, Lists or
indexed properties
- //loopDescriptor.setUpdater( new MethodUpdater( writeMethod ) );
- if ( Map.class.isAssignableFrom( type ) ) {
- loopDescriptor.setQualifiedName( "entry" );
- // add elements for reading
- loopDescriptor.addElementDescriptor( new ElementDescriptor( "key" )
);
- loopDescriptor.addElementDescriptor( new ElementDescriptor( "value"
) );
- }
-
- ElementDescriptor elementDescriptor = new ElementDescriptor();
- elementDescriptor.setWrapCollectionsInElement(
configuration.isWrapCollectionsInElement() );
- elementDescriptor.setElementDescriptors( new ElementDescriptor[] {
loopDescriptor } );
- descriptor = elementDescriptor;
+ if ( Map.class.isAssignableFrom( type )) {
+ descriptor = createDescriptorForMap(configuration,
propertyExpression);
+ } else {
+ descriptor = createDescriptorForCollective( configuration,
propertyExpression);
+ }
} else {
if (log.isTraceEnabled()) {
log.trace( "Standard property: " + name);
}
- ElementDescriptor elementDescriptor = new ElementDescriptor();
- elementDescriptor.setContextExpression( propertyExpression );
- if ( propertyUpdater != null ) {
- elementDescriptor.setUpdater( propertyUpdater );
- }
-
- descriptor = elementDescriptor;
- }
-
- if (descriptor instanceof NodeDescriptor) {
- NodeDescriptor nodeDescriptor = (NodeDescriptor) descriptor;
- if (descriptor instanceof AttributeDescriptor) {
- // we want to use the attributemapper only when it is an
attribute..
- nodeDescriptor.setLocalName(
- configuration.getAttributeNameMapper().mapTypeToElementName(
name ) );
-
- } else {
- nodeDescriptor.setLocalName(
- configuration.getElementNameMapper().mapTypeToElementName( name
) );
- }
+ descriptor =
+ createDescriptorForStandard(
+ propertyExpression,
+ propertyUpdater);
}
-
+
+ NameMapper nameMapper = configuration.getElementNameMapper();
+ if (descriptor instanceof AttributeDescriptor) {
+ // we want to use the attributemapper only when it is an attribute..
+ nameMapper = configuration.getAttributeNameMapper();
+
+ }
+
+ descriptor.setLocalName( nameMapper.mapTypeToElementName( name ));
descriptor.setPropertyName( name );
descriptor.setPropertyType( type );
-
- // XXX: associate more bean information with the descriptor?
- //nodeDescriptor.setDisplayName( propertyDescriptor.getDisplayName() );
- //nodeDescriptor.setShortDescription(
propertyDescriptor.getShortDescription() );
-
+
if (log.isTraceEnabled()) {
log.trace( "Created descriptor:" );
log.trace( descriptor );
+ }
+ return descriptor;
+ }
+
+ /**
+ * Creates an <code>ElementDescriptor</code> for a standard property
+ * @param propertyExpression
+ * @param propertyUpdater
+ * @return
+ */
+ private ElementDescriptor createDescriptorForStandard(
+ Expression propertyExpression,
+ Updater propertyUpdater) {
+
+ ElementDescriptor result;
+
+ ElementDescriptor elementDescriptor = new ElementDescriptor();
+ elementDescriptor.setContextExpression( propertyExpression );
+ if ( propertyUpdater != null ) {
+ elementDescriptor.setUpdater( propertyUpdater );
+ }
+
+ result = elementDescriptor;
+ return result;
+ }
+
+ /**
+ * Creates an ElementDescriptor for an <code>Map</code> type property
+ * @param configuration
+ * @param propertyExpression
+ * @return
+ */
+ private ElementDescriptor createDescriptorForMap(
+ IntrospectionConfiguration configuration,
+ Expression propertyExpression) {
+
+ ElementDescriptor result;
+
+ ElementDescriptor loopDescriptor = new ElementDescriptor();
+ loopDescriptor.setContextExpression(
+ new IteratorExpression( propertyExpression )
+ );
+ loopDescriptor.setWrapCollectionsInElement(
configuration.isWrapCollectionsInElement() );
+
+ loopDescriptor.setQualifiedName( "entry" );
+ // add elements for reading
+ loopDescriptor.addElementDescriptor( new ElementDescriptor( "key" ) );
+ loopDescriptor.addElementDescriptor( new ElementDescriptor( "value" ) );
+
+
+ ElementDescriptor elementDescriptor = new ElementDescriptor();
+ elementDescriptor.setWrapCollectionsInElement(
configuration.isWrapCollectionsInElement() );
+ elementDescriptor.setElementDescriptors( new ElementDescriptor[] {
loopDescriptor } );
+
+ result = elementDescriptor;
+ return result;
+ }
+
+ /**
+ * Creates an <code>ElementDescriptor</code> for a collective type property
+ * @param configuration
+ * @param propertyExpression
+ * @return
+ */
+ private ElementDescriptor createDescriptorForCollective(
+ IntrospectionConfiguration configuration,
+ Expression propertyExpression) {
+
+ ElementDescriptor result;
+
+ ElementDescriptor loopDescriptor = new ElementDescriptor();
+ loopDescriptor.setContextExpression(
+ new IteratorExpression( propertyExpression )
+ );
+ loopDescriptor.setWrapCollectionsInElement(
configuration.isWrapCollectionsInElement() );
+
+ ElementDescriptor elementDescriptor = new ElementDescriptor();
+ elementDescriptor.setWrapCollectionsInElement(
configuration.isWrapCollectionsInElement() );
+ elementDescriptor.setElementDescriptors( new ElementDescriptor[] {
loopDescriptor } );
+
+ result = elementDescriptor;
+ return result;
+ }
+
+ /**
+ * Creates a NodeDescriptor for a primitive type node
+ * @param configuration
+ * @param name
+ * @param log
+ * @param propertyExpression
+ * @param propertyUpdater
+ * @return
+ */
+ private NodeDescriptor createDescriptorForPrimitive(
+ IntrospectionConfiguration configuration,
+ String name,
+ Log log,
+ Expression propertyExpression,
+ Updater propertyUpdater) {
+ NodeDescriptor descriptor;
+ if (log.isTraceEnabled()) {
+ log.trace( "Primitive type: " + name);
+ }
+ if ( configuration.isAttributesForPrimitives() ) {
+ if (log.isTraceEnabled()) {
+ log.trace( "Adding property as attribute: " + name );
+ }
+ descriptor = new AttributeDescriptor();
+ } else {
+ if (log.isTraceEnabled()) {
+ log.trace( "Adding property as element: " + name );
+ }
+ descriptor = new ElementDescriptor();
+ }
+ descriptor.setTextExpression( propertyExpression );
+ if ( propertyUpdater != null ) {
+ descriptor.setUpdater( propertyUpdater );
}
return descriptor;
}
1.27.2.6 +45 -75
jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/XMLIntrospector.java
Index: XMLIntrospector.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/XMLIntrospector.java,v
retrieving revision 1.27.2.5
retrieving revision 1.27.2.6
diff -u -r1.27.2.5 -r1.27.2.6
--- XMLIntrospector.java 19 Jan 2004 20:55:59 -0000 1.27.2.5
+++ XMLIntrospector.java 19 Jan 2004 22:38:08 -0000 1.27.2.6
@@ -80,12 +80,10 @@
import org.apache.commons.betwixt.digester.XMLBeanInfoDigester;
import org.apache.commons.betwixt.digester.XMLIntrospectorHelper;
import org.apache.commons.betwixt.expression.EmptyExpression;
-import org.apache.commons.betwixt.expression.Expression;
import org.apache.commons.betwixt.expression.IteratorExpression;
import org.apache.commons.betwixt.expression.MapEntryAdder;
import org.apache.commons.betwixt.expression.MethodUpdater;
import org.apache.commons.betwixt.expression.StringExpression;
-import org.apache.commons.betwixt.expression.Updater;
import org.apache.commons.betwixt.registry.DefaultXMLBeanInfoRegistry;
import org.apache.commons.betwixt.registry.XMLBeanInfoRegistry;
import org.apache.commons.betwixt.strategy.ClassNormalizer;
@@ -682,6 +680,9 @@
* be optimized by caching. Multiple hash maps are created and getMethods is
* called multiple times. This is relatively expensive and so it'd be better
* to push into a proper class and cache.
+ * <br>
+ * TODO this probably does work properly with DynaBeans: need to push
+ * implementation into an class and expose it on BeanType.
*
* @param introspector use this <code>XMLIntrospector</code> for introspection
* @param rootDescriptor add defaults to this descriptor
@@ -690,6 +691,7 @@
public void defaultAddMethods(
ElementDescriptor rootDescriptor,
Class beanClass ) {
+
// lets iterate over all methods looking for one of the form
// add*(PropertyType)
if ( beanClass != null ) {
@@ -725,14 +727,16 @@
}
}
+ Map elementsByPropertyName = makeElementDescriptorMap( rootDescriptor );
+
for (Iterator it=singleParameterAdders.iterator();it.hasNext();) {
Method singleParameterAdder = (Method) it.next();
- setIteratorAdder(rootDescriptor, singleParameterAdder);
+ setIteratorAdder(elementsByPropertyName, singleParameterAdder);
}
for (Iterator it=twinParameterAdders.iterator();it.hasNext();) {
Method twinParameterAdder = (Method) it.next();
- setMapAdder(rootDescriptor, twinParameterAdder);
+ setMapAdder(elementsByPropertyName, twinParameterAdder);
}
}
}
@@ -743,12 +747,12 @@
* @param singleParameterAdder
*/
private void setIteratorAdder(
- ElementDescriptor rootDescriptor,
+ Map elementsByPropertyName,
Method singleParameterAdderMethod) {
String adderName = singleParameterAdderMethod.getName();
String propertyName = Introspector.decapitalize(adderName.substring(3));
- ElementDescriptor matchingDescriptor = getMatchForAdder(propertyName,
rootDescriptor);
+ ElementDescriptor matchingDescriptor = getMatchForAdder(propertyName,
elementsByPropertyName);
if (matchingDescriptor != null) {
//TODO defensive code: probably should check descriptor type
@@ -788,11 +792,11 @@
* @param singleParameterAdder
*/
private void setMapAdder(
- ElementDescriptor rootDescriptor,
+ Map elementsByPropertyName,
Method twinParameterAdderMethod) {
String adderName = twinParameterAdderMethod.getName();
String propertyName = Introspector.decapitalize(adderName.substring(3));
- ElementDescriptor matchingDescriptor = getMatchForAdder(propertyName,
rootDescriptor);
+ ElementDescriptor matchingDescriptor = getMatchForAdder(propertyName,
elementsByPropertyName);
if ( matchingDescriptor != null && Map.class.isAssignableFrom(
matchingDescriptor.getPropertyType() )) {
// this may match a map
getLog().trace("Matching map");
@@ -855,76 +859,45 @@
* @param rootDescriptor
* @return
*/
- private ElementDescriptor getMatchForAdder(String propertyName,
ElementDescriptor rootDescriptor) {
+ private ElementDescriptor getMatchForAdder(
+ String propertyName,
+ Map elementsByPropertyName) {
ElementDescriptor matchingDescriptor = null;
-
if (propertyName.length() > 0) {
-
- // now lets try find the ElementDescriptor which displays
- // a property which starts with propertyName
- // and if so, we'll set a new Updater on it if there
- // is not one already
- matchingDescriptor =
- findGetCollectionDescriptor(rootDescriptor, propertyName);
-
- if (getLog().isDebugEnabled()) {
- getLog().debug("!! " + propertyName + " -> " + matchingDescriptor);
- getLog().debug(
- "!! "
- + propertyName
- + " -> "
- + (matchingDescriptor != null
- ? matchingDescriptor.getPropertyName()
- : ""));
+ if ( getLog().isTraceEnabled() ) {
+ getLog().trace( "findPluralDescriptor( " + propertyName
+ + " ):root property name=" + propertyName );
+ }
+
+ PluralStemmer stemmer = getPluralStemmer();
+ matchingDescriptor = stemmer.findPluralDescriptor( propertyName,
elementsByPropertyName );
+
+ if ( getLog().isTraceEnabled() ) {
+ getLog().trace(
+ "findPluralDescriptor( " + propertyName
+ + " ):ElementDescriptor=" + matchingDescriptor );
}
}
return matchingDescriptor;
}
// Implementation methods
- //-------------------------------------------------------------------------
-
- /**
- * Attempts to find the element descriptor for the getter property that
- * typically matches a collection or array. The property name is used
- * to match. e.g. if an addChild() method is detected the
- * descriptor for the 'children' getter property should be returned.
- *
- * @param introspector use this <code>XMLIntrospector</code>
- * @param rootDescriptor the <code>ElementDescriptor</code> whose child element
will be
- * searched for a match
- * @param propertyName the name of the 'adder' method to match
- * @return <code>ElementDescriptor</code> for the matching getter
- */
- private ElementDescriptor findGetCollectionDescriptor(
- ElementDescriptor rootDescriptor,
- String propertyName ) {
- // create the Map of propertyName -> descriptor that the PluralStemmer will
choose
- Map map = new HashMap();
- //String propertyName = rootDescriptor.getPropertyName();
- if ( getLog().isTraceEnabled() ) {
- getLog().trace( "findPluralDescriptor( " + propertyName
- + " ):root property name=" + rootDescriptor.getPropertyName() );
- }
-
- if (rootDescriptor.getPropertyName() != null) {
- map.put(propertyName, rootDescriptor);
- }
- makeElementDescriptorMap( rootDescriptor, map );
-
- PluralStemmer stemmer = getPluralStemmer();
- ElementDescriptor elementDescriptor = stemmer.findPluralDescriptor(
propertyName, map );
-
- if ( getLog().isTraceEnabled() ) {
- getLog().trace(
- "findPluralDescriptor( " + propertyName
- + " ):ElementDescriptor=" + elementDescriptor );
+ //-------------------------------------------------------------------------
+
+
+ /**
+ * Creates a map where the keys are the property names and the values are the
ElementDescriptors
+ */
+ private Map makeElementDescriptorMap( ElementDescriptor rootDescriptor ) {
+ Map result = new HashMap();
+ String rootPropertyName = rootDescriptor.getPropertyName();
+ if (rootPropertyName != null) {
+ result.put(rootPropertyName, rootDescriptor);
}
-
- return elementDescriptor;
+ makeElementDescriptorMap( rootDescriptor, result );
+ return result;
}
-
-
+
/**
* Creates a map where the keys are the property names and the values are the
ElementDescriptors
*
@@ -1428,7 +1401,4 @@
return properties;
}
}
-
-
-
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]