rdonkin 2004/01/29 14:15:22
Modified: betwixt/src/java/org/apache/commons/betwixt Tag:
REFACTORING-BRANCH_2004-01-13 BeanProperty.java
XMLIntrospector.java
betwixt/src/test/org/apache/commons/betwixt/introspection
Tag: REFACTORING-BRANCH_2004-01-13
TestDeclarativeIntrospection.java
Log:
Refactored map description code.
Revision Changes Path
No revision
No revision
1.4.2.7 +30 -17
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.6
retrieving revision 1.4.2.7
diff -u -r1.4.2.6 -r1.4.2.7
--- BeanProperty.java 26 Jan 2004 22:20:01 -0000 1.4.2.6
+++ BeanProperty.java 29 Jan 2004 22:15:22 -0000 1.4.2.7
@@ -328,23 +328,36 @@
//TODO: need to clean the element descriptors so that the wrappers are plain
ElementDescriptor result;
- ElementDescriptor loopDescriptor = new ElementDescriptor();
- loopDescriptor.setContextExpression(
+ ElementDescriptor entryDescriptor = new ElementDescriptor();
+ entryDescriptor.setContextExpression(
new IteratorExpression( propertyExpression )
);
- loopDescriptor.setQualifiedName( "entry" );
- // add elements for reading
- loopDescriptor.addElementDescriptor( new ElementDescriptor( "key" ) );
- loopDescriptor.addElementDescriptor( new ElementDescriptor( "value" ) );
+ entryDescriptor.setLocalName( "entry" );
+ entryDescriptor.setPropertyName( getPropertyName() );
+ entryDescriptor.setPropertyType( getPropertyType() );
+ // add elements for reading
+ ElementDescriptor keyDescriptor = new ElementDescriptor( "key" );
+ keyDescriptor.setHollow( true );
+ entryDescriptor.addElementDescriptor( keyDescriptor );
- ElementDescriptor elementDescriptor = new ElementDescriptor();
- elementDescriptor.setElementDescriptors( new ElementDescriptor[] {
loopDescriptor } );
+ ElementDescriptor valueDescriptor = new ElementDescriptor( "value" );
+ valueDescriptor.setHollow( true );
+ entryDescriptor.addElementDescriptor( valueDescriptor );
- result = elementDescriptor;
- configureDescriptor(result, configuration);
+ if ( configuration.isWrapCollectionsInElement() ) {
+ ElementDescriptor wrappingDescriptor = new ElementDescriptor();
+ wrappingDescriptor.setElementDescriptors( new ElementDescriptor[] {
entryDescriptor } );
+ NameMapper nameMapper = configuration.getElementNameMapper();
+ wrappingDescriptor.setLocalName( nameMapper.mapTypeToElementName(
propertyName ));
+ result = wrappingDescriptor;
+
+ } else {
+ result = entryDescriptor;
+ }
+
return result;
}
1.27.2.8 +19 -29
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.7
retrieving revision 1.27.2.8
diff -u -r1.27.2.7 -r1.27.2.8
--- XMLIntrospector.java 24 Jan 2004 13:36:17 -0000 1.27.2.7
+++ XMLIntrospector.java 29 Jan 2004 22:15:22 -0000 1.27.2.8
@@ -790,7 +790,8 @@
String adderName = twinParameterAdderMethod.getName();
String propertyName = Introspector.decapitalize(adderName.substring(3));
ElementDescriptor matchingDescriptor = getMatchForAdder(propertyName,
elementsByPropertyName);
- if ( matchingDescriptor != null && Map.class.isAssignableFrom(
matchingDescriptor.getPropertyType() )) {
+ if ( matchingDescriptor != null
+ && Map.class.isAssignableFrom( matchingDescriptor.getPropertyType() )) {
// this may match a map
getLog().trace("Matching map");
ElementDescriptor[] children
@@ -806,39 +807,28 @@
Class keyType = types[0];
Class valueType = types[1];
- // loop through grandchildren
+ // loop through children
// adding updaters for key and value
- ElementDescriptor[] grandchildren
- = children[0].getElementDescriptors();
MapEntryAdder adder = new MapEntryAdder(twinParameterAdderMethod);
for (
int n=0,
- noOfGrandChildren = grandchildren.length;
+ noOfGrandChildren = children.length;
n < noOfGrandChildren;
n++ ) {
- if ( "key".equals(
- grandchildren[n].getLocalName() ) ) {
-
- grandchildren[n].setUpdater(
- adder.getKeyUpdater() );
- grandchildren[n].setSingularPropertyType(
- keyType );
+ if ( "key".equals( children[n].getLocalName() ) ) {
+
+ children[n].setUpdater( adder.getKeyUpdater() );
+ children[n].setSingularPropertyType( keyType );
if ( getLog().isTraceEnabled() ) {
- getLog().trace(
- "Key descriptor: " + grandchildren[n]);
+ getLog().trace( "Key descriptor: " + children[n]);
}
- } else if (
- "value".equals(
- grandchildren[n].getLocalName() ) ) {
+ } else if ( "value".equals( children[n].getLocalName() ) ) {
- grandchildren[n].setUpdater(
- adder.getValueUpdater() );
- grandchildren[n].setSingularPropertyType(
- valueType );
- if ( getLog().isTraceEnabled() ) {
- getLog().trace(
- "Value descriptor: " + grandchildren[n]);
+ children[n].setUpdater( adder.getValueUpdater() );
+ children[n].setSingularPropertyType( valueType );
+ if ( getLog().isTraceEnabled() ) {
+ getLog().trace( "Value descriptor: " + children[n]);
}
}
}
No revision
No revision
1.1.2.5 +72 -4
jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/introspection/Attic/TestDeclarativeIntrospection.java
Index: TestDeclarativeIntrospection.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/betwixt/src/test/org/apache/commons/betwixt/introspection/Attic/TestDeclarativeIntrospection.java,v
retrieving revision 1.1.2.4
retrieving revision 1.1.2.5
diff -u -r1.1.2.4 -r1.1.2.5
--- TestDeclarativeIntrospection.java 24 Jan 2004 13:36:18 -0000 1.1.2.4
+++ TestDeclarativeIntrospection.java 29 Jan 2004 22:15:22 -0000 1.1.2.5
@@ -171,6 +171,74 @@
assertEquals("Collective element name should match adder", "number" ,
hollowPhoneNumberDescriptor.getQualifiedName());
}
+ public void testUnwrappedMap() throws Exception {
+ XMLIntrospector introspector = new XMLIntrospector();
+ introspector.getConfiguration().setWrapCollectionsInElement(false);
+ introspector.getConfiguration().setAttributesForPrimitives(true);
+ XMLBeanInfo out = introspector.introspect(DateFormatterBean.class);
+
+ ElementDescriptor formatterDescriptor = out.getElementDescriptor();
+ ElementDescriptor[] formatterChildDescriptors =
formatterDescriptor.getElementDescriptors();
+
+ assertEquals("Only one top level child", 1,
formatterChildDescriptors.length);
+
+ ElementDescriptor entryDescriptor = formatterChildDescriptors[0];
+ assertEquals("Must be called entry", "entry" ,
entryDescriptor.getLocalName());
+ assertFalse("Is not hollow", entryDescriptor.isHollow());
+ assertNull("No updater for entry spacer", entryDescriptor.getUpdater());
+
+ ElementDescriptor[] entryChildDesciptors =
entryDescriptor.getElementDescriptors();
+ assertEquals("Entry has two children", 2, entryChildDesciptors.length);
+
+ ElementDescriptor keyDescriptor = entryChildDesciptors[0];
+ assertEquals("Must be called key", "key", keyDescriptor.getLocalName());
+ assertTrue("Is not simple therefore hollow", keyDescriptor.isHollow());
+ assertNotNull("Key should have an updater", keyDescriptor.getUpdater());
+
+ ElementDescriptor valueDescriptor = entryChildDesciptors[1];
+ assertEquals("Must be called key", "value", valueDescriptor.getLocalName());
+ assertTrue("Is not simple therefore hollow", valueDescriptor.isHollow());
+ assertNotNull("Value should have an updater", valueDescriptor.getUpdater());
+ }
+
+ public void testWrappedMap() throws Exception {
+ XMLIntrospector introspector = new XMLIntrospector();
+ introspector.getConfiguration().setWrapCollectionsInElement(true);
+ introspector.getConfiguration().setAttributesForPrimitives(true);
+ XMLBeanInfo out = introspector.introspect(DateFormatterBean.class);
+
+ ElementDescriptor formatterDescriptor = out.getElementDescriptor();
+ ElementDescriptor[] formatterChildDescriptors =
formatterDescriptor.getElementDescriptors();
+
+ assertEquals("Only one top level child", 1,
formatterChildDescriptors.length);
+
+ ElementDescriptor spacerDescriptor = formatterChildDescriptors[0];
+ assertEquals("Spacer must be called formats", "formats" ,
spacerDescriptor.getLocalName());
+ assertFalse("Is not hollow", spacerDescriptor.isHollow());
+ assertNull("No updater for entry spacer", spacerDescriptor.getUpdater());
+
+ ElementDescriptor[] spacerChildDescriptors =
spacerDescriptor.getElementDescriptors();
+ assertEquals("Only one top level child", 1, spacerChildDescriptors.length);
+
+ ElementDescriptor entryDescriptor = spacerChildDescriptors[0];
+ assertEquals("Must be called entry", "entry" ,
entryDescriptor.getLocalName());
+ assertFalse("Is not hollow", entryDescriptor.isHollow());
+ assertNull("No updater for entry spacer", entryDescriptor.getUpdater());
+
+ ElementDescriptor[] entryChildDesciptors =
entryDescriptor.getElementDescriptors();
+ assertEquals("Entry has two children", 2, entryChildDesciptors.length);
+
+ ElementDescriptor keyDescriptor = entryChildDesciptors[0];
+ assertEquals("Must be called key", "key", keyDescriptor.getLocalName());
+ assertTrue("Is not simple therefore hollow", keyDescriptor.isHollow());
+ assertNotNull("Key should have an updater", keyDescriptor.getUpdater());
+
+ ElementDescriptor valueDescriptor = entryChildDesciptors[1];
+ assertEquals("Must be called key", "value", valueDescriptor.getLocalName());
+ assertTrue("Is not simple therefore hollow", valueDescriptor.isHollow());
+ assertNotNull("Value should have an updater", valueDescriptor.getUpdater());
+ }
+
public void testIsSimpleForPrimitives() throws Exception {
XMLIntrospector introspector = new XMLIntrospector();
introspector.getConfiguration().setWrapCollectionsInElement(true);
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]