You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by rd...@apache.org on 2003/08/24 18:54:56 UTC
cvs commit: jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/strategy DefaultObjectStringConverter.java ObjectStringConverter.java
rdonkin 2003/08/24 09:54:56
Modified: betwixt/src/java/org/apache/commons/betwixt
BindingConfiguration.java ElementDescriptor.java
betwixt/src/java/org/apache/commons/betwixt/digester
ElementRule.java
betwixt/src/java/org/apache/commons/betwixt/io
AbstractBeanWriter.java BeanRuleSet.java
betwixt/src/java/org/apache/commons/betwixt/io/read
BeanCreationList.java ChainedBeanCreator.java
ChainedBeanCreatorFactory.java ElementMapping.java
ReadContext.java
betwixt/src/java/org/apache/commons/betwixt/strategy
DefaultObjectStringConverter.java
ObjectStringConverter.java
Log:
Added class attribute to .betwixt file. This allows an implementation class to be specified for instantiation during bean reading.
Revision Changes Path
1.3 +7 -7 jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/BindingConfiguration.java
Index: BindingConfiguration.java
===================================================================
RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/BindingConfiguration.java,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -r1.2 -r1.3
--- BindingConfiguration.java 21 Aug 2003 22:42:47 -0000 1.2
+++ BindingConfiguration.java 24 Aug 2003 16:54:55 -0000 1.3
@@ -89,7 +89,7 @@
private boolean mapIDs = true;
/** Converts objects <-> strings */
private ObjectStringConverter objectStringConverter;
-
+ /** The name of the classname attribute used when creating derived beans */
private String classNameAttribute = "className";
/**
@@ -124,7 +124,7 @@
*/
public void setObjectStringConverter(ObjectStringConverter objectStringConverter) {
this.objectStringConverter = objectStringConverter;
- }
+ }
/**
* Should <code>ID</code>'s and <code>IDREF</code> attributes
1.12 +28 -5 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.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- ElementDescriptor.java 13 Jul 2003 21:28:35 -0000 1.11
+++ ElementDescriptor.java 24 Aug 2003 16:54:55 -0000 1.12
@@ -132,6 +132,11 @@
*/
private boolean wrapCollectionsInElement = true;
+ /** specifies a separate implementation class that should be instantiated
+ * when reading beans
+ * or null if there is no separate implementation */
+ private Class implementationClass = null;
+
/**
* Constructs an <code>ElementDescriptor</code> that refers to a primitive type.
*/
@@ -495,6 +500,24 @@
}
}
return contentList;
+ }
+
+ /**
+ * Gets the class which should be used for instantiation.
+ * @return the class which should be used for instantiation of beans
+ * mapped from this element, null if the standard class should be used
+ */
+ public Class getImplementationClass() {
+ return implementationClass;
+ }
+
+ /**
+ * Sets the class which should be used for instantiation.
+ * @param implementationClass the class which should be used for instantiation
+ * or null to use the mapped type
+ */
+ public void setImplementationClass(Class implementationClass) {
+ this.implementationClass = implementationClass;
}
/**
1.11 +19 -1 jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/digester/ElementRule.java
Index: ElementRule.java
===================================================================
RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/digester/ElementRule.java,v
retrieving revision 1.10
retrieving revision 1.11
diff -u -r1.10 -r1.11
--- ElementRule.java 8 Apr 2003 13:41:40 -0000 1.10
+++ ElementRule.java 24 Aug 2003 16:54:56 -0000 1.11
@@ -103,6 +103,7 @@
* @throws SAXException 1. If this tag's parent is not either an info or element tag.
* 2. If the name attribute is not valid XML element name.
* 3. If the name attribute is not present
+ * 4. If the class attribute is not a loadable (fully qualified) class name
*/
public void begin(Attributes attributes) throws SAXException {
String name = attributes.getValue( "name" );
@@ -141,6 +142,23 @@
getPropertyType( propertyType, beanClass, propertyName )
);
+ String implementationClass = attributes.getValue( "class" );
+ if ( log.isTraceEnabled() ) {
+ log.trace("'class' attribute=" + implementationClass);
+ }
+ if ( implementationClass != null ) {
+ try {
+
+ Class clazz = Class.forName(implementationClass);
+ descriptor.setImplementationClass( clazz );
+
+ } catch (Exception e) {
+ if ( log.isDebugEnabled() ) {
+ log.debug("Cannot load class named: " + implementationClass, e);
+ }
+ throw new SAXException("Cannot load class named: " + implementationClass);
+ }
+ }
if ( propertyName != null && propertyName.length() > 0 ) {
configureDescriptor(descriptor, attributes.getValue( "updater" ));
1.20 +8 -6 jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/AbstractBeanWriter.java
Index: AbstractBeanWriter.java
===================================================================
RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/AbstractBeanWriter.java,v
retrieving revision 1.19
retrieving revision 1.20
diff -u -r1.19 -r1.20
--- AbstractBeanWriter.java 21 Aug 2003 22:47:40 -0000 1.19
+++ AbstractBeanWriter.java 24 Aug 2003 16:54:56 -0000 1.20
@@ -1498,13 +1498,15 @@
*/
private String convertToString( Object value , Descriptor descriptor, Context context ) {
return getBindingConfiguration()
- .getObjectStringConverter().objectToString( value, descriptor.getPropertyType(), null, context );
+ .getObjectStringConverter()
+ .objectToString( value, descriptor.getPropertyType(), null, context );
}
/**
* Factory method for new contexts.
* Ensure that they are correctly configured.
* @param bean make a new Context for this bean
+ * @return not null
*/
private Context makeContext(Object bean) {
return new Context( bean, log, bindingConfiguration );
1.12 +17 -19 jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/BeanRuleSet.java
Index: BeanRuleSet.java
===================================================================
RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/BeanRuleSet.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- BeanRuleSet.java 21 Aug 2003 22:42:47 -0000 1.11
+++ BeanRuleSet.java 24 Aug 2003 16:54:56 -0000 1.12
@@ -77,7 +77,6 @@
import org.apache.commons.betwixt.expression.Updater;
import org.apache.commons.betwixt.io.read.ReadContext;
import org.apache.commons.betwixt.io.read.ReadConfiguration;
-import org.apache.commons.betwixt.io.read.BeanCreationChain;
import org.apache.commons.betwixt.io.read.ElementMapping;
@@ -153,7 +152,7 @@
* @param basePath specifies the (Digester-style) path under which the rules will be attached
* @param baseElementDescriptor the <code>ElementDescriptor</code> used to create the rules
* @param baseBeanClass the <code>Class</code> whose mapping rules will be created
- * @param baseContext the root Context that bean carrying Contexts should be obtained from,
+ * @param context the root Context that bean carrying Contexts should be obtained from,
* not null
* @deprecated use the constructor which takes a ReadContext instead
*/
@@ -267,7 +266,8 @@
if ( context.getClassLoader() == null ) {
context.setClassLoader( digester.getClassLoader() );
}
- BeanRule rule = new BeanRule( basePath + "/" , baseElementDescriptor, baseBeanClass, context );
+ BeanRule rule
+ = new BeanRule( basePath + "/" , baseElementDescriptor, baseBeanClass, context );
addRule( basePath, rule , baseElementDescriptor, context );
if ( log.isDebugEnabled() ) {
@@ -480,7 +480,10 @@
* @param elementDescriptor update this <code>ElementDescriptor</code> with the body text
* @param context the <code>ReadContext</code> against which the elements will be evaluated
*/
- private void addRule( String path, ElementDescriptor elementDescriptor, ReadContext context ) {
+ private void addRule(
+ String path,
+ ElementDescriptor elementDescriptor,
+ ReadContext context ) {
BeanRule rule = new BeanRule( path + '/', elementDescriptor, context );
addRule( path, rule, elementDescriptor, context );
}
@@ -612,10 +615,8 @@
//-------------------------------------------------------------------------
/**
- * Process the beginning of this element.
- *
- * @param attributes The attribute list of this element
- */
+ * @see Rule#begin(String, String, Attributes)
+ */
public void begin(String namespace, String name, Attributes attributes) {
log.debug( "Called with descriptor: " + descriptor
+ " propertyType: " + descriptor.getPropertyType() );
@@ -644,9 +645,8 @@
instance = createBean( namespace, name, attributes );
if ( instance != null ) {
createdBean = true;
-
context.setBean( instance );
- digester.push(instance);
+ digester.push( instance );
// if we are a reference to a type we should lookup the original
@@ -713,9 +713,7 @@
}
/**
- * Called by digester with the (concatinated) body text.
- *
- * @param text the String comprising all the body text
+ * @see Rule#body(String, String, String)
*/
public void body(String namespace, String name, String text) {
@@ -802,12 +800,12 @@
mapping.setNamespace( namespace );
mapping.setName( name );
mapping.setAttributes( attributes );
+ mapping.setDescriptor( descriptor );
Object newInstance = context.getBeanCreationChain().create( mapping, context );
return newInstance;
}
-
/**
* Return something meaningful for logging.
1.2 +10 -9 jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/read/BeanCreationList.java
Index: BeanCreationList.java
===================================================================
RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/read/BeanCreationList.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- BeanCreationList.java 21 Aug 2003 22:38:17 -0000 1.1
+++ BeanCreationList.java 24 Aug 2003 16:54:56 -0000 1.2
@@ -118,7 +118,7 @@
/**
* Gets the number of BeanCreators in the wrapped chain.
- * @param the number of <code>Bean</code> in the current chain
+ * @return the number of <code>ChainedBeanCreator</code>'s in the current chain
*/
public int getSize() {
return beanCreators.size();
@@ -131,7 +131,8 @@
*
* @param index index at which the creator should be inserted
* @param beanCreator the <code>BeanCreator</code> to be inserted, not null
- * @throws IndexOutOfBoundsException if the index is out of the range <code>(index < 0 || index > getSize())
+ * @throws IndexOutOfBoundsException if the index is out of the range
+ * <code>(index < 0 || index > getSize())
*/
public void insertBeanCreator(
int index,
@@ -158,14 +159,14 @@
/** Worker class walks a chain */
private class ChainWorker extends BeanCreationChain {
/** Iterator for the creator list */
- Iterator iterator;
+ private Iterator iterator;
/** Creates the iterator */
ChainWorker() {
iterator = beanCreators.iterator();
}
/**
- * see BeanCreationChain#create
+ * @see BeanCreationChain#create
*/
public Object create( ElementMapping elementMapping, ReadContext readContext ) {
if ( iterator.hasNext() ) {
1.2 +9 -6 jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/read/ChainedBeanCreator.java
Index: ChainedBeanCreator.java
===================================================================
RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/read/ChainedBeanCreator.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ChainedBeanCreator.java 21 Aug 2003 22:38:41 -0000 1.1
+++ ChainedBeanCreator.java 24 Aug 2003 16:54:56 -0000 1.2
@@ -81,6 +81,9 @@
* @param chain not null
* @return the Object created, possibly null
*/
- public Object create(ElementMapping elementMapping, ReadContext context, BeanCreationChain chain);
+ public Object create(
+ ElementMapping elementMapping,
+ ReadContext context,
+ BeanCreationChain chain);
}
1.2 +35 -10 jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/read/ChainedBeanCreatorFactory.java
Index: ChainedBeanCreatorFactory.java
===================================================================
RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/read/ChainedBeanCreatorFactory.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ChainedBeanCreatorFactory.java 21 Aug 2003 22:39:00 -0000 1.1
+++ ChainedBeanCreatorFactory.java 24 Aug 2003 16:54:56 -0000 1.2
@@ -61,6 +61,10 @@
*/
package org.apache.commons.betwixt.io.read;
+import org.apache.commons.logging.Log;
+
+import org.apache.commons.betwixt.ElementDescriptor;
+
/**
* Group of factory methods for <code>ChainedBeanCreator</code>'s.
* The standard implementations used by Betwixt are present here.
@@ -78,14 +82,16 @@
ReadContext context,
BeanCreationChain chain) {
- String className = elementMapping.getAttributes().getValue( context.getClassNameAttribute() );
+ String className
+ = elementMapping
+ .getAttributes().getValue( context.getClassNameAttribute() );
if ( className != null ) {
try {
// load the class we should instantiate
ClassLoader classLoader = context.getClassLoader();
if ( classLoader == null ) {
context.getLog().warn(
- "Could not create derived instance: read context classloader not set." );
+ "Could not create derived instance: read context classloader not set." );
}
Class clazz = classLoader.loadClass( className );
return clazz.newInstance();
@@ -120,16 +126,35 @@
ElementMapping element,
ReadContext context,
BeanCreationChain chain) {
- // create based on type
- Class theClass = element.getType();
+ Log log = context.getLog();
+ Class theClass = null;
+
+ ElementDescriptor descriptor = element.getDescriptor();
+ if ( descriptor != null ) {
+ // created based on implementation class
+ theClass = descriptor.getImplementationClass();
+ }
+
+ if ( theClass == null ) {
+ // create based on type
+ theClass = element.getType();
+ }
+
+ if ( log.isTraceEnabled() ) {
+ log.trace(
+ "Creating instance of class " + theClass.getName()
+ + " for element " + element.getName());
+ }
+
try {
return theClass.newInstance();
} catch (Exception e) {
// it would be nice to have a pluggable strategy for exception management
- context.getLog().warn( "Could not create instance of type: " + theClass.getName() );
+ context.getLog().warn(
+ "Could not create instance of type: " + theClass.getName() );
context.getLog().debug( "Create new instance failed: ", e );
return null;
}
1.2 +26 -6 jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/read/ElementMapping.java
Index: ElementMapping.java
===================================================================
RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/io/read/ElementMapping.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ElementMapping.java 21 Aug 2003 22:39:22 -0000 1.1
+++ ElementMapping.java 24 Aug 2003 16:54:56 -0000 1.2
@@ -63,6 +63,8 @@
import org.xml.sax.Attributes;
+import org.apache.commons.betwixt.ElementDescriptor;
+
/**
* Describes a mapping between an xml element and a betwixt element.
*
@@ -79,6 +81,8 @@
private Attributes attributes;
/** The base type of the mapped bean */
private Class type;
+ /** The mapped descriptor */
+ private ElementDescriptor descriptor;
/** Base constructor */
public ElementMapping() {}
@@ -122,7 +126,7 @@
*/
public Attributes getAttributes() {
return attributes;
- }
+ }
/**
* Sets the element's attributes
@@ -148,5 +152,21 @@
*/
public void setType(Class type) {
this.type = type;
+ }
+
+ /**
+ * Gets the mapped element descriptor.
+ * @return the mapped ElementDescriptor
+ */
+ public ElementDescriptor getDescriptor() {
+ return descriptor;
+ }
+
+ /**
+ * Sets the mapped element descriptor.
+ * @param descriptor set this descriptor
+ */
+ public void setDescriptor(ElementDescriptor descriptor) {
+ this.descriptor = descriptor;
}
}
1.2 +9 -9 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.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ReadContext.java 21 Aug 2003 22:40:03 -0000 1.1
+++ ReadContext.java 24 Aug 2003 16:54:56 -0000 1.2
@@ -103,7 +103,7 @@
public ReadContext(
BindingConfiguration bindingConfiguration,
ReadConfiguration readConfiguration ) {
- this(
+ this(
LogFactory.getLog( ReadContext.class ),
bindingConfiguration,
readConfiguration);
@@ -124,9 +124,9 @@
}
/**
- * Constructs a <code>ReadContext</code> with the same settings as an existing <code>Context</code>.
+ * Constructs a <code>ReadContext</code>
+ * with the same settings as an existing <code>Context</code>.
* @param readContext not null
- * @param readConfiguration not null
*/
public ReadContext( ReadContext readContext ) {
super( readContext );
@@ -168,7 +168,7 @@
*/
public ClassLoader getClassLoader() {
return classLoader;
- }
+ }
/**
* Sets the classloader to be used.
1.4 +9 -7 jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/strategy/DefaultObjectStringConverter.java
Index: DefaultObjectStringConverter.java
===================================================================
RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/strategy/DefaultObjectStringConverter.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- DefaultObjectStringConverter.java 21 Aug 2003 22:41:50 -0000 1.3
+++ DefaultObjectStringConverter.java 24 Aug 2003 16:54:56 -0000 1.4
@@ -125,8 +125,10 @@
* Converts an object to a string representation using ConvertUtils.
*
* @param value the String to be converted, not null
- * @param the property class to be returned (if possible), not null
- * @param flavour a string allow symantic differences in formatting to be communicated (ignored)
+ * @param type the property class to be returned (if possible), not null
+ * @param flavour a string allow symantic differences
+ * in formatting to be communicated (ignored)
+ * @param context not null
* @return an Object converted from the String, not null
*/
public Object stringToObject(String value, Class type, String flavour, Context context) {
1.4 +6 -5 jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/strategy/ObjectStringConverter.java
Index: ObjectStringConverter.java
===================================================================
RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/strategy/ObjectStringConverter.java,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -r1.3 -r1.4
--- ObjectStringConverter.java 21 Aug 2003 22:41:50 -0000 1.3
+++ ObjectStringConverter.java 24 Aug 2003 16:54:56 -0000 1.4
@@ -92,6 +92,7 @@
* @param object the object to be converted, possibly null
* @param type the property class of the object, not null
* @param flavour a string allow symantic differences in formatting to be communicated
+ * @param context the context, not null
* @return a String representation, not null
*/
public String objectToString(Object object, Class type, String flavour, Context context) {
@@ -110,7 +111,7 @@
* @param value the String to be converted
* @param type the property class to be returned (if possible), not null
* @param flavour a string allow symantic differences in formatting to be communicated
- * @param context not null
+ * @param context the context, not null
* @return an Object converted from the String, not null
*/
public Object stringToObject(String value, Class type, String flavour, Context context) {