XStream: is it possible to re-set alias on the fly?

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

XStream: is it possible to re-set alias on the fly?

Alex Y. Matiash
Hello world!

I've tried to google as hard as I can, but I didn't find an answer for my problems.

1. The problem is that due to some reason our protocol has identical XML structure for all packets, so I can't create permanent aliases, neither using annotations nor by calling XStream.alias().
Also I have to mention that calling XStream.processAnnotations() before each packet parsing is not appropriate because it is (AFAIK) "heavy" operation.
I tried to call xStream.alias("A", ClassA.class) (ClassA, ClassB, ClassC, etc) on the fly before parsing each packet, but nothing (good) happened.
Then I've digged into the sources, and found that ClassAliasingMapper.addTypeAlias() only adds aliases, and in case of any duplicates (both name and class) there is no way to removed old alias.
Is it true, or I'm missing something? If it is true there is an another question - is it possible to do anything in such case (the same XML structure for all packets)?
In case if it is impossible now - I would be extremely grateful if somebody can add something like XStream.removeAlias();

2. I have very simple XML with structure: <D><I></I><D>, but doing XStream.alias("D", ClassD.class); XStream.alias("I', ClassI.class); results in an exception telling me that there is no class registered to save contents of tag "I".
Only XStream.alias("D", ClassD.class); XStream.aliasField("I', ClassD.class, "fieldI"); works as required. Why it is impossible to just set two aliases for two pairs tag-class?

Thanks to everybody in advance.

--
10:36  13.04.2015             Alex Y. Matiash   mailto:[hidden email]


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

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: XStream: is it possible to re-set alias on the fly?

Jörg Schaible-4
Hi Alex,

Alex Y. Matiash wrote:

> Hello world!
>
> I've tried to google as hard as I can, but I didn't find an answer for my
> problems.
>
> 1. The problem is that due to some reason our protocol has identical XML
> structure for all packets, so I can't create permanent aliases, neither
> using annotations nor by calling XStream.alias(). Also I have to mention
> that calling XStream.processAnnotations() before each packet parsing is
> not appropriate because it is (AFAIK) "heavy" operation. I tried to call
> xStream.alias("A", ClassA.class) (ClassA, ClassB, ClassC, etc) on the fly
> before parsing each packet, but nothing (good) happened. Then I've digged
> into the sources, and found that ClassAliasingMapper.addTypeAlias() only
> adds aliases, and in case of any duplicates (both name and class) there is
> no way to removed old alias. Is it true, or I'm missing something?

Yes, this is true.

> If it
> is true there is an another question - is it possible to do anything in
> such case (the same XML structure for all packets)? In case if it is
> impossible now - I would be extremely grateful if somebody can add
> something like XStream.removeAlias();

No. This is not possible. XStream is designed for concurrent
(un-)marshalling processes. Modifying the configuration on-the-fly will
cause severe and unpredictable side-effects. XStream is supposed to be
immutable after configuration.
>
> 2. I have very simple XML with structure: <D><I></I><D>, but doing
> XStream.alias("D", ClassD.class); XStream.alias("I', ClassI.class);
> results in an exception telling me that there is no class registered to
> save contents of tag "I".

Right.

> Only XStream.alias("D", ClassD.class);
> XStream.aliasField("I', ClassD.class, "fieldI"); works as required. Why it
> is impossible to just set two aliases for two pairs tag-class?

XStream is "Java to XML and back", i.e. your XML represents a complete
object graph. You have defined that your XML root is represented by a class:

class ClassD {
...
}

Since ClassD contains now fields, the inner tags reflect these names. It
cannot represent a type (it's already defined by the field definition). In
your case it is a field of ClassD named "I" or whatever name you have used
with defining a field alias.

It looks like you try to setup XStream to read an incoming XML. Actually
this is not recommended at all. Always go the other way round. Build your
Java object model and marshal it to XML. If the resulting XML matches the
required structure, you will also be able to read it. It is an error-prone
and annoying task to setup an XStream instance just by looking at the XML
and simply guessing what XStream might do with it while unmarshalling.

> Thanks to everybody in advance.

Cheers,
Jörg



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

    http://xircles.codehaus.org/manage_email