Ajith, thanks again!  Very nice of you to not embarass me on the list, although I managed to do that myself pretty well.  :-)

For the benefit of others using 0.94, the corrected patch is attached to this message.  I also updated the bean schema for completeness, although it appears it is missing a couple other attributes as well so probably not important.

Chuck


Ajith Ranabahu <[EMAIL PROTECTED]> wrote on 01/24/2006 09:04:56 PM:

Well Actualy when I went through the patch I figured out the mistake:) It's already corrected :)

On 1/25/06, Chuck Williams < [EMAIL PROTECTED]> wrote:
Thanks Ajith!  Unfortunately, I need to send you a revised version.  There is a stupid mistake in my xsl that broke the other types!  I had only verified that the <choice> code worked, not yet verified that the rest of the generated code wasn't affected.  It's a trivial change I'm testing now, so should be just few.  Mea culpa -- sorry for the confusion.

Regarding the code, it is very easy to read.  I compliment you on the organizaiton.

Chuck


Ajith Ranabahu <[EMAIL PROTECTED]> wrote on 01/24/2006 08:52:44 PM:

Hi Chuck,
I looked at your solution and it is reasonable for handling choice. I applied the patch (Actually modified the classes looking at the patch. TortoiseSVN  throws an exception when I try to apply the given patch)
Thanks a lot for your effort in participating in the development effort. We really appreciate you coming forward and solving this issue. I've already solved the ref issue.

BTW you seem to have understood the workings of the code well. Did it take a lot of effort to figure out the stuff ?


On 1/25/06, Chuck Williams <[EMAIL PROTECTED] > wrote:
Dims and Ajith,

I took Dims' advice and created the attached patch to modules/codegen/src of Axis2 0.94.  This may not be the best implementation of <choice> but it is very simple and meets my use cases.  The idea is this.  A <choice> is treated just like an <all> where each enclosed element has minOccurs=0, except that in the generated code setting any constituent element automatically unsets all the others.  Thus, whatever choice you set last is the choice you get and you only get one.  (The code does not enforce what happens if you don't set any; this, additional error checking, and accessors to fully determine the state of what has been chosen, could all be improved.)

I'm just starting to work with it so have not tested it much yet, but the generated code looks right to me.  It's very simple because it reuses all the code that is already there and makes just a few mods.

I haven't tried complex nesting cases to see if they all work, and probably wont' because I only need <choice> of <element>'s, although in principle any nesting that <all> handles, choice should too.

I noticed in doing this that none of the particles handle the <element ref=...> construction.  This is somewhat painful, but livable.  If I had more time, I'd try to fix that.  It looks like it might be more complex than this hack due to the way the names are used in the data structures.

I'd appreciate if you guys would add this to axis2 so I don't have to patch future releases.  If it is not deemed suitable, please let me know why as perhaps I could fix it, or just fix it!

Thanks,

Chuck







--
Ajith Ranabahu




--
Ajith Ranabahu



diff -ur codegen.src.orig/org/apache/axis2/schema/BeanWriterMetaInfoHolder.java codegen.src.new/org/apache/axis2/schema/BeanWriterMetaInfoHolder.java
--- codegen.src.orig/org/apache/axis2/schema/BeanWriterMetaInfoHolder.java	2006-01-24 18:14:07.000000000 -1000
+++ codegen.src.new/org/apache/axis2/schema/BeanWriterMetaInfoHolder.java	2006-01-24 21:09:20.000000000 -1000
@@ -32,6 +32,7 @@
 
 
     private boolean ordered = false;
+    private boolean choice = false;
     private boolean anonymous = false;
     private boolean extension = false;
     private String extensionClassName = "";
@@ -115,6 +116,24 @@
     public void setOrdered(boolean ordered) {
         this.ordered = ordered;
     }
