Hi Scott,

Castor doesn't support exactly your XML format + Object model. If I
understand correctly you want the following:


<OrderAttribute>
    <AttributeName>MMOrderAttribute</AttributeName>
       <MMOrderAttribute>
           <OrderAction>Install</OrderAction>
       </MMOrderAttribute>
</OrderAttribute>

to create an MMOrderAttribute object and the OrderAction, and have the
AttributeName be set on the MMOrderAttribute.

So basically you'd require unwrapping the OrderAttribute element and
stuffing the AttributeName element beneath the MMOrderAttribute element.

Castor can't deal with that directly. You'll either need to use XSLT to
dynamically transform the XML so that it more closely resembles the
object model, or perhaps use a wrapper class in your object model so
that your object model more closely resembles your XML.

For the wrapper class you could have a simple:

public class OrderAttributeWrapper extends OrderAttribute {

   private OrderAttribute _orderAttribute = null;

   public OrderAttributeWrapper() {
      super();
   }

   public OrderAttribute getOrderAttribute() {
      return _orderAttribute;
   }

   public void setOrderAttribute(OrderAttribute orderAttribute) {
        _orderAttribute = orderAttribute;
   }
}


Then you could write a simple addOrderAttribute method to your Order
class as such:

public class Order {

...

   public void addOrderAttribute(OrderAttribute orderAttribute) {
       if (orderAttribute instanceof OrderAttributeWrapper) {
           orderAttribute =
((OrderAttributeWrapper)orderAttribute).getOrderAttribute();
       }
       orderAttributes.add(orderAttribute);
   }

...

}

Castor can be configured to use the addOrderAttribute method so that
each time it unmarshals an orderAttribute you can unwrap it.

--Keith

[EMAIL PROTECTED] wrote:
> 
> Keith & Everyone,
>         I am having a problem to create a mapping based on the xml format I
> am going to receive and java classes we have defined.
>         The problems are on the abstract class which defines a set common
> attribute for the subclasses, and also is a element in the xml file, Here
> are four classes I created for the testing purpose:
> 
> OrderAttribute is a abstract class which MMOrderAttribute and
> VoiceOrderAttribute extends from.
> The Order Object has a list of OrderAttribute which could be
> MMOrderAttribute or VoiceOrderAttribute.
> 
> I know there is a "deriveByClass: which can be used for the abstract class
> and interface. But this only works when the abstract class/interface does
> not appear in the xml string. My problem is that I have to have a element
> which represents the abstract class (OrderAttribute) in the xml
> String(please read the xml format I included in this email). I am wondering
> if Castor support this kind of xml format for the abstract/interface class.
> If it does, what the mapping should looks like?
> Please help! Thanks
> 
> Scott
> --------------------------------
> Order.java
> 
> package data.order;
> 
> import java.util.Collection;
> import java.util.ArrayList;
> import java.util.List;
> import java.util.Iterator;
> 
> public class Order {
>   public Order() {}
> 
>   public long getOrderObjId() {
>     return this.orderObjId;
>   }
> 
>   public void setOrderObjId(long aOrderObjId) {
>     this.orderObjId = aOrderObjId;
>   }
> 
>   public List getOrderAttributes() {
>     return this.orderAttributes;
>   }
> 
>   public void setOrderAttributes(Collection aOrderAttributes) {
>     this.orderAttributes.addAll(aOrderAttributes);
>   }
> 
>   public String toString() {
>     StringBuffer temp = new StringBuffer(1);
>     temp.append("The Order ObjId: " + orderObjId + "\n");
>     for (Iterator it = orderAttributes.iterator(); it.hasNext();) {
>       temp.append(((OrderAttribute)it.next()).toString());
>     }
>     return temp.toString();
>   }
> 
>   private ArrayList orderAttributes = new ArrayList(1);
>   private long orderObjId;
> }
> 
> ---------------------------
> OrderAttribute.java
> 
> package data.order;
> 
> public abstract class OrderAttribute {
> 
>   public OrderAttribute() {}
> 
>   public String getAttributeName(){
>     return this.attributeName;
>   }
> 
>   public void setAttributeName(String aAttributeName){
>     this.attributeName = aAttributeName;
>   }
> 
>   public String toString() {
>     StringBuffer temp = new StringBuffer();
>     temp.append("The Attribute Name: " + attributeName + "\n");
>     return temp.toString();
>   }
> 
>   private String attributeName;
> }
> ---------------------------------
> MMOrderAttribute.java
> 
> package data.order;
> 
> public class MMOrderAttribute extends OrderAttribute {
> 
>   public MMOrderAttribute() { }
> 
>   public String getOrderAction(){
>     return this.orderAction;
>   }
> 
>   public void setOrderAction(String aOrderAction) {
>     this.orderAction = aOrderAction;
>   }
> 
>   public String toString() {
>     StringBuffer temp = new StringBuffer(super.toString());
>     temp.append("The Order Action: " + orderAction + "\n");
>     return temp.toString();
>   }
> 
>   private String orderAction;
> }
> ----------------------------------
> VoiceOrderAttribute.java
> 
> package data.order;
> 
> public class VoiceOrderAttribute extends OrderAttribute {
> 
>   public VoiceOrderAttribute() { }
> 
>   public String getOrderType(){
>     return this.orderType;
>   }
> 
>   public void setOrderType(String aOrderType) {
>     this.orderType = aOrderType;
>   }
> 
>   public String toString() {
>     StringBuffer temp = new StringBuffer(super.toString());
>     temp.append("The Order Type: " + orderType + "\n");
>     return temp.toString();
>   }
> 
>   private String orderType;
> }
> 
> Here is the xml file which we try to generate these objects based from
> _____________________________________
> 
> <?xml version="1.0" encoding="UTF-8"?>
> <!DOCTYPE Order SYSTEM
> "D:\Personal_Projects\CastorInterface\src\service\interfaceTesting.dtd">
> <Order>
>         <OrderObjId>1234567890</OrderObjId>
>         <OrderAttribute>
>                 <AttributeName>MMOrderAttribute</AttributeName>
>                 <MMOrderAttribute>
>                         <OrderAction>Install</OrderAction>
>                 </MMOrderAttribute>
>         </OrderAttribute>
>         <OrderAttribute>
>                 <AttributeName>VoiceOrderAttribute</AttributeName>
>                 <VoiceOrderAttribute>
>                         <OrderType>New</OrderType>
>                 </VoiceOrderAttribute>
>         </OrderAttribute>
> </Order>

----------------------------------------------------------- 
If you wish to unsubscribe from this mailing, send mail to
[EMAIL PROTECTED] with a subject of:
        unsubscribe castor-dev

Reply via email to