1 17 package com.sun.org.apache.xml.internal.security.utils; 18 19 20 21 22 import org.w3c.dom.Attr ; 23 import org.w3c.dom.Document ; 24 import org.w3c.dom.Element ; 25 import org.w3c.dom.Node ; 26 27 import java.util.Arrays ; 28 import java.util.WeakHashMap ; 29 import java.lang.ref.WeakReference ; 30 31 32 47 public class IdResolver { 48 49 50 static java.util.logging.Logger log = 51 java.util.logging.Logger.getLogger(IdResolver.class.getName()); 52 53 static WeakHashMap docMap = new WeakHashMap (); 54 55 59 private IdResolver() { 60 61 } 63 64 70 public static void registerElementById(Element element, String idValue) { 71 Document doc = element.getOwnerDocument(); 72 WeakHashMap elementMap = (WeakHashMap ) docMap.get(doc); 73 if(elementMap == null) { 74 elementMap = new WeakHashMap (); 75 docMap.put(doc, elementMap); 76 } 77 elementMap.put(idValue, new WeakReference (element)); 78 } 79 80 86 public static void registerElementById(Element element, Attr id) { 87 IdResolver.registerElementById(element, id.getNodeValue()); 88 } 89 90 97 public static Element getElementById(Document doc, String id) { 98 99 Element result = null; 100 101 result = IdResolver.getElementByIdType(doc, id); 102 103 if (result != null) { 104 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, 105 "I could find an Element using the simple getElementByIdType method: " 106 + result.getTagName()); 107 108 return result; 109 } 110 111 result = IdResolver.getElementByIdUsingDOM(doc, id); 112 113 if (result != null) { 114 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, 115 "I could find an Element using the simple getElementByIdUsingDOM method: " 116 + result.getTagName()); 117 118 return result; 119 } 120 result = IdResolver.getElementBySearching(doc, id); 123 124 if (result != null) { 125 IdResolver.registerElementById(result, id); 126 127 return result; 128 } 129 130 return null; 131 } 132 133 134 141 private static Element getElementByIdUsingDOM(Document doc, String id) { 142 if (true) 143 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "getElementByIdUsingDOM() Search for ID " + id); 144 return doc.getElementById(id); 145 } 146 147 154 private static Element getElementByIdType(Document doc, String id) { 155 if (true) 156 if (log.isLoggable(java.util.logging.Level.FINE)) log.log(java.util.logging.Level.FINE, "getElementByIdType() Search for ID " + id); 157 WeakHashMap elementMap = (WeakHashMap ) docMap.get(doc); 158 if (elementMap != null) { 159 WeakReference weakReference = (WeakReference ) elementMap.get(id); 160 if (weakReference != null) 161 { 162 return (Element ) weakReference.get(); 163 } 164 } 165 return null; 166 } 167 168 169 static java.util.List names; 170 static { 171 String namespaces[]={ Constants.SignatureSpecNS, 172 EncryptionConstants.EncryptionSpecNS, 173 "http://schemas.xmlsoap.org/soap/security/2000-12", 174 "http://www.w3.org/2002/03/xkms#" 175 }; 176 names=Arrays.asList(namespaces); 177 } 178 179 180 private static Element getElementBySearching(Node root,String id) { 181 Element []els=new Element [5]; 182 getElementBySearching(root,id,els); 183 for (int i=0;i<els.length;i++) { 184 if (els[i]!=null) { 185 return els[i]; 186 } 187 } 188 return null; 189 190 } 191 private static int getElementBySearching(Node root,String id,Element []els) { 192 switch (root.getNodeType()) { 193 case Node.ELEMENT_NODE: 194 Element el=(Element )root; 195 if (el.hasAttributes()) { 196 int index=names.indexOf(el.getNamespaceURI()); 197 if (index<0) { 198 index=4; 199 } 200 if (el.getAttribute("Id").equals(id)) { 201 els[index]=el; 202 if (index==0) { 203 return 1; 204 } 205 } else if ( el.getAttribute("id").equals(id) ) { 206 if (index!=2) { 207 index=4; 208 } 209 els[index]=el; 210 } else if ( el.getAttribute("ID").equals(id) ) { 211 if (index!=3) { 212 index=4; 213 } 214 els[index]=el; 215 } else if ((index==3)&&( 216 el.getAttribute("OriginalRequestID").equals(id) || 217 el.getAttribute("RequestID").equals(id) || 218 el.getAttribute("ResponseID" ).equals(id))) { 219 els[3]=el; 220 } 221 } 222 case Node.DOCUMENT_NODE: 223 Node sibling=root.getFirstChild(); 224 while (sibling!=null) { 225 if (getElementBySearching(sibling,id,els)==1) 226 return 1; 227 sibling=sibling.getNextSibling(); 228 } 229 } 230 return 0; 231 } 232 233 } 234 | Popular Tags |