+        
+    /**
+     * Gets the choice status.
+     *
+     * @return Returns boolean.
+     */
+    public boolean isChoice() {
+        return choice;
+    }
+
+    /**
+     * Sets the choice flag. 
+     *
+     * @param choice
+     */
+    public void setChoice(boolean choice) {
+        this.choice = choice;
+    }
 
     /**
      * Registers a mapping.
diff -ur codegen.src.orig/org/apache/axis2/schema/SchemaCompiler.java codegen.src.new/org/apache/axis2/schema/SchemaCompiler.java
--- codegen.src.orig/org/apache/axis2/schema/SchemaCompiler.java	2006-01-24 18:14:07.000000000 -1000
+++ codegen.src.new/org/apache/axis2/schema/SchemaCompiler.java	2006-01-24 21:09:20.000000000 -1000
@@ -497,7 +497,9 @@
             XmlSchemaObjectCollection items = ((XmlSchemaAll) particle).getItems();
             process(items, metainfHolder, false);
         } else if (particle instanceof XmlSchemaChoice) {
-            //put the code here to handle the choice!
+            XmlSchemaObjectCollection items = ((XmlSchemaChoice) particle).getItems();
+            metainfHolder.setChoice(true);
+            process(items, metainfHolder, false);
         }
     }
 
diff -ur codegen.src.orig/org/apache/axis2/schema/template/ADBBeanTemplate.xsl codegen.src.new/org/apache/axis2/schema/template/ADBBeanTemplate.xsl
--- codegen.src.orig/org/apache/axis2/schema/template/ADBBeanTemplate.xsl	2006-01-24 18:14:07.000000000 -1000
+++ codegen.src.new/org/apache/axis2/schema/template/ADBBeanTemplate.xsl	2006-01-24 21:09:20.000000000 -1000
@@ -32,6 +32,8 @@
     <xsl:template match="bean">
 
         <xsl:variable name="name"><xsl:value-of select="@name"/></xsl:variable>
+        <xsl:variable name="choice" select="@choice"/>
+        
     <!-- write the class header. this should be done only when unwrapped -->
 
         <xsl:if test="not(not(@unwrapped) or (@skip-write))">
@@ -64,8 +66,18 @@
 
             </xsl:otherwise>
         </xsl:choose>
-
-
+        
+        <xsl:if test="$choice">
+            /** Whenever a new property is set ensure all others are unset
+             *  There can be only one choice and the last one wins
+             */
+            private void clearAllSettingTrackers() {
+            <xsl:for-each select="property">
+                local<xsl:value-of select="@javaname"/>Tracker = false;
+            </xsl:for-each>
+            }
+        </xsl:if>
+        
         <xsl:for-each select="property">
             <xsl:variable name="propertyType"><xsl:value-of select="@type"></xsl:value-of></xsl:variable>
             <xsl:variable name="propertyName"><xsl:value-of select="@name"></xsl:value-of></xsl:variable>
@@ -81,7 +93,7 @@
             private <xsl:value-of select="$propertyType"/><xsl:text> </xsl:text><xsl:value-of select="$varName" /> ;
             <!-- Generate a tracker only if the min occurs is zero, which means if the user does
                  not bother to set that value, we do not send it -->
-            <xsl:if test="$min=0">
+            <xsl:if test="($min=0) or $choice">
             /*  This tracker boolean wil be used to detect whether the user called the set method
                 for this attribute. It will be used to determine whether to include this field
                 in the serialized XML
@@ -115,8 +127,9 @@
                     }
                 </xsl:if>
             </xsl:if>
-             <xsl:if test="$min=0">
+             <xsl:if test="($min=0) or $choice">
              //update the setting tracker
+             clearAllSettingTrackers();
              <xsl:value-of select="$settingTracker"/> = true;
              </xsl:if>
             this.<xsl:value-of select="$varName"/>=param;
@@ -143,7 +156,7 @@
                     <xsl:variable name="settingTracker">local<xsl:value-of select="@javaname"/>Tracker</xsl:variable>
 
 
-                    <xsl:if test="$min=0"> if (<xsl:value-of select="$settingTracker"/>){</xsl:if>
+                    <xsl:if test="($min=0) or $choice"> if (<xsl:value-of select="$settingTracker"/>){</xsl:if>
                     <xsl:choose>
                         <xsl:when test="@ours or @any">
                             elementList.add(new javax.xml.namespace.QName("<xsl:value-of select="$namespace"/>",
@@ -160,7 +173,7 @@
                              elementList.add(org.apache.axis2.databinding.utils.ConverterUtil.convertToString(<xsl:value-of select="$varName"/>));
                         </xsl:otherwise>
                     </xsl:choose>
-                    <xsl:if test="$min=0">}</xsl:if>
+                    <xsl:if test="($min=0) or $choice">}</xsl:if>
                 </xsl:for-each>
 
                 <xsl:for-each select="[EMAIL PROTECTED]">
diff -ur codegen.src.orig/org/apache/axis2/schema/writer/JavaBeanWriter.java codegen.src.new/org/apache/axis2/schema/writer/JavaBeanWriter.java
--- codegen.src.orig/org/apache/axis2/schema/writer/JavaBeanWriter.java	2006-01-24 18:14:07.000000000 -1000
+++ codegen.src.new/org/apache/axis2/schema/writer/JavaBeanWriter.java	2006-01-24 21:09:20.000000000 -1000
@@ -342,6 +342,11 @@
         if (metainf.isExtension()) {
             XSLTUtils.addAttribute(model, "extension", metainf.getExtensionClassName(), rootElt);
         }
+        
+        if (metainf.isChoice()) {
+            XSLTUtils.addAttribute(model, "choice", "yes", rootElt);
+        }
+        
         // go in the loop and add the part elements
         QName[] qNames;
         if (metainf.isOrdered()) {

Reply via email to