commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Inger, Matthew" <In...@Synygy.com>
Subject RE: [betwixt] Inheritance version 2
Date Fri, 28 May 2004 22:41:44 GMT
Here's the diff which allows either approach:

Index: InfoRule.java
===================================================================
RCS file:
/home/cvspublic/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/
digester/InfoRule.java,v
retrieving revision 1.8
diff -u -r1.8 InfoRule.java
--- InfoRule.java	28 Feb 2004 13:38:32 -0000	1.8
+++ InfoRule.java	28 May 2004 22:41:51 -0000
@@ -1,25 +1,31 @@
 /*
  * Copyright 2001-2004 The Apache Software Foundation.
- * 
+ *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
- */ 
+ */
 package org.apache.commons.betwixt.digester;
 
+import org.apache.commons.betwixt.AttributeDescriptor;
+import org.apache.commons.betwixt.ElementDescriptor;
 import org.apache.commons.betwixt.XMLBeanInfo;
+import org.apache.commons.betwixt.XMLIntrospector;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.xml.sax.Attributes;
 import org.xml.sax.SAXException;
+import java.beans.IntrospectionException;
+import java.util.ArrayList;
+import java.util.List;
 
 /** <p><code>InfoRule</code> the digester Rule for parsing the info
element.</p>
   *
@@ -32,14 +38,16 @@
     private static final Log log = LogFactory.getLog( InfoRule.class );
     /** <code>XMLBeanInfo</code> being created */
     private XMLBeanInfo xmlBeanInfo;
-    
+    private boolean inherited = false;
+    private boolean inheritedElement = false;
+
     /** Base constructor */
     public InfoRule() {
     }
-    
+
     // Rule interface
-
//-------------------------------------------------------------------------

-    
+
//-------------------------------------------------------------------------
+
     /**
      * Process the beginning of this element.
      *
@@ -48,32 +56,66 @@
      */
     public void begin(Attributes attributes) throws SAXException {
         Class beanClass = getBeanClass();
-        
+
         xmlBeanInfo = new XMLBeanInfo( beanClass );
-        
+
         String value = attributes.getValue( "primitiveTypes" );
         if ( value != null ) {
             if ( value.equalsIgnoreCase( "element" ) ) {
                 getXMLInfoDigester().setAttributesForPrimitives( false );
-                
+
             } else if ( value.equalsIgnoreCase( "attribute" ) ) {
                 getXMLInfoDigester().setAttributesForPrimitives( true );
-                
+
             } else {
                 throw new SAXException(
                     "Invalid value inside element <info> for attribute
'primitiveTypes'."
                     + " Value should be 'element' or 'attribute'" );
             }
         }
-        
-        getDigester().push(xmlBeanInfo);       
+
+        String inherit = attributes.getValue( "inherit" );
+        if (inherit != null && inherit.equalsIgnoreCase("true")) {
+            inherited = true;
+        }
+
+        String inheritElement = attributes.getValue( "inheritelement" );
+        if (inheritElement != null && inheritElement
.equalsIgnoreCase("true")) {
+            inherited = true;
+            inheritedElement = true;
+        }
+
+        getDigester().push(xmlBeanInfo);
     }
 
 
     /**
      * Process the end of this element.
      */
-    public void end() {
+    public void end() throws IntrospectionException {
+        Class beanClass = getBeanClass();
         Object top = getDigester().pop();
+
+        ElementDescriptor descriptor = xmlBeanInfo.getElementDescriptor();
+        if (inherited) {
+            XMLIntrospector introspector = new XMLIntrospector();
+            Class clazz = beanClass.getSuperclass();
+            XMLBeanInfo info = introspector.introspect(clazz);
+            ElementDescriptor ed = info.getElementDescriptor();
+
+            if (inheritedElement) {
+                descriptor.addElementDescriptor(ed);
+            }
+            else {
+                AttributeDescriptor att[] = ed.getAttributeDescriptors();
+                for (int i=0;i<att.length;i++) {
+                    descriptor.addAttributeDescriptor(att[i]);
+                }
+                ElementDescriptor eld[] = ed.getElementDescriptors();
+                for (int i=0;i<eld.length;i++) {
+                    descriptor.addElementDescriptor(eld[i]);
+                }
+            }
+        }
     }
 }


-----Original Message-----
From: Inger, Matthew [mailto:Inger@Synygy.com]
Sent: Friday, May 28, 2004 6:28 PM
To: 'Jakarta Commons Developers List'
Subject: RE: [betwixt] Inheritance


FYI:  The following patch will take care of the inheritance issue.  It still
behaves,
by default, as it used to but there is now an "inherit" attribute which can
be
specified on the <info> element which will cause the InfoRule to introspect
the
immediate superclass of the current class being mapped, and take all of it's
superclasses' element and attribute descriptors.

This will put all elements and attributes of the superclass directly under
the root element for the object.

<child>
   <parentProperty>1</parentProperty>
</child>

