1 23 24 package org.apache.webdav.lib.util; 25 26 import java.util.StringTokenizer ; 27 import java.util.Vector ; 28 29 import org.w3c.dom.Attr ; 30 import org.w3c.dom.Document ; 31 import org.w3c.dom.Element ; 32 import org.w3c.dom.NamedNodeMap ; 33 import org.w3c.dom.Node ; 34 import org.w3c.dom.NodeList ; 35 import org.w3c.dom.Text ; 36 37 44 public class DOMUtils { 45 46 protected static Class [] getElementsByNSParameterTypes = 47 { String .class, String .class }; 48 49 60 public static String findDavPrefix(Document document) { 61 Element multistatus = document.getDocumentElement(); 62 NamedNodeMap list = multistatus.getAttributes(); 63 String prefix = "DAV:"; 64 for (int i = 0; i < list.getLength(); i++) { 65 try { 66 Attr attr = (Attr ) list.item(i); 67 if (attr.getName() != null && 68 attr.getName().startsWith("xmlns") && 69 attr.getValue().equals("DAV:")) { 70 int indx = attr.getName().indexOf(":"); 71 if ((indx >= 0) && (indx < attr.getName().length()-1)) { 72 prefix = attr.getName().substring(indx + 1) + ":"; 73 } else { 74 prefix = ""; 75 } 76 } 77 } catch (ClassCastException e) { 78 } 79 } 80 return prefix; 81 } 82 83 84 94 public static String getTextValue(Node node) { 95 96 98 StringBuffer text = new StringBuffer (); 99 NodeList nodeList = node.getChildNodes(); 100 for (int i = 0; i < nodeList.getLength(); i++) { 101 if (nodeList.item(i).getNodeType() == Node.TEXT_NODE 102 || nodeList.item(i).getNodeType() == Node.CDATA_SECTION_NODE) { 103 text.append(((Text ) nodeList.item(i)).getData()); 104 } else { 105 text.append(getTextValue(nodeList.item(i))); 106 } 107 } 108 return text.toString(); 109 } 110 111 124 public static int parseStatus(String statusString) { 125 int status = -1; 126 if (statusString != null) { 127 StringTokenizer tokenizer = new StringTokenizer (statusString); 128 if (tokenizer.countTokens() >= 2) { 129 tokenizer.nextElement(); 130 String statusCode = tokenizer.nextElement().toString(); 131 try { 132 status = Integer.parseInt(statusCode); 133 } catch (NumberFormatException e) { 134 throw new IllegalArgumentException ( 135 "Status code is not numeric"); 136 } 137 } else { 138 throw new IllegalArgumentException ( 139 "There aren't enough words in the input argument"); 140 } 141 } 142 return status; 143 } 144 145 public static String getElementNamespaceURI(Element element) { 146 String namespace = null; 147 148 if (element == null) { 149 throw new IllegalArgumentException ( 150 "The element cannot be null"); 151 } else { 152 try { 153 namespace = element.getNamespaceURI(); 154 } 155 catch (NoSuchMethodError e) { 156 String tagName = element.getTagName(); 157 String attribute = "xmlns"; 158 int index = tagName.indexOf(":"); 159 if (index > 0 && index < (tagName.length()-1)) { 160 attribute += (":" + tagName.substring(0,index)); 161 } 162 163 boolean found = false; 164 for (Node node = element; !found && node != null; 165 node = node.getParentNode()) { 166 try { 167 String tmp = ((Element ) node).getAttribute(attribute); 168 if (tmp != null && !tmp.equals("")) { 169 namespace = tmp; 170 found = true; 171 } 172 } 173 catch (ClassCastException f) { 174 } 176 } 177 } 178 } 179 180 return namespace; 181 } 182 183 public static String getElementLocalName(Element element) { 184 String localName = null; 185 186 if (element == null) { 187 throw new IllegalArgumentException ( 188 "The element cannot be null"); 189 } else { 190 try { 191 localName = element.getLocalName(); 192 } 193 catch (NoSuchMethodError e) { 194 localName = element.getTagName(); 195 int index = localName.indexOf(":"); 196 if (index > 0 && index < (localName.length()-1)) { 197 localName = localName.substring(index + 1); 198 } 199 } 200 } 201 return localName; 202 } 203 204 207 public static NodeList getElementsByTagNameNS( 208 Node node, String tagName, String namespace) { 209 210 NodeList list = null; 211 212 if (node == null) { 213 return null; 214 } 215 else if (!(node instanceof Document ) && !(node instanceof Element )) { 216 throw new IllegalArgumentException ( 217 "The node parameter must be an Element or a Document node"); 218 } 219 else { 220 try { 221 list = ((Element ) node).getElementsByTagNameNS(namespace, tagName); 222 } 223 catch (NoSuchMethodError e) { 224 Vector vector = new Vector (); 225 getChildElementsByTagNameNS(vector, node, tagName, namespace); 226 list = new NodeListImpl(vector); 227 } 228 } 229 return list; 230 } 231 232 233 protected static void getChildElementsByTagNameNS( 234 Vector vector, Node node, String tagName, String namespace) { 235 236 NodeList list = node.getChildNodes(); 237 for (int i = 0; list != null && i < list.getLength(); i++) { 238 try { 239 Element element = (Element ) list.item(i); 240 241 if (tagName.equals(getElementLocalName(element)) && 242 namespace.equals(getElementNamespaceURI(element))) { 243 244 vector.addElement(element); 245 } else { 246 getChildElementsByTagNameNS(vector, element, 248 tagName, namespace); 249 } 250 } catch (ClassCastException e) { 251 } 252 } 253 } 254 255 256 264 public static Element getFirstElement(Node node, String namespace, 265 String name) { 266 NodeList children = node.getChildNodes(); 267 if (children == null) 268 return null; 269 for (int i = 0; i < children.getLength(); i++) { 270 try { 271 Element child = (Element ) children.item(i); 272 if (name.equals(getElementLocalName(child)) && 273 namespace.equals(getElementNamespaceURI(child))) { 274 return child; 275 } 276 } catch (ClassCastException e) { 277 } 278 } 279 return null; 280 } 281 282 283 285 286 290 static class NodeListImpl implements NodeList { 291 private Vector vector = null; 292 293 NodeListImpl(Vector vector) { 294 this.vector = vector; 295 } 296 297 public int getLength() { 298 return vector.size(); 299 } 300 301 public Node item(int i) { 302 return (Node ) vector.elementAt(i); 303 } 304 } 305 } 306 | Popular Tags |