users@jaxb.java.net

Re: pattern in a simple type

From: Ryan Shoemaker - JavaSoft East <Ryan.Shoemaker_at_Sun.COM>
Date: Wed, 18 Feb 2004 15:25:41 -0500

oro7d3_at_netscape.net wrote:
> Hi,
>
> I have a question in jaxb. I have this simple type attribute with a pattern value "0" or "1". I have another element which reference this attribute.
> JAXB creates a setMushUnderstand( boolean value); for me.
> But when it marshalls the object, it marshalls 'mustUnderstand="false"' instead of using '0' or '1'.
>
> Is there a way to configure JAXB to marshall either "0" for true, "1" for false?
>

Yes, you just need to specify your own print and parse methods.
Take a look at the attached sample for more detail.

Once you have this all wired up, you should be able to do some-
thing like:

        org.acme.foo.Foo f = (new org.acme.foo.ObjectFactory()).createFoo();
         f.setValue("blarg");
        f.setMyAttr(true);
         m.marshal(f, System.out);

and see output like this:

         <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
         <foo myAttr="1">blarg</foo>

Thanks,

--Ryan


<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema"
            xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
            jxb:version="1.0">

  <xsd:annotation>
    <xsd:appinfo>
      <jxb:schemaBindings>
        <jxb:package name="org.acme.foo"/>
      </jxb:schemaBindings>
    </xsd:appinfo>
  </xsd:annotation>

  <xsd:element name="foo">
    <xsd:complexType>
      <xsd:simpleContent>
        <xsd:extension base="xsd:string">
          <xsd:attribute ref="myAttr"/>
        </xsd:extension>
      </xsd:simpleContent>
    </xsd:complexType>
  </xsd:element>

  <xsd:attribute name="myAttr">
    <xsd:simpleType>
      <xsd:annotation>
        <xsd:appinfo>
          <jxb:javaType name="boolean"
                        parseMethod="org.acme.foo.Converter.parseBoolean"
                        printMethod="org.acme.foo.Converter.printBoolean" />
        </xsd:appinfo>
      </xsd:annotation>
      <xsd:restriction base="xsd:boolean">
        <xsd:pattern value="0|1"/>
      </xsd:restriction>
    </xsd:simpleType>
  </xsd:attribute>

</xsd:schema>


package org.acme.foo;

public class Converter {

    public static boolean parseBoolean( String lexicalBoolean ) {
        return "1".equals(lexicalBoolean) ? true : false;
    }

    public static String printBoolean( boolean booleanValue ) {
        return booleanValue ? "1" : "0";
   }

}



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