diff --git a/xml/pom.xml b/xml/pom.xml index 4230350ed9..38bf6bf42e 100644 --- a/xml/pom.xml +++ b/xml/pom.xml @@ -7,6 +7,32 @@ xml + + + jibx.sf.net + JiBX repository + http://jibx.sf.net/maven2 + + never + + + false + + + + + + jibx.sf.net + JiBX repository + http://jibx.sf.net/maven2 + + never + + + false + + + @@ -46,6 +72,17 @@ ${commons-lang3.version} + + org.jibx + jibx-run + ${jibx-version} + + + commons-lang + commons-lang + ${commons-lang.version} + + @@ -74,8 +111,8 @@ maven-compiler-plugin ${maven-compiler-plugin.version} - 1.8 - 1.8 + ${java-version} + ${java-version} @@ -83,21 +120,206 @@ org.apache.maven.plugins maven-surefire-plugin ${maven-surefire-plugin.version} + + + CustomerTest.java + + + + + schemaGen + + + + org.jibx + maven-jibx-plugin + ${maven-jibx-plugin.version} + + + generate-java-code-from-schema + + schema-codegen + + + src/main/resources + + Order.xsd + + true + + + + compile-binding + + bind + + + target/generated-sources + true + true + + true + + + + generate-test-code-from-schema + generate-test-sources + + test-schema-codegen + + + + compile-test-binding + process-test-classes + + test-bind + + + target/generated-test-sources + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + ${java-version} + ${java-version} + + + + + org.apache.maven.plugins + maven-surefire-plugin + ${maven-surefire-plugin.version} + + + CustomerTest.java + + + + + + + + + + bindGen + + + + + org.jibx + maven-jibx-plugin + + + + + + org.jibx + maven-jibx-plugin + ${maven-jibx-plugin.version} + + src/main/resources + + *-binding.xml + + + template-binding.xml + + true + + + + process-classes + + bind + + + + + + org.jibx + maven-jibx-plugin + 1.3.1 + + src/main/resources + + *-binding.xml + + true + + + + process-test-classes + + test-bind + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + + ${java-version} + ${java-version} + + + + + maven-assembly-plugin + + + + jar-with-dependencies + + + + + + com.baeldung.xml.jibx.JiBXDemoApplication + + + + + + + make-assembly + package + + attached + + + + + + + + + + - 1.6.1 - 1.1.6 - 2.0.6 + 1.6.1 + 1.1.6 + 2.0.6 2.5 4.1 + 1.2.4.5 3.5 + 2.4 + 1.8 4.12 @@ -105,6 +327,7 @@ 3.6.0 2.19.1 + 1.3.1 diff --git a/xml/src/main/java/com/baeldung/xml/jibx/Customer.java b/xml/src/main/java/com/baeldung/xml/jibx/Customer.java new file mode 100644 index 0000000000..fabd464663 --- /dev/null +++ b/xml/src/main/java/com/baeldung/xml/jibx/Customer.java @@ -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); + } +} diff --git a/xml/src/main/java/com/baeldung/xml/jibx/Identity.java b/xml/src/main/java/com/baeldung/xml/jibx/Identity.java new file mode 100644 index 0000000000..15aab4e417 --- /dev/null +++ b/xml/src/main/java/com/baeldung/xml/jibx/Identity.java @@ -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; + } +} diff --git a/xml/src/main/java/com/baeldung/xml/jibx/Person.java b/xml/src/main/java/com/baeldung/xml/jibx/Person.java new file mode 100644 index 0000000000..54ce75e257 --- /dev/null +++ b/xml/src/main/java/com/baeldung/xml/jibx/Person.java @@ -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); + } +} diff --git a/xml/src/main/java/com/baeldung/xml/jibx/Phone.java b/xml/src/main/java/com/baeldung/xml/jibx/Phone.java new file mode 100644 index 0000000000..b24950289a --- /dev/null +++ b/xml/src/main/java/com/baeldung/xml/jibx/Phone.java @@ -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; + } +} diff --git a/xml/src/main/resources/Order.xsd b/xml/src/main/resources/Order.xsd new file mode 100644 index 0000000000..16e689a342 --- /dev/null +++ b/xml/src/main/resources/Order.xsd @@ -0,0 +1,49 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/xml/src/main/resources/customer-binding.xml b/xml/src/main/resources/customer-binding.xml new file mode 100755 index 0000000000..54ce4a925f --- /dev/null +++ b/xml/src/main/resources/customer-binding.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/xml/src/test/java/com/baeldung/xml/jibx/CustomerTest.java b/xml/src/test/java/com/baeldung/xml/jibx/CustomerTest.java new file mode 100644 index 0000000000..c678ce8406 --- /dev/null +++ b/xml/src/test/java/com/baeldung/xml/jibx/CustomerTest.java @@ -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()); + + } +} diff --git a/xml/src/test/resources/Customer1.xml b/xml/src/test/resources/Customer1.xml new file mode 100644 index 0000000000..3941563ffe --- /dev/null +++ b/xml/src/test/resources/Customer1.xml @@ -0,0 +1,21 @@ + + + + 12345 + Stefan + Jaeger + + + 1 + 234 + 234678 + + + + 1 + 234 + 234678 + + Davos Dorf + + \ No newline at end of file