users@jaxb.java.net

RE: RE: RE: Polymorphism problems

From: Andrew Ferguson <Andrew.Ferguson_at_arm.com>
Date: Tue, 29 Jun 2004 14:36:23 +0100

hi Matt,

 sorry if I have missed something but I think JAXB does behave how you
want?

> Why can't the XML marshalling work in the same way?

I'm not sure what you mean here - if you marshall a list of objects then
you will get xml output corresponding to the bindings of those objects?
that's what my original code was suggesting is the correct behaviour

> The bottom line is that inheritance, to me, means that there is an
"is-a" relationship between classes and subclasses. Since Employee *is
> a* Person, then it should be perfectly acceptable to use an Employee
instance in every case where a Person instance is allowed. If the
> classes generated by xjc do not comply to this basic principle, then I
will assume, lacking an explanation, that they unsuitable for my
> purposes.

I was playing with exactly this yesterday - and it does work. The
restricted types implement the interface generated for the base type.

also I'm a little confused by the schema snippet you posted - the XML
Primer says you must duplicate the content from the base in the derived
definition (unlike how java inheritance is declared) - which from my
reading would mean that Employee would need to declare both "name" and
"title". although this is all new to me (but I would like to
understand..)

thanks,
Andrew

-----Original Message-----
From: Matt Munz [mailto:mmunz_at_apelon.com]
Sent: 29 June 2004 14:28
To: users_at_jaxb.dev.java.net
Subject: RE: RE: Polymorphism problems

Andrew,

> saying that, am not sure how you could achieve what you are trying?

  I think your analogy is weak. Consider the following method, using
the classes you posited.

void printNames(List people)
{
  while(people.hasNext())
  {
    System.out.println(((Person) people.next()).name);
  }
}

  Whether the input is a list of Person, Employee, or a mix of these,
the method will work properly. This is a perfectly legitimate use of
polymorphism. Why can't the XML marshalling work in the same way?

> Are you hoping for the object tree you construct not to be validated?

  No. I can't see the helpfulness of using a schema, much less JAXB, in
this case.

  My limited experience of Castor tells me that that framework does
something similar to what I want, but not exactly. The entire XML of
employee is written in the slot for person, with an added attribute that
somehow tells the parser how to handle it (as a sub-type instance)...

  The bottom line is that inheritance, to me, means that there is an
"is-a" relationship between classes and subclasses. Since Employee *is
a* Person, then it should be perfectly acceptable to use an Employee
instance in every case where a Person instance is allowed. If the
classes generated by xjc do not comply to this basic principle, then I
will assume, lacking an explanation, that they unsuitable for my
purposes.

  Does anyone on this list have any experiences with frameworks that
generate classes that adhere to this principle? Is there any document
that outlines the general principles of OO design to which the JAXB RI
compiler adheres?

  - Matt

-----Original Message-----
From: Andrew Ferguson [mailto:Andrew.Ferguson_at_arm.com]
Sent: Tuesday, June 29, 2004 5:52 AM
To: users_at_jaxb.dev.java.net
Subject: RE: Polymorphism problems

hi Matt,

 I think your expected output may be wrong, and the actual output be
correct eg consider this code:

import java.util.*;

class Person {
        String name;
        public String toString() { return "Person("+name+")"; } }

class Employee extends Person {
        String title;
        public String toString() { return "Employee("+name+",
"+title+")"; } }

public class JBTest {
        public static void main(String[]arg) {
                List l = new ArrayList();
                
                Person p = new Person();
                p.name="Joe Sample";
                
                Employee q = new Employee();
                q.name="Jane Sample Employee";
                q.title="Engineer";
                
                l.add(p);
                l.add(q);
        
                System.out.println(l);
        }
}

which outputs

        [Person(Joe Sample), Employee(Jane Sample Employee, Engineer)]

So although the List is untyped (am not keeping up with generics..
sorry!) in the above code, even if it were a Person list it would still
be legitimate to add an Employee to the list (since an Employee is-a
Person)

saying that, am not sure how you could achieve what you are trying? are
you hoping for the object tree you construct not to be validated?

thanks,
Andrew

-----Original Message-----
From: Matt Munz [mailto:mmunz_at_apelon.com]
Sent: 27 June 2004 19:39
To: users_at_jaxb.dev.java.net
Subject: Polymorphism problems

Hi all,

  I'm new to this list and JAXB, so please bear with me. I searched the
archives, but didn't see a clear answer to this question. The following
was also posted to the Java Technology & XML web forum.

 I am finding little use in JAXB's polymorphism. As such, I think I'm
going to need to switch to another framework that has a more useful
polymorphism feature (or else none at all).

As indicated below, I have a schema that includes a "Person" complex
type, and an "Employee" complex type that extends it. There is also an
element "people" that is a sequence of "Person" elements. I have used
JAXB-generated classes to add instances of Person and Employee
(generated classes) to an instance of the People class. The marshaller
then erroneously outputs the extensions to Person found in Employee (see
below).

Having seen this, I wonder what use polymorphism (in the classes
generated by JAXB) has, if it is not acceptable to use a subclass
wherever its superclass is allowed. Perhaps there is some other way to
do polymorphism that I'm missing?

I really want to have a functionality such as the code example below
implies. Does JAXB have the feature I'm looking for, or do I need to
switch to another framework? If I need to switch, which one should I
use?

Thanks for any help that can be offered.

schema:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
elementFormDefault="qualified" attributeFormDefault="unqualified">
<xs:element name="people">
<xs:annotation>
<xs:documentation>Comment describing your root
element</xs:documentation> </xs:annotation> <xs:complexType>
<xs:sequence> <xs:element name="person" type="Person"
maxOccurs="unbounded"/> </xs:sequence> </xs:complexType> </xs:element>
<xs:complexType name="Person"> <xs:sequence> <xs:element name="name"
type="xs:string"/> </xs:sequence> </xs:complexType> <xs:complexType
name="Employee"> <xs:complexContent> <xs:extension base="Person">
<xs:sequence> <xs:element name="title" type="xs:string"/> </xs:sequence>
</xs:extension> </xs:complexContent> </xs:complexType> </xs:schema>

expected JAXB output:

<people>
<person>
<name>Joe Sample</name>
</person>
<person>
<name>Jane Sample Employee</name>
</person>
</people>

actual JAXB output (invalid):

<people>
<person>
<name>Joe Sample</name>
</person>
<person>
<name>Jane Sample Employee</name>
<title>Engineer</title>
</person>
</people>

Java code:

Person person = factory.createPerson();
person.setName("Joe Sample");
Employee employee = factory.createEmployee(); employee.setName("Jane
Sample Employee"); employee.setTitle("Engineer"); People people =
factory.createPeople(); people.getPerson().add(person);
people.getPerson().add(employee);

- Matt Munz



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
For additional commands, e-mail: users-help_at_jaxb.dev.java.net




---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
For additional commands, e-mail: users-help_at_jaxb.dev.java.net



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
For additional commands, e-mail: users-help_at_jaxb.dev.java.net




---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
For additional commands, e-mail: users-help_at_jaxb.dev.java.net