Hi Martin,

i don't think you can register custom (collection) handlers ... easily. Actually, I'd rather add a new collection handler to Castor to be able to cope with new types such as MultiValueMap.

Regards
Werner

On 26.05.2010 23:47, Martin Bolz wrote:
Good point Werner. Do you know where to register custom collection handlers? 
Thanks!

Regards,
Martin

-------- Original-Nachricht --------
Datum: Wed, 26 May 2010 19:44:19 +0200
Von: Werner Guttmann<[email protected]>
An: [email protected]
CC: [email protected]
Betreff: Re: AW: [castor-user] Problem with mapping of a MultiValueMap

Hi,

looking at this thread, I'd rather suggest to implement a new collection
handler for MultiValueMap. I think that should be rather straight-forward.

Regards
Werner

On 26.05.2010 19:05, [email protected] wrote:
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.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

     http://xircles.codehaus.org/manage_email



---------------------------------------------------------------------
To unsubscribe from this list, please visit:

   http://xircles.codehaus.org/manage_email


Reply via email to