Looks like the best approach to me, too. Using element names as part of 
the data is kind of stretching the boundaries of JiBX usage, but with 
custom marshaller/unmarshaller code all things are possible.

  - Dennis

Dennis M. Sosnoski
SOA and Web Services in Java
Training and Consulting
http://www.sosnoski.com - http://www.sosnoski.co.nz
Seattle, WA +1-425-939-0576 - Wellington, NZ +64-4-298-6117



Varghese C V wrote:
> What you need is a marshaller/unmarshaller for the animal tags. A field 
> in the animal class holds the tag name. PFA the relevant artifacts.
> This way you can add an animal to the zoo without adding any classes. 
> What do you think?
>
> regards
> Varghese C V
>
> binding.xml
> ---------------
> <binding>
>
>    <mapping name="ResponseObject" class="zoo.ResponseObject">
>        <collection field="animals"
>                item-type="zoo.Animal"
>                factory="org.jibx.runtime.Utility.arrayListFactory">
>            <structure map-as="zoo.Animal"/>          </collection>
>    </mapping>
>
>    <mapping class="zoo.Animal" marshaller="zoo.AnimalMapper" 
> unmarshaller="zoo.AnimalMapper"/>
> </binding>
>
> Animal.java
> ---------------
> package zoo;
> public class Animal {
>    public String tagName;    public String name;
>    public String typeCode;
>      public Animal() {
>    }
>    public Animal(String tagName) {
>        this.tagName = tagName;
>    }
> }
>
> ResponseObject.java
> -----------------------
> package zoo;
>
> import java.util.ArrayList;
> import java.util.List;
>
> public class ResponseObject {
>    List animals = new ArrayList();
>   }
>
> AnimalMapper.java
> ----------------------
> package zoo;
>
> import org.jibx.runtime.IAliasable;
> import org.jibx.runtime.IMarshaller;
> import org.jibx.runtime.IMarshallingContext;
> import org.jibx.runtime.IUnmarshaller;
> import org.jibx.runtime.IUnmarshallingContext;
> import org.jibx.runtime.JiBXException;
> import org.jibx.runtime.impl.MarshallingContext;
> import org.jibx.runtime.impl.UnmarshallingContext;
>
> public class AnimalMapper implements IMarshaller, IUnmarshaller, 
> IAliasable {
>
>    private static final String ANIMAL_ATTRIBUTE_TYPECODE = "tp_cd";
>    private static final String ANIMAL_ATTRIBUTE_NAME = "name";
>    private String m_uri;
>    private int m_index;
>
>    public AnimalMapper() {
>        m_uri = null;
>        m_index = 0;
>    }
>
>    public boolean isExtension(int index) {
>        return false;
>    }
>
>    public void marshal(Object obj, IMarshallingContext ictx) throws 
> JiBXException {
>
>        if (!(obj instanceof Animal)) {
>            throw new JiBXException("Invalid object type for marshaller");
>        } else if (!(ictx instanceof MarshallingContext)) {
>            throw new JiBXException("Invalid object type for marshaller");
>        } else {
>            MarshallingContext ctx = (MarshallingContext) ictx;
>            Animal animal = (Animal) obj;
>            ctx.startTag(m_index, animal.tagName);
>            ctx.startTag(m_index, 
> ANIMAL_ATTRIBUTE_NAME).content(animal.name).endTag(m_index, 
> ANIMAL_ATTRIBUTE_NAME);
>            ctx.startTag(m_index, 
> ANIMAL_ATTRIBUTE_TYPECODE).content(animal.typeCode).endTag(m_index, 
> ANIMAL_ATTRIBUTE_TYPECODE);
>            ctx.endTag(m_index, animal.tagName);
>        }
>    }
>
>    public boolean isPresent(IUnmarshallingContext ctx) throws 
> JiBXException {
>        return !ctx.isEnd();
>    }
>
>    public Object unmarshal(Object obj, IUnmarshallingContext ictx) 
> throws JiBXException {
>        UnmarshallingContext ctx = (UnmarshallingContext)ictx;
>              String tagName = ctx.toStart();
>        Animal animal = (Animal) obj;
>        if (animal == null) {
>            animal = new Animal(tagName);
>        }
>        ctx.parsePastStartTag(m_uri, tagName);
>        String name = ctx.parseElementText(m_uri, ANIMAL_ATTRIBUTE_NAME);
>        String typeCode = ctx.parseElementText(m_uri, 
> ANIMAL_ATTRIBUTE_TYPECODE);
>        animal.name = name;
>        animal.typeCode = typeCode;
>        ctx.parsePastEndTag(m_uri, tagName);
>              return animal;
>    }
> }
>
>
> [EMAIL PROTECTED] wrote:
>   
>> List users,
>>
>> I'm having an issue with trying to map a collection of 
>> (sorta)-abstract objects. The issue is that, in the XML that I'm 
>> unmarshalling, the overall structure is
>> the same, but the element name will differ. Here's an example:
>>
>>                 <ResponseObject>
>>                         <CatTable>
>>                                 <name>Boots</name>
>>                                 <tp_cd>1001</tp_cd>
>>                         </CatTable>
>>                         <CatTable>
>>                                 <name>Fluffy</name>
>>                                 <tp_cd>1002</tp_cd>
>>                         </CatTable>
>>                         .....
>>                 </ResponseObject>
>>
>> The next XML I will unmarshall (from the same service call) is like this:
>>
>>                 <ResponseObject>
>>                         <DogTable>
>>                                 <name>Rex</name>
>>                                 <tp_cd>1003</tp_cd>
>>                         </DogTable>
>>                         <DogTable>
>>                                 <name>King</name>
>>                                 <tp_cd>1004</tp_cd>
>>                         </DogTable>
>>                         .....
>>                 </ResponseObject>
>>
>> All I really need is a class for Animal, with no differentiation 
>> between cats, dogs, beavers, etc., and there are over a hundred 
>> animals. If I have an Animal class, like this:
>>
>> public class Animal {
>>         private String name;
>>         private String typeCode;
>> }
>>
>> I want to unmarshall to an Animal object, since that is all I need in 
>> this case. There is NO Cat or Dog class. All responses will look the 
>> same with the exception of the element name (like CatTable, DogTable).
>>
>> If I define my unmarshalling XML like this:
>>
>>         <mapping name="ResponseObject" class="ResponseObject">
>>             <collection field="animals"  item-type="Animal" />
>>           </mapping>
>>
>> I can then do the following for just a single animal:
>>
>>         <mapping name="CatTable" class="Animal" abstract="true">
>>             <value name="name" field="typeValue" usage="required"/>
>>             <value name="tp_cd" field="typeCode" usage="required"/>
>>         </mapping>
>>
>> If I try to define a second animal:
>>
>>         <mapping name="DogTable" class="Animal" abstract="true">
>>             <value name="name" field="typeValue" usage="required"/>
>>             <value name="tp_cd" field="typeCode" usage="required"/>
>>         </mapping>
>>
>> I get an error because the Animal class is multiply declared.
>>
>> The bottom line is that there seems to be no way to have an element 
>> name map to anything in JiBX without actually having a class that it 
>> concretely maps to. I have tried many combinations of things to get 
>> this to work, and can't find a solution. What I don't want to have to 
>> do is create classes like this:
>>
>>         public class Dog extends Animal {}
>>
>>         public class Cat extends Animal{}
>>
>> and have hundreds of class files.
>>
>> I have also tried:
>>
>>         <mapping class="Animal" abstract="true" type-name="lookup"     
>>    >
>>             <value name="name" field="name" usage="required"/>
>>             <value name="tp_cd" field="typeCode" usage="required"/>
>>          </mapping>
>>           <mapping name="CatTable" map-as="lookup" />
>>           <mapping name="DogTable" map-as="lookup" />
>>
>> with no success. I have tried many other variants, all with new 
>> interesting errors to read up on.
>>
>> One of the justifications for this is that I want to be able to add a 
>> new Animal to my zoo without creating a class for it, so that through 
>> JiBX and Spring alone I can configure my app. The actual XML and 
>> objects are not those you see above, but the example is analogous and 
>> simple.
>>
>> I appreciate any thoughts on this.
>>
>> Brian
>> ------------------------------------------------------------------------
>>
>> -------------------------------------------------------------------------
>> This SF.net email is sponsored by: Splunk Inc.
>> Still grepping through log files to find problems?  Stop.
>> Now Search log events and configuration files using AJAX and a browser.
>> Download your FREE copy of Splunk now >>  http://get.splunk.com/
>> ------------------------------------------------------------------------
>>
>> _______________________________________________
>> jibx-users mailing list
>> jibx-users@lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/jibx-users
>>   
>>     
>
>
>
> [EMAIL PROTECTED] wrote:
>   
>> List users,
>>
>> I'm having an issue with trying to map a collection of 
>> (sorta)-abstract objects. The issue is that, in the XML that I'm 
>> unmarshalling, the overall structure is
>> the same, but the element name will differ. Here's an example:
>>
>>                 <ResponseObject>
>>                         <CatTable>
>>                                 <name>Boots</name>
>>                                 <tp_cd>1001</tp_cd>
>>                         </CatTable>
>>                         <CatTable>
>>                                 <name>Fluffy</name>
>>                                 <tp_cd>1002</tp_cd>
>>                         </CatTable>
>>                         .....
>>                 </ResponseObject>
>>
>> The next XML I will unmarshall (from the same service call) is like this:
>>
>>                 <ResponseObject>
>>                         <DogTable>
>>                                 <name>Rex</name>
>>                                 <tp_cd>1003</tp_cd>
>>                         </DogTable>
>>                         <DogTable>
>>                                 <name>King</name>
>>                                 <tp_cd>1004</tp_cd>
>>                         </DogTable>
>>                         .....
>>                 </ResponseObject>
>>
>> All I really need is a class for Animal, with no differentiation 
>> between cats, dogs, beavers, etc., and there are over a hundred 
>> animals. If I have an Animal class, like this:
>>
>> public class Animal {
>>         private String name;
>>         private String typeCode;
>> }
>>
>> I want to unmarshall to an Animal object, since that is all I need in 
>> this case. There is NO Cat or Dog class. All responses will look the 
>> same with the exception of the element name (like CatTable, DogTable).
>>
>> If I define my unmarshalling XML like this:
>>
>>         <mapping name="ResponseObject" class="ResponseObject">
>>             <collection field="animals"  item-type="Animal" />
>>           </mapping>
>>
>> I can then do the following for just a single animal:
>>
>>         <mapping name="CatTable" class="Animal" abstract="true">
>>             <value name="name" field="typeValue" usage="required"/>
>>             <value name="tp_cd" field="typeCode" usage="required"/>
>>         </mapping>
>>
>> If I try to define a second animal:
>>
>>         <mapping name="DogTable" class="Animal" abstract="true">
>>             <value name="name" field="typeValue" usage="required"/>
>>             <value name="tp_cd" field="typeCode" usage="required"/>
>>         </mapping>
>>
>> I get an error because the Animal class is multiply declared.
>>
>> The bottom line is that there seems to be no way to have an element 
>> name map to anything in JiBX without actually having a class that it 
>> concretely maps to. I have tried many combinations of things to get 
>> this to work, and can't find a solution. What I don't want to have to 
>> do is create classes like this:
>>
>>         public class Dog extends Animal {}
>>
>>         public class Cat extends Animal{}
>>
>> and have hundreds of class files.
>>
>> I have also tried:
>>
>>         <mapping class="Animal" abstract="true" type-name="lookup"     
>>    >
>>             <value name="name" field="name" usage="required"/>
>>             <value name="tp_cd" field="typeCode" usage="required"/>
>>          </mapping>
>>           <mapping name="CatTable" map-as="lookup" />
>>           <mapping name="DogTable" map-as="lookup" />
>>
>> with no success. I have tried many other variants, all with new 
>> interesting errors to read up on.
>>
>> One of the justifications for this is that I want to be able to add a 
>> new Animal to my zoo without creating a class for it, so that through 
>> JiBX and Spring alone I can configure my app. The actual XML and 
>> objects are not those you see above, but the example is analogous and 
>> simple.
>>
>> I appreciate any thoughts on this.
>>
>> Brian
>> ------------------------------------------------------------------------
>>
>> -------------------------------------------------------------------------
>> This SF.net email is sponsored by: Splunk Inc.
>> Still grepping through log files to find problems?  Stop.
>> Now Search log events and configuration files using AJAX and a browser.
>> Download your FREE copy of Splunk now >>  http://get.splunk.com/
>> ------------------------------------------------------------------------
>>
>> _______________________________________________
>> jibx-users mailing list
>> jibx-users@lists.sourceforge.net
>> https://lists.sourceforge.net/lists/listinfo/jibx-users
>>   
>>     
>
>
> -------------------------------------------------------------------------
> This SF.net email is sponsored by: Splunk Inc.
> Still grepping through log files to find problems?  Stop.
> Now Search log events and configuration files using AJAX and a browser.
> Download your FREE copy of Splunk now >>  http://get.splunk.com/
> _______________________________________________
> jibx-users mailing list
> jibx-users@lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/jibx-users
>
>   

-------------------------------------------------------------------------
This SF.net email is sponsored by: Splunk Inc.
Still grepping through log files to find problems?  Stop.
Now Search log events and configuration files using AJAX and a browser.
Download your FREE copy of Splunk now >>  http://get.splunk.com/
_______________________________________________
jibx-users mailing list
jibx-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jibx-users

Reply via email to