Author: rdonkin
Date: Sun Jul 31 12:06:27 2005
New Revision: 226681

URL: http://svn.apache.org/viewcvs?rev=226681&view=rev
Log:
Added new guess-names attribute for addDefaults. This is needed to support 
mixed collections when addDefaults is used.

Modified:
    
jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/XMLIntrospector.java
    
jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/digester/AddDefaultsRule.java
    jakarta/commons/proper/betwixt/trunk/src/resources/dotbetwixt.dtd
    jakarta/commons/proper/betwixt/trunk/xdocs/guide/reading.xml
    jakarta/commons/proper/betwixt/trunk/xdocs/guide/start.xml
    jakarta/commons/proper/betwixt/trunk/xdocs/tasks.xml

Modified: 
jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/XMLIntrospector.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/XMLIntrospector.java?rev=226681&r1=226680&r2=226681&view=diff
==============================================================================
--- 
jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/XMLIntrospector.java
 (original)
+++ 
jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/XMLIntrospector.java
 Sun Jul 31 12:06:27 2005
@@ -816,6 +816,7 @@
     /** 
      * Add any addPropety(PropertyType) methods as Updaters 
      * which are often used for 1-N relationships in beans.
+     * This method does not preserve null property names.
      * <br>
      * The tricky part here is finding which ElementDescriptor corresponds
      * to the method. e.g. a property 'items' might have an Element descriptor
@@ -840,6 +841,34 @@
     public void defaultAddMethods( 
                                             ElementDescriptor rootDescriptor, 
                                             Class beanClass ) {
+        defaultAddMethods(rootDescriptor, beanClass, false);
+    }
+    
+    /** 
+     * Add any addPropety(PropertyType) methods as Updaters 
+     * which are often used for 1-N relationships in beans.
+     * <br>
+     * The tricky part here is finding which ElementDescriptor corresponds
+     * to the method. e.g. a property 'items' might have an Element descriptor
+     * which the method addItem() should match to. 
+     * <br>
+     * So the algorithm we'll use 
+     * by default is to take the decapitalized name of the property being added
+     * and find the first ElementDescriptor that matches the property starting 
with
+     * the string. This should work for most use cases. 
+     * e.g. addChild() would match the children property.
+     * <br>
+     * TODO this probably needs refactoring. It probably belongs in the bean 
wrapper
+     * (so that it'll work properly with dyna-beans) and so that the 
operations can 
+     * 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>
+     * 
+     * @param rootDescriptor add defaults to this descriptor
+     * @param beanClass the <code>Class</code> to which descriptor corresponds
+     */
+    public void defaultAddMethods( ElementDescriptor rootDescriptor, Class 
beanClass, boolean preservePropertyName ) {
         // TODO: this probably does work properly with DynaBeans: need to push
         // implementation into an class and expose it on BeanType.  
         
@@ -882,7 +911,7 @@
             
             for (Iterator it=singleParameterAdders.iterator();it.hasNext();) {
                 Method singleParameterAdder = (Method) it.next();
-                setIteratorAdder(elementsByPropertyName, singleParameterAdder);
+                setIteratorAdder(elementsByPropertyName, singleParameterAdder, 
preservePropertyName);
             }
             
             for (Iterator it=twinParameterAdders.iterator();it.hasNext();) {
@@ -919,7 +948,8 @@
      */
     private void setIteratorAdder(
         Map elementsByPropertyName,
-        Method singleParameterAdderMethod) {
+        Method singleParameterAdderMethod,
+        boolean preserveNullPropertyName) {
         
         String adderName = singleParameterAdderMethod.getName();
         String propertyName = 
Introspector.decapitalize(adderName.substring(3));
@@ -938,9 +968,9 @@
             matchingDescriptor.setSingularPropertyType( singularType );
             matchingDescriptor.setHollow(!isPrimitiveType(singularType));
             String localName = matchingDescriptor.getLocalName();
-            if ( localName == null || localName.length() == 0 ) {
+            if ( !preserveNullPropertyName && ( localName == null || 
localName.length() == 0 )) {
                 matchingDescriptor.setLocalName( 
-                    getElementNameMapper()
+                    getConfiguration().getElementNameMapper()
                         .mapTypeToElementName( propertyName ) );
             }
                                     

Modified: 
jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/digester/AddDefaultsRule.java
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/digester/AddDefaultsRule.java?rev=226681&r1=226680&r2=226681&view=diff
==============================================================================
--- 
jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/digester/AddDefaultsRule.java
 (original)
+++ 
jakarta/commons/proper/betwixt/trunk/src/java/org/apache/commons/betwixt/digester/AddDefaultsRule.java
 Sun Jul 31 12:06:27 2005
@@ -69,24 +69,31 @@
             addAdders = 
Boolean.valueOf(addAddersAttributeValue).booleanValue();
         }
         
+        boolean guessNames = true;
+        String guessNamesAttributeValue = attributes.getValue("guess-names");
+        if (guessNamesAttributeValue != null)
+        {
+            guessNames = 
Boolean.valueOf(guessNamesAttributeValue).booleanValue();
+        }
+        
         if (addProperties) {
             addDefaultProperties();
         }
         
         if (addAdders) {
-            addAdders();
+            addAdders(guessNames);
         }
     }
 
     /**
      * Adds default adder methods
      */
-    private void addAdders() {
+    private void addAdders(boolean guessNames) {
         Class beanClass = getBeanClass();
         // default any addProperty() methods
         getXMLIntrospector().defaultAddMethods( 
                                             getRootElementDescriptor(), 
-                                            beanClass );
+                                            beanClass, !guessNames);
     }
 
     /**

Modified: jakarta/commons/proper/betwixt/trunk/src/resources/dotbetwixt.dtd
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/src/resources/dotbetwixt.dtd?rev=226681&r1=226680&r2=226681&view=diff
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/src/resources/dotbetwixt.dtd (original)
+++ jakarta/commons/proper/betwixt/trunk/src/resources/dotbetwixt.dtd Sun Jul 
31 12:06:27 2005
@@ -31,7 +31,17 @@
     property CDATA #IMPLIED
   >
 
+<!-- 
+addDefaults element indicates that betwixt should use standard introspection
+to deduce any unspecified values. The attributes allow the behaviour to be
+customised but reasonal defaults have been chosen.
+-->
 <!ELEMENT addDefaults EMPTY>
+<!ATTLIST attribute
+    add-properties (true|false) CDATA #IMPLIED
+    add-adders (true|false) CDATA #IMPLIED
+    guess-names (true|false) CDATA #IMPLIED
+  >
 
 <!--
     Represents an element in the xml.

Modified: jakarta/commons/proper/betwixt/trunk/xdocs/guide/reading.xml
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/xdocs/guide/reading.xml?rev=226681&r1=226680&r2=226681&view=diff
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/xdocs/guide/reading.xml (original)
+++ jakarta/commons/proper/betwixt/trunk/xdocs/guide/reading.xml Sun Jul 31 
12:06:27 2005
@@ -365,7 +365,7 @@
                        <p>
 By default, in this circumstance Betwixt will try to guess the correct 
resolution
 by searching all registered <code>XMLBeanInfo</code>'s for an appropriate 
match.
-If more thn one is found, an arbitrary one is used. 
+If more than one is found, an arbitrary one is used. 
 In many cases, this accords well with intuition. There are
 occasions when more finely grained control may be required. The resolution is
 therefore factored into <code>PolymorphicReferenceResolver</code> 
@@ -377,6 +377,11 @@
 Therefore, when using a custom registry a custom resolver must also
 be used.
                </p>
+    <p>
+<strong>Note:</strong> when using mixed collections with dot betwixt files 
containing 
+<code>addDefaults</code> it may be neccessary to set the 
<code>guess-names</code> 
+attribute to false. 
+    </p>
     </subsection>
 </section>
 

Modified: jakarta/commons/proper/betwixt/trunk/xdocs/guide/start.xml
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/xdocs/guide/start.xml?rev=226681&r1=226680&r2=226681&view=diff
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/xdocs/guide/start.xml (original)
+++ jakarta/commons/proper/betwixt/trunk/xdocs/guide/start.xml Sun Jul 31 
12:06:27 2005
@@ -166,6 +166,11 @@
                be suppressed.</li>
                <li><strong><code>add-adders</code></strong> if this property 
is set to
                any value other than <code>true</code> the matching of adders 
will be suppressed.</li>
+               <li><strong><code>guess-name</code></strong> if this property 
is set to
+               any value other than <code>true</code> the guessing of names 
for elements without a name attribute
+    will be suppressed. Note that this may need to be set to 
<code>false</code> when mapping mixed
+    collections.
+    By default, Betwixt will guess names.</li>
        </ul>
        
        <p>Using <code>&lt;addDefaults add-properties='false' 
add-adders='false'/&gt;</code>

Modified: jakarta/commons/proper/betwixt/trunk/xdocs/tasks.xml
URL: 
http://svn.apache.org/viewcvs/jakarta/commons/proper/betwixt/trunk/xdocs/tasks.xml?rev=226681&r1=226680&r2=226681&view=diff
==============================================================================
--- jakarta/commons/proper/betwixt/trunk/xdocs/tasks.xml (original)
+++ jakarta/commons/proper/betwixt/trunk/xdocs/tasks.xml Sun Jul 31 12:06:27 
2005
@@ -188,7 +188,11 @@
     </subsection>
 </section>
 <section name='Completed'>
-    <subsection name='Since 0.7'>
+    <subsection name='Since 0.7'>            
+        <ul>
+          <li>Added guess-name attribute to addDefaults element in dot betwixt 
file.
+          This allows mixed collections to be used with add-adders and 
addDefaults.</li>  
+        </ul>
         <ul>
             <li>Fixed map custom updater in dot betwixt file bug.</li>  
         </ul>



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to