Merge pull request #390 from ankur-singhal/XStream-Introduction---Object-to-Xml

XStream introduction - Object to XML
This commit is contained in:
David Morley 2016-03-26 16:24:19 -05:00
commit 4e16ccc0a1
13 changed files with 439 additions and 0 deletions

View File

@ -0,0 +1,14 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>xstream-introduction</name>
<comment>An Introduction To XStream. NO_M2ECLIPSE_SUPPORT: Project files created with the maven-eclipse-plugin are not supported in M2Eclipse.</comment>
<projects/>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>

View File

@ -0,0 +1,30 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.baeldung</groupId>
<artifactId>xstream-introduction</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>xstream-introduction</name>
<description>An Introduction To XStream</description>
<dependencies>
<dependency>
<groupId>com.thoughtworks.xstream</groupId>
<artifactId>xstream</artifactId>
<version>1.4.5</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>1.2.17</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,19 @@
package com.baeldung.initializer;
import com.thoughtworks.xstream.XStream;
public class SimpleXstreamInitializer {
private static XStream xstreamInstance;
public static XStream getXstreamInstance() {
if (xstreamInstance == null) {
synchronized (SimpleXstreamInitializer.class) {
if (xstreamInstance == null) {
xstreamInstance = new XStream();
}
}
}
return xstreamInstance;
}
}

View File

