BAEL-707 Introduction to JiBX (#1340)
This commit is contained in:
parent
00d42129ef
commit
a5f8bf91be
233
xml/pom.xml
233
xml/pom.xml
|
@ -7,6 +7,32 @@
|
|||
|
||||
<name>xml</name>
|
||||
|
||||
<pluginRepositories>
|
||||
<pluginRepository>
|
||||
<id>jibx.sf.net</id>
|
||||
<name>JiBX repository</name>
|
||||
<url>http://jibx.sf.net/maven2</url>
|
||||
<releases>
|
||||
<updatePolicy>never</updatePolicy>
|
||||
</releases>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</pluginRepository>
|
||||
</pluginRepositories>
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>jibx.sf.net</id>
|
||||
<name>JiBX repository</name>
|
||||
<url>http://jibx.sf.net/maven2</url>
|
||||
<releases>
|
||||
<updatePolicy>never</updatePolicy>
|
||||
</releases>
|
||||
<snapshots>
|
||||
<enabled>false</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
</repositories>
|
||||
<dependencies>
|
||||
<!-- xml libraries -->
|
||||
<dependency>
|
||||
|
@ -46,6 +72,17 @@
|
|||
<version>${commons-lang3.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.jibx</groupId>
|
||||
<artifactId>jibx-run</artifactId>
|
||||
<version>${jibx-version}</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>commons-lang</groupId>
|
||||
<artifactId>commons-lang</artifactId>
|
||||
<version>${commons-lang.version}</version>
|
||||
</dependency>
|
||||
|
||||
|
||||
<!-- test scoped -->
|
||||
|
||||
|
@ -74,8 +111,8 @@
|
|||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>${maven-compiler-plugin.version}</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
<source>${java-version}</source>
|
||||
<target>${java-version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
|
@ -83,21 +120,206 @@
|
|||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>CustomerTest.java</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
</plugins>
|
||||
|
||||
</build>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>schemaGen</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.jibx</groupId>
|
||||
<artifactId>maven-jibx-plugin</artifactId>
|
||||
<version>${maven-jibx-plugin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>generate-java-code-from-schema</id>
|
||||
<goals>
|
||||
<goal>schema-codegen</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<directory>src/main/resources</directory>
|
||||
<includes>
|
||||
<include>Order.xsd</include>
|
||||
</includes>
|
||||
<verbose>true</verbose>
|
||||
</configuration>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>compile-binding</id>
|
||||
<goals>
|
||||
<goal>bind</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<directory>target/generated-sources</directory>
|
||||
<load>true</load>
|
||||
<validate>true</validate>
|
||||
<!--<verbose>true</verbose> -->
|
||||
<verify>true</verify>
|
||||
</configuration>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>generate-test-code-from-schema</id>
|
||||
<phase>generate-test-sources</phase>
|
||||
<goals>
|
||||
<goal>test-schema-codegen</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
<execution>
|
||||
<id>compile-test-binding</id>
|
||||
<phase>process-test-classes</phase>
|
||||
<goals>
|
||||
<goal>test-bind</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<directory>target/generated-test-sources</directory>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>${java-version}</source>
|
||||
<target>${java-version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<version>${maven-surefire-plugin.version}</version>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>CustomerTest.java</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
|
||||
</build>
|
||||
</profile>
|
||||
|
||||
<profile>
|
||||
<id>bindGen</id>
|
||||
<build>
|
||||
<pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.jibx</groupId>
|
||||
<artifactId>maven-jibx-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</pluginManagement>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.jibx</groupId>
|
||||
<artifactId>maven-jibx-plugin</artifactId>
|
||||
<version>${maven-jibx-plugin.version}</version>
|
||||
<configuration>
|
||||
<directory>src/main/resources</directory>
|
||||
<includes>
|
||||
<includes>*-binding.xml</includes>
|
||||
</includes>
|
||||
<excludes>
|
||||
<exclude>template-binding.xml</exclude>
|
||||
</excludes>
|
||||
<verbose>true</verbose>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>process-classes</phase>
|
||||
<goals>
|
||||
<goal>bind</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.jibx</groupId>
|
||||
<artifactId>maven-jibx-plugin</artifactId>
|
||||
<version>1.3.1</version>
|
||||
<configuration>
|
||||
<schemaBindingDirectory>src/main/resources</schemaBindingDirectory>
|
||||
<includeSchemaBindings>
|
||||
<includeSchemaBindings>*-binding.xml</includeSchemaBindings>
|
||||
</includeSchemaBindings>
|
||||
<verbose>true</verbose>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>process-test-classes</phase>
|
||||
<goals>
|
||||
<goal>test-bind</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<source>${java-version}</source>
|
||||
<target>${java-version}</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<!-- NOTE: We don't need a groupId specification because the group
|
||||
is org.apache.maven.plugins ...which is assumed by default. -->
|
||||
<artifactId>maven-assembly-plugin</artifactId>
|
||||
<configuration>
|
||||
<descriptorRefs>
|
||||
<descriptorRef>
|
||||
jar-with-dependencies
|
||||
</descriptorRef>
|
||||
</descriptorRefs>
|
||||
<archive>
|
||||
<manifest>
|
||||
<mainClass>
|
||||
com.baeldung.xml.jibx.JiBXDemoApplication
|
||||
</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>make-assembly</id><!-- this is used for inheritance merges -->
|
||||
<phase>package</phase><!-- append to the packaging phase. -->
|
||||
<goals>
|
||||
<goal>attached</goal><!-- goals == mojos -->
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
|
||||
|
||||
</profiles>
|
||||
<properties>
|
||||
<dom4j.version>1.6.1</dom4j.version>
|
||||
<jaxen.version>1.1.6</jaxen.version>
|
||||
<jdom2.version>2.0.6</jdom2.version>
|
||||
<dom4j.version>1.6.1</dom4j.version>
|
||||
<jaxen.version>1.1.6</jaxen.version>
|
||||
<jdom2.version>2.0.6</jdom2.version>
|
||||
<commons-io.version>2.5</commons-io.version>
|
||||
<commons-collections4.version>4.1</commons-collections4.version>
|
||||
<jibx-version>1.2.4.5</jibx-version>
|
||||
|
||||
<!-- util -->
|
||||
<commons-lang3.version>3.5</commons-lang3.version>
|
||||
<commons-lang.version>2.4</commons-lang.version>
|
||||
<java-version>1.8</java-version>
|
||||
|
||||
<!-- testing -->
|
||||
<junit.version>4.12</junit.version>
|
||||
|
@ -105,6 +327,7 @@
|
|||
<!-- maven plugins -->
|
||||
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version>
|
||||
<maven-surefire-plugin.version>2.19.1</maven-surefire-plugin.version>
|
||||
<maven-jibx-plugin.version>1.3.1</maven-jibx-plugin.version>
|
||||
|
||||
</properties>
|
||||
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -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>
|
|
@ -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>
|
|
@ -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());
|
||||
|
||||
}
|
||||
}
|
|
@ -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>
|
Loading…
Reference in New Issue