BAEL-707 Introduction to JiBX (#1340)

This commit is contained in:
baljeet20
2017-03-09 13:36:06 +05:30
committed by maibin
parent 00d42129ef
commit a5f8bf91be
9 changed files with 516 additions and 5 deletions

View File

@@ -0,0 +1,53 @@
/*
* Customer.java 06.06.2008
*
* Copyright 2008 Stefan Jäger
*
*/
package com.baeldung.xml.jibx;
import org.apache.commons.lang.builder.ToStringBuilder;
public class Customer {
private Person person;
private String city;
private Phone homePhone;
private Phone officePhone;
public Person getPerson() {
return person;
}
public void setPerson(Person person) {
this.person = person;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public Phone getHomePhone() {
return homePhone;
}
public void setHomePhone(Phone homePhone) {
this.homePhone = homePhone;
}
public Phone getOfficePhone() {
return officePhone;
}
public void setOfficePhone(Phone officePhone) {
this.officePhone = officePhone;
}
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}

View File

@@ -0,0 +1,14 @@
package com.baeldung.xml.jibx;
public class Identity {
long customerId;
public long getCustomerId() {
return customerId;
}
public void setCustomerId(long customerId) {
this.customerId = customerId;
}
}

View File

@@ -0,0 +1,34 @@
/*
* Person.java 06.06.2008
*
* Copyright 2008 Stefan Jäger
*
*/
package com.baeldung.xml.jibx;
import org.apache.commons.lang.builder.ToStringBuilder;
public class Person extends Identity {
private String firstName;
private String lastName;
public String getFirstName() {
return firstName;
}
public void setFirstName(String firstName) {
this.firstName = firstName;
}
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}

View File

@@ -0,0 +1,32 @@
package com.baeldung.xml.jibx;
public class Phone {
private String countryCode;
private String networkPrefix;
private String number;
public String getCountryCode() {
return countryCode;
}
public void setCountryCode(String countryCode) {
this.countryCode = countryCode;
}
public String getNetworkPrefix() {
return networkPrefix;
}
public void setNetworkPrefix(String networkPrefix) {
this.networkPrefix = networkPrefix;
}
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
}

View File

@@ -0,0 +1,49 @@
<xs:schema attributeFormDefault="unqualified"
elementFormDefault="qualified" targetNamespace="http://www.jibx.xml.baeldung.com/"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="Order">
<xs:complexType>
<xs:sequence>
<xs:element name="Address" maxOccurs="unbounded"
minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="Name" />
<xs:element type="xs:string" name="Street" />
<xs:element type="xs:string" name="City" />
<xs:element type="xs:string" name="State" />
<xs:element type="xs:int" name="Zip" />
<xs:element type="xs:string" name="Country" />
</xs:sequence>
<xs:attribute type="xs:string" name="Type" use="optional" />
</xs:complexType>
</xs:element>
<xs:element type="xs:string" name="DeliveryNotes" />
<xs:element name="Items">
<xs:complexType>
<xs:sequence>
<xs:element name="Item" maxOccurs="unbounded"
minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="ProductName" />
<xs:element type="xs:byte" name="Quantity" />
<xs:element type="xs:float" name="USPrice" />
<xs:element type="xs:string" name="Comment"
minOccurs="0" />
<xs:element type="xs:date" name="ShipDate"
minOccurs="0" />
</xs:sequence>
<xs:attribute type="xs:string" name="PartNumber"
use="optional" />
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute type="xs:int" name="OrderNumber" />
<xs:attribute type="xs:date" name="OrderDate" />
</xs:complexType>
</xs:element>
</xs:schema>

View File

@@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<binding>
<mapping class="com.baeldung.xml.jibx.Identity" abstract="true">
<value name="customer-id" field="customerId" />
</mapping>
<mapping name="customer" class="com.baeldung.xml.jibx.Customer">
<structure field="person" />
<structure name="home-phone" field="homePhone" usage="optional" />
<structure name="office-phone" field="officePhone" usage="optional" />
<value name="city" field="city" />
</mapping>
<mapping name="person" class="com.baeldung.xml.jibx.Person"
extends="com.baeldung.xml.jibx.Identity">
<structure map-as="com.baeldung.xml.jibx.Identity" />
<value name="first-name" field="firstName" />
<value name="last-name" field="lastName" />
</mapping>
<mapping class="com.baeldung.xml.jibx.Phone" abstract="true">
<value name="country-code" field="countryCode" />
<value name="network-prefix" field="networkPrefix" />
<value name="number" field="number" />
</mapping>
</binding>

View File

@@ -0,0 +1,56 @@
package com.baeldung.xml.jibx;
import org.jibx.runtime.BindingDirectory;
import org.jibx.runtime.IBindingFactory;
import org.jibx.runtime.IUnmarshallingContext;
import org.jibx.runtime.JiBXException;
import org.junit.Test;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import static junit.framework.Assert.assertEquals;
public class CustomerTest {
@Test
public void whenUnmarshalXML_ThenFieldsAreMapped() throws JiBXException, FileNotFoundException {
IBindingFactory bfact = BindingDirectory.getFactory(Customer.class);
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream inputStream = classLoader.getResourceAsStream("Customer1.xml");
Customer customer = (Customer) uctx.unmarshalDocument(inputStream, null);
assertEquals("Stefan", customer.getPerson().getFirstName());
assertEquals("Jaeger", customer.getPerson().getLastName());
assertEquals("Davos Dorf", customer.getCity());
}
@Test
public void WhenUnmarshal_ThenMappingInherited() throws JiBXException, FileNotFoundException {
IBindingFactory bfact = BindingDirectory.getFactory(Customer.class);
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream inputStream = classLoader.getResourceAsStream("Customer1.xml");
Customer customer = (Customer) uctx.unmarshalDocument(inputStream, null);
assertEquals(12345, customer.getPerson().getCustomerId());
}
@Test
public void WhenUnmarshal_ThenPhoneMappingRead() throws JiBXException, FileNotFoundException {
IBindingFactory bfact = BindingDirectory.getFactory(Customer.class);
IUnmarshallingContext uctx = bfact.createUnmarshallingContext();
ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
InputStream inputStream = classLoader.getResourceAsStream("Customer1.xml");
Customer customer = (Customer) uctx.unmarshalDocument(inputStream, null);
assertEquals("1", customer.getHomePhone().getCountryCode());
assertEquals("234", customer.getHomePhone().getNetworkPrefix());
assertEquals("234678", customer.getHomePhone().getNumber());
}
}

View File

@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="UTF-8"?>
<customer>
<person>
<customer-id>12345</customer-id>
<first-name>Stefan</first-name>
<last-name>Jaeger</last-name>
</person>
<home-phone>
<country-code>1</country-code>
<network-prefix>234</network-prefix>
<number>234678</number>
</home-phone>
<office-phone>
<country-code>1</country-code>
<network-prefix>234</network-prefix>
<number>234678</number>
</office-phone>
<city>Davos Dorf</city>
</customer>