Map<String,Set<String>> | Convert To XML

classic Classic list List threaded Threaded
5 messages Options
Raj
Reply | Threaded
Open this post in threaded view
|

Map<String,Set<String>> | Convert To XML

Raj
Hi

Thanks for quick repsonses.

I have map below.

Map<String,Set<String>> desksDetails = new HashMap<String, Set<String>>();

Map key is "ABC"
and value is Set holding usernames.

I want to covert this into xml like
<desk name="ABC">
        <user name="rpaynter"/>
        <user name="yatesn"/>
        <user name="gnishok"/>
        <user name="bingminc"/>
        <user name="nixmar"/>
        <user name="tanyun"/>
        <user name="choo"/>
</desk>

I looked into NamedMapConverter but the map here is key,value as String.
In my case value is collection.

Any pointer will be higly appreciated.

Regards
Raj


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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Map<String,Set<String>> | Convert To XML

Jörg Schaible-2
Hi Raj,

Raj wrote:

> Hi
>
> Thanks for quick repsonses.
>
> I have map below.
>
> Map<String,Set<String>> desksDetails = new HashMap<String, Set<String>>();
>
> Map key is "ABC"
> and value is Set holding usernames.
>
> I want to covert this into xml like
> <desk name="ABC">
>         <user name="rpaynter"/>
>         <user name="yatesn"/>
>         <user name="gnishok"/>
>         <user name="bingminc"/>
>         <user name="nixmar"/>
>         <user name="tanyun"/>
>         <user name="choo"/>
> </desk>
>
> I looked into NamedMapConverter but the map here is key,value as String.
> In my case value is collection.
>
> Any pointer will be higly appreciated.


Did you try:

 new NamedMapConverter(xstream.getMapper(), "desk", "name", String.class,
   null, HashSet.class, true, false, xstream.getConverterLookup());

That should be written as:

 <map>
   <desk name="ABC">
      <string>rpaynter</string>
      <!-- rest omitted -->
   </entry>
 </map>

You will have to register an additional NamedCollectionConverter for HashSet
in general though to influence the inner set. However, that would affect any
HashSet in your object graph.

The only alternative is otherwise a gain a custom converter, probably
derived from the MapConverter.

- Jörg


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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Map<String,Set<String>> | Convert To XML

Jörg Schaible-4
Jörg Schaible wrote:

> Hi Raj,
>
> Raj wrote:
>
>> Hi
>>
>> Thanks for quick repsonses.
>>
>> I have map below.
>>
>> Map<String,Set<String>> desksDetails = new HashMap<String,
>> Set<String>>();
>>
>> Map key is "ABC"
>> and value is Set holding usernames.
>>
>> I want to covert this into xml like
>> <desk name="ABC">
>>         <user name="rpaynter"/>
>>         <user name="yatesn"/>
>>         <user name="gnishok"/>
>>         <user name="bingminc"/>
>>         <user name="nixmar"/>
>>         <user name="tanyun"/>
>>         <user name="choo"/>
>> </desk>
>>
>> I looked into NamedMapConverter but the map here is key,value as String.
>> In my case value is collection.
>>
>> Any pointer will be higly appreciated.
>
>
> Did you try:
>
>  new NamedMapConverter(xstream.getMapper(), "desk", "name", String.class,
>    null, HashSet.class, true, false, xstream.getConverterLookup());
>
> That should be written as:
>
>  <map>
>    <desk name="ABC">
>       <string>rpaynter</string>
>       <!-- rest omitted -->
>    </entry>
>  </map>
>
> You will have to register an additional NamedCollectionConverter for
> HashSet in general though to influence the inner set. However, that would
> affect any HashSet in your object graph.
>
> The only alternative is otherwise a gain a custom converter, probably
> derived from the MapConverter.

Actually there's a third option by injecting a modified ConverterLookup:

  new NamedMapConverter(xstream.getMapper(), "desk", "name", String.class,
    null, HashSet.class, true, false, new ConverterLookup() {
    @Override
    Converter lookupConverterForClass(Class type) {
      if (type == HashSet.class) {
        return new NamedCollectionConverter( ... );
      } else
        return xstream.getConverterLookup().lookupConverterForClass(type);
    }
  });

You get the idea ...

Cheers,
Jörg


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

    http://xircles.codehaus.org/manage_email


Raj
Reply | Threaded
Open this post in threaded view
|

Re: Map<String,Set<String>> | Convert To XML

Raj
Jörg Schaible <joerg.schaible@...> writes:

>
> Jörg Schaible wrote:
>
> > Hi Raj,
> >
> > Raj wrote:
> >
> >> Hi
> >>
> >> Thanks for quick repsonses.
> >>
> >> I have map below.
> >>
> >> Map<String,Set<String>> desksDetails = new HashMap<String,
> >> Set<String>>();
> >>
> >> Map key is "ABC"
> >> and value is Set holding usernames.
> >>
> >> I want to covert this into xml like
> >> <desk name="ABC">
> >>         <user name="rpaynter"/>
> >>         <user name="yatesn"/>
> >>         <user name="gnishok"/>
> >>         <user name="bingminc"/>
> >>         <user name="nixmar"/>
> >>         <user name="tanyun"/>
> >>         <user name="choo"/>
> >> </desk>
> >>
> >> I looked into NamedMapConverter but the map here is key,value as
String.
> >> In my case value is collection.
> >>
> >> Any pointer will be higly appreciated.
> >
> >
> > Did you try:
> >
> >  new NamedMapConverter(xstream.getMapper(), "desk", "name",
String.class,

> >    null, HashSet.class, true, false, xstream.getConverterLookup());
> >
> > That should be written as:
> >
> >  <map>
> >    <desk name="ABC">
> >       <string>rpaynter</string>
> >       <!-- rest omitted -->
> >    </entry>
> >  </map>
> >
> > You will have to register an additional NamedCollectionConverter for
> > HashSet in general though to influence the inner set. However, that
would