@ -0,0 +1,40 @@
package com.baeldung.pojo;
import java.util.List;
import com.thoughtworks.xstream.annotations.XStreamAlias;
@XStreamAlias("AddressDetails")
public class AddressDetails {
private String address;
private String zipcode;
private List<ContactDetails> contactDetails;
public String getZipcode() {
return zipcode;
}
public void setZipcode(String zipcode) {
this.zipcode = zipcode;
}
public List<ContactDetails> getContactDetails() {
return contactDetails;
}
public void setContactDetails(List<ContactDetails> contactDetails) {
this.contactDetails = contactDetails;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
}

View File

@ -0,0 +1,28 @@
package com.baeldung.pojo;
import com.thoughtworks.xstream.annotations.XStreamAlias;
@XStreamAlias("ContactDetails")
public class ContactDetails {
private String mobile;
private String landline;
public String getMobile() {
return mobile;
}
public void setMobile(String mobile) {
this.mobile = mobile;
}
public String getLandline() {
return landline;
}
public void setLandline(String landline) {
this.landline = landline;
}
}

View File

@ -0,0 +1,55 @@
package com.baeldung.pojo;
import java.util.Date;
import java.util.List;
import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamImplicit;
import com.thoughtworks.xstream.annotations.XStreamOmitField;
@XStreamAlias("customer")
public class Customer {
//@XStreamOmitField
private String firstName;
private String lastName;
private Date dob;
@XStreamImplicit
private List<ContactDetails> contactDetailsList;
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 Date getDob() {
return dob;
}
public void setDob(Date dob) {
this.dob = dob;
}
public List<ContactDetails> getContactDetailsList() {
return contactDetailsList;
}
public void setContactDetailsList(List<ContactDetails> contactDetailsList) {
this.contactDetailsList = contactDetailsList;
}
}

View File

@ -0,0 +1,50 @@
package com.baeldung.pojo;
import java.util.List;
import com.thoughtworks.xstream.annotations.XStreamAlias;
@XStreamAlias("CustomerAddressDetails")
public class CustomerAddressDetails {
private List<AddressDetails> addressDetails;
private String firstName;
private String lastName;
private int age;
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public List<AddressDetails> getAddressDetails() {
return addressDetails;
}
public void setAddressDetails(List<AddressDetails> addressDetails) {
this.addressDetails = addressDetails;
}
}

View File

@ -0,0 +1,20 @@
package com.baeldung.pojo;
import java.util.List;
import com.thoughtworks.xstream.annotations.XStreamAlias;
@XStreamAlias("CustomerPortfolio")
public class CustomerPortfolio {
private List<CustomerAddressDetails> customerAddressDetailsList;
public List<CustomerAddressDetails> getCustomerAddressDetailsList() {
return customerAddressDetailsList;
}
public void setCustomerAddressDetailsList(List<CustomerAddressDetails> customerAddressDetailsList) {
this.customerAddressDetailsList = customerAddressDetailsList;
}
}

View File

@ -0,0 +1,40 @@
package com.baeldung.utility;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import com.thoughtworks.xstream.converters.ConversionException;
import com.thoughtworks.xstream.converters.Converter;
import com.thoughtworks.xstream.converters.MarshallingContext;
import com.thoughtworks.xstream.converters.UnmarshallingContext;
import com.thoughtworks.xstream.io.HierarchicalStreamReader;
import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
public class MyDateConverter implements Converter {
private SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
@Override
public boolean canConvert(Class clazz) {
return Date.class.isAssignableFrom(clazz);
}
@Override
public void marshal(Object value , HierarchicalStreamWriter writer , MarshallingContext arg2) {
Date date = (Date) value;
writer.setValue(formatter.format(date));
}
@Override
public Object unmarshal(HierarchicalStreamReader reader , UnmarshallingContext arg1) {
GregorianCalendar calendar = new GregorianCalendar();
try {
calendar.setTime(formatter.parse(reader.getValue()));
} catch (ParseException e) {
throw new ConversionException(e.getMessage() , e);
}
return calendar;
}
}

View File

@ -0,0 +1,28 @@
package com.baeldung.utility;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.baeldung.pojo.Customer;
import com.thoughtworks.xstream.converters.SingleValueConverter;
public class MySingleValueConverter implements SingleValueConverter {
@Override
public boolean canConvert(Class clazz) {
return Customer.class.isAssignableFrom(clazz);
}
@Override
public Object fromString(String arg0) {
return null;
}
@Override
public String toString(Object obj) {
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy");
Date date = ((Customer) obj).getDob();
return ((Customer) obj).getFirstName() + "," + ((Customer) obj).getLastName() + "," + formatter.format(date);
}
}

View File

@ -0,0 +1,37 @@
package com.baeldung.utility;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import com.baeldung.pojo.ContactDetails;
import com.baeldung.pojo.Customer;
public class SimpleDataGeneration {
public static Customer generateData() {
Customer customer = new Customer();
Calendar cal = Calendar.getInstance();
cal.set(1986 , 01 , 14);
customer.setDob(cal.getTime());
customer.setFirstName("XStream");
customer.setLastName("Java");
List<ContactDetails> contactDetailsList = new ArrayList<ContactDetails>();
ContactDetails contactDetails1 = new ContactDetails();
contactDetails1.setLandline("0124-2460311");
contactDetails1.setMobile("6673543265");
ContactDetails contactDetails2 = new ContactDetails();
contactDetails2.setLandline("0120-223312");
contactDetails2.setMobile("4676543565");
contactDetailsList.add(contactDetails1);
contactDetailsList.add(contactDetails2);
customer.setContactDetailsList(contactDetailsList);
return customer;
}
}

View File

@ -0,0 +1,16 @@
# Root logger option
log4j.rootLogger=DEBUG, file
# Redirect log messages to console
# log4j.appender.stdout=org.apache.log4j.ConsoleAppender
# log4j.appender.stdout.Target=System.out
# log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
# log4j.appender.stdout.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
# Redirect log messages to a log file, support file rolling.
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=D:\\Test\\xstream-application.log
log4j.appender.file.MaxFileSize=5MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n

View File

@ -0,0 +1,62 @@
package com.baeldung.utility;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import com.baeldung.initializer.SimpleXstreamInitializer;
import com.baeldung.pojo.AddressDetails;
import com.baeldung.pojo.ContactDetails;
import com.baeldung.pojo.Customer;
import com.baeldung.utility.SimpleDataGeneration;
import com.thoughtworks.xstream.XStream;
public class XStreamSimpleXmlTest {
private Customer customer = null;
private String dataXml = null;
private XStream xstream = null;
@Before
public void dataSetup() {
customer = SimpleDataGeneration.generateData();
xstream = SimpleXstreamInitializer.getXstreamInstance();
xstream.processAnnotations(Customer.class);
xstream.processAnnotations(AddressDetails.class);
xstream.processAnnotations(ContactDetails.class);
xstream.omitField(Customer.class , "lastName");
xstream.registerConverter(new MyDateConverter());
// xstream.registerConverter(new MySingleValueConverter());
xstream.aliasField("fn" , Customer.class , "firstName");
dataXml = xstream.toXML(customer);
System.out.println(dataXml);
}
@Test
public void testClassAliasedAnnotation() {
Assert.assertNotEquals(-1 , dataXml.indexOf("<customer>"));
}
@Test
public void testFieldAliasedAnnotation() {
Assert.assertNotEquals(-1 , dataXml.indexOf("<fn>"));
}
@Test
public void testImplicitCollection() {
Assert.assertEquals(-1 , dataXml.indexOf("contactDetailsList"));
}
@Test
public void testDateFieldFormating() {
Assert.assertEquals("14-02-1986" , dataXml.substring(dataXml.indexOf("<dob>") + 5 , dataXml.indexOf("</dob>")));
}
@Test
public void testOmitField() {
Assert.assertEquals(-1 , dataXml.indexOf("lastName"));
}
}