commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From rdon...@apache.org
Subject cvs commit: jakarta-commons/betwixt/src/resources dotbetwixt.dtd
Date Tue, 08 Apr 2003 13:41:41 GMT
rdonkin     2003/04/08 06:41:41

  Modified:    betwixt/src/java/org/apache/commons/betwixt/digester
                        ElementRule.java MappedPropertyRule.java
                        XMLIntrospectorHelper.java
               betwixt/src/resources dotbetwixt.dtd
  Log:
  Added support for custom updaters specified in the .betwixt file. The idea is that by adding
a value to the updater attribute, the method used to write the value for the given element
can be specified directly by name. Adding this attribute override the usual plural stemming.
  
  Revision  Changes    Path
  1.10      +33 -7     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.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- ElementRule.java	19 Mar 2003 22:59:01 -0000	1.9
  +++ ElementRule.java	8 Apr 2003 13:41:40 -0000	1.10
  @@ -57,8 +57,6 @@
    * 
    */
   
  -import java.beans.BeanInfo;
  -import java.beans.Introspector;
   import java.beans.PropertyDescriptor;
   
   import org.apache.commons.betwixt.ElementDescriptor;
  @@ -80,7 +78,15 @@
   public class ElementRule extends MappedPropertyRule {
   
       /** Logger */
  -    private static final Log log = LogFactory.getLog( ElementRule.class );
  +    private static Log log = LogFactory.getLog( ElementRule.class );
  +    /** 
  +     * Sets the log for this class 
  +     * 
  +     * @param newLog the new Log implementation for this class to use
  +     */
  +    public static final void setLog(Log newLog) {
  +        log = newLog;
  +    }
   
       /** Class for which the .bewixt file is being digested */
       private Class beanClass;
  @@ -137,7 +143,7 @@
           
           
           if ( propertyName != null && propertyName.length() > 0 ) {
  -            configureDescriptor(descriptor);
  +            configureDescriptor(descriptor, attributes.getValue( "updater" ));
               
           } else {
               String value = attributes.getValue( "value" );
  @@ -177,21 +183,41 @@
       //-------------------------------------------------------------------------    
       
       /** 
  -     * Set the Expression and Updater from a bean property name 
  +     * Sets the Expression and Updater from a bean property name 
  +     * Uses the default updater (from the standard java bean property).
        *
        * @param elementDescriptor configure this <code>ElementDescriptor</code>
        */
       protected void configureDescriptor(ElementDescriptor elementDescriptor) {
  +        configureDescriptor( elementDescriptor, null );
  +    }       
  +    
  +    /** 
  +     * Sets the Expression and Updater from a bean property name 
  +     * Allows a custom updater to be passed in.
  +     *
  +     * @param elementDescriptor configure this <code>ElementDescriptor</code>
  +     * @param updateMethodName custom update method. If null, then use standard
  +     */
  +    protected void configureDescriptor(
  +                                        ElementDescriptor elementDescriptor,
  +                                        String updateMethodName) {
           Class beanClass = getBeanClass();
           if ( beanClass != null ) {
               String name = elementDescriptor.getPropertyName();
               PropertyDescriptor descriptor = 
                   getPropertyDescriptor( beanClass, name );
  +            
               if ( descriptor != null ) { 
                   XMLIntrospectorHelper
  -                    .configureProperty( elementDescriptor, descriptor );
  +                    .configureProperty( 
  +                                        elementDescriptor, 
  +                                        descriptor, 
  +                                        updateMethodName, 
  +                                        beanClass );
  +                
                   getProcessedPropertyNameSet().add( name );
               }
           }
  -    }        
  +    }  
   }
  
  
  
  1.2       +4 -4      jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/digester/MappedPropertyRule.java
  
  Index: MappedPropertyRule.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/digester/MappedPropertyRule.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- MappedPropertyRule.java	19 Mar 2003 22:59:01 -0000	1.1
  +++ MappedPropertyRule.java	8 Apr 2003 13:41:40 -0000	1.2
  @@ -74,7 +74,7 @@
     * @author Robert Burrell Donkin
     * @version $Revision$
     */
  -abstract public class MappedPropertyRule extends RuleSupport {
  +public abstract class MappedPropertyRule extends RuleSupport {
   
       /** Logger */
       private static final Log log = LogFactory.getLog( MappedPropertyRule.class );   
  
  
  
  1.19      +71 -4     jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/digester/XMLIntrospectorHelper.java
  
  Index: XMLIntrospectorHelper.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/digester/XMLIntrospectorHelper.java,v
  retrieving revision 1.18
  retrieving revision 1.19
  diff -u -r1.18 -r1.19
  --- XMLIntrospectorHelper.java	13 Feb 2003 18:41:48 -0000	1.18
  +++ XMLIntrospectorHelper.java	8 Apr 2003 13:41:40 -0000	1.19
  @@ -233,7 +233,8 @@
       }
       
       /**
  -     * Configure an <code>ElementDescriptor</code> from a <code>PropertyDescriptor</code>
  +     * Configure an <code>ElementDescriptor</code> from a <code>PropertyDescriptor</code>.
  +     * This uses default element updater (the write method of the property).
        *
        * @param elementDescriptor configure this <code>ElementDescriptor</code>
        * @param propertyDescriptor configure from this <code>PropertyDescriptor</code>
  @@ -241,6 +242,27 @@
       public static void configureProperty( 
                                       ElementDescriptor elementDescriptor, 
                                       PropertyDescriptor propertyDescriptor ) {
  +                                    
  +        configureProperty( elementDescriptor, propertyDescriptor, null, null);
  +    }
  +                                    
  +    /**
  +     * Configure an <code>ElementDescriptor</code> from a <code>PropertyDescriptor</code>.
  +     * A custom update method may be set.
  +     *
  +     * @param elementDescriptor configure this <code>ElementDescriptor</code>
  +     * @param propertyDescriptor configure from this <code>PropertyDescriptor</code>
  +     * @param updateMethodName the name of the custom updater method to user. 
  +     * If null, then then 
  +     * @param beanClass the <code>Class</code> from which the update method
should be found.
  +     * This may be null only when <code>updateMethodName</code> is also null.
  +     */
  +    public static void configureProperty( 
  +                                    ElementDescriptor elementDescriptor, 
  +                                    PropertyDescriptor propertyDescriptor,
  +                                    String updateMethodName,
  +                                    Class beanClass ) {
  +        
           Class type = propertyDescriptor.getPropertyType();
           Method readMethod = propertyDescriptor.getReadMethod();
           Method writeMethod = propertyDescriptor.getWriteMethod();
  @@ -285,9 +307,54 @@
               log.trace( "Standard property" );
               elementDescriptor.setContextExpression( new MethodExpression( readMethod )
);
           }
  -        
  -        if ( writeMethod != null ) {
  -            elementDescriptor.setUpdater( new MethodUpdater( writeMethod ) );
  +    
  +        // see if we have a custom method update name
  +        if (updateMethodName == null) {
  +            // set standard write method
  +            if ( writeMethod != null ) {
  +                elementDescriptor.setUpdater( new MethodUpdater( writeMethod ) );
  +            }
  +            
  +        } else {
  +            // see if we can find and set the custom method
  +            if ( log.isTraceEnabled() ) {
  +                log.trace( "Finding custom method: " );
  +                log.trace( "  on:" + beanClass );
  +                log.trace( "  name:" + updateMethodName );
  +            }
  +            
  +            Method updateMethod = null;
  +            Method[] methods = beanClass.getMethods();
  +            for ( int i = 0, size = methods.length; i < size; i++ ) {
  +                Method method = methods[i];
  +                if ( updateMethodName.equals( method.getName() ) ) {
  +                    // we have a matching name
  +                    // check paramters are correct
  +                    if (methods[i].getParameterTypes().length == 1) {
  +                        // we'll use first match
  +                        updateMethod = methods[i];
  +                        if ( log.isTraceEnabled() ) {
  +                            log.trace("Matched method:" + updateMethod);
  +                        } 
  +                        // done since we're using the first match
  +                        break;
  +                    }
  +                }
  +            }
  +            
  +            if (updateMethod == null) {
  +                if ( log.isInfoEnabled() ) {
  +                    
  +                    log.info("No method with name '" + updateMethodName + "' found for
update");
  +                }
  +            } else {
  +    
  +                elementDescriptor.setUpdater( new MethodUpdater( updateMethod ) );
  +                elementDescriptor.setSingularPropertyType( updateMethod.getParameterTypes()[0]
);
  +                if ( log.isTraceEnabled() ) {
  +                    log.trace( "Set custom updater on " + elementDescriptor);
  +                }
  +            }
           }
       }
       
  
  
  
  1.6       +12 -1     jakarta-commons/betwixt/src/resources/dotbetwixt.dtd
  
  Index: dotbetwixt.dtd
  ===================================================================
  RCS file: /home/cvs/jakarta-commons/betwixt/src/resources/dotbetwixt.dtd,v
  retrieving revision 1.5
  retrieving revision 1.6
  diff -u -r1.5 -r1.6
  --- dotbetwixt.dtd	19 Mar 2003 22:54:29 -0000	1.5
  +++ dotbetwixt.dtd	8 Apr 2003 13:41:40 -0000	1.6
  @@ -14,6 +14,16 @@
   
   <!ELEMENT addDefaults EMPTY>
   
  +<!--
  +    Represents an element in the xml.
  +    An element may be mapped to properties of the bean
  +    (when it has a property attribute)
  +    or it can be purely structure (when it does not)
  +    
  +    the updater attribute can be used 
  +    (in conjunction with the property attribute) to specify a custom update
  +    method.
  +-->
   <!ELEMENT element (attribute|addDefaults|element|text)*>
   <!ATTLIST element
       name CDATA #REQUIRED
  @@ -21,6 +31,7 @@
       uri CDATA #IMPLIED
       value CDATA #IMPLIED
       property CDATA #IMPLIED
  +    updater CDATA #IMPLIED
     >
   
   <!-- Use for mixed content text -->
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message