> > affect any HashSet in your object graph.
> >
> > The only alternative is otherwise a gain a custom converter, probably
> > derived from the MapConverter.
>
> Actually there's a third option by injecting a modified ConverterLookup:
>
>   new NamedMapConverter(xstream.getMapper(), "desk", "name", String.class,
>     null, HashSet.class, true, false, new ConverterLookup() {
>      <at> Override
>     Converter lookupConverterForClass(Class type) {
>       if (type == HashSet.class) {
>         return new NamedCollectionConverter( ... );
>       } else
>         return xstream.getConverterLookup().lookupConverterForClass(type);
>     }
>   });
>
> You get the idea ...
>
> Cheers,
> Jörg
>
> ---------------------------------------------------------------------
> To unsubscribe from this list, please visit:
>
>     http://xircles.codehaus.org/manage_email
>
>


Hi

Actually i am using the custom converter.

so i am doing something like

public void marshal(Object source, HierarchicalStreamWriter writer,
                        MarshallingContext context) {
MCS mcs = (MCS) source;
//couple of the other customization here
if(mcs.getDesks()!=null)
context.convertAnother(mcs.getDesks(),new DeskCollectionConverter());

}

Now i want my DeskCollectionConverter to extend NamedCollectionConverter.

The
NamedCollectionConverter constructor parameters are
NamedCollectionConverter(Class type, Mapper mapper, String itemName, Class
itemType)

How to pass these parameters ?

I am learning and soon will be perfect with XStream.

Regards
Raj



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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Map<String,Set<String>> | Convert To XML

Jörg Schaible-4
Hello Raj,

Raj wrote:

> Jörg Schaible <joerg.schaible@...> writes:
>
>>
>> Jörg Schaible wrote:
>>
>> > Hi Raj,
>> >
>> > Raj wrote:
>> >
>> >> Hi
>> >>
>> >> Thanks for quick repsonses.
>> >>
>> >> I have map below.
>> >>
>> >> Map<String,Set<String>> desksDetails = new HashMap<String,
>> >> Set<String>>();
>> >>
>> >> Map key is "ABC"
>> >> and value is Set holding usernames.
>> >>
>> >> I want to covert this into xml like
>> >> <desk name="ABC">
>> >>         <user name="rpaynter"/>
>> >>         <user name="yatesn"/>
>> >>         <user name="gnishok"/>
>> >>         <user name="bingminc"/>
>> >>         <user name="nixmar"/>
>> >>         <user name="tanyun"/>
>> >>         <user name="choo"/>
>> >> </desk>
>> >>
>> >> I looked into NamedMapConverter but the map here is key,value as
> String.
>> >> In my case value is collection.
>> >>
>> >> Any pointer will be higly appreciated.
>> >
>> >
>> > Did you try:
>> >
>> >  new NamedMapConverter(xstream.getMapper(), "desk", "name",
> String.class,
>> >    null, HashSet.class, true, false, xstream.getConverterLookup());
>> >
>> > That should be written as:
>> >
>> >  <map>
>> >    <desk name="ABC">
>> >       <string>rpaynter</string>
>> >       <!-- rest omitted -->
>> >    </entry>
>> >  </map>
>> >
>> > You will have to register an additional NamedCollectionConverter for
>> > HashSet in general though to influence the inner set. However, that
> would
>> > affect any HashSet in your object graph.
>> >
>> > The only alternative is otherwise a gain a custom converter, probably
>> > derived from the MapConverter.
>>
>> Actually there's a third option by injecting a modified ConverterLookup:
>>
>>   new NamedMapConverter(xstream.getMapper(), "desk", "name",
>>   String.class,
>>     null, HashSet.class, true, false, new ConverterLookup() {
>>      <at> Override
>>     Converter lookupConverterForClass(Class type) {
>>       if (type == HashSet.class) {
>>         return new NamedCollectionConverter( ... );
>>       } else
>>         return
>>         xstream.getConverterLookup().lookupConverterForClass(type);
>>     }
>>   });
>>
>> You get the idea ...
>>
>> Cheers,
>> Jörg
>>
>> ---------------------------------------------------------------------
>> To unsubscribe from this list, please visit:
>>
>>     http://xircles.codehaus.org/manage_email
>>
>>
>
>
> Hi
>
> Actually i am using the custom converter.
>
> so i am doing something like
>
> public void marshal(Object source, HierarchicalStreamWriter writer,
> MarshallingContext context) {
> MCS mcs = (MCS) source;
> //couple of the other customization here
> if(mcs.getDesks()!=null)
> context.convertAnother(mcs.getDesks(),new DeskCollectionConverter());
>
> }


Normally you use a ConverterLookup instead of instantiating converters
types, because the converter instances are typically reused. However, my
solution from above was not that different in the end. You may create the
converter in the constructor though and keep it as member to reuse it.

 
> Now i want my DeskCollectionConverter to extend NamedCollectionConverter.
>
> The
> NamedCollectionConverter constructor parameters are
> NamedCollectionConverter(Class type, Mapper mapper, String itemName, Class
> itemType)
>
> How to pass these parameters ?


The only parameter you should be curious about is the Mapper. All the others
should be known. Normally you pass the Mapper in with the constructor of
your "outer" converter:

============== %< =============
 class YourCustomConverter extends Converter {
   Converter collectionConverter;
   YourCustomConverter(Mapper mapper) {
     collectionConverter = new DeskCollectionConverter(mapper);
   }
 }
============== %< =============

> I am learning and soon will be perfect with XStream.

Fine. ;-)

Cheers,
Jörg


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

    http://xircles.codehaus.org/manage_email