Or we could always just add the ElementDescriptor for the superclass, so
that
we would use the following XML:

<child>
   <parent>
     <parentProperty>1</parentProperty>
   </parent>
</child>

The patch below deals with the first scenario, but i'm willing to keep
working
on it to allow inheritance to be done in either fashion, maybe something
like

<info inheritinline="true">...</info>

OR

<info inheritelement="true">...</info>

Thoughts?


Index: InfoRule.java
===================================================================
RCS file:
/home/cvspublic/jakarta-commons/betwixt/src/java/org/apache/commons/betwixt/
digester/InfoRule.java,v
retrieving revision 1.8
diff -u -r1.8 InfoRule.java
--- InfoRule.java	28 Feb 2004 13:38:32 -0000	1.8
+++ InfoRule.java	28 May 2004 22:22:59 -0000
@@ -1,25 +1,31 @@
 /*
  * Copyright 2001-2004 The Apache Software Foundation.
- * 
+ *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
- * 
+ *
  *      http://www.apache.org/licenses/LICENSE-2.0
- * 
+ *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
- */ 
+ */
 package org.apache.commons.betwixt.digester;
 
+import org.apache.commons.betwixt.AttributeDescriptor;
+import org.apache.commons.betwixt.ElementDescriptor;
 import org.apache.commons.betwixt.XMLBeanInfo;
+import org.apache.commons.betwixt.XMLIntrospector;
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.xml.sax.Attributes;
 import org.xml.sax.SAXException;
+import java.beans.IntrospectionException;
+import java.util.ArrayList;
+import java.util.List;
 
 /** <p><code>InfoRule</code> the digester Rule for parsing the info
element.</p>
   *
@@ -32,14 +38,15 @@
     private static final Log log = LogFactory.getLog( InfoRule.class );
     /** <code>XMLBeanInfo</code> being created */
     private XMLBeanInfo xmlBeanInfo;
-    
+    private boolean inherited = false;
+
     /** Base constructor */
     public InfoRule() {
     }
-    
+
     // Rule interface
-
//-------------------------------------------------------------------------

-    
+
//-------------------------------------------------------------------------
+
     /**
      * Process the beginning of this element.
      *
@@ -48,32 +55,56 @@
      */
     public void begin(Attributes attributes) throws SAXException {
         Class beanClass = getBeanClass();
-        
+
         xmlBeanInfo = new XMLBeanInfo( beanClass );
-        
+
         String value = attributes.getValue( "primitiveTypes" );
         if ( value != null ) {
             if ( value.equalsIgnoreCase( "element" ) ) {
                 getXMLInfoDigester().setAttributesForPrimitives( false );
-                
+
             } else if ( value.equalsIgnoreCase( "attribute" ) ) {
                 getXMLInfoDigester().setAttributesForPrimitives( true );
-                
+
             } else {
                 throw new SAXException(
                     "Invalid value inside element <info> for attribute
'primitiveTypes'."
                     + " Value should be 'element' or 'attribute'" );
             }
         }
-        
-        getDigester().push(xmlBeanInfo);       
+
+        String inherit = attributes.getValue( "inherit" );
+        if (inherit != null && inherit.equalsIgnoreCase("true")) {
+            inherited = true;
+        }
+
+        getDigester().push(xmlBeanInfo);
     }
 
 
     /**
      * Process the end of this element.
      */
-    public void end() {
+    public void end() throws IntrospectionException {
+        Class beanClass = getBeanClass();
         Object top = getDigester().pop();
+
+        ElementDescriptor descriptor = xmlBeanInfo.getElementDescriptor();
+        if (inherited) {
+           XMLIntrospector introspector = new XMLIntrospector();
+           Class clazz = beanClass.getSuperclass();
+           XMLBeanInfo info = introspector.introspect(clazz);
+           ElementDescriptor ed = info.getElementDescriptor();
+           AttributeDescriptor att[] = ed.getAttributeDescriptors();
+           for (int i=0;i<att.length;i++) {
+               descriptor.addAttributeDescriptor(att[i]);
+           }
+           ElementDescriptor eld[] = ed.getElementDescriptors();
+           for (int i=0;i<eld.length;i++) {
+               descriptor.addElementDescriptor(eld[i]);
+           }
+        }
+
+
     }
 }



-----Original Message-----
From: Inger, Matthew [mailto:Inger@Synygy.com]
Sent: Friday, May 28, 2004 4:06 PM
To: 'Jakarta Commons Developers List'
Subject: [betwixt] Inheritance


It's possible i'm just missing something, but i've
been unsucessful in trying to achieve mapping inheritance
in betwixt.

I have an abstract base class, which has some properties which are
mapped.  There is then a subclass with it's own additional properties
for mapping.

I cannnot seem to get betwixt to output the properties from the
superclass unless i specifically refer to each of those properties
in the mapping for the subclass.

Shouldn't mappings inherit (at least optionally) from the mapping
for their superclass?

If anyone has any hints, please let me know.

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

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

---------------------------------------------------------------------
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