KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > firstpartners > nounit > utility > XmlUtil


1 package net.firstpartners.nounit.utility;
2
3 /**
4  * Title: NoUnit - Identify Classes that are not being unit Tested
5  *
6  * Copyright (C) 2001 Paul Browne , FirstPartners.net
7  *
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22  *
23  * @author Paul Browne
24  * @version 0.7
25  */

26
27 import java.util.HashMap JavaDoc;
28 import java.util.HashSet JavaDoc;
29 import java.util.List JavaDoc;
30 import java.util.ListIterator JavaDoc;
31 import java.util.Vector JavaDoc;
32
33 import org.jdom.Attribute;
34 import org.jdom.Document;
35 import org.jdom.Element;
36
37 /**
38  * General XML Utility Functions
39  */

40 public class XmlUtil {
41     
42   /**
43    * indexes the nodes in the document that is passed in , via a HashMap mapping
44    * mapping is in the format <index> as String , handle to <element> of node<BR>
45    * Strings are used as they are better lookup in the hashmap.
46    * @param inXmlDocument to generated the hashmap from
47    * @param uniqueAttribute to do the index on (i.e. key in HashMap). Examples
48    * of uniqueAttributes are id's or names.
49    * @return HashMap containing mappings
50    */

51   public static HashMap JavaDoc getNodeIndex(Document inXmlDocument,
52                                         String JavaDoc uniqueAttribute) {
53
54     //Internal Variables
55
int stackPointer=0;
56     String JavaDoc locationId =null;
57     Attribute tmpAttribute=null;
58     Element thisElement=null;
59     ListIterator JavaDoc deepestList=null;
60
61     HashMap JavaDoc mappings = new HashMap JavaDoc();
62     List JavaDoc stack = new Vector JavaDoc();
63
64     //Get the list information for the entire document
65
stack.add(inXmlDocument.getContent().listIterator());
66
67     //Loop though the elements on list
68
while (!stack.isEmpty()){
69
70       //Get the last list on the stack
71
deepestList = (ListIterator JavaDoc)stack.get(stack.size()-1);
72
73       //Does this list have more elements?
74
if (deepestList.hasNext()) {
75
76         //if so Get Next element from this list
77
thisElement = (Element)deepestList.next();
78
79         //Add Mapping for this element to hashtable
80
tmpAttribute = thisElement.getAttribute(uniqueAttribute);
81
82         //Attibute can be null for non folder elements (e.g. root element) - if so ignore
83
if (tmpAttribute!=null) {
84           locationId= tmpAttribute.getValue();
85           if ((locationId!=null)&&(locationId!="")) {
86              mappings.put(locationId.toString(),thisElement);
87
88           }
89         } //end add mapping
90

91         //does this list have children ?
92
if(thisElement.hasChildren()){
93
94           //if so add to the stack
95
stackPointer++;
96           stack.add(thisElement.getChildren().listIterator());
97         }
98       }
99       else
100       {
101         //if not , remove this list from the stack
102
stack.remove(stackPointer);
103         stackPointer--;
104
105       } // end if stack has more elements
106

107     }
108
109     return mappings;
110   }
111
112   /**
113    * gets all elements in the XML Document Being Passed in <BR>
114    * @param inXmlDocument to generated the hashmap from
115    * @return nodeList containing nodes
116    */

117   public static HashSet JavaDoc getAllNodes(Document inXmlDocument) {
118
119     //Internal Variables
120
int stackPointer=0;
121     int index=1;
122     String JavaDoc locationId=null;
123     Element currentElement=null;
124     Element parentElement=null;
125     Element thisElement=null;
126     Attribute tmpAttribute=null;
127     List JavaDoc elementList=null;
128     ListIterator JavaDoc deepestList=null;
129
130     HashMap JavaDoc mappings = new HashMap JavaDoc();
131     HashSet JavaDoc nodeList = new HashSet JavaDoc();
132     List JavaDoc stack = new Vector JavaDoc(); //Implements list interface
133

134     //Get the list information for the entire document - kick start loop
135
stack.add(inXmlDocument.getContent().listIterator());
136
137     //Loop though the elements on list
138
while (!stack.isEmpty()){
139
140       //Get the last list on the stack
141
deepestList = (ListIterator JavaDoc)stack.get(stack.size()-1);
142
143       //Does this list have more elements?
144
if (deepestList.hasNext()) {
145
146         //if so Get Next element from this list
147
thisElement = (Element)deepestList.next();
148
149         // add this element to the list
150
nodeList.add(thisElement);
151  
152         //does this list have children ?
153
if(thisElement.hasChildren()){
154
155           //if so add to the stack
156
stackPointer++;
157           stack.add(thisElement.getChildren().listIterator());
158         }
159       }
160       else
161       {
162         //if not , remove this list from the stack
163
stack.remove(stackPointer);
164         stackPointer--;
165
166       } // end if stack has more elements
167

168     }
169
170     return nodeList;
171   }
172   
173   /**
174    * Search for Nodes within Jdom Document<BR>
175    * @param inDocumentToSearch XML-JDOM Document
176    * @param uniqueIdentifierName we can use to index the document (unique
177    * attribute like id or name present on the node we are searching for)
178    * @param uniqueIdentifierToFind in the indexed document
179    */

180    public static Element findNode(Document inDocumentToSearch
181                             , String JavaDoc uniqueIdentifierName
182                             , String JavaDoc uniqueIdentifierToFind) {
183        
184         // index document
185
HashMap JavaDoc index = getNodeIndex(inDocumentToSearch,
186                                      uniqueIdentifierName);
187                                         
188         // Now get required element from index
189
return (Element)index.get(uniqueIdentifierToFind);
190                                 
191                         
192    }
193 }
194
Popular Tags