Accessing field names in custom converters

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

Accessing field names in custom converters

Ivan Brusic
I inherited a project that was built using XStream 1.2.2. It will be possible upgrade to 1.4.4, but there is no testing around XStream, so I would need to do a lot of manual testing.

Trying to come up with a more automatic way of creating output. The XML is normalized in standard XML fields. For example:
String foo = "bar";

results in

<field name="foo">bar</field>

Although I would like an automatic way to have this format for all attributes way, I was not able to find an easy solution. I then looked at annotating classes in order to use a custom converter:

@XStreamConverter(MyFieldConverter.class)
String foo;

However, it seems I cannot access the variable name "foo". I can see the value in the writer's element stack, but the stack is not publicly accessible. Is there a way to retrieve the field name?

Another option I can see is to post-process the XML using XSLT. Is it possible to associate an XSLT with a class? 

If my code has:
xstream.marshal(obj, new CompactWriter(out));
can I associate an XSLT with obj's class? If not, do I setup the traxsource/transformer as shown in the example before my marshal call?

Cheers,

Ivan



Reply | Threaded
Open this post in threaded view
|

Re: Accessing field names in custom converters

Jörg Schaible-2
Hi Ivan,

Ivan Brusic wrote:

> I inherited a project that was built using XStream 1.2.2. It will be
> possible upgrade to 1.4.4, but there is no testing around XStream, so I
> would need to do a lot of manual testing.
>
> Trying to come up with a more automatic way of creating output. The XML is
> normalized in standard XML fields. For example:
> String foo = "bar";
>
> results in
>
> <field name="foo">bar</field>
>
> Although I would like an automatic way to have this format for all
> attributes way, I was not able to find an easy solution. I then looked at
> annotating classes in order to use a custom converter:
>
> @XStreamConverter(MyFieldConverter.class)
> String foo;
>
> However, it seems I cannot access the variable name "foo".

You have registered a local converter for a String. Class java.lang.String
has no member "foo". Definitely.

> I can see the
> value in the writer's element stack, but the stack is not publicly
> accessible. Is there a way to retrieve the field name?

If you declare the converter for the proper type ... ;-)

> Another option I can see is to post-process the XML using XSLT. Is it
> possible to associate an XSLT with a class?
>
> If my code has:
> xstream.marshal(obj, new CompactWriter(out));
> can I associate an XSLT with obj's class? If not, do I setup the
> traxsource/transformer as shown in the example before my marshal call?

I'd go with a converter ...

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: Accessing field names in custom converters

Ivan Brusic
I don't want to apply a converter to the top-level class. That would require too much work whenever a new field is added. Adding a simple annotation to a single variable is already a compromise. I am trying to clean up the current logic which is not maintainable.

So basically Converters do not have access to the field name of the variable they are annotating? I will only be annotating Java primitives (although I can switch to the class equivalents) and Strings.

Back to the XSL route. I already created an XSLT that output the format that I require, but according the the example, it seems like the transformation occurs parallel to the marshal call. I need the transformation to occur after marshaling has occurred. I have not actually run the code, but that is my take on the code.

Cheers,

Ivan




On Fri, Jul 19, 2013 at 11:11 AM, Jörg Schaible <[hidden email]> wrote:
Hi Ivan,

Ivan Brusic wrote:

> I inherited a project that was built using XStream 1.2.2. It will be
> possible upgrade to 1.4.4, but there is no testing around XStream, so I
> would need to do a lot of manual testing.
>
> Trying to come up with a more automatic way of creating output. The XML is
> normalized in standard XML fields. For example:
> String foo = "bar";
>
> results in
>
> <field name="foo">bar</field>
>
> Although I would like an automatic way to have this format for all
> attributes way, I was not able to find an easy solution. I then looked at
> annotating classes in order to use a custom converter:
>
> @XStreamConverter(MyFieldConverter.class)
> String foo;
>
> However, it seems I cannot access the variable name "foo".

You have registered a local converter for a String. Class java.lang.String
has no member "foo". Definitely.

> I can see the
> value in the writer's element stack, but the stack is not publicly
> accessible. Is there a way to retrieve the field name?

If you declare the converter for the proper type ... ;-)

> Another option I can see is to post-process the XML using XSLT. Is it
> possible to associate an XSLT with a class?
>
> If my code has:
> xstream.marshal(obj, new CompactWriter(out));
> can I associate an XSLT with obj's class? If not, do I setup the
> traxsource/transformer as shown in the example before my marshal call?

I'd go with a converter ...

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: Accessing field names in custom converters

Jörg Schaible-2
Hi Ivan,

Ivan Brusic wrote:

> I don't want to apply a converter to the top-level class. That would
> require too much work whenever a new field is added.

That's why there are reflection-based converters.

> Adding a simple
> annotation to a single variable is already a compromise. I am trying to
> clean up the current logic which is not maintainable.
>
> So basically Converters do not have access to the field name of the
> variable they are annotating?

No. This is by design.

> I will only be annotating Java primitives
> (although I can switch to the class equivalents) and Strings.

Look at the code of the ToAttributedValueConverter. That one does something
similar, you should be able to adapt the code to an own converter that does
what you need.

> Back to the XSL route. I already created an XSLT that output the format
> that I require, but according the the example, it seems like the
> transformation occurs parallel to the marshal call. I need the
> transformation to occur after marshaling has occurred. I have not actually
> run the code, but that is my take on the code.

XStream has a streaming model. If you want to post-process the resulting
XML, simply act when XStream is finished.

Cheers,
Jörg


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

    http://xircles.codehaus.org/manage_email