001: package net.firstpartners.nounit.utility;
002:
003: /**
004: * Title: NoUnit - Identify Classes that are not being unit Tested
005: *
006: * Copyright (C) 2001 Paul Browne , FirstPartners.net
007: *
008: *
009: * This program is free software; you can redistribute it and/or
010: * modify it under the terms of the GNU General Public License
011: * as published by the Free Software Foundation; either version 2
012: * of the License, or (at your option) any later version.
013: *
014: * This program is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
017: * GNU General Public License for more details.
018: *
019: * You should have received a copy of the GNU General Public License
020: * along with this program; if not, write to the Free Software
021: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
022: *
023: * @author Paul Browne
024: * @version 0.7
025: */
026:
027: import java.util.HashMap;
028: import java.util.HashSet;
029: import java.util.List;
030: import java.util.ListIterator;
031: import java.util.Vector;
032:
033: import org.jdom.Attribute;
034: import org.jdom.Document;
035: import org.jdom.Element;
036:
037: /**
038: * General XML Utility Functions
039: */
040: public class XmlUtil {
041:
042: /**
043: * indexes the nodes in the document that is passed in , via a HashMap mapping
044: * mapping is in the format <index> as String , handle to <element> of node<BR>
045: * Strings are used as they are better lookup in the hashmap.
046: * @param inXmlDocument to generated the hashmap from
047: * @param uniqueAttribute to do the index on (i.e. key in HashMap). Examples
048: * of uniqueAttributes are id's or names.
049: * @return HashMap containing mappings
050: */
051: public static HashMap getNodeIndex(Document inXmlDocument,
052: String uniqueAttribute) {
053:
054: //Internal Variables
055: int stackPointer = 0;
056: String locationId = null;
057: Attribute tmpAttribute = null;
058: Element this Element = null;
059: ListIterator deepestList = null;
060:
061: HashMap mappings = new HashMap();
062: List stack = new Vector();
063:
064: //Get the list information for the entire document
065: stack.add(inXmlDocument.getContent().listIterator());
066:
067: //Loop though the elements on list
068: while (!stack.isEmpty()) {
069:
070: //Get the last list on the stack
071: deepestList = (ListIterator) stack.get(stack.size() - 1);
072:
073: //Does this list have more elements?
074: if (deepestList.hasNext()) {
075:
076: //if so Get Next element from this list
077: this Element = (Element) deepestList.next();
078:
079: //Add Mapping for this element to hashtable
080: tmpAttribute = this Element
081: .getAttribute(uniqueAttribute);
082:
083: //Attibute can be null for non folder elements (e.g. root element) - if so ignore
084: if (tmpAttribute != null) {
085: locationId = tmpAttribute.getValue();
086: if ((locationId != null) && (locationId != "")) {
087: mappings
088: .put(locationId.toString(), this Element);
089:
090: }
091: } //end add mapping
092:
093: //does this list have children ?
094: if (this Element.hasChildren()) {
095:
096: //if so add to the stack
097: stackPointer++;
098: stack.add(this Element.getChildren().listIterator());
099: }
100: } else {
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 getAllNodes(Document inXmlDocument) {
118:
119: //Internal Variables
120: int stackPointer = 0;
121: int index = 1;
122: String locationId = null;
123: Element currentElement = null;
124: Element parentElement = null;
125: Element this Element = null;
126: Attribute tmpAttribute = null;
127: List elementList = null;
128: ListIterator deepestList = null;
129:
130: HashMap mappings = new HashMap();
131: HashSet nodeList = new HashSet();
132: List stack = new Vector(); //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) 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: this Element = (Element) deepestList.next();
148:
149: // add this element to the list
150: nodeList.add(this Element);
151:
152: //does this list have children ?
153: if (this Element.hasChildren()) {
154:
155: //if so add to the stack
156: stackPointer++;
157: stack.add(this Element.getChildren().listIterator());
158: }
159: } else {
160: //if not , remove this list from the stack
161: stack.remove(stackPointer);
162: stackPointer--;
163:
164: } // end if stack has more elements
165:
166: }
167:
168: return nodeList;
169: }
170:
171: /**
172: * Search for Nodes within Jdom Document<BR>
173: * @param inDocumentToSearch XML-JDOM Document
174: * @param uniqueIdentifierName we can use to index the document (unique
175: * attribute like id or name present on the node we are searching for)
176: * @param uniqueIdentifierToFind in the indexed document
177: */
178: public static Element findNode(Document inDocumentToSearch,
179: String uniqueIdentifierName, String uniqueIdentifierToFind) {
180:
181: // index document
182: HashMap index = getNodeIndex(inDocumentToSearch,
183: uniqueIdentifierName);
184:
185: // Now get required element from index
186: return (Element) index.get(uniqueIdentifierToFind);
187:
188: }
189: }
|