The Source for Java Technology Collaboration
Webmaster Alert: Posting to Jive Forums is currently not working. Estimated time for fix is unknown.

Home » java.net Forums » GlassFish » Metro and JAXB

Thread: @XmlAttribute/@XmlValue need to reference a Java type that maps to text in

Welcome, Guest Help
Login Login
Guest Settings Guest Settings
This question is answered.

Reply to this Thread Reply to this Thread Search Forum Search Forum Back to Thread List Back to Thread List

Permlink Replies: 9 - Last Post: Jul 9, 2007 4:38 PM by: kohsuke
thierry_giguere

Posts: 6
@XmlAttribute/@XmlValue need to reference a Java type that maps to text in
Posted: May 30, 2007 1:47 PM
 
  Click to reply to this thread Reply

Hello,

I have generated my Java classes that match the schema. Next step I try to marshal a basic sample. I caught the following exception right on the first line :

JAXBContext jc = JAXBContext.newInstance("myschema");

Exception in thread "main" junit.framework.AssertionFailedError: com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExcep
tions
@XmlAttribute/@XmlValue need to reference a Java type that maps to text in XML.
this problem is related to the following location:

at protected java.lang.Object myschema.AttrOAnySimple.value

So what does it really mean and what step should I take to solve this issue. Is there any tutorial I should refer to? Why it's not working right out of the box?

Thanks!

------------

So far the only workaround I have found is customization. I have added the following schema and now it works. I got a mapping to String and an Adapter that convert String to/from String. Is this the best way to solve this issue?

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema elementFormDefault="qualified" version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:jaxb="http://java.sun.com/xml/ns/jaxb" jaxb:version="2.0">
<xs:annotation>
<xs:appinfo>
<jaxb:globalBindings>
<jaxb:javaType name="java.lang.String"
xmlType="xs:anySimpleType"
parseMethod="javax.xml.bind.DatatypeConverter.parseAnySimpleType" printMethod="javax.xml.bind.DatatypeConverter.printAnySimpleType"
/>
</jaxb:globalBindings>
</xs:appinfo>
</xs:annotation>
</xs:schema>

Message was edited by: thierry_giguere

mshaffer55

Posts: 396
Re: @XmlAttribute/@XmlValue need to reference a Java type that maps to text
Posted: Jun 1, 2007 6:38 AM   in response to: thierry_giguere
 
  Click to reply to this thread Reply

Do you have control over the schema? Do you really want to allow the attribute in question to be any simple type, or is it really always a string?

kohsuke

Posts: 3,963
Re: @XmlAttribute/@XmlValue need to reference a Java type that maps to text
Posted: Jun 1, 2007 12:45 PM   in response to: thierry_giguere
Correct
  Click to reply to this thread Reply

I guess this could be said as a "bug" in the spec. We should have bound that to String, not Object.

An issue in http://jaxb.dev.java.net/issues/ would be appreciated.

mshaffer55

Posts: 396
Re: @XmlAttribute/@XmlValue need to reference a Java type that maps to text
Posted: Jun 4, 2007 4:07 AM   in response to: kohsuke
 
  Click to reply to this thread Reply

Why do you say that? The original post does not provide the schema, but it sounds as though the attribute in question is untyped. An untyped attribute is by default mapped to xs:anySimpleType. I'm not sure it's reasonable to automatically map such an attribute to String. If it really is a String then it should be defined as type="xs:string" in the schema.

mshaffer55

Posts: 396
Re: @XmlAttribute/@XmlValue need to reference a Java type that maps to text
Posted: Jun 7, 2007 8:38 AM   in response to: mshaffer55
 
  Click to reply to this thread Reply

My mistake. The JAXB spec says that, in the case of attributes, xs:anySimpleType should be mapped to java.lang.String, vs. to java.lang.Object for elements of xs:anySimpleType.

kohsuke

Posts: 3,963
Re: @XmlAttribute/@XmlValue need to reference a Java type that maps to text
Posted: Jun 7, 2007 11:36 AM   in response to: mshaffer55
 
  Click to reply to this thread Reply

Or really? In that case this is a bug in the RI.

mshaffer55

Posts: 396
Re: @XmlAttribute/@XmlValue need to reference a Java type that maps to text
Posted: Jun 7, 2007 11:41 AM   in response to: kohsuke
 
  Click to reply to this thread Reply

Well that's the way I am interpreting it. See Table 6-1 in section 6.2.2 (JAXB 2.1).

kohsuke

Posts: 3,963
Re: @XmlAttribute/@XmlValue need to reference a Java type that maps to text
Posted: Jun 8, 2007 10:15 AM   in response to: mshaffer55
 
  Click to reply to this thread Reply

... and when I tried the following schema:
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
  xmlns:jaxb="http://java.sun.com/xml/ns/jaxb"
  jaxb:version="2.0">
  
  <xs:element name="root">
    <xs:complexType>
      <xs:attribute name="foo" type="xs:anySimpleType" />
    </xs:complexType>
  </xs:element>
</xs:schema>

I get the following output:
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "")
@XmlRootElement(name = "root")
public class Root {
 
    @XmlAttribute
    @XmlSchemaType(name = "anySimpleType")
    protected String foo;
 
   ...
}

So it looks like the RI is working as expected, at least as of 2.1.3.

So my question to the original poster is, which version of the JAXB RI are you using? Can you try the latest?

thierry_giguere

Posts: 6
Re: @XmlAttribute/@XmlValue need to reference a Java type that maps to text
Posted: Jul 6, 2007 6:38 AM   in response to: kohsuke
 
  Click to reply to this thread Reply

Release number :
ant -verbose jaxb

output the following at standard output stream :
[xjc] build id of XJC is 2.1.3-b01-fcs

The JDK :
Detected Java version: 1.5 in: c:\Program Files\Java\jdk1.5.0_11\jre

So I have try your basic schema with this setup and I get exactly the same result as you : a mapping to String.

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "")
@XmlRootElement(name = "root")
public class Root {

@XmlAttribute
@XmlSchemaType(name = "anySimpleType")
protected String foo;

Now using the same shell session I try again my schema but run into issue http://forums.java.net/jive/thread.jspa?threadID=26841&tstart=15. So I can't test using the same setup and I need to use the old jaxb from jwsdp2.0

[xjc] build id of XJC is 2.0-b26-ea3

But just to compare I use the jwsdp2.0 jaxb release with your basic schema and got a mapping to String also. So it seems linked to schema more than jaxb release.

@XmlAccessorType(AccessType.FIELD)
@XmlType(name = "")
@XmlRootElement(name = "root")
public class Root {

@XmlAttribute
protected String foo;

We notice some change in annotation so the setup for test seems to be ok.

kohsuke

Posts: 3,963
Re: @XmlAttribute/@XmlValue need to reference a Java type that maps to text
Posted: Jul 9, 2007 4:38 PM   in response to: thierry_giguere
 
  Click to reply to this thread Reply

Please don't use JAXB in JWSDP 2.0. It's way too old, and it's "early access" release.




 XML java.net RSS