Lab Sheet 7 Serialization

rangesatanskingdomSoftware and s/w Development

Dec 2, 2013 (3 years and 4 months ago)

71 views

Lab Sheet 7


Serialization


The goal of this lab will be to explore various options for serializing/deserializing the
Address Book as a way to implement its import/export functionality..


Part 1
-

Basic Serialization


You may have already explored this opt
ion in previous labs, but in any case, this is the
time to look into using the Java Serialization mechanism to export and import the
Address Book… Make the AddressBook class (and anything else that need be!)
implement Serializable, and reimplement the impo
rt and export methods to make use of
it. The JUnit tests that you wrote for lab 6 to test import/export will probably not be
affected by the changes if the method signatures of import/export are the same (that is the
beauty of regression testing with JUnit
!) and should still be provided to evaluate the
correctness of the implementation.



Part 2
-

Serialization with XML


Now let’s use XML to serialize our Address Book... To do so, we first need to find an
XML Parser…


In Eclipse you might actually have a cho
ice between two SAX Parsers! If the Java SDK
version is 1.4 or up, it comes with the JAXP library, which can simply be found by
importing the org.xml.sax.* packages. If not, you can also use the Xerces parser, which is
bundled with Eclipse. In the latter c
ase, you need to add it to your “build path”, i.e. tell
the Java compiler where to find the code for that framework. Just like you did for JUnit,
you’d need to:

1.

Open the project that contains your Address Book application

2.

Right
-
click on the project name an
d select “Properties”

3.

Select the “Libraries” tab.

4.

Click on “Add External JARs” (JARs are like Zip files for Java classes)

5.

Look for a file called “xmlParserAPIs.jar”, which should be in a folder called
“org.apache.xerces_4.0.7” (or some more recent version
), itself contained in a folder
called “plugins”, itself contained in the “eclipse” folder. The location might be
different on your installation of Eclipse, but I am sure you will find it!

6.

Select the JAR file, click open and you should be ready to go!


The

next step is to actually come up with an XML format for the Address Book. The
simplest idea is to have an element called AddressBook, have BuddyInfo and other
instance variables of AddressBook nested as subelements of AddressBook, and finally
the instance

variables of BuddyInfo nested as subelements of BuddyInfo…


Since we do not want to rewrite the import/export methods again as we are happy with
the Java Serialization solution from Part 1, let’s define the following methods:

-

public String toXML(): will s
hadow the toString() methods already defined for
AddressBook and BuddyInfo except that the output will be in the XML format
that you have defined.

-

exportToXmlFile() and importFromXmlFile() (complete signature is left to you to
define!) provided in AddressB
ook.

-

you also need to handle some of the SAX events generated by the parser. One
place is to do so is the AddressBook class, which would then have to extend
HandlerBase…


Of course, the new import/export functionality should be demonstrated in new JUnit
t
ests!


Deliverables: the source code of the following classes: AddressBookTest,
BuddyInfoTest, and the corresponding AddressBook and BuddyInfo. Make sure that your
code is properly commented!


Submit your files on the course WebCT page.
Deadline: Tuesday
March 2nd, 5:30pm.