Re: [Simple-support] ignored name attribute of Root annotation
Brought to you by:
niallg
|
From: Adam R. <ada...@gm...> - 2012-05-07 09:20:21
|
Hi Niall,
thanks for the quick answers. From what you tell me, it seems that turning
on the validation of the root element's name is quite complicated. I know
it's really not my place, but would you consider changes to make it
easier? I think the default way it works now (that the name of the root
element is ignored) is quite suprising and counter-intuitive, and I expect
that many users of your library will want to alter that behavior. It seems
that creating a mechanism to do it quickly and easily would be valuable.
The simplest way (from the user's point of view) to achieve that would be
to change the semantics of the name attribute of the @Root annotation when
it's used on the root class being deserialized (in other words, the one
whose Class object you pass as the first argument to read() - RootEl in my
examples). I think that would make it work in the most intuitive way.
However, it's just a suggestion - the decision is yours, of course :).
Take care,
Adam
On Sat, May 5, 2012 at 6:03 AM, Niall Gallagher
<gal...@ya...>wrote:
> You could do a number of things, for example you could extend the
> Persister and override the method taking the InputNode, here you could
> check the root node name. You could also use a Visitor, and check it there
> with the InputNode. Another way is a Strategy, but this is pretty much the
> same as a Visitor with a few more complications.
>
>
> --- On *Fri, 4/5/12, Adam Ruka <ada...@gm...>* wrote:
>
>
> From: Adam Ruka <ada...@gm...>
> Subject: Re: [Simple-support] ignored name attribute of Root annotation
> To: sim...@li...
> Received: Friday, 4 May, 2012, 1:18 PM
>
> Hi,
>
> that's reasonable, altough it doesn't solve my problem, which is: how to
> force the XML root element to have a specific name? In other words, what
> changes have to be made to the RootEl class to make that third test that I
> posted earlier fail?
>
>
> On Fri, May 4, 2012 at 4:15 PM, Niall Gallagher <gal...@ya...<http://mc/compose?to=...@ya...>
> > wrote:
>
> Hi,
>
> Yes this is true, this is an intentional feature. You need to be able to
> override the name of an entry within a list. This allows each element to
> override the name of the entry.
>
> Niall
>
> --- On *Fri, 4/5/12, Adam Ruka <ada...@gm...<http://mc/compose?to=...@gm...>
> >* wrote:
>
>
> From: Adam Ruka <ada...@gm...<http://mc/compose?to=...@gm...>
> >
> Subject: [Simple-support] ignored name attribute of Root annotation
> To: sim...@li...<http://mc/compose?to=...@li...>
> Received: Friday, 4 May, 2012, 1:26 AM
>
> Hello everybody,
>
> first of all, I want to say that Simple is the best XML library I've
> ever used. I absolutely love it. Having said that, I stumbled upon
> some weird behavior lately: the name attribute of the Root annotation
> seems to be ignored when deserializing XML to an object. Here is a
> small test case which illustrates the problem:
>
> import static org.junit.Assert.*;
>
> import org.junit.Test;
> import org.simpleframework.xml.Attribute;
> import org.simpleframework.xml.Root;
> import org.simpleframework.xml.Serializer;
> import org.simpleframework.xml.core.PersistenceException;
> import org.simpleframework.xml.core.Persister;
>
> public class BadRootNameTest {
> private static final Serializer persister = new Persister();
>
> @Root(name="root")
> public static class RootEl {
> @Attribute
> public String attr;
> }
>
> @Test
> public void testGoodRoot() throws Exception {
> // passes OK
> RootEl rootEl = persister.read(RootEl.class, "<root
> attr=\"xxx\"/>");
> assertNotNull(rootEl);
> assertEquals("xxx", rootEl.attr);
> }
>
> @Test(expected=PersistenceException.class)
> public void testBadAttr() throws Exception {
> // throws an exception, as is expected
> persister.read(RootEl.class, "<root xxx=\"yyyy\"/>");
> }
>
> @Test
> public void testBadRootName() throws Exception {
> // does NOT throw an exception!
> RootEl rootEl = persister.read(RootEl.class,
> "<xxxxxxxxxx attr=\"yyy\"/>");
> assertNotNull(rootEl);
> assertEquals("yyy", rootEl.attr);
> }
> }
>
> I believe the same thing happens with the ElementList annotation when
> inline is set to false. Is this a bug, or do you need to do something
> special for the name attribute to take effect? Thanks in advance for
> any help.
>
> Best wishes,
>
> Adam Ruka
>
>
> ------------------------------------------------------------------------------
> Live Security Virtual Conference
> Exclusive live event will cover all the ways today's security and
> threat landscape has changed and how IT managers can respond. Discussions
> will include endpoint security, mobile security and the latest in malware
> threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
> _______________________________________________
> Simple-support mailing list
> Sim...@li...<http://mc/compose?to=...@li...>
> https://lists.sourceforge.net/lists/listinfo/simple-support
>
>
>
> -----Inline Attachment Follows-----
>
>
>
> ------------------------------------------------------------------------------
> Live Security Virtual Conference
> Exclusive live event will cover all the ways today's security and
> threat landscape has changed and how IT managers can respond. Discussions
> will include endpoint security, mobile security and the latest in malware
> threats. http://www.accelacomm.com/jaw/sfrnl04242012/114/50122263/
>
> -----Inline Attachment Follows-----
>
>
> _______________________________________________
> Simple-support mailing list
> Sim...@li...<http://mc/compose?to=...@li...>
> https://lists.sourceforge.net/lists/listinfo/simple-support
>
>
|