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 .. 23 24 25 26 27 28 < 29 > 30 31 32 33 34 35 .. 166 >> Next

¦¦ Enable all assertions:
java -ea org.javapitfalls.item7.MyClass
¦¦ Enable system assertions only:
java -esa org.javapitfalls.item7.MyClass
My Assertions Are Not Gratuitous! 65
¦¦ Enable all assertions in the org.javapitfalls package and its sub-packages:
java -ea:org.javapitfalls org.javapitfalls.item7.MyClass
¦¦ Enable all assertions in the org.javapitfalls package and its sub-packages, but disable the ones in AnotherAssertionExample:
java -ea:org.javapitfalls -da:
org.javapitfalls.item7.AnotherAssertionExample
org.javapitfalls.item7.MyClass
Also, there are situations where you want to require that assertions be enabled in your class. An example would be if you had some safety-sensitive class that should operate only if the assertions are true (in addition to normal control checking). Listing 7.3 shows our previous example with assertions always on.
01 package org.javapitfalls.item7;
02
03 public class AnotherAssertionExample {
04
05 static {
06 boolean assertions = false;
07 assert assertions = true;
08
09 if (assertions==false)
10 throw new RuntimeException(' You must enable assertions 2
to use this class.");
11 }
12
13 private double taxRate;
14 // [...] remaining code Identical to listing 7.2
Listing 7.3 AnotherAssertionExample.java (modified)
So, if you run this example without assertions enabled, you receive the following message:
C:\pitfallsBook\#7>java -cp . 2
org.javapitfalls.item7.AnotherAssertionExample
Exception in thread "main" java.lang.ExceptionlnlnitializerError Caused by: java.lang.RuntimeException: You must enable assertions to use this class.
at org.javapitfalls.item7.AnotherAssertionExample.<clinit>(Unknown
Source)
66 Item 8
In closing, there are a few rules to follow in dealing with assertions:
¦¦ DO use assertions to test postconditions on methods.
¦¦ DO use assertions to test places where you believe control flow should not execute. ¦¦ DO NOT use assertions to test preconditions on public methods.
¦¦ DO use assertions to test preconditions on helper methods.
¦¦ DO NOT use assertions that affect the normal operation of the code.
Item 8: The Wrong Way to Search a DOM2
All well-formed XML files have a tree structure. For example, Listing 8.1 can be represented by a tree with two ADDRESS nodes:
01: <?xml version=”1.0”?>
02: <!DOCTYPE ADDRESS_BOOK SYSTEM "abml.dtd”>
03: <ADDRESS_BOOK>
04 : <ADDRESS>
05: <NAME>Joe Jones </NAME>
06: <STREET>4332 Sunny Hill Road </STREET>
07 : <CITY>Fairfax</CITY>
08 : < STATE>VA</STATE>
09 : <ZIP>21220</ZIP>
10 : </ADDRESS>
11 : <ADDRESS>
12 : <NAME>Sterling Software </NAME>
13: <STREET> 7900 Sudley Road</STREET>
14 : <STREET> Suite 500</STREET>
15 : <CITY>Manassas</CITY>
16 : <STATE>VA </STATE>
17 : <ZIP>20109 </ZIP>
18 : </ADDRESS>
19: </ADDRESS_BOOK>
Listing 8.1 myaddresses.xml
The pitfall is assuming the DOM tree will look exactly like your mental picture of the XML document. Let's say we have a task to find the first NAME element of the first ADDRESS. By looking at Listing 8.1, you may say that the third node in the DOM (line 05) is the one we want. Listing 8.2 attempts to find the node in that way.
2 This pitfall was first printed by JavaWorld (www.javaworld.com) in the article, "An API's looks can be deceiving", June 2001, (http://www.javaworld.com/javaworld/jw-06-2001/jw-0622-traps.html?) and is reprinted here with permission. The pitfall has been updated from reader feedback.
The Wrong Way to Search a DOM 67
01 package 8 m e t i s l l a LM t i p a v a j g. r o
02
03 import javax.xml.parsers.*;
04 import java.io.*;
05 import org.w3c.dom.*;
06
07 public class BadDomLookup
08 {
09 public static void main(String args[])
10 {
11 try
12 {
13 if (args.length < 1)
14 {
15 System.out.println("USAGE: " +
16 "org.javapitfalls.item8.BadDomLookup xmlfile");
17 System.exit(1);
18 }
19
20 DocumentBuilderFactory dbf =
21 DocumentBuilderFactory.newInstance();
22 DocumentBuilder db = dbf.newDocumentBuilder();
23 Document doc = db.parse(new File(args[0]));
24
25 // get first Name of first Address
26 NodeList nl = doc.getElementsByTagName("ADDRESS") ;
27 int count = nl.getLength();
28 System.out.println("# of \"ADDRESS\" elements: " + count);
29
30 if (count > 0)
31 {
32 Node n = nl.item(0);
33 System.out.println("This node name is: " +
n.getNodeName());
34 // get the NAME node of this ADDRESS node
35 Node nameNode = n.getFirstChild();
36 System.out.println("This node name is: "
37 + nameNode.getNodeName()) ;
38 }
39 } catch (Throwable t)
40 {
41 t.printStackTrace();
42 }
43 }
44 }
45
Listing 8.2 BadDomLookup.java
68 Item 8
The simple program, BadDomLookup, uses the Java API for XML Processing (JAXP) to parse the DOM (this example was tested with both Xerces and Sun's default JAXP parser). After we get the W3C Document object, we retrieve a NodeList of ADDRESS elements (line 26) and then look to get the first NAME element by accessing the first child under ADDRESS (line 35).
Previous << 1 .. 23 24 25 26 27 28 < 29 > 30 31 32 33 34 35 .. 166 >> Next