Clean up XStream examples
This commit is contained in:
parent
dd61036a31
commit
ed629e7a49
|
@ -1,14 +0,0 @@
|
|||
<?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>
|
|
@ -1,46 +0,0 @@
|
|||
package com.baeldung.annotation.pojo;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||
|
||||
@XStreamAlias("customer")
|
||||
public class Customer {
|
||||
|
||||
@XStreamAlias("fn")
|
||||
private String firstName;
|
||||
|
||||
private String lastName;
|
||||
|
||||
private Date dob;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Customer [firstName=" + firstName + ", lastName=" + lastName
|
||||
+ ", dob=" + dob + "]";
|
||||
}
|
||||
}
|
|
@ -1,50 +0,0 @@
|
|||
package com.baeldung.annotation.pojo;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||
import com.thoughtworks.xstream.annotations.XStreamOmitField;
|
||||
|
||||
|
||||
@XStreamAlias("customer")
|
||||
public class CustomerOmitField {
|
||||
|
||||
@XStreamOmitField
|
||||
private String firstName;
|
||||
|
||||
private String lastName;
|
||||
|
||||
private Date dob;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "CustomerOmitAnnotation [firstName=" + firstName + ", lastName="
|
||||
+ lastName + ", dob=" + dob + "]";
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -1,46 +0,0 @@
|
|||
package com.baeldung.complex.pojo;
|
||||
|
||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
|
||||
|
||||
@XStreamAlias("ContactDetails")
|
||||
public class ContactDetails {
|
||||
|
||||
private String mobile;
|
||||
|
||||
private String landline;
|
||||
|
||||
@XStreamAsAttribute
|
||||
private String contactType;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public String getContactType() {
|
||||
return contactType;
|
||||
}
|
||||
|
||||
public void setContactType(String contactType) {
|
||||
this.contactType = contactType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ContactDetails [mobile=" + mobile + ", landline=" + landline
|
||||
+ ", contactType=" + contactType + "]";
|
||||
}
|
||||
|
||||
}
|
|
@ -1,57 +0,0 @@
|
|||
package com.baeldung.complex.pojo;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||
|
||||
@XStreamAlias("customer")
|
||||
public class Customer {
|
||||
|
||||
private String firstName;
|
||||
|
||||
private String lastName;
|
||||
|
||||
private Date dob;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Customer [firstName=" + firstName + ", lastName=" + lastName
|
||||
+ ", dob=" + dob + ", contactDetailsList=" + contactDetailsList
|
||||
+ "]";
|
||||
}
|
||||
}
|
|
@ -1,46 +0,0 @@
|
|||
package com.baeldung.implicit.collection.pojo;
|
||||
|
||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
|
||||
|
||||
@XStreamAlias("ContactDetails")
|
||||
public class ContactDetails {
|
||||
|
||||
private String mobile;
|
||||
|
||||
private String landline;
|
||||
|
||||
@XStreamAsAttribute
|
||||
private String contactType;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public String getContactType() {
|
||||
return contactType;
|
||||
}
|
||||
|
||||
public void setContactType(String contactType) {
|
||||
this.contactType = contactType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ContactDetails [mobile=" + mobile + ", landline=" + landline
|
||||
+ ", contactType=" + contactType + "]";
|
||||
}
|
||||
|
||||
}
|
|
@ -1,59 +0,0 @@
|
|||
package com.baeldung.implicit.collection.pojo;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||
import com.thoughtworks.xstream.annotations.XStreamImplicit;
|
||||
|
||||
@XStreamAlias("customer")
|
||||
public class Customer {
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Customer [firstName=" + firstName + ", lastName=" + lastName
|
||||
+ ", dob=" + dob + ", contactDetailsList=" + contactDetailsList
|
||||
+ "]";
|
||||
}
|
||||
}
|
|
@ -1,19 +0,0 @@
|
|||
package com.baeldung.initializer;
|
||||
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
|
||||
public class SimpleXstreamInitializer {
|
||||
|
||||
private XStream xtreamInstance;
|
||||
|
||||
public XStream getXstreamInstance() {
|
||||
if (xtreamInstance == null) {
|
||||
synchronized (SimpleXstreamInitializer.class) {
|
||||
if (xtreamInstance == null) {
|
||||
xtreamInstance = new XStream();
|
||||
}
|
||||
}
|
||||
}
|
||||
return xtreamInstance;
|
||||
}
|
||||
}
|
|
@ -1,40 +0,0 @@
|
|||
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;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
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;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,57 +0,0 @@
|
|||
package com.baeldung.pojo;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||
import com.thoughtworks.xstream.annotations.XStreamImplicit;
|
||||
|
||||
@XStreamAlias("customer")
|
||||
public class Customer {
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Customer [firstName=" + firstName + ", lastName=" + lastName + ", dob=" + dob + "]";
|
||||
}
|
||||
}
|
|
@ -1,50 +0,0 @@
|
|||
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;
|
||||
}
|
||||
}
|
|
@ -1,20 +0,0 @@
|
|||
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;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,40 +0,0 @@
|
|||
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;
|
||||
}
|
||||
}
|
|
@ -1,28 +0,0 @@
|
|||
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);
|
||||
}
|
||||
|
||||
}
|
|
@ -1,37 +0,0 @@
|
|||
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;
|
||||
}
|
||||
|
||||
}
|
|
@ -1,39 +0,0 @@
|
|||
package com.baeldung.pojo.test;
|
||||
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.complex.pojo.Customer;
|
||||
import com.baeldung.initializer.SimpleXstreamInitializer;
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
|
||||
public class ComplexXmlToObjectAnnotationTest {
|
||||
|
||||
private XStream xstream = null;
|
||||
|
||||
@Before
|
||||
public void dataSetup() {
|
||||
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
|
||||
xstream = simpleXstreamInitializer.getXstreamInstance();
|
||||
xstream.processAnnotations(Customer.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertXmlToObjectFromFile() {
|
||||
try {
|
||||
ClassLoader classLoader = getClass().getClassLoader();
|
||||
FileReader reader = new FileReader(classLoader.getResource("data-file-alias-field-complex.xml").getFile());
|
||||
Customer customer = (Customer) xstream.fromXML(reader);
|
||||
Assert.assertNotNull(customer);
|
||||
Assert.assertNotNull(customer.getContactDetailsList());
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,43 +0,0 @@
|
|||
package com.baeldung.pojo.test;
|
||||
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.complex.pojo.ContactDetails;
|
||||
import com.baeldung.complex.pojo.Customer;
|
||||
import com.baeldung.initializer.SimpleXstreamInitializer;
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
|
||||
public class ComplexXmlToObjectAttributeCollectionTest {
|
||||
|
||||
private XStream xstream = null;
|
||||
|
||||
@Before
|
||||
public void dataSetup() {
|
||||
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
|
||||
xstream = simpleXstreamInitializer.getXstreamInstance();
|
||||
xstream.processAnnotations(Customer.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertXmlToObjectFromFile() {
|
||||
try {
|
||||
ClassLoader classLoader = getClass().getClassLoader();
|
||||
FileReader reader = new FileReader(classLoader.getResource("data-file-alias-field-complex.xml").getFile());
|
||||
Customer customer = (Customer) xstream.fromXML(reader);
|
||||
Assert.assertNotNull(customer);
|
||||
Assert.assertNotNull(customer.getContactDetailsList());
|
||||
for(ContactDetails contactDetails : customer.getContactDetailsList()){
|
||||
Assert.assertNotNull(contactDetails.getContactType());
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,40 +0,0 @@
|
|||
package com.baeldung.pojo.test;
|
||||
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.implicit.collection.pojo.Customer;
|
||||
import com.baeldung.initializer.SimpleXstreamInitializer;
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
|
||||
public class ComplexXmlToObjectCollectionTest {
|
||||
|
||||
private XStream xstream = null;
|
||||
|
||||
@Before
|
||||
public void dataSetup() {
|
||||
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
|
||||
xstream = simpleXstreamInitializer.getXstreamInstance();
|
||||
xstream.processAnnotations(Customer.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertXmlToObjectFromFile() {
|
||||
try {
|
||||
ClassLoader classLoader = getClass().getClassLoader();
|
||||
FileReader reader = new FileReader(classLoader.getResource("data-file-alias-implicit-collection.xml").getFile());
|
||||
Customer customer = (Customer) xstream.fromXML(reader);
|
||||
Assert.assertNotNull(customer);
|
||||
Assert.assertNotNull(customer.getContactDetailsList());
|
||||
//System.out.println(customer);
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,38 +0,0 @@
|
|||
package com.baeldung.pojo.test;
|
||||
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.initializer.SimpleXstreamInitializer;
|
||||
import com.baeldung.pojo.Customer;
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
|
||||
public class XmlToObjectAliasTest {
|
||||
|
||||
private XStream xstream = null;
|
||||
|
||||
@Before
|
||||
public void dataSetup() {
|
||||
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
|
||||
xstream = simpleXstreamInitializer.getXstreamInstance();
|
||||
xstream.alias("customer" , Customer.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertXmlToObjectFromFile() {
|
||||
try {
|
||||
ClassLoader classLoader = getClass().getClassLoader();
|
||||
FileReader reader = new FileReader(classLoader.getResource("data-file-alias.xml").getFile());
|
||||
Customer customer = (Customer) xstream.fromXML(reader);
|
||||
Assert.assertNotNull(customer);
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,39 +0,0 @@
|
|||
package com.baeldung.pojo.test;
|
||||
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.annotation.pojo.Customer;
|
||||
import com.baeldung.initializer.SimpleXstreamInitializer;
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
|
||||
public class XmlToObjectAnnotationTest {
|
||||
|
||||
private XStream xstream = null;
|
||||
|
||||
@Before
|
||||
public void dataSetup() {
|
||||
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
|
||||
xstream = simpleXstreamInitializer.getXstreamInstance();
|
||||
xstream.processAnnotations(Customer.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertXmlToObjectFromFile() {
|
||||
try {
|
||||
ClassLoader classLoader = getClass().getClassLoader();
|
||||
FileReader reader = new FileReader(classLoader.getResource("data-file-alias-field.xml").getFile());
|
||||
Customer customer = (Customer) xstream.fromXML(reader);
|
||||
Assert.assertNotNull(customer);
|
||||
Assert.assertNotNull(customer.getFirstName());
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,40 +0,0 @@
|
|||
package com.baeldung.pojo.test;
|
||||
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.initializer.SimpleXstreamInitializer;
|
||||
import com.baeldung.pojo.Customer;
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
|
||||
public class XmlToObjectFieldAliasTest {
|
||||
|
||||
private XStream xstream = null;
|
||||
|
||||
@Before
|
||||
public void dataSetup() {
|
||||
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
|
||||
xstream = simpleXstreamInitializer.getXstreamInstance();
|
||||
xstream.alias("customer" , Customer.class);
|
||||
xstream.aliasField("fn", Customer.class, "firstName");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertXmlToObjectFromFile() {
|
||||
try {
|
||||
ClassLoader classLoader = getClass().getClassLoader();
|
||||
FileReader reader = new FileReader(classLoader.getResource("data-file-alias-field.xml").getFile());
|
||||
Customer customer = (Customer) xstream.fromXML(reader);
|
||||
Assert.assertNotNull(customer);
|
||||
Assert.assertNotNull(customer.getFirstName());
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,39 +0,0 @@
|
|||
package com.baeldung.pojo.test;
|
||||
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.initializer.SimpleXstreamInitializer;
|
||||
import com.baeldung.pojo.Customer;
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
|
||||
public class XmlToObjectIgnoreFieldsTest {
|
||||
|
||||
private XStream xstream = null;
|
||||
|
||||
@Before
|
||||
public void dataSetup() {
|
||||
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
|
||||
xstream = simpleXstreamInitializer.getXstreamInstance();
|
||||
xstream.alias("customer" , Customer.class);
|
||||
xstream.ignoreUnknownElements();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertXmlToObjectFromFile() {
|
||||
try {
|
||||
ClassLoader classLoader = getClass().getClassLoader();
|
||||
FileReader reader = new FileReader(classLoader.getResource("data-file-ignore-field.xml").getFile());
|
||||
Customer customer = (Customer) xstream.fromXML(reader);
|
||||
Assert.assertNotNull(customer);
|
||||
//System.out.println(customer);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,47 +0,0 @@
|
|||
package com.baeldung.pojo.test;
|
||||
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.baeldung.initializer.SimpleXstreamInitializer;
|
||||
import com.baeldung.pojo.Customer;
|
||||
import com.baeldung.utility.SimpleDataGeneration;
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
|
||||
public class XmlToObjectTest {
|
||||
|
||||
private XStream xstream = null;
|
||||
|
||||
@Before
|
||||
public void dataSetup() {
|
||||
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
|
||||
xstream = simpleXstreamInitializer.getXstreamInstance();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertXmlToObjectFromFile() {
|
||||
try {
|
||||
ClassLoader classLoader = getClass().getClassLoader();
|
||||
FileReader reader = new FileReader(classLoader.getResource("data-file.xml").getFile());
|
||||
Customer customer = (Customer) xstream.fromXML(reader);
|
||||
Assert.assertNotNull(customer);
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertXmlToObjectFromString() {
|
||||
Customer customer = SimpleDataGeneration.generateData();
|
||||
String dataXml = xstream.toXML(customer);
|
||||
Customer convertedCustomer = (Customer) xstream.fromXML(dataXml);
|
||||
Assert.assertNotNull(convertedCustomer);
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -1,61 +0,0 @@
|
|||
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();
|
||||
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
|
||||
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);
|
||||
}
|
||||
|
||||
@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"));
|
||||
}
|
||||
}
|
|
@ -1,15 +0,0 @@
|
|||
<customer>
|
||||
<firstName>XStream</firstName>
|
||||
<lastName>Java</lastName>
|
||||
<dob>1986-02-14 04:14:05.874 UTC</dob>
|
||||
<contactDetailsList>
|
||||
<ContactDetails contactType="Home">
|
||||
<mobile>6673543265</mobile>
|
||||
<landline>0124-2460311</landline>
|
||||
</ContactDetails>
|
||||
<ContactDetails contactType="Office">
|
||||
<mobile>4676543565</mobile>
|
||||
<landline>0120-223312</landline>
|
||||
</ContactDetails>
|
||||
</contactDetailsList>
|
||||
</customer>
|
|
@ -1,5 +0,0 @@
|
|||
<customer>
|
||||
<fn>XStream</fn>
|
||||
<lastName>Java</lastName>
|
||||
<dob>1986-02-14 03:46:16.381 UTC</dob>
|
||||
</customer>
|
|
@ -1,13 +0,0 @@
|
|||
<customer>
|
||||
<firstName>XStream</firstName>
|
||||
<lastName>Java</lastName>
|
||||
<dob>1986-02-14 04:14:20.541 UTC</dob>
|
||||
<ContactDetails contactType="Home">
|
||||
<mobile>6673543265</mobile>
|
||||
<landline>0124-2460311</landline>
|
||||
</ContactDetails>
|
||||
<ContactDetails contactType="Office">
|
||||
<mobile>4676543565</mobile>
|
||||
<landline>0120-223312</landline>
|
||||
</ContactDetails>
|
||||
</customer>
|
|
@ -1,5 +0,0 @@
|
|||
<customer>
|
||||
<firstName>XStream</firstName>
|
||||
<lastName>Java</lastName>
|
||||
<dob>1986-02-14 03:46:16.381 UTC</dob>
|
||||
</customer>
|
|
@ -1,6 +0,0 @@
|
|||
<customer>
|
||||
<firstName>XStream</firstName>
|
||||
<lastName>Java</lastName>
|
||||
<dob>1986-02-14 04:14:20.541 UTC</dob>
|
||||
<fullName>XStream Java</fullName>
|
||||
</customer>
|
|
@ -1,5 +0,0 @@
|
|||
<com.baeldung.pojo.Customer>
|
||||
<firstName>XStream</firstName>
|
||||
<lastName>Java</lastName>
|
||||
<dob>1986-02-14 03:46:16.381 UTC</dob>
|
||||
</com.baeldung.pojo.Customer>
|
|
@ -27,4 +27,18 @@
|
|||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<version>3.5.1</version>
|
||||
<configuration>
|
||||
<source>1.8</source>
|
||||
<target>1.8</target>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
</project>
|
|
@ -0,0 +1,46 @@
|
|||
package com.baeldung.annotation.pojo;
|
||||
|
||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
@XStreamAlias("customer")
|
||||
public class Customer {
|
||||
|
||||
@XStreamAlias("fn")
|
||||
private String firstName;
|
||||
|
||||
private String lastName;
|
||||
|
||||
private Date dob;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Customer [firstName=" + firstName + ", lastName=" + lastName
|
||||
+ ", dob=" + dob + "]";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
package com.baeldung.annotation.pojo;
|
||||
|
||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||
import com.thoughtworks.xstream.annotations.XStreamOmitField;
|
||||
|
||||
import java.util.Date;
|
||||
|
||||
|
||||
@XStreamAlias("customer")
|
||||
public class CustomerOmitField {
|
||||
|
||||
@XStreamOmitField
|
||||
private String firstName;
|
||||
|
||||
private String lastName;
|
||||
|
||||
private Date dob;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "CustomerOmitAnnotation [firstName=" + firstName + ", lastName="
|
||||
+ lastName + ", dob=" + dob + "]";
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package com.baeldung.complex.pojo;
|
||||
|
||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
|
||||
|
||||
@XStreamAlias("ContactDetails")
|
||||
public class ContactDetails {
|
||||
|
||||
private String mobile;
|
||||
|
||||
private String landline;
|
||||
|
||||
@XStreamAsAttribute
|
||||
private String contactType;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public String getContactType() {
|
||||
return contactType;
|
||||
}
|
||||
|
||||
public void setContactType(String contactType) {
|
||||
this.contactType = contactType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ContactDetails [mobile=" + mobile + ", landline=" + landline
|
||||
+ ", contactType=" + contactType + "]";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
package com.baeldung.complex.pojo;
|
||||
|
||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@XStreamAlias("customer")
|
||||
public class Customer {
|
||||
|
||||
private String firstName;
|
||||
|
||||
private String lastName;
|
||||
|
||||
private Date dob;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Customer [firstName=" + firstName + ", lastName=" + lastName
|
||||
+ ", dob=" + dob + ", contactDetailsList=" + contactDetailsList
|
||||
+ "]";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package com.baeldung.implicit.collection.pojo;
|
||||
|
||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||
import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
|
||||
|
||||
@XStreamAlias("ContactDetails")
|
||||
public class ContactDetails {
|
||||
|
||||
private String mobile;
|
||||
|
||||
private String landline;
|
||||
|
||||
@XStreamAsAttribute
|
||||
private String contactType;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
public String getContactType() {
|
||||
return contactType;
|
||||
}
|
||||
|
||||
public void setContactType(String contactType) {
|
||||
this.contactType = contactType;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "ContactDetails [mobile=" + mobile + ", landline=" + landline
|
||||
+ ", contactType=" + contactType + "]";
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,59 @@
|
|||
package com.baeldung.implicit.collection.pojo;
|
||||
|
||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||
import com.thoughtworks.xstream.annotations.XStreamImplicit;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@XStreamAlias("customer")
|
||||
public class Customer {
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Customer [firstName=" + firstName + ", lastName=" + lastName
|
||||
+ ", dob=" + dob + ", contactDetailsList=" + contactDetailsList
|
||||
+ "]";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
package com.baeldung.initializer;
|
||||
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
|
||||
public class SimpleXstreamInitializer {
|
||||
|
||||
private XStream xtreamInstance;
|
||||
|
||||
public XStream getXstreamInstance() {
|
||||
if (xtreamInstance == null) {
|
||||
synchronized (SimpleXstreamInitializer.class) {
|
||||
if (xtreamInstance == null) {
|
||||
xtreamInstance = new XStream();
|
||||
}
|
||||
}
|
||||
}
|
||||
return xtreamInstance;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package com.baeldung.pojo;
|
||||
|
||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
package com.baeldung.pojo;
|
||||
|
||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||
import com.thoughtworks.xstream.annotations.XStreamImplicit;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.List;
|
||||
|
||||
@XStreamAlias("customer")
|
||||
public class Customer {
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Customer [firstName=" + firstName + ", lastName=" + lastName + ", dob=" + dob + "]";
|
||||
}
|
||||
}
|
|
@ -0,0 +1,50 @@
|
|||
package com.baeldung.pojo;
|
||||
|
||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,20 @@
|
|||
package com.baeldung.pojo;
|
||||
|
||||
import com.thoughtworks.xstream.annotations.XStreamAlias;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@XStreamAlias("CustomerPortfolio")
|
||||
public class CustomerPortfolio {
|
||||
|
||||
private List<CustomerAddressDetails> customerAddressDetailsList;
|
||||
|
||||
public List<CustomerAddressDetails> getCustomerAddressDetailsList() {
|
||||
return customerAddressDetailsList;
|
||||
}
|
||||
|
||||
public void setCustomerAddressDetailsList(List<CustomerAddressDetails> customerAddressDetailsList) {
|
||||
this.customerAddressDetailsList = customerAddressDetailsList;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,40 @@
|
|||
package com.baeldung.utility;
|
||||
|
||||
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;
|
||||
|
||||
import java.text.ParseException;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
import java.util.GregorianCalendar;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,28 @@
|
|||
package com.baeldung.utility;
|
||||
|
||||
import com.baeldung.pojo.Customer;
|
||||
import com.thoughtworks.xstream.converters.SingleValueConverter;
|
||||
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package com.baeldung.utility;
|
||||
|
||||
import com.baeldung.pojo.ContactDetails;
|
||||
import com.baeldung.pojo.Customer;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Calendar;
|
||||
import java.util.List;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package com.baeldung.pojo.test;
|
||||
|
||||
import com.baeldung.complex.pojo.Customer;
|
||||
import com.baeldung.initializer.SimpleXstreamInitializer;
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
public class ComplexXmlToObjectAnnotationTest {
|
||||
|
||||
private XStream xstream = null;
|
||||
|
||||
@Before
|
||||
public void dataSetup() {
|
||||
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
|
||||
xstream = simpleXstreamInitializer.getXstreamInstance();
|
||||
xstream.processAnnotations(Customer.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertXmlToObjectFromFile() {
|
||||
try {
|
||||
ClassLoader classLoader = getClass().getClassLoader();
|
||||
FileReader reader = new FileReader(classLoader.getResource("data-file-alias-field-complex.xml").getFile());
|
||||
Customer customer = (Customer) xstream.fromXML(reader);
|
||||
Assert.assertNotNull(customer);
|
||||
Assert.assertNotNull(customer.getContactDetailsList());
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,42 @@
|
|||
package com.baeldung.pojo.test;
|
||||
|
||||
import com.baeldung.complex.pojo.ContactDetails;
|
||||
import com.baeldung.complex.pojo.Customer;
|
||||
import com.baeldung.initializer.SimpleXstreamInitializer;
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
public class ComplexXmlToObjectAttributeCollectionTest {
|
||||
|
||||
private XStream xstream = null;
|
||||
|
||||
@Before
|
||||
public void dataSetup() {
|
||||
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
|
||||
xstream = simpleXstreamInitializer.getXstreamInstance();
|
||||
xstream.processAnnotations(Customer.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertXmlToObjectFromFile() {
|
||||
try {
|
||||
ClassLoader classLoader = getClass().getClassLoader();
|
||||
FileReader reader = new FileReader(classLoader.getResource("data-file-alias-field-complex.xml").getFile());
|
||||
Customer customer = (Customer) xstream.fromXML(reader);
|
||||
Assert.assertNotNull(customer);
|
||||
Assert.assertNotNull(customer.getContactDetailsList());
|
||||
for (ContactDetails contactDetails : customer.getContactDetailsList()) {
|
||||
Assert.assertNotNull(contactDetails.getContactType());
|
||||
}
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package com.baeldung.pojo.test;
|
||||
|
||||
import com.baeldung.implicit.collection.pojo.Customer;
|
||||
import com.baeldung.initializer.SimpleXstreamInitializer;
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
public class ComplexXmlToObjectCollectionTest {
|
||||
|
||||
private XStream xstream = null;
|
||||
|
||||
@Before
|
||||
public void dataSetup() {
|
||||
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
|
||||
xstream = simpleXstreamInitializer.getXstreamInstance();
|
||||
xstream.processAnnotations(Customer.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertXmlToObjectFromFile() {
|
||||
try {
|
||||
ClassLoader classLoader = getClass().getClassLoader();
|
||||
FileReader reader = new FileReader(classLoader.getResource("data-file-alias-implicit-collection.xml").getFile());
|
||||
Customer customer = (Customer) xstream.fromXML(reader);
|
||||
Assert.assertNotNull(customer);
|
||||
Assert.assertNotNull(customer.getContactDetailsList());
|
||||
//System.out.println(customer);
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,37 @@
|
|||
package com.baeldung.pojo.test;
|
||||
|
||||
import com.baeldung.initializer.SimpleXstreamInitializer;
|
||||
import com.baeldung.pojo.Customer;
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
public class XmlToObjectAliasTest {
|
||||
|
||||
private XStream xstream = null;
|
||||
|
||||
@Before
|
||||
public void dataSetup() {
|
||||
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
|
||||
xstream = simpleXstreamInitializer.getXstreamInstance();
|
||||
xstream.alias("customer", Customer.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertXmlToObjectFromFile() {
|
||||
try {
|
||||
ClassLoader classLoader = getClass().getClassLoader();
|
||||
FileReader reader = new FileReader(classLoader.getResource("data-file-alias.xml").getFile());
|
||||
Customer customer = (Customer) xstream.fromXML(reader);
|
||||
Assert.assertNotNull(customer);
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package com.baeldung.pojo.test;
|
||||
|
||||
import com.baeldung.annotation.pojo.Customer;
|
||||
import com.baeldung.initializer.SimpleXstreamInitializer;
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
public class XmlToObjectAnnotationTest {
|
||||
|
||||
private XStream xstream = null;
|
||||
|
||||
@Before
|
||||
public void dataSetup() {
|
||||
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
|
||||
xstream = simpleXstreamInitializer.getXstreamInstance();
|
||||
xstream.processAnnotations(Customer.class);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertXmlToObjectFromFile() {
|
||||
try {
|
||||
ClassLoader classLoader = getClass().getClassLoader();
|
||||
FileReader reader = new FileReader(classLoader.getResource("data-file-alias-field.xml").getFile());
|
||||
Customer customer = (Customer) xstream.fromXML(reader);
|
||||
Assert.assertNotNull(customer);
|
||||
Assert.assertNotNull(customer.getFirstName());
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,39 @@
|
|||
package com.baeldung.pojo.test;
|
||||
|
||||
import com.baeldung.initializer.SimpleXstreamInitializer;
|
||||
import com.baeldung.pojo.Customer;
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
public class XmlToObjectFieldAliasTest {
|
||||
|
||||
private XStream xstream = null;
|
||||
|
||||
@Before
|
||||
public void dataSetup() {
|
||||
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
|
||||
xstream = simpleXstreamInitializer.getXstreamInstance();
|
||||
xstream.alias("customer", Customer.class);
|
||||
xstream.aliasField("fn", Customer.class, "firstName");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertXmlToObjectFromFile() {
|
||||
try {
|
||||
ClassLoader classLoader = getClass().getClassLoader();
|
||||
FileReader reader = new FileReader(classLoader.getResource("data-file-alias-field.xml").getFile());
|
||||
Customer customer = (Customer) xstream.fromXML(reader);
|
||||
Assert.assertNotNull(customer);
|
||||
Assert.assertNotNull(customer.getFirstName());
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,38 @@
|
|||
package com.baeldung.pojo.test;
|
||||
|
||||
import com.baeldung.initializer.SimpleXstreamInitializer;
|
||||
import com.baeldung.pojo.Customer;
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
public class XmlToObjectIgnoreFieldsTest {
|
||||
|
||||
private XStream xstream = null;
|
||||
|
||||
@Before
|
||||
public void dataSetup() {
|
||||
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
|
||||
xstream = simpleXstreamInitializer.getXstreamInstance();
|
||||
xstream.alias("customer", Customer.class);
|
||||
xstream.ignoreUnknownElements();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertXmlToObjectFromFile() {
|
||||
try {
|
||||
ClassLoader classLoader = getClass().getClassLoader();
|
||||
FileReader reader = new FileReader(classLoader.getResource("data-file-ignore-field.xml").getFile());
|
||||
Customer customer = (Customer) xstream.fromXML(reader);
|
||||
Assert.assertNotNull(customer);
|
||||
//System.out.println(customer);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,46 @@
|
|||
package com.baeldung.pojo.test;
|
||||
|
||||
import com.baeldung.initializer.SimpleXstreamInitializer;
|
||||
import com.baeldung.pojo.Customer;
|
||||
import com.baeldung.utility.SimpleDataGeneration;
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import java.io.FileReader;
|
||||
import java.io.IOException;
|
||||
|
||||
public class XmlToObjectTest {
|
||||
|
||||
private XStream xstream = null;
|
||||
|
||||
@Before
|
||||
public void dataSetup() {
|
||||
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
|
||||
xstream = simpleXstreamInitializer.getXstreamInstance();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertXmlToObjectFromFile() {
|
||||
try {
|
||||
ClassLoader classLoader = getClass().getClassLoader();
|
||||
FileReader reader = new FileReader(classLoader.getResource("data-file.xml").getFile());
|
||||
Customer customer = (Customer) xstream.fromXML(reader);
|
||||
Assert.assertNotNull(customer);
|
||||
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void convertXmlToObjectFromString() {
|
||||
Customer customer = SimpleDataGeneration.generateData();
|
||||
String dataXml = xstream.toXML(customer);
|
||||
Customer convertedCustomer = (Customer) xstream.fromXML(dataXml);
|
||||
Assert.assertNotNull(convertedCustomer);
|
||||
}
|
||||
|
||||
|
||||
}
|
|
@ -0,0 +1,57 @@
|
|||
package com.baeldung.utility;
|
||||
|
||||
import com.baeldung.initializer.SimpleXstreamInitializer;
|
||||
import com.baeldung.pojo.AddressDetails;
|
||||
import com.baeldung.pojo.ContactDetails;
|
||||
import com.baeldung.pojo.Customer;
|
||||
import com.thoughtworks.xstream.XStream;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
public class XStreamSimpleXmlTest {
|
||||
|
||||
private Customer customer;
|
||||
private String dataXml;
|
||||
private XStream xstream;
|
||||
|
||||
@Before
|
||||
public void dataSetup() {
|
||||
customer = SimpleDataGeneration.generateData();
|
||||
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
|
||||
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);
|
||||
}
|
||||
|
||||
@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"));
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
<customer>
|
||||
<firstName>XStream</firstName>
|
||||
<lastName>Java</lastName>
|
||||
<dob>1986-02-14 04:14:05.874 UTC</dob>
|
||||
<contactDetailsList>
|
||||
<ContactDetails contactType="Home">
|
||||
<mobile>6673543265</mobile>
|
||||
<landline>0124-2460311</landline>
|
||||
</ContactDetails>
|
||||
<ContactDetails contactType="Office">
|
||||
<mobile>4676543565</mobile>
|
||||
<landline>0120-223312</landline>
|
||||
</ContactDetails>
|
||||
</contactDetailsList>
|
||||
</customer>
|
|
@ -0,0 +1,5 @@
|
|||
<customer>
|
||||
<fn>XStream</fn>
|
||||
<lastName>Java</lastName>
|
||||
<dob>1986-02-14 03:46:16.381 UTC</dob>
|
||||
</customer>
|
|
@ -0,0 +1,13 @@
|
|||
<customer>
|
||||
<firstName>XStream</firstName>
|
||||
<lastName>Java</lastName>
|
||||
<dob>1986-02-14 04:14:20.541 UTC</dob>
|
||||
<ContactDetails contactType="Home">
|
||||
<mobile>6673543265</mobile>
|
||||
<landline>0124-2460311</landline>
|
||||
</ContactDetails>
|
||||
<ContactDetails contactType="Office">
|
||||
<mobile>4676543565</mobile>
|
||||
<landline>0120-223312</landline>
|
||||
</ContactDetails>
|
||||
</customer>
|
|
@ -0,0 +1,5 @@
|
|||
<customer>
|
||||
<firstName>XStream</firstName>
|
||||
<lastName>Java</lastName>
|
||||
<dob>1986-02-14 03:46:16.381 UTC</dob>
|
||||
</customer>
|
|
@ -0,0 +1,6 @@
|
|||
<customer>
|
||||
<firstName>XStream</firstName>
|
||||
<lastName>Java</lastName>
|
||||
<dob>1986-02-14 04:14:20.541 UTC</dob>
|
||||
<fullName>XStream Java</fullName>
|
||||
</customer>
|
|
@ -0,0 +1,5 @@
|
|||
<com.baeldung.pojo.Customer>
|
||||
<firstName>XStream</firstName>
|
||||
<lastName>Java</lastName>
|
||||
<dob>1986-02-14 03:46:16.381 UTC</dob>
|
||||
</com.baeldung.pojo.Customer>
|
Loading…
Reference in New Issue