Site Sponsors:
XML IO Using Generics - Before Java 1.7 
In as much as we often teach students who are using an older version of Java, we decided to put together another demonstration. -An example that can readily be targeted for earlier versions of Java.

Remember - the use of Java Serialization is NOT required. In order to use the XML serialization technique, all we need is a POJO!

First, the I/O Demonstration (note that checking for nulls (etc) has been omitted so as to enhance conceptual review):

package com.soft9000.file;

import java.beans.XMLDecoder;
import java.beans.XMLEncoder;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;

public class XmlClassIO<T> {
public String sLastError = "";

public boolean write(String fileName, T ref) {
try {
XMLEncoder encoder = new XMLEncoder(new BufferedOutputStream(
new FileOutputStream(fileName)));
encoder.writeObject(ref);
encoder.close();
return true;
} catch (Exception ex) {
this.sLastError = ex.getMessage();
return false;
}
}

public T read(String fileName) {
try {
XMLDecoder decoder = new XMLDecoder(new BufferedInputStream(
new FileInputStream(fileName)));
T result = (T) decoder.readObject();
decoder.close();
return result;
} catch (Exception ex) {
this.sLastError = ex.getMessage();
return null;
}
}
}

Next, the testable class:

package com.soft9000.file;

import java.util.List;
import java.util.ArrayList;

public class BigBadFoo {

private ArrayList<String> list = new ArrayList<String>();
private String foo;

public BigBadFoo() {

}

public void setList(List<String> someList) {
this.list = new ArrayList<String>(someList);
}

public List<String> getList() {
return this.list;
}

public void setFoo(String string) {
this.foo = string;
}

public String getFoo() {
return this.foo;
}

public boolean equals(Object obj) {
if (obj instanceof BigBadFoo) {
if (super.equals(obj))
return true;
BigBadFoo ref = (BigBadFoo) obj;
if (foo.equals(ref.foo) == true) {
return list.equals(ref.list);
}
}
return false;
}

}

Finally, the test case:

package com.soft9000.file;

import java.util.ArrayList;

/**
* Proof of concept.
*
* @author Profnagy
*
*/
public class Main {

public static void main(String[] args) {
ArrayList<String> SomeList = new ArrayList();
SomeList.add("one");
SomeList.add("two");
SomeList.add("three");
BigBadFoo foo1 = new BigBadFoo();
foo1.setFoo("12345");
foo1.setList(SomeList);
com.soft9000.file.XmlClassIO<BigBadFoo> io = new XmlClassIO<BigBadFoo>();
io.write("test.xml", foo1);

BigBadFoo foo2 = io.read("test.xml");
if(foo2.equals(foo1) == false) {
System.out.println("Serialization Failure.");
System.exit(-1);
}
System.out.println("Serialization Success.");
}
}

Notes


The above has been designed to be cut-and-pasted.

Be sure to re-format the code after doing so.


Enjoy,

-Rn

Student Note: Regression


While fine for illustrative purposes, over time things can change.

In as much as we might want to be sure that things actually break when they should, it is always nice to add-in a few "should break" tests, as well. Such will help us a-lot as we perform feature regression tests, over time:

Here is a simple example:
      
foo2.setFoo("shazam");
if(foo2.equals(foo1) == true) {
System.out.println("Assignment/Equality Regression.");
System.exit(-1);
}


[ add comment ] ( 2093 views )   |  permalink  |  related link

<<First <Back | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | Next> Last>>