Books
in black and white
Main menu
Share a book About us Home
Books
Biology Business Chemistry Computers Culture Economics Fiction Games Guide History Management Mathematical Medicine Mental Fitnes Physics Psychology Scince Sport Technics
Ads

More Java Pitfalls Share Reactor - Daconta M,C.

Daconta M,C. More Java Pitfalls Share Reactor - Wiley publishing, 2003. - 476 p.
ISBN: 0-471-23751-5
Download (direct link): morejavapitfallssharereactor2003.pdf
Previous << 1 .. 27 28 29 30 31 32 < 33 > 34 35 36 37 38 39 .. 166 >> Next

The part of the DOM specification that solves our dilemma is the specification to Load and Save a DOM. Table 9.2 details the interfaces defined in the specification. It is important to note that JAXP will even change its method for bootstrapping parsers, since its current method is slightly different than the DOM Level 3 load interfaces (specifically, DOMBuilder versus DocumentBuilder).
The Xerces parser implements the DOM Level 3 specification. Listing 9.2 uses the Xerces parser to demonstrate both the loading and saving of a DOM via the DOM Level 3 standard.
01
02
03
04
05
06
package org.javapitfalls.item9;
import org.apache.xerces.jaxp.*; import org.apache.xerces.dom3.ls.*;
import org.apache.xerces.dom.DOMImplementationlmpl; import org.apache.xerces.dom3.ls.DOMImplementationLS;
Listing 9.2 XercesSave.java (continued)
78 Item 9
07
08 import javax.xml.parsers.*;
09 import java.io.*;
10 import org.w3c.dom.*;
11
12 class XercesSave
13 {
14 public static void main(String args[])
15 {
16 try
17 {
// ... command line check omitted for brevity ...
24
25 // Xerces 2 implements DOM Level 3
26 // get DOM implementation
27 DOMImplementationLS domlmpl =
28 (DOMImplementationLS) 2
DOMImplementationImpl.getDOMImplementation();
29
30 // Create a DOM Level 3 - DOMBuilder
31 DOMBuilder db =
32 domImpl.createDOMBuilder( 2
DOMImplementationLS.MODE_SYNCHRONOUS);
33 DOMInputSource dis = domImpl.createDOMInputSource();
34 dis.setByteStream(new FileInputStream(args[0]));
35 Document doc = db.parse(dis);
36
37 // save to output file
38 FileOutputStream fos = new FileOutputStream(args[1]);
39
40 // create a DOM Writer
41 DOMWriter writer = domImpl.createDOMWriter();
42 writer.writeNode(fos, doc);
43
44 fos.close();
45 } catch (Throwable t)
46 {
47 t.printStackTrace();
48 }
49 }
50 }
51
Listing 9.2 (continued)
XercesSave.java uses the "LS" (which stands for Load and Save) version of the DOMImplemenation to create the DOMBuilder (line 31) and DOMWriter (line 41)
The Saving-a-DOM Dilemma 79
objects. Once created, the DOMBuilder creates the DOM via the parse() method, and the DOMWriter saves the DOM via the writeNode() method. The writeNode() method can write either all or part of a DOM.
One final implementation worth mentioning is the load and save operations in the Java Document Object Model (JDOM). JDOM is a reimplementation of DOM for Java and is optimized for seamless integration into the Java platform. JDOM stresses ease of use for Java developers, whereas the W3C DOM is designed to be language-neutral (specified in CORBAIDL) and then provides bindings to specific languages. Of all the examples in this item, the JDOM implementation is the simplest; however, its functionality often lags behind the W3C DOM; its Document class is not a subclass of the W3C Document class and is thus incompatible with third-party software that expects a W3C DOM as an argument (although conversion is provided). Listing 9.3 demonstrates the load and save operations in JDOM.
01 package org.javapitfalls.item9;
02
03 import org.jdom.*;
04 import org.jdom.input.*;
05 import org.jdom.output.*;
06 import java.io.*;
07
08 class JdomSave
09 {
10 public static void main(String args[])
11 {
12 try
13 {
// ... command line check omitted for brevity ...
20
21 // load the document
22 DOMBuilder db = new DOMBuilder();
23 Document doc = db.build(new File(args[0]));
24
25 // save to output file
26 FileOutputStream fos = new FileOutputStream(args[1]);
27 XMLOutputter xout = new XMLOutputter();
28 xout.output(doc, fos);
29 fos.close();
30 } catch (Throwable t)
31 {
32 t.printStackTrace();
33 }
34 }
35 }
36
Listing 9.3 JdomSave.java
80 Item 10
Like the W3C Load and Save specification, JDOM also uses a DOMBuilder class (but bootstraps it differently) and then builds an org.jdom.Document (line 23). It is important to note that a JDOM Document is NOT a W3C Document. To save the JDOM Document, an XMLOutputterclass is instantiated that can output() (line 28) a document.
All three implementations of the program (JaxpSave, XercesSave, and JdomSave) produce nearly identical outputs, and thus it is not necessary to list them here. In conclusion, at this time of rapid evolution of the DOM, the safest bet is to align your code to the W3C standards and implementations that follow them. Thus, to save a DOM, the Xerces implementation is currently the best choice.
Item 10: Mouse Button Portability
Unfortunately for cross-platform computing, all computer mice are not created equal. There are one-button mice, two-button mice, three-button mice, and two-button-with-mouse-wheel mice. Like the AWT, to cover all of these options, Java initially took a least common denominator (LCD) approach that supported receiving a single mouse event and using modifiers and modifier keys to differentiate between the different types. A second problem Java programmers encounter regarding the mouse is that the Java platform evolves its mouse support with each major release, adding support for new features (like mouse wheels) and new convenience methods. Table 10.1 lists the interfaces and methods for capturing mouse events.
Previous << 1 .. 27 28 29 30 31 32 < 33 > 34 35 36 37 38 39 .. 166 >> Next