users@jaxb.java.net

Re: How to force JAXBElement<?> as data type

From: Claus Nagel <claus.nagel_at_gmail.com>
Date: Wed, 22 Oct 2008 14:25:39 -0700 (PDT)

Perhaps I should add why the data type "Object" is not really helpful. In
order to inject a property into A:Person, I wrote the following Java class
which basically represents the schema B from my previous post:

[Java XML registry]
@XmlRegistry
public class PersonName {

  @XmlElementDecl(namespace="B", name="personName",
substitutionHeadNamespace="A", substitutionHeadName="_PersonHook")
  public JAXBElement<String> createPersonName(String value) {
    return new JAXBElement<String>(
      new QName("B", "personName"),
      ((Class) String.class),
      null,
      value
    );
  }
}
[/Java XML registry]

If the personHook in class Person is of data type List<JAXBElement<?>>, I
can add a personHook via the createPersonName method. Both marshalling and
unmarshalling is possible and works as expected.

However, if the personHook in class Person is a List of Objects and I pass a
personName via the createPersonName method, the following error is yielded:

"Instance of "javax.xml.bind.JAXBElement" is substituting
"java.lang.Object", but "javax.xml.bind.JAXBElement" is bound to an
anonymous type"

Regards,
Claus





Claus Nagel wrote:
>
> Hi all -
>
> I have a schema A like follows:
>
> [schema A, namespace="a"]
> <xs:schema xmlns="A">
> ...
> <xs:complexType name="Person">
> <xs:sequence>
> ...
> <xs:element ref="_PersonHook" minOccurs="0" maxOccurs="unbounded"/>
> </xs:sequence>
> </xs:complexType>
> <xs:element name="_PersonHook" type="xs:anyType" abstract="true"/>
> ...
> </xs:schema>
> [/schema A]
>
> The purpose of the abstract element "A:_PersonHook" is to augment the
> type "A:Person" with additional property elements without the need for
> subclassing. For example, there could be another schema B as follows:
>
> [schema B, namespace "B"]
> <xs:schema xmlns="B">
> <xs:import "schemaA"/>
> ...
> <xs:element name="personName" type="xs:string"
> substitutionGroup="A:_PersonHook"/>
> ...
> </xs:schema>
> [/schema B]
>
> A corresponding instance documents could look like this:
>
> [XML instance]
> ...
> <A:Person>
> <B:personName>name</B:personName>
> </A:Person>
> ...
> [/XML instance]
>
> By this means, A:Person can be augmented by arbitrary elements from
> different namespaces.
>
> And now to JAXB:
> In my project, I am just working with schema A. If I generate Java
> classes from A using xjc (2.1.8), this results in the following class
> for Person:
>
> [Java class Person]
> public class Person {
> protected List personHook;
>
> public List get_PersonHook() { ... }
> }
> [/Java class]
>
> The data type "Object" is not really helpful. However, if I feed xjc
> both with schemas A and B, I get the following code for Person:
>
> [Java class Person]
> public class Person {
> protected List<JAXBElement<?>> personHook;
>
> public List<JAXBElement<?>> get_PersonHook() { ... }
> }
> [/Java class]
>
> That's exactly what I want to have! By this means I can declare my
> "hooks" using @XmlElementDecl to inject elements from different
> namespaces into A:Person.
> So it seems that xjc needs the information that "A:_PersonHook" is the
> head of a substitutionGroup in order to correctly map it to
> JAXBElement<?>. However, I just have schema A in my project.
>
> So here comes my question: How can I force xjc to map A:_PersonHook to
> a JAXBElement<?> and not just to Object if I have just schema A at
> hand? The problem is, that schema A is lacking the information that
> A._PersonHook is the head of a substitutionGroup... Well, at least
> this is my guess...
>
> At the moment I am doing a hack: I create an artifical schema B for
> all my "hooks" in schema A (there are quite a lot of them!). By this
> means, xjc generates JAXBElement<?>s. The generated classes for B are
> deleted right after xjc has finished.
>
> I am sure there is a smarter way to handle this!
> Thanks for suggestions and advice,
> Claus
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>
>
>

-- 
View this message in context: http://www.nabble.com/How-to-force-JAXBElement%3C-%3E-as-data-type-tp20116243p20120084.html
Sent from the java.net - jaxb users mailing list archive at Nabble.com.