23.07.2013 Views

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

Java IO.pdf - Nguyen Dang Binh

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

<strong>Java</strong> I/O<br />

For example, valid XML documents are essentially trees of elements combined with a<br />

document type definition (DTD). The DTD defines a grammar the document must follow. [3]<br />

The Document Object Model (DOM) defines a means of representing XML (and HTML)<br />

documents as instances of <strong>Java</strong> classes and interfaces, including XMLNode, EntityReference,<br />

EntityDeclaration , DocumentType, ElementDefinition , AttributeDefinition , and<br />

others.<br />

An XML document could be saved as a set of these serialized objects. In that case, when you<br />

deserialized the document, you would want to check that the deserialized document is still<br />

valid; that is, that the document adheres to the grammar given in the DTD. You can't do this<br />

until the entire document—all its elements, and its entire DTD—has been read. There are also<br />

a number of smaller checks you might want to perform. For instance, well-formedness (wellformedness<br />

is a slightly less stringent requirement than validity) requires that all entity<br />

references like &date; be defined in the DTD. To check this, it's not enough to have<br />

deserialized the EntityReference object. You must also have deserialized the corresponding<br />

DocumentType object that contains the necessary EntityDeclaration objects.<br />

You can use the ObjectInputStream class's registerValidation() method to specify an<br />

ObjectInputValidation object that will be notified of the object after its entire graph has<br />

been reconstructed but before readObject() has returned it. This gives the validator an<br />

opportunity to make sure that the object doesn't violate any implicit assertions about the state<br />

of the system.<br />

public synchronized void registerValidation(ObjectInputValidation oiv,<br />

int priority) throws NotActiveException, InvalidObjectException<br />

This method is invoked inside the readObject() method of the object that needs to be<br />

validated. Every time the readObject() method is called to read an object, that object is<br />

registered with the stream as needing to be validated when the rest of the graph is available.<br />

Invoking the registerValidation() method from anywhere except the readObject()<br />

method throws a NotActiveException. The oiv argument is the object that implements the<br />

ObjectInputValidation interface and that will validate deserialized objects. Most of the<br />

time, this is the object that has the readObject() method; that is, objects tend to validate<br />

themselves. The priority argument determines the order in which objects will be validated<br />

if there's more than one registered ObjectInputValidation object for the class. Validators<br />

with higher priorities are invoked first.<br />

The ObjectInputValidation interface declares a single method, validateObject():<br />

public abstract void validateObject() throws InvalidObjectException<br />

If the object is invalid, validateObject() throws an InvalidObjectException.<br />

For example, let's suppose an application maintains a hashtable of Person objects, each of<br />

which is identified primarily by its Social Security Number. Let's further suppose that the<br />

application doesn't allow two Person objects with the same Social Security Number to exist<br />

at the same time. You can use an ObjectInputValidation to ensure that this doesn't happen.<br />

Example 11.6 demonstrates.<br />

3 For more details, see XML: Extensible Markup Language, by Elliotte Rusty Harold, IDG Books, 1998.<br />

262

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!