Set both collection name and item name

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

Set both collection name and item name

Paul B. Anderson
I'm trying to handle an ArrayList<String> to achieve XML like the following:

    <entries>
      <entry>Alpha</entry>
      <entry>Bravo</entry>
      <entry>Charlie</entry>
    </entries>

The examples Ive seen let me name the outer <entries> element or
eliminate the outer element and name the inner elements. Is there a way to
achieve both?  I'm using annotations but I'm not tied to them.


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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Set both collection name and item name

Jörg Schaible-2
Hi Paul,

Paul B. Anderson wrote:

> I'm trying to handle an ArrayList<String> to achieve XML like the
> following:
>
>     <entries>
>       <entry>Alpha</entry>
>       <entry>Bravo</entry>
>       <entry>Charlie</entry>
>     </entries>
>
> The examples Ive seen let me name the outer <entries> element or
> eliminate the outer element and name the inner elements. Is there a way to
> achieve both?  I'm using annotations but I'm not tied to them.

Register a local converter of type NamedCollectionConverter for the field.
This is also possible with annotations, but a bit tedious. You may have a
look at the ParameterizedConverterTest for an example.

Cheers,
Jörg


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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Set both collection name and item name

Paul B. Anderson
I'm still struggling.  Where can I find ParameterizedConverterTest?

One additional aspect:  I have three instances of the ArrayList<String>
in my object, each with different inner element names.

On 2/24/14 1:46 PM, Jörg Schaible wrote:

> Hi Paul,
>
> Paul B. Anderson wrote:
>
>> I'm trying to handle an ArrayList<String> to achieve XML like the
>> following:
>>
>>      <entries>
>>        <entry>Alpha</entry>
>>        <entry>Bravo</entry>
>>        <entry>Charlie</entry>
>>      </entries>
>>
>> The examples Ive seen let me name the outer <entries> element or
>> eliminate the outer element and name the inner elements. Is there a way to
>> achieve both?  I'm using annotations but I'm not tied to them.
> Register a local converter of type NamedCollectionConverter for the field.
> This is also possible with annotations, but a bit tedious. You may have a
> look at the ParameterizedConverterTest for an example.
>
> Cheers,
> Jörg
>
>
> ---------------------------------------------------------------------
> 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 | Threaded
Open this post in threaded view
|

Re: Re: Set both collection name and item name

Jörg Schaible-2
Hi Paul,

Paul B. Anderson wrote:

> I'm still struggling.  Where can I find ParameterizedConverterTest?

https://fisheye.codehaus.org/browse/xstream/trunk/xstream/src/test/com/thoughtworks/acceptance/annotations/ParametrizedConverterTest.java?r=2131

> One additional aspect:  I have three instances of the ArrayList<String>
> in my object, each with different inner element names.

Register a local converter for each instance.

Also have a look at the parallel thread with the CollectionConverter and
@XStreamConverter annotation.

Cheers,
Jörg


>
> On 2/24/14 1:46 PM, Jörg Schaible wrote:
>> Hi Paul,
>>
>> Paul B. Anderson wrote:
>>
>>> I'm trying to handle an ArrayList<String> to achieve XML like the
>>> following:
>>>
>>>      <entries>
>>>        <entry>Alpha</entry>
>>>        <entry>Bravo</entry>
>>>        <entry>Charlie</entry>
>>>      </entries>
>>>
>>> The examples Ive seen let me name the outer <entries> element or
>>> eliminate the outer element and name the inner elements. Is there a way
>>> to
>>> achieve both?  I'm using annotations but I'm not tied to them.
>> Register a local converter of type NamedCollectionConverter for the
>> field. This is also possible with annotations, but a bit tedious. You may
>> have a look at the ParameterizedConverterTest for an example.
>>
>> Cheers,
>> Jörg
>>
>>
>> ---------------------------------------------------------------------
>> 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



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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Re: Set both collection name and item name

Paul B. Anderson
I managed to create three almost identical converters for the three
ArrayList<String> elements of my Java structure and I can both read and
write XML in the desired format.

