Java Reference
In-Depth Information
You have already defined a DTD in the previous chapter that is suitable for defining a sketch. The code to
store a sketch as an XML document instead of as a serialized object simply maps sketch elements to the
corresponding XML elements. These elements are child nodes for a document node representing the entire
sketch. You can create a Document object with a DocumentType node specifying sketcher.dtd as the DTD
via a DOMImplementation object from a DocumentBuilder object. You can do this with statements in a try
block:
Document doc = null;
try {
DocumentBuilderFactory builderFactory =
DocumentBuilderFactory.newInstance();
builderFactory.setNamespaceAware(true);
builderFactory.setValidating(true);
builderFactory.setIgnoringElementContentWhitespace(true);
Path dtdFile = Paths.get(System.getProperty("user.home")).
resolve("Beginning Java Stuff").resolve("sketcher.dtd");
DOMImplementation domImpl =
builderFactory.newDocumentBuilder().getDOMImplementation();
doc = domImpl.createDocument(null, "sketch", domImpl.createDocumentType(
"sketch", null,
dtdFile.toString()));
} catch(ParserConfigurationException e) {
e.printStackTrace();
// Display the error and terminate the current activity...
} catch(DOMException e) {
// Determine the kind of error from the error code,
// display the error, and terminate the current activity...
}
The first statement creates a DocumentBuilderFactory object. The factory object is set to be
namespace-aware and validating, so any document builder object you create validates the document against
the DTD. A DOMImplementation object is obtained and stored in domImpl . This is used in the next state-
ment to create the Document object for a sketch and its DocumentType object defining the DOCTYPE declara-
tion for sketcher.dtd . Eventually you add code like this to the SketcherFrame class, but I'm leaving that
to one side for the moment and looking at how you can fill out the detail of the Document object from the
objects representing elements in a sketch.
A sketch in XML is a simple two-level structure. The root node in an XML representation of a sketch
is a <sketch> element, and the child elements are XML elements defining sketch elements. To define the
complete XML structure you need only to add an org.w3c.dom.Element node as a child of the root node
for each element in the sketch. A good way to implement this would be to add a method to each of the sketch
Element inner classes that creates its own org.w3c.dom.Element node and adds it to the root node for a
Document object. This makes each object that encapsulates a sketch element able to create its own XML
representation.
You have to modify the Element class as well as its inner classes that define concrete Sketcher class ele-
ments. The inner classes are Element.Line , Element.Rectangle , Element.Circle , Element.Curve , and
Element.Text . The nodes that you must add for each kind of geometric element derive directly from the
declaration in the DTD, so it helps if you have this handy while you go through these classes. If you have it
as a file from when I discussed it in the last chapter, maybe you can print a copy.
Search WWH ::




Custom Search