JAXB Overview Vladimir Gapeyev
XML data binding Java: XML: ObjectFactory f = <book> new ObjectFactory(); Book b = f.createBook(); b.setTitle(“Compilers”); List a = b.getAuthor(); a.add(“Aho”); a.add(“Sethi”); a.add(“Ullman”); b.setPages(796); <book> <title>Compilers</title> <author>Aho</author> <author>Sethi</author> <author>Ullman</author> <pages>796</pages> </book> 12/5/2018 JAXB Overview
Schema-based XML data binding compile Schema Classes instance of conforms unmarshal Objects Documents marshal 12/5/2018 JAXB Overview
JAXB: Java Architecture for XML Binding Specification: Subset of XML Schema Default bindings XML names Java names Schema atomic types predefined Java types Schema structured types Java interfaces javax.xml.bind – framework interfaces Language for binding customization Reference implementation: Schema-to-Java compliler Generates classes as well 12/5/2018 JAXB Overview
Mapping for atomic types xs:string java.lang.string xs:decimal java.math.BigDecimal xs:int int xs:QName javax.xml.namespace.QName xs:date java.util.Calendar Etc… 12/5/2018 JAXB Overview
Elements with simple content interface Zip extends javax.xml.bind.Element { int getValue(); void setValue(int); } <xs:element name=“zip” type=“xs:int” /> 12/5/2018 JAXB Overview
Elements with sequence and repetition <xs:element name=“book” > <xs:ComplexType> <xs:sequence> <xs:element name=“title” type=“xs:string”/> <xs:element name=“author” type=“xs:string” maxOccurs=“unbounded”/> <xs:element name=“pages” type=“xs:int”/> </xs:sequence> </xs:ComplexType> </xs:element> interface Book extends BookType, javax.xml.bind.Element {} interface BookType { String getTitile(); void setTitle(String x); List getAuthor(); int getPages(); void setPages(int x); } 12/5/2018 JAXB Overview
Elements with choice <xs:complexType name=“volume”> <xs:choice> <xs:element ref=“book”/> ref=“journal”/> </xs:choice> </xs:complexType> interface Volume { BookType getBook(); void setBook(BookType); JournalType getJournal(); void setJournal(JournalType); } 12/5/2018 JAXB Overview
Customization: isSet() method <xs:annotation><xs:appinfo> <jxb:globalBindings generateIsSetMethod=“true"/> </xs:appinfo></xs:annotation> <xs:complexType name=“volume”> <xs:choice> <xs:element ref=“book”/> ref=“journal”/> </xs:choice> </xs:complexType> interface Volume { BookType getBook(); void setBook(BookType); boolean isSetBook(); JournalType getJournal(); void setJournal(JournalType); boolean isSetJournal(); } 12/5/2018 JAXB Overview
No roundtripping interface T { type t { getA(); setA(); getB(); setB(); } type t { (element a, element b) | (element b, element a) } Type t members: <a/><b/> and <b/><a/> unmarshal; marshal =/= identity unmarshal; marshal; unmarshal = unmarshal 12/5/2018 JAXB Overview
“General content style” <xs:complexType name=“t”> <xs:choice maxOccurs=“unbounded”> <element ref=“book”/> <element ref=“journal”/> </xs:choice> </xs:complexType> interface T { List getBookOrJournal() } Plus a constraint: The only list members are Book objects Journal objects 12/5/2018 JAXB Overview
Model groups, by default <xs:group name="bookName"> <xs:sequence> <xs:element name="title" type="xs:string"/> <xs:element name="author" type="xs:string“/> </xs:sequence> </xs:group> <xs:complexType name="bookType"> <xs:group ref="bookName"/> <xs:element name="pages" type="xs:int"/> </xs:complexType> interface BookType { String getTitle(); void setTitle(String x); String getAuthor(); void setAuthor(String); int getPages(); void setPages(int x); } 12/5/2018 JAXB Overview
Model groups, customized <xs:group name="bookName"> <xs:sequence> <xs:element name="title" type="xs:string"/> <xs:element name="author" type="xs:string“/> </xs:sequence> </xs:group> <xs:complexType name="bookType"> <xs:group ref="bookName"/> <xs:element name="pages" type="xs:int"/> </xs:complexType> interface BookName { String getTitle(); void setTitle(String x); String getAuthor(); void setAuthor(String); } interface BookType { BookName getBookName(); void setBookName(BookName); int getPages(); void setPages(int x); 12/5/2018 JAXB Overview
Summary of observations Mapping often loses static typing information – need to employ dynamic constraints Complex content models with choice are most problematic Alternative mapping styles No good declarative specifications for the mappings 12/5/2018 JAXB Overview
Opportunities Formal specification of mapping styles Formulate desired mapping properties Verify that the mapping styles enjoy the properties A toolkit for generating mapping compilers (as opposed to a single compiler) 12/5/2018 JAXB Overview