Documente Academic
Documente Profesional
Documente Cultură
Architecture For
)XML Binding )JAXB
:By
Yoav Zibin
Sharon Krisher
Motivation for JAXB
The main purpose of XML Schema is:
Validation of XML documents
Any other purposes?
Hint 1: determinism requirement
Hint 2: the “default” attribute has nothing to do
with validation
<xs:attribute name="country" use=“optional”
default="Israel“ />
Answer: Given a valid XML document, its
schema defines a unique data model
Motivation for JAXB
Problem: How to manipulate this data model?
DOM )data object model) solution:
Pros: simple, general )a schema is not even required)
Cons: no types, no compile-time checking
DOM pseudo-code example
root.getChild("Address").getChild("Number").getText()
root.getAddress().getNumber()
returns a number
JAXB solution:
Mapping XML Schema to Java interfaces
Binding Compiler
Source Java
schema interface
s
Binding Compiler
interface AddressType
interface PersonType
AddressType and PersonType
public interface AddressType {
Must be non-negative
long getNumber();
void setNumber(long value);
String getStreet();
void setStreet(String value);
}
Must be non-null
System.out.println("Person name=" +
person.getName() );
// Delete
List addressList = person.getAddress();
addressList.clear();
What happens if we validate there?
// Create
ObjectFactory objectFactory = new ObjectFactory();
AddressType newAddr = objectFactory.createAddressType();
newAddr.setStreet("Hanoter");
part of the demo package
newAddr.setNumber(5);
addressList.add( newAddr );
uses the factory pattern
Step 8: Validate on-demand
Validator validator = context.createValidator();
validator.validate(newAddr);
validator.validate(person);
output.xml
<Person>
<Name>Yoav Zibin</Name>
<Address>
<Street>Hanoter</Street>
<Number>5</Number>
</Address>
</Person>
!And now, the Demo
First Part Summary
Similar Technologies
Liquid XML Data Binding
Similar to JAXB
Supports all Schema constructs
Relaxer
Instead of Schema uses Relax
Castor.org
Second Part Outline
Validation
Mapping XML Schema to Java
Naming
Java Properties
General content
<xs:any/> Object getAny();
void setAny(Object elementOrValue);
Naming
Problem: sometimes XML names
are not legal java names
do not comply to java naming standards
AddressType[] getAddress();
void setAddress(AddressType[] value);
date Calendar
maxInclusive
integer BigInteger
Enumeration
int int Next (2)
Simple Type:Union
<xs:complexType name="Date">
<xs:sequence>
<xs:element name="Month">
<xs:simpleType>
<xs:union memberTypes="xs:int xs:string"/>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
Common supertype
Public interface Date { of )Integer, String)
Object getMonth(); is Object
void setMonth(Object);
}
Type SafeEnumeration
<xs:simpleType name="USState">
<xs:restriction base="xs:NCName">
<xs:enumeration value="AK"/>
<xs:enumeration value="NY"/>
</xs:restriction>
</xs:simpleType>
*Extension Extension
Elements
Attributes
Restriction
abstract
*
The interface
use
default
nillable
minOccurs *
fixed maxOccurs
extends the base
type’s interface Represented as
( ) Next
* Java properties
Complex Types
Represented as a Java interface
Anonymous type
An interface is created.
The name is derived from the name of the
interface InternationalPrice {
int getValue();
void setValue(int);
String getCurrency();
void setCurrency(String);
}
Complex Type:Aggregation
<xs:element name="A" type="xs:int"/>
interface Foo {
int getA(); void setA(int);
int getB(); void setB(int);
int getC(); void setC(int);
}
Complex Type: Mixed Content
<xs:complexType name=“LetterBody"
mixed="true">
<xs:sequence>
<xs:element name="name" type="xs:string"/>
…
</xs:sequence> XML fragment
</xs:complexType> Dear Mr.<name>Robert Smith</name>, …
interface lb
LetterBody LetterBody {
= ObjectFactory.createLetterBody();
interface
List gcl Name extends Element {
= lb.getContent();
String
gcl.add("Dear getValue();
Mr.");
void setValue(String);
gcl.add(ObjectFactory.createLetterBodyName("Robert Smith"));
}
… The list may contain
List getContent(); elements and strings
}
Complex Type:Choice
<xs:complexType name="FooBarType">
The programmer
<xs:choice>
is responsible to
<xs:element name="Foo" type="xs:int"/>
only set one of
<xs:element name="Bar"
Foo or Bar
type="xs:string"/>
</xs:choice>
Default
</xs:complexType>
Customization (Not implemented yet) public interface FooBarType {
int getFoo();
public interface FooBarType {
void setFoo(int value);
Object getFooOrBar();
String getBar();
void setFooOrBar(Object);
void setBar(String value);
}
boolean isSetFoo();
Common supertype of void unsetFoo();
)Integer, String) is boolean isSetBar();
Object void unsetBar();
Similar to union }
WhenmaxOccurs>1
<xs:complexType name="FooBarType">
<xs:choice maxOccurs="unbounded">
<xs:element name="Foo" type="xs:int"/>
<xs:element name="Bar" type="xs:string"/>
</xs:choice>
</xs:complexType>
Integer getAge();
void setAge(Integer value);
“User is responsible …”
Any questions?