I was unable to see how to get the CollectionConverter or
NamedCollectionConverter to fit in.  I don't have distinct types for the
three lists and the examples I saw assumed a distinct type for the
collection.

I was trying to use the annotation approach because that seemed to be
the only way to be specific about which of the ArrayLists I was
referring to.  It was not clear how to proceed and trial and error was
not effective for me.

What I would really like is an annotation or combination of annotations
that combines a top level alias with an implicit alias, something like

   @XStreamCollection("movies", "title")
   ArrayList<String> movies;

   @XStreamCollection("plays", "title")
   ArrayList<String> plays;

etc., producing
   ...
   <movies>
     <title>Title 1</title>
     <title>Title 2</title>
   </movies>
   <plays>
     <title>Title 1</title>
     <title>Title 2</title>
   </plays>

It seems to me that simple collections or maps are somewhat special.  If
it were more complex than a simple collection, a person would typically
want to develop a class but for just a list of items a special class is
much less compelling.


On 2/24/14 5:03 PM, Jörg Schaible wrote:

> Hi Paul,
>
> Paul B. Anderson wrote:
>
>> I'm still struggling.  Where can I find ParameterizedConverterTest?
> https://fisheye.codehaus.org/browse/xstream/trunk/xstream/src/test/com/thoughtworks/acceptance/annotations/ParametrizedConverterTest.java?r=2131
>
>> One additional aspect:  I have three instances of the ArrayList<String>
>> in my object, each with different inner element names.
> Register a local converter for each instance.
>
> Also have a look at the parallel thread with the CollectionConverter and
> @XStreamConverter annotation.
>
> Cheers,
> Jörg
>
>
>> On 2/24/14 1:46 PM, Jörg Schaible wrote:
>>> Hi Paul,
>>>
>>> Paul B. Anderson wrote:
>>>
>>>> I'm trying to handle an ArrayList<String> to achieve XML like the
>>>> following:
>>>>
>>>>       <entries>
>>>>         <entry>Alpha</entry>
>>>>         <entry>Bravo</entry>
>>>>         <entry>Charlie</entry>
>>>>       </entries>
>>>>
>>>> The examples Ive seen let me name the outer <entries> element or
>>>> eliminate the outer element and name the inner elements. Is there a way
>>>> to
>>>> achieve both?  I'm using annotations but I'm not tied to them.
>>> Register a local converter of type NamedCollectionConverter for the
>>> field. This is also possible with annotations, but a bit tedious. You may
>>> have a look at the ParameterizedConverterTest for an example.
>>>
>>> Cheers,
>>> Jörg
>>>
>>>
>>> ---------------------------------------------------------------------
>>> 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
>
>
> ---------------------------------------------------------------------
> 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 | Threaded
Open this post in threaded view
|

Re: Re: Re: Set both collection name and item name

Jörg Schaible-2
Hi Paul,

Paul B. Anderson wrote:

> I managed to create three almost identical converters for the three
> ArrayList<String> elements of my Java structure and I can both read and
> write XML in the desired format.
>
> I was unable to see how to get the CollectionConverter or
> NamedCollectionConverter to fit in.  I don't have distinct types for the
> three lists and the examples I saw assumed a distinct type for the
> collection.

?? You register an individual instance of the NamedCollectionConverter as
local converter directly for each list ... why do you assume you have to
have distinct types?

>
> I was trying to use the annotation approach because that seemed to be
> the only way to be specific about which of the ArrayLists I was
> referring to.  It was not clear how to proceed and trial and error was
> not effective for me.
>
> What I would really like is an annotation or combination of annotations
> that combines a top level alias with an implicit alias, something like
>
>    @XStreamCollection("movies", "title")
>    ArrayList<String> movies;
>
>    @XStreamCollection("plays", "title")
>    ArrayList<String> plays;

  @XStreamConverter(value=NamedCollectionConverter.class, strings={"title"},
types={String.class})
  ArrayList<String> movies;
 
  @XStreamConverter(value=NamedCollectionConverter.class, strings={"title"},
types={String.class})
  ArrayList<String> plays;

