Polymorphism

View: New views
2 Messages — Rating Filter:   Alert me  

Polymorphism

by James Mantyka :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

There was a message from Quirino Zagarese in the mail list ( 2009-03-28
10:36 ).  Any I'm quite interested in the response from it.  Right now
we are converting some jaxb code into simple and one of the artifacts
that remains is the "choice", we think the proper solution would be to
use polymorphism to achieve ambiguity between the derived classes.  I
used the following test code:

public class TestPoly
{
    public static void main(String[] args)
    {
        Serializer serializer = new Persister();
        BaseClass num1 = new BaseClass("Base");
        BaseClass num2 = new Deriv1("Deriv","1");
        List<BaseClass> list = new ArrayList<BaseClass>();
        list.add(num1);
        list.add(num2);
       
        Holder myHolder = new Holder(list);
       
        File result = new File("./testPoly.txt");
       
        try {
            serializer.write(myHolder, result);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       
    }
   

    @Root(name="holder")
    public static class Holder
    {
        /**
         * @param curr
         */
        public Holder(List<BaseClass> curr) {
            super();
            this.curr = curr;
        }

        @ElementList(inline=true)
        public List<BaseClass> curr;
       
    }
   
    @Root(name="base")
    public static class BaseClass
    {
        /**
         * @param id
         */
        public BaseClass(String id) {
            super();
            Id = id;
        }

        @Element(name="Id")
        public String Id;
    }
   
    @Root(name="deriv1")
    public static class Deriv1 extends BaseClass
    {
        /**
         * @param id
         * @param name
         */
        public Deriv1(String id, String name) {
            super(id);
            Name = name;
        }

        @Element(name="Name")
        public String Name;
    }
}

What I saw coming out was:
<holder>
   <base>
      <Id>Base</Id>
   </base>
   <base class="TestPoly$Deriv1">
      <Id>Deriv</Id>
      <Name>1</Name>
   </base>
</holder>

I think it would be much more intuitive to see:

<holder>
   <base>
      <Id>Base</Id>
   </base>
   <Deriv1>
      <Id>Deriv</Id>
      <Name>1</Name>
   </Deriv1>
</holder>


Is this a possibility?

Thanks,
James Mantyka

------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
Simple-support mailing list
Simple-support@...
https://lists.sourceforge.net/lists/listinfo/simple-support

Re: Polymorphism

by niall.gallagher :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi,

To do this I need to add a setName method to the org.simpleframework.xml.stream.Node interface. This would allow you to change the node name using the class name. For now you can only change the class= attribute to be something generic like.

<base type='SomeMappingNameToTheClass'>

And have some sort of resolution to the class in an org.simpleframework.xml.strategy.Strategy implementation such as.

Class type = map.get(typeMapping);

This would work.

Niall


Niall Gallagher
RBS Global Banking & Markets
Office: +44 7879498724  

-----Original Message-----
From: James Mantyka [mailto:jam233@...]
Sent: 25 September 2009 22:31
To: simple-support@...
Subject: [Simple-support] Polymorphism

Hi,

There was a message from Quirino Zagarese in the mail list ( 2009-03-28
10:36 ).  Any I'm quite interested in the response from it.  Right now we are converting some jaxb code into simple and one of the artifacts that remains is the "choice", we think the proper solution would be to use polymorphism to achieve ambiguity between the derived classes.  I used the following test code:

public class TestPoly
{
    public static void main(String[] args)
    {
        Serializer serializer = new Persister();
        BaseClass num1 = new BaseClass("Base");
        BaseClass num2 = new Deriv1("Deriv","1");
        List<BaseClass> list = new ArrayList<BaseClass>();
        list.add(num1);
        list.add(num2);
       
        Holder myHolder = new Holder(list);
       
        File result = new File("./testPoly.txt");
       
        try {
            serializer.write(myHolder, result);
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
       
    }
   

    @Root(name="holder")
    public static class Holder
    {
        /**
         * @param curr
         */
        public Holder(List<BaseClass> curr) {
            super();
            this.curr = curr;
        }

        @ElementList(inline=true)
        public List<BaseClass> curr;
       
    }
   
    @Root(name="base")
    public static class BaseClass
    {
        /**
         * @param id
         */
        public BaseClass(String id) {
            super();
            Id = id;
        }

        @Element(name="Id")
        public String Id;
    }
   
    @Root(name="deriv1")
    public static class Deriv1 extends BaseClass
    {
        /**
         * @param id
         * @param name
         */
        public Deriv1(String id, String name) {
            super(id);
            Name = name;
        }

        @Element(name="Name")
        public String Name;
    }
}

What I saw coming out was:
<holder>
   <base>
      <Id>Base</Id>
   </base>
   <base class="TestPoly$Deriv1">
      <Id>Deriv</Id>
      <Name>1</Name>
   </base>
</holder>

I think it would be much more intuitive to see:

<holder>
   <base>
      <Id>Base</Id>
   </base>
   <Deriv1>
      <Id>Deriv</Id>
      <Name>1</Name>
   </Deriv1>
</holder>


Is this a possibility?

Thanks,
James Mantyka

------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA is the only developer event you need to attend this year. Jumpstart your developing skills, take BlackBerry mobile applications to market and stay ahead of the curve. Join us from November 9-12, 2009. Register now! http://p.sf.net/sfu/devconf _______________________________________________
Simple-support mailing list
Simple-support@...
https://lists.sourceforge.net/lists/listinfo/simple-support

***********************************************************************************
The Royal Bank of Scotland plc. Registered in Scotland No 90312. Registered Office: 36 St Andrew Square, Edinburgh EH2 2YB.
Authorised and regulated by the Financial Services Authority.
 
This e-mail message is confidential and for use by the
addressee only. If the message is received by anyone other
than the addressee, please return the message to the sender
by replying to it and then delete the message from your
computer. Internet e-mails are not necessarily secure. The
Royal Bank of Scotland plc does not accept responsibility for
changes made to this message after it was sent.

Whilst all reasonable care has been taken to avoid the
transmission of viruses, it is the responsibility of the recipient to
ensure that the onward transmission, opening or use of this
message and any attachments will not adversely affect its
systems or data. No responsibility is accepted by The
Royal Bank of Scotland plc in this regard and the recipient should carry
out such virus and other checks as it considers appropriate.

Visit our website at www.rbs.com

***********************************************************************************


------------------------------------------------------------------------------
Come build with us! The BlackBerry® Developer Conference in SF, CA
is the only developer event you need to attend this year. Jumpstart your
developing skills, take BlackBerry mobile applications to market and stay
ahead of the curve. Join us from November 9-12, 2009. Register now!
http://p.sf.net/sfu/devconf
_______________________________________________
Simple-support mailing list
Simple-support@...
https://lists.sourceforge.net/lists/listinfo/simple-support