users@jaxb.java.net

Re: Validate Error

From: Yuemin Wang <ywang_at_todo1.com>
Date: Tue, 03 Dec 2002 12:00:58 -0500

Hi Ed,

Sorry, you are right, the jaxb-1.0-beta only supports XML Schema. My
environment was still point at jaxb-1.0-ea.

Because all our messeage schemas are DTDs and it is not going to be changed
in years, I have to use jaxb-1.0-ea.

Now we are talking about DTD and jaxb-1.0-ea.

I still believe the validation for XXXX? type of element have bug. My work
around is adding the "if (XXXX != null)" in the java code which contains '?'
type of element after I generate the code.

To reproduce the bug I did a test as following:
---------------------------------------------------------------
1. Simple DTD (test.dtd):
<!ELEMENT TEST (APPLE, ORANGE?)>
<!ELEMENT ORANGE (COLOR, WEIGHT)>
<!ELEMENT APPLE (#PCDATA)>
<!ELEMENT COLOR (#PCDATA)>
<!ELEMENT WEIGHT (#PCDATA)>

2. Binding-schema (test.js)
<xml-java-binding-schema version="1.0ea">
   <element name="TEST" type="class" root="true"/>
</xml-java-binding-schema>

3. Generate code:
TEST.java
ORANGE.java

4. A test programe(testapp.java):
import java.io.*;
import java.util.*;
import javax.xml.bind.*;
import javax.xml.marshal.*;

public class testapp {

    public static void main(String args[])
    {
        String xml="<TEST><APPLE>abc</APPLE></TEST>";

        System.out.println ("Xml String: " + xml);

        TEST ts = new TEST();

        ByteArrayInputStream bs = new ByteArrayInputStream(xml.getBytes());

        try {
            ts = ts.unmarshal((InputStream)bs);
            System.out.println("Ok for unmarshal");
        } catch(Exception e) {
            System.out.println("Fail in unmarshal:" + e.toString());
        }


        TEST tn = new TEST();
        tn.setAPPLE("12345");

        ByteArrayOutputStream out = new ByteArrayOutputStream();

        try {
            tn.validate();
            tn.marshal(out);
            System.out.println ("Ok after marshal");
        } catch(Exception e) {
            System.out.println ("Marshal error" + e.toString());
        }

        String xmlout = out.toString();
        System.out.println ("Xml Out String: " + xmlout);

    }

}

5. Compile and run the test:
Xml String: <TEST><APPLE>abc</APPLE></TEST>
Ok for unmarshal
Marshal errorjava.lang.NullPointerException
Xml Out String:

FAIL at the validation.

6. Modify the TEST.java:
...
    public void validate(Validator v)
        throws StructureValidationException
    {
        if ( _ORANGE != null )
            v.validate(_ORANGE);
    }
...

7. Re-compile and run the test again:
Xml String: <TEST><APPLE>abc</APPLE></TEST>
Ok for unmarshal
Ok for marshal
Xml Out String: <?xml version="1.0" encoding="UTF-8"?>

<TEST>
  <APPLE>12345</APPLE></TEST>

The way DTD expected!
------------------------------------------------------------------

I include all the files in attachment for you to try.


Thank you

Yuemin

-----Original Message-----
From: Discussion list for the Java Architecture for XML Binding
[mailto:JAXB-INTEREST_at_JAVA.SUN.COM]On Behalf Of Ed Mooney
Sent: Monday, December 02, 2002 4:03 PM
To: JAXB-INTEREST_at_JAVA.SUN.COM
Subject: Re: Validate Error


Hi Yuemin,

Have you ported your application to jaxb-1.0-beta? If so, I'm still not
understanding the problem you're having. So we have a common
understanding, I make these points:

- jaxb-1.0-beta supports XML Schema, not DTD.
- The validate method is a member of the Validator class, specified in
Section 3.4 of the specification[1].
- [2] describes the Validator API.
- [3] shows how to do on-demand validation.

Does any of this help?

Regards,
--
Ed Mooney         |Sun Microsystems, Inc.|Time flies like
Java Web Services |UBUR02-201            |an arrow, but
Ed.Mooney_at_Sun.COM |1 Network Drive       |fruit flies like
781-442-0459      |Burlington, MA  01803 |a banana. Groucho
[1] http://java.sun.com/xml/downloads/jaxb.html
[2] http://java.sun.com/xml/jaxb/api/javax/xml/bind/Validator.html
[3] http://java.sun.com/xml/jaxb/users-guide/jaxb-using.html#sampapp5
Yuemin Wang wrote:
> Hi Ed,
>
> You misunderstood my point, "return OK" in my mind is "no exception is
> throwing".
>
> You know all my marshall is not OK because of this validation.  All the
> field with "?", I have to generate an dummy field in order to pass
> validation, this against the DTD definition of an option field.  That is
why
> I think this is a bug.
>
> Thank you
>
> Yuemin
[ ... ]