Hi Maghen,

 

I tried to use FieldHandlers like this before…also failed. I think the 
intension of FieldHandlers is not to construct the xml-representation yourself, 
but provide Castor with 

something it can handle by default (in terms of data mediation you create a 
programmatic mapping between classes, not classes and xml). In your case I 
would try to do the following: 

 

----Info.java

package userhelp;

class Info {

                private String name;

                private String value;

                //getters + setters for name and value as well as appropriate 
constructor for Info

}

----

 

----mapping.xml

<mapping>

                …

                <class name=”userhelp.Info” auto-complete=”true”>

                               <field name=“name” type=”java.lang.String”>

                                               <bind-xml 
node=”attribute”></bind-xml>

</field>

</class>

<field name=”value” container=”true” type=”java.lang.String”></field>

                …

</mapping>

----

 

…which produced the following output for me:

<info name=”key”>value</info>

 

Now you could try to modify your FieldHandler to return the following in the 
convertUponGet:

 

return new Info(key.toString(), value.toString());

 

But if you do it like that you have to rely on the collectionIteration of the 
fieldhandler and convert  each item of the collection in the convertUponGet 
(you could also try to populate an ArrayList with the Info items and  return it 
if you really need to suppress automatic collectionIteration). 

 

Hope it helps.

 

Regards, 

 

Martin

 

Von: Maghen Calinghee [mailto:[email protected]] 
Gesendet: Dienstag, 25. Mai 2010 15:04
An: [email protected]
Betreff: [castor-user] Problem with mapping of a MultiValueMap

 

Hello,

I'm trying to produce an xml code from a map with multiple values.
I'm using the map class org.apache.commons.collections.map.MultiValueMap which 
allows multiple values for one key.

I would like to produce something like that :
        <Info name="key1">value1</Info>
        <Info name="key1">value2</Info>
        <Info name="key2">value2</Info>
        <Info name="keyN">valueN</Info>

But the best I could have is :
    <Info name="key1">value1</Info>
    <Info>value2</Info>
    <Info name="key2">value2</Info>
    <Info name="keyN">valueN</Info>

I'm using the following mapping! (with MapItem) :
    <class name="org.exolab.castor.mapping.MapItem">
        <map-to xml="Info" />
        <field name="key" type="string" >
            <bind-xml name="name" location="Info" node="attribute" />
        </field>
        <field name="value" type="string" collection="arraylist">
            <bind-xml name="Info" node="element" />
        </field>
    </class>

Another solution is to use a handler which allows me to get the MultiValueMap 
object then i construct the xml code (see the following code) :
public class MultiValueMapHan! dler extends GeneralizedFieldHandler {

    public MultiValueMapHandler() {
        setCollectionIteration(false);
    }

    @SuppressWarnings("unchecked")
    @Override
    public Class getFieldType() {
        return MultiValueMap.class;
    }

    @Override
    public Object convertUponGet(Object source) {
       
        MultiValueMap map = (MultiValueMap) source;
        StringBuilder xml = new StringBuilder();
        for (Object key : map.keySet()) {
            for (Object value : map.getCollecti on(key)) {
         !        xml.append("<Info 
name=\""+key.toString()+"\">").append(value.toString()).append("<Info/>");
            }
        }
        return xml.toString();
    }
   
    @Override
    public Object convertUponSet(Object source) {
        // There is no need to implement this method
        return null;
    }
}

And i got the following xml code :
<Request type="keep-put" version="1">
    <ModuleId>KEEPClient</ModuleId>
    <RequestId>REQUEST001</RequestId>
    <Document>c2FtcGxlIGRvY3VtZW50</Document! >
    <DocumentInfo>
           &lt;Info name="key1"&gt;value1&lt;Info/&gt;
           &lt;Info name="key1"&gt;value2&lt;Info/&gt;
           &lt;Info name="key2"&gt;value2&lt;Info/&gt;
           &lt;Info name="keyN"&gt;valueN&lt;Info/&gt;
    </DocumentInfo>
</Request>

As you could see the '<' and '>' is not escaped and i can't produce the right 
xml code.

Does someone have any idea to solve this problem?

Thanks you in advance.

Maghen.

Reply via email to