rdonkin 2004/01/22 03:00:03
Modified: betwixt/src/java/org/apache/commons/betwixt Tag:
REFACTORING-BRANCH_2004-01-13 BeanProperty.java
ElementDescriptor.java
betwixt/src/test/org/apache/commons/betwixt/introspection
Tag: REFACTORING-BRANCH_2004-01-13
TestDeclarativeIntrospection.java
Log:
Implementation and tests for isHollow
Revision Changes Path
No revision
No revision
1.4.2.4 +57 -37
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.3
retrieving revision 1.4.2.4
diff -u -r1.4.2.3 -r1.4.2.4
--- BeanProperty.java 19 Jan 2004 22:38:08 -0000 1.4.2.3
+++ BeanProperty.java 22 Jan 2004 11:00:03 -0000 1.4.2.4
@@ -87,9 +87,9 @@
public class BeanProperty {
/** The bean name for the property (not null) */
- private String propertyName;
+ private final String propertyName;
/** The type of this property (not null) */
- private Class propertyType;
+ private final Class propertyType;
/** The Expression used to read values of this property (possibly null) */
private Expression propertyExpression;
/** The Updater used to write values of this property (possibly null) */
@@ -189,12 +189,10 @@
* @return a correctly configured <code>NodeDescriptor</code> for the property
*/
public Descriptor createXMLDescriptor( IntrospectionConfiguration configuration
) {
- String name = getPropertyName();
- Class type = getPropertyType();
Log log = configuration.getIntrospectionLog();
if (log.isTraceEnabled()) {
log.trace("Creating descriptor for property: name="
- + name + " type=" + type);
+ + getPropertyName() + " type=" + getPropertyType());
}
NodeDescriptor descriptor = null;
@@ -204,7 +202,7 @@
if ( propertyExpression == null ) {
if (log.isTraceEnabled()) {
log.trace( "No read method for property: name="
- + name + " type=" + type);
+ + getPropertyName() + " type=" + getPropertyType());
}
return null;
}
@@ -216,7 +214,7 @@
// choose response from property type
// XXX: ignore class property ??
- if ( Class.class.equals( type ) && "class".equals( name ) ) {
+ if ( Class.class.equals( getPropertyType() ) && "class".equals(
getPropertyName() ) ) {
log.trace( "Ignoring class property" );
return null;
@@ -226,48 +224,39 @@
// on the type
//TODO replace with simple type support
- if ( XMLIntrospectorHelper.isPrimitiveType( type ) ) {
+ if ( XMLIntrospectorHelper.isPrimitiveType( getPropertyType() ) ) {
descriptor =
createDescriptorForPrimitive(
configuration,
- name,
- log,
propertyExpression,
propertyUpdater);
- } else if ( XMLIntrospectorHelper.isLoopType( type ) ) {
+ } else if ( XMLIntrospectorHelper.isLoopType( getPropertyType() ) ) {
if (log.isTraceEnabled()) {
- log.trace("Loop type: " + name);
+ log.trace("Loop type: " + getPropertyName());
log.trace("Wrap in collections? " +
configuration.isWrapCollectionsInElement());
}
- if ( Map.class.isAssignableFrom( type )) {
- descriptor = createDescriptorForMap(configuration,
propertyExpression);
+ if ( Map.class.isAssignableFrom( getPropertyType() )) {
+ descriptor = createDescriptorForMap( configuration,
propertyExpression );
} else {
- descriptor = createDescriptorForCollective( configuration,
propertyExpression);
+ descriptor
+ = createDescriptorForCollective( configuration,
propertyExpression );
}
} else {
if (log.isTraceEnabled()) {
- log.trace( "Standard property: " + name);
+ log.trace( "Standard property: " + getPropertyName());
}
descriptor =
createDescriptorForStandard(
propertyExpression,
- propertyUpdater);
+ propertyUpdater,
+ configuration);
}
-
- 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 );
+
if (log.isTraceEnabled()) {
log.trace( "Created descriptor:" );
@@ -275,6 +264,29 @@
}
return descriptor;
}
+
+ /**
+ * Configures descriptor (in the standard way).
+ * This sets the common properties.
+ *
+ * @param propertyName the name of the property mapped to the Descriptor, not
null
+ * @param propertyType the type of the property mapped to the Descriptor, not
null
+ * @param descriptor Descriptor to map, not null
+ * @param configuration IntrospectionConfiguration, not null
+ */
+ private void configureDescriptor(
+ NodeDescriptor descriptor,
+ IntrospectionConfiguration configuration) {
+ 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( propertyName ));
+ descriptor.setPropertyName( getPropertyName() );
+ descriptor.setPropertyType( getPropertyType() );
+ }
/**
* Creates an <code>ElementDescriptor</code> for a standard property
@@ -284,7 +296,8 @@
*/
private ElementDescriptor createDescriptorForStandard(
Expression propertyExpression,
- Updater propertyUpdater) {
+ Updater propertyUpdater,
+ IntrospectionConfiguration configuration) {
ElementDescriptor result;
@@ -294,7 +307,11 @@
elementDescriptor.setUpdater( propertyUpdater );
}
+ elementDescriptor.setHollow(true);
+
result = elementDescriptor;
+
+ configureDescriptor(result, configuration);
return result;
}
@@ -327,6 +344,8 @@
elementDescriptor.setElementDescriptors( new ElementDescriptor[] {
loopDescriptor } );
result = elementDescriptor;
+
+ configureDescriptor(result, configuration);
return result;
}
@@ -353,6 +372,7 @@
elementDescriptor.setElementDescriptors( new ElementDescriptor[] {
loopDescriptor } );
result = elementDescriptor;
+ configureDescriptor(result, configuration);
return result;
}
@@ -367,22 +387,21 @@
*/
private NodeDescriptor createDescriptorForPrimitive(
IntrospectionConfiguration configuration,
- String name,
- Log log,
Expression propertyExpression,
Updater propertyUpdater) {
+ Log log = configuration.getIntrospectionLog();
NodeDescriptor descriptor;
if (log.isTraceEnabled()) {
- log.trace( "Primitive type: " + name);
+ log.trace( "Primitive type: " + getPropertyName());
}
if ( configuration.isAttributesForPrimitives() ) {
if (log.isTraceEnabled()) {
- log.trace( "Adding property as attribute: " + name );
+ log.trace( "Adding property as attribute: " + getPropertyName() );
}
descriptor = new AttributeDescriptor();
} else {
if (log.isTraceEnabled()) {
- log.trace( "Adding property as element: " + name );
+ log.trace( "Adding property as element: " + getPropertyName() );
}
descriptor = new ElementDescriptor();
}
@@ -390,6 +409,7 @@
if ( propertyUpdater != null ) {
descriptor.setUpdater( propertyUpdater );
}
+ configureDescriptor(descriptor, configuration);
return descriptor;
}
}
1.14.2.4 +26 -6
jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/ElementDescriptor.java
Index: ElementDescriptor.java
===================================================================
RCS file:
/home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/ElementDescriptor.java,v
retrieving revision 1.14.2.3
retrieving revision 1.14.2.4
diff -u -r1.14.2.3 -r1.14.2.4
--- ElementDescriptor.java 18 Jan 2004 12:30:57 -0000 1.14.2.3
+++ ElementDescriptor.java 22 Jan 2004 11:00:03 -0000 1.14.2.4
@@ -126,6 +126,13 @@
/** Whether this element refers to a primitive type (or property of a parent
object) */
private boolean primitiveType;
+ /**
+ * Is this element hollow?
+ * In other words, is this descriptor a place holder indicating the name
+ * and update for a root ElementDescriptor for this type obtained by
introspection
+ * TODO: this would probably be better modeled as a separate subclass
+ */
+ private boolean isHollow = false;
/**
* Whether this collection element can be used
@@ -659,7 +666,20 @@
* @return true if this is hollow
*/
public boolean isHollow() {
- // TODO implementation
- return false;
+ return isHollow;
}
+
+ /**
+ * Sets whether this descriptor is hollow.
+ * A hollow descriptor is one which gives only the class that the subgraph
+ * is mapped to rather than describing the entire subgraph.
+ * A new <code>XMLBeanInfo</code> should be introspected
+ * and that used to describe the subgraph.
+ * A hollow descriptor should not have any child descriptors.
+ * TODO: consider whether a subclass would be better
+ * @param true if this is hollow
+ */
+ public void setHollow(boolean isHollow) {
+ this.isHollow = isHollow;
+ }
}
No revision
No revision
1.1.2.3 +41 -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.2
retrieving revision 1.1.2.3
diff -u -r1.1.2.2 -r1.1.2.3
--- TestDeclarativeIntrospection.java 18 Jan 2004 22:25:23 -0000 1.1.2.2
+++ TestDeclarativeIntrospection.java 22 Jan 2004 11:00:03 -0000 1.1.2.3
@@ -78,6 +78,43 @@
super(name);
}
+ /** Tests whether a standard property's ElementDescriptor is hollow (as
expected) */
+ public void testStandardPropertyIsHollow() throws Exception {
+ XMLIntrospector introspector = new XMLIntrospector();
+ introspector.getConfiguration().setAttributesForPrimitives(true);
+ XMLBeanInfo out = introspector.introspect(CompanyBean.class);
+
+ ElementDescriptor companyBeanDescriptor = out.getElementDescriptor();
+ ElementDescriptor[] childDescriptors =
companyBeanDescriptor.getElementDescriptors();
+ assertEquals("Correct number of child descriptors", 1,
childDescriptors.length);
+
+ ElementDescriptor addressDescriptor = childDescriptors[0];
+ assertEquals("standard property is hollow", true,
addressDescriptor.isHollow());
+ }
+
+
+ /** Tests whether a simple element's ElementDescriptor is hollow */
+ public void testSimpleElementIsHollow() throws Exception {
+ XMLIntrospector introspector = new XMLIntrospector();
+ introspector.getConfiguration().setAttributesForPrimitives(false);
+ XMLBeanInfo out = introspector.introspect(CompanyBean.class);
+
+ ElementDescriptor companyBeanDescriptor = out.getElementDescriptor();
+ ElementDescriptor[] childDescriptors =
companyBeanDescriptor.getElementDescriptors();
+ assertEquals("Correct number of child descriptors", 2,
childDescriptors.length);
+
+ ElementDescriptor nameDescriptor = null;
+ for (int i=0, size=childDescriptors.length; i<size; i++)
+ {
+ if ("name".equals(childDescriptors[i].getLocalName())) {
+ nameDescriptor = childDescriptors[i];
+ }
+ }
+
+ assertNotNull("Expected to find an element descriptor for 'name'",
nameDescriptor);
+ assertFalse("Expected simple element not to be hollow",
nameDescriptor.isHollow());
+ }
+
public void _testWrappedCollective() throws Exception {
XMLIntrospector introspector = new XMLIntrospector();
introspector.getConfiguration().setWrapCollectionsInElement(true);
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]