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 .. 40 41 42 43 44 45 < 46 > 47 48 49 50 51 52 .. 166 >> Next

06 <topic value = "4" text = "J2SE">
07 <category>2611</category>
08 </topic>
09 <topic value = "5" text = "J2EE">
10 <category>2612</category>
11 </topic>
12 <topic value = "6" text = "J2ME">
13 <category>2613</category>
14 </topic>
<!- - ... some topics omitted for brevity ... -- >
32 </topic>
33 <topic value = "13" text = "Network Technology">
34 <category>2612</category>
35 <category>2700</category>
46 </topic>
47 <topic value = "22" text = "Hardware">
48 <topic value = "23" text = "Server Technology">
49 <category>1511</category>
50 </topic>
51 <topic value = "24" text = "Desktop Technology">
52 <category>1512</category>
53 </topic>
54 <topic value = "25" text = "Wireless Technology">
55 <category>1513</category>
56 </topic>
57 <... >
58 </taxonomy>
59 </navigation>
Listing 14.1 Navigation.xml
114 Item 14
Listing 14.2 demonstrates the code that is used to process the Navigation.xml file for supporting the Web site. It has been stripped down to show the necessary methods and the one of interest (listCategories).
001 package org.javapitfalls;
002
003 import java.io.*;
004 import java.net.*;
005 import java.util.*;
006 import org.dom4j.Document;
// . .. some dom4j Imports removed for brevity ... Code available on Web
site
014
015 public class BadNavigationUtils {
017 private Document document;
018
019 public BadNavigationUtils () {
020 try {
021 setFile(getClass().getResource("/Navigation.xml"));
022 setDates();
023 } catch (Exception e) { e.printStackTrace(); }
024 }
025
026 public void setFile ( String path ) throws DocumentException {
027 SAXReader reader = new SAXReader();
028 document = reader.read(path);
029 }
030
031 public void setFile ( URL path ) throws DocumentException {
032 SAXReader reader = new SAXReader();
033 document = reader.read(path);
034 }
035
036
037 public void writeFile (String path) {
039 try {
040
041 // write to a file
042 XMLWriter writer = new XMLWriter(
043 new FileWriter( path ), 2
OutputFormat.createPrettyPrint() );
044 writer.write( document );
045 writer.close();
046
047 } catch (IOException ioe ) {
048
049 ioe.printStackTrace();
051 }
Listing 14.2 BadNavigationUtils.java
What Do You Collect? 115
052 053 054 }
055 public List listCategories (String[] topics) {
057 HashSet mySet = new HashSet();
060 for (int i=0; i < topics.length - 1; i++) {
062 List list = document.selectNodes( "//topic[@value='" + 2
topics[i] + "]/category" );
064 mySet.addAll(list);
066 067 }
068 ArrayList theList = new ArrayList();
070 theList.addAll(mySet);
073 074 return theList;
075 076 }
077 public static void main(String[] args) {
079 080 try {
081 String [] topics = new String[3];
083 topics[0] = "4";
084 topics[1] = "5";
085 086 topics[2] = "13";
087 (); 089 BadNavigationUtils topicCategory = new BadNavigationUtils 2
topicCategory.setFile("Navigation.xml");
091 List categories = topicCategory.listCategories(topics);
092 for ( int i = 0; i < categories.size(); i++ ) {
093 Element myElement = (Element) categories.get(i);
094 System.out.println(myElement.getText());
096 }
099 } catch (Exception e) { e.printStackTrace();}
101 }
102 103 }
Listing 14.2 BadNavigationUtils.java (continued)
BadNavigationUtils.java uses DOM4J to parse an XML file into a DOM tree, and then uses an XPath expression to pull back a List of category nodes (in the listCat-egories method). This is the result of executing this code:
01: 2611 02: 2612 03: 2612
116 Item 14
04: 2700
05: 2710
06: 2711
07: 2712
08: 2713
09: 2714
10: 2715
11: 2720
12: 2730
13: 2740
14: 2750
The idea was to filter out the duplicates in the list, but it is clear that this didn't work. The category "2612" is shown twice. Why didn't this work? After all, HashSet is supposed to not allow duplicates in the collection.
As it turns out, this method returns a list of nodes to the user. Those nodes represent the location of that particular element on the tree. Therefore, there is a distinction between the 2612 in the topic with id of 5 and the 2612 in the topic with id of 13. When we print out the text of each of the nodes, we find that there are duplicate values.
So, if we want to make sure we have a true Set, we need to pay more careful attention to the type of object being stored in the collection. To handle this problem, we modify the code to actually store the text value of the nodes. Listing 14.3 shows how we do that.
01 public List listCategories (String[] topics) {
03 TreeSet mySet = new TreeSet();
06 for (int i=0; i < topics.length - 1; i++) {
08 List list = document.selectNodes( "//topic[@value='" + 2
topics[i] + "]/descendant-or-self::*/category" );
09
10 { 11 for ( Iterator it = list.listIterator(); it.hasNext(); )
mySet.add( ((Element) it.next()).getText() );
12 }
14 }
15 16 ArrayList theList = new ArrayList();
Previous << 1 .. 40 41 42 43 44 45 < 46 > 47 48 49 50 51 52 .. 166 >> Next