1 package net.firstpartners.nounit.utility; 2 3 26 27 import java.util.HashMap ; 28 import java.util.HashSet ; 29 import java.util.List ; 30 import java.util.ListIterator ; 31 import java.util.Vector ; 32 33 import org.jdom.Attribute; 34 import org.jdom.Document; 35 import org.jdom.Element; 36 37 40 public class XmlUtil { 41 42 51 public static HashMap getNodeIndex(Document inXmlDocument, 52 String uniqueAttribute) { 53 54 int stackPointer=0; 56 String locationId =null; 57 Attribute tmpAttribute=null; 58 Element thisElement=null; 59 ListIterator deepestList=null; 60 61 HashMap mappings = new HashMap (); 62 List stack = new Vector (); 63 64 stack.add(inXmlDocument.getContent().listIterator()); 66 67 while (!stack.isEmpty()){ 69 70 deepestList = (ListIterator )stack.get(stack.size()-1); 72 73 if (deepestList.hasNext()) { 75 76 thisElement = (Element)deepestList.next(); 78 79 tmpAttribute = thisElement.getAttribute(uniqueAttribute); 81 82 if (tmpAttribute!=null) { 84 locationId= tmpAttribute.getValue(); 85 if ((locationId!=null)&&(locationId!="")) { 86 mappings.put(locationId.toString(),thisElement); 87 88 } 89 } 91 if(thisElement.hasChildren()){ 93 94 stackPointer++; 96 stack.add(thisElement.getChildren().listIterator()); 97 } 98 } 99 else 100 { 101 stack.remove(stackPointer); 103 stackPointer--; 104 105 } 107 } 108 109 return mappings; 110 } 111 112 117 public static HashSet getAllNodes(Document inXmlDocument) { 118 119 int stackPointer=0; 121 int index=1; 122 String locationId=null; 123 Element currentElement=null; 124 Element parentElement=null; 125 Element thisElement=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 (); 134 stack.add(inXmlDocument.getContent().listIterator()); 136 137 while (!stack.isEmpty()){ 139 140 deepestList = (ListIterator )stack.get(stack.size()-1); 142 143 if (deepestList.hasNext()) { 145 146 thisElement = (Element)deepestList.next(); 148 149 nodeList.add(thisElement); 151 152 if(thisElement.hasChildren()){ 154 155 stackPointer++; 157 stack.add(thisElement.getChildren().listIterator()); 158 } 159 } 160 else 161 { 162 stack.remove(stackPointer); 164 stackPointer--; 165 166 } 168 } 169 170 return nodeList; 171 } 172 173 180 public static Element findNode(Document inDocumentToSearch 181 , String uniqueIdentifierName 182 , String uniqueIdentifierToFind) { 183 184 HashMap index = getNodeIndex(inDocumentToSearch, 186 uniqueIdentifierName); 187 188 return (Element)index.get(uniqueIdentifierToFind); 190 191 192 } 193 } 194 | Popular Tags |