users@jaxb.java.net

Re: Need a schema representation of a multivalue attribute for JAXB

From: Kohsuke Kawaguchi <Kohsuke.Kawaguchi_at_Sun.COM>
Date: Mon, 23 Jun 2003 13:39:15 -0700

> I am writing a program that uses an XML file in conjunction with JAXB to
> generically access a database. This is my first foray into Schema, and
> it has been fun, but I'm having trouble with one small part.

Wow, so you actually enjoy working with XML Schema?
I don't believe it :-) Try RELAX NG...


Anyway, here is what I would do:

1. define three constants by yourself:

    public class EditLevel {
        public static final int ADD = 1;
        public static final int CHANGE = 2;
        public static final int DELETE = 4;
    }

2. define a method that takes a String and returns the combination of
the above constants. This will be used to parse the attribute value.

    public class EditLevel {
        ...
        public static int parse( String value ) {
            int r=0;
            if( value.indexOf("ADD")!=-1 ) r |= ADD;
            if( value.indexOf("CHANGE")!=-1 ) r |= CHANGE;
            if( value.indexOf("DELETE")!=-1 ) r |= DELETE;
            return r;
        }
    }

3. define another method that does the opposite. This will be used to
print out the combination of constants into a string.

    public class EditLevel {
        ...
        public static String print( int value ) {
            ...
        }
    }

4. use a <javaType> customization.

    <xs:attribute name="editLevel" use="required">
     <xs:simpleType>
      <xs:annotation>
       <xs:appinfo>
        <jaxb:javaType name="int"
            parseMethod="your.package.EditLevel.parse"
            printMethod="your.package.EditLevel.print"
       </xs:appinfo>
      </xs:annotation>
      <xs:restriction base="xs:NCName">
        <xs:enumeration value="ADD" />
        <xs:enumeration value="ADD_DELETE" />
        ...
      </xs:restriction>
     </xs:simpleType>
    </xs:attribute>

4. compile this class before XJC invocation.

5. compile the schema with XJC. Be warned that you need to have the
   EditLevel class available when compiling the schema. Use -classpath
   option of XJC or <classpath> nested element if you are using Ant.



--
Kohsuke Kawaguchi <kohsuke.kawaguchi_at_sun.com>