XStream-Introduction

Merging changes for Article XML - to Object
This commit is contained in:
ankur-singhal 2016-04-04 11:27:07 +05:30
parent b55bc6206c
commit dd61036a31
29 changed files with 14 additions and 159 deletions

View File

@ -4,16 +4,16 @@ import com.thoughtworks.xstream.XStream;
public class SimpleXstreamInitializer {
private static XStream xstreamInstance;
private XStream xtreamInstance;
public static XStream getXstreamInstance() {
if (xstreamInstance == null) {
public XStream getXstreamInstance() {
if (xtreamInstance == null) {
synchronized (SimpleXstreamInitializer.class) {
if (xstreamInstance == null) {
xstreamInstance = new XStream();
if (xtreamInstance == null) {
xtreamInstance = new XStream();
}
}
}
return xstreamInstance;
return xtreamInstance;
}
}

View File

@ -5,12 +5,10 @@ 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;
@ -52,4 +50,8 @@ public class Customer {
this.contactDetailsList = contactDetailsList;
}
@Override
public String toString() {
return "Customer [firstName=" + firstName + ", lastName=" + lastName + ", dob=" + dob + "]";
}
}

View File

@ -30,7 +30,7 @@ public class ComplexXmlToObjectCollectionTest {
Customer customer = (Customer) xstream.fromXML(reader);
Assert.assertNotNull(customer);
Assert.assertNotNull(customer.getContactDetailsList());
System.out.println(customer);
//System.out.println(customer);
} catch (IOException e) {
e.printStackTrace();

View File

@ -30,7 +30,7 @@ public class XmlToObjectIgnoreFieldsTest {
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);
//System.out.println(customer);
} catch (IOException e) {
e.printStackTrace();
}

View File

@ -22,7 +22,8 @@ public class XStreamSimpleXmlTest {
@Before
public void dataSetup() {
customer = SimpleDataGeneration.generateData();
xstream = SimpleXstreamInitializer.getXstreamInstance();
SimpleXstreamInitializer simpleXstreamInitializer = new SimpleXstreamInitializer();
xstream = simpleXstreamInitializer.getXstreamInstance();
xstream.processAnnotations(Customer.class);
xstream.processAnnotations(AddressDetails.class);
xstream.processAnnotations(ContactDetails.class);
@ -30,9 +31,7 @@ public class XStreamSimpleXmlTest {
xstream.registerConverter(new MyDateConverter());
// xstream.registerConverter(new MySingleValueConverter());
xstream.aliasField("fn" , Customer.class , "firstName");
dataXml = xstream.toXML(customer);
System.out.println(dataXml);
}
@Test

View File

@ -1,14 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>xstream-xmlToObject</name>
<comment>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

@ -1,36 +0,0 @@
<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>com.baeldung</groupId>
<artifactId>xstream-xmlToObject</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>xstream-xmlToObject</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
<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

@ -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;
}
}

View File

@ -1,42 +0,0 @@
package com.baeldung.pojo;
import java.util.Date;
public class Customer {
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 + "]";
}
}

View File

@ -1,19 +0,0 @@
package com.baeldung.utility;
import java.util.Calendar;
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");
return customer;
}
}

View File

@ -1,16 +0,0 @@
# 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