produces:
 

> etc., producing
>    ...
>    <movies>
>      <title>Title 1</title>
>      <title>Title 2</title>
>    </movies>
>    <plays>
>      <title>Title 1</title>
>      <title>Title 2</title>
>    </plays>

This XML is *not* implicit, since you have a surrounding tag for your
elements - at least this is what *implicit* everywhere means for XStream.

> It seems to me that simple collections or maps are somewhat special.  If
> it were more complex than a simple collection, a person would typically
> want to develop a class but for just a list of items a special class is
> much less compelling.

You don't have to. The alternative would have been to register the local
converters using the XStream facade. Funny enough, the converter instance
can be the same here, because the name and type of the items are the same
(assuming Foo is the name of the class containing those two lists):

  Converter namingConverter = new NamedCollectionConverter(
    xstream.getMapper(), "title", String.class);
  xstream.registerLocalConverter(Foo.class, "movies", namingConverter);
  xstream.registerLocalConverter(Foo.class, "plays", namingConverter);


Cheers,
Jörg


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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Re: Re: Set both collection name and item name

Paul B. Anderson
Thank you.  This helps a lot.

On 2/25/14 6:31 PM, Jörg Schaible wrote:

> Hi Paul,
>
> Paul B. Anderson wrote:
>
>> I managed to create three almost identical converters for the three
>> ArrayList<String> elements of my Java structure and I can both read and
>> write XML in the desired format.
>>
>> I was unable to see how to get the CollectionConverter or
>> NamedCollectionConverter to fit in.  I don't have distinct types for the
>> three lists and the examples I saw assumed a distinct type for the
>> collection.
> ?? You register an individual instance of the NamedCollectionConverter as
> local converter directly for each list ... why do you assume you have to
> have distinct types?
>
>> I was trying to use the annotation approach because that seemed to be
>> the only way to be specific about which of the ArrayLists I was
>> referring to.  It was not clear how to proceed and trial and error was
>> not effective for me.
>>
>> What I would really like is an annotation or combination of annotations
>> that combines a top level alias with an implicit alias, something like
>>
>>     @XStreamCollection("movies", "title")
>>     ArrayList<String> movies;
>>
>>     @XStreamCollection("plays", "title")
>>     ArrayList<String> plays;
>    @XStreamConverter(value=NamedCollectionConverter.class, strings={"title"},
> types={String.class})
>    ArrayList<String> movies;
>  
>    @XStreamConverter(value=NamedCollectionConverter.class, strings={"title"},
> types={String.class})
>    ArrayList<String> plays;
>
> produces:
>  
>> etc., producing
>>     ...
>>     <movies>
>>       <title>Title 1</title>
>>       <title>Title 2</title>
>>     </movies>
>>     <plays>
>>       <title>Title 1</title>
>>       <title>Title 2</title>
>>     </plays>
> This XML is *not* implicit, since you have a surrounding tag for your
> elements - at least this is what *implicit* everywhere means for XStream.
>
>> It seems to me that simple collections or maps are somewhat special.  If
>> it were more complex than a simple collection, a person would typically
>> want to develop a class but for just a list of items a special class is
>> much less compelling.
> You don't have to. The alternative would have been to register the local
> converters using the XStream facade. Funny enough, the converter instance
> can be the same here, because the name and type of the items are the same
> (assuming Foo is the name of the class containing those two lists):
>
>    Converter namingConverter = new NamedCollectionConverter(
>      xstream.getMapper(), "title", String.class);
>    xstream.registerLocalConverter(Foo.class, "movies", namingConverter);
>    xstream.registerLocalConverter(Foo.class, "plays", namingConverter);
>
>
> Cheers,
> Jörg
>
>
> ---------------------------------------------------------------------
> 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 | Threaded
Open this post in threaded view
|

Re: Re: Re: Set both collection name and item name

Paul B. Anderson
I made the changes you suggested and everything is working as desired.  
Thank you again.

I'm curious, in the annotation version, how can I know what parameters
the annotation takes?  I see how the XStream facade works in the JAVADOCs.

Paul

On 2/25/14 7:28 PM, Paul B. Anderson wrote:

> Thank you.  This helps a lot.
>
> On 2/25/14 6:31 PM, Jörg Schaible wrote:
>> Hi Paul,
>>
>> Paul B. Anderson wrote:
>>
>>> I managed to create three almost identical converters for the three
>>> ArrayList<String> elements of my Java structure and I can both read and
>>> write XML in the desired format.
>>>
>>> I was unable to see how to get the CollectionConverter or
>>> NamedCollectionConverter to fit in.  I don't have distinct types for
>>> the
>>> three lists and the examples I saw assumed a distinct type for the
>>> collection.
>> ?? You register an individual instance of the
>> NamedCollectionConverter as
>> local converter directly for each list ... why do you assume you have to
>> have distinct types?
>>
>>> I was trying to use the annotation approach because that seemed to be
>>> the only way to be specific about which of the ArrayLists I was
>>> referring to.  It was not clear how to proceed and trial and error was
>>> not effective for me.
>>>
>>> What I would really like is an annotation or combination of annotations
>>> that combines a top level alias with an implicit alias, something like
>>>
>>>     @XStreamCollection("movies", "title")
>>>     ArrayList<String> movies;
>>>
>>>     @XStreamCollection("plays", "title")
>>>     ArrayList<String> plays;
>>    @XStreamConverter(value=NamedCollectionConverter.class,
>> strings={"title"},
>> types={String.class})
>>    ArrayList<String> movies;
>>      @XStreamConverter(value=NamedCollectionConverter.class,
>> strings={"title"},
>> types={String.class})
>>    ArrayList<String> plays;
>>
>> produces:
>>> etc., producing
>>>     ...
>>>     <movies>
>>>       <title>Title 1</title>
>>>       <title>Title 2</title>
>>>     </movies>
>>>     <plays>
>>>       <title>Title 1</title>
>>>       <title>Title 2</title>
>>>     </plays>
>> This XML is *not* implicit, since you have a surrounding tag for your
>> elements - at least this is what *implicit* everywhere means for
>> XStream.
>>
>>> It seems to me that simple collections or maps are somewhat
>>> special.  If
>>> it were more complex than a simple collection, a person would typically
>>> want to develop a class but for just a list of items a special class is
>>> much less compelling.
>> You don't have to. The alternative would have been to register the local
>> converters using the XStream facade. Funny enough, the converter
>> instance
>> can be the same here, because the name and type of the items are the
>> same
>> (assuming Foo is the name of the class containing those two lists):
>>
>>    Converter namingConverter = new NamedCollectionConverter(
>>      xstream.getMapper(), "title", String.class);
>>    xstream.registerLocalConverter(Foo.class, "movies", namingConverter);
>>    xstream.registerLocalConverter(Foo.class, "plays", namingConverter);
>>
>>
>> Cheers,
>> Jörg
>>
>>
>> ---------------------------------------------------------------------
>> 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
>
>
>


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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Re: Re: Re: Set both collection name and item name

Jörg Schaible-4
Hi Paul,

Paul B. Anderson wrote:

> I made the changes you suggested and everything is working as desired.
> Thank you again.
>
> I'm curious, in the annotation version, how can I know what parameters
> the annotation takes?  I see how the XStream facade works in the JAVADOCs.

Its explained in the Javadocs for @XStreamConverter (and in the thread here
on the mailing list last week about the CollectionConverter in combination
with this annotation).

In short: XStream uses a poor man's dependency injection (DI) when
instantiating a Converter by looking at its constructors. Apart from
internal objects (like the Mapper) you can define additional constants of
various types with the annotation. The DI algorithm starts matching the
constructor with the highest number of arguments with all available possible
arguments and uses it if it matches successful. It respects definition
sequences i.e. the first string constant defined in the annotation will be
passed as the first String argument of the constructor.

Cheers,
Jörg


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

    http://xircles.codehaus.org/manage_email