1 package org.jivesoftware.util; 2 3 import java.util.ArrayList ; 4 import java.util.Arrays ; 5 import java.util.Iterator ; 6 import java.util.List ; 7 import java.util.StringTokenizer ; 8 import org.dom4j.Element; 9 import org.dom4j.Node; 10 11 39 public class ElementUtil { 40 41 42 private ElementUtil() { 43 } 44 45 46 53 public static String getProperty(Element element, String name) { 54 String value = null; 55 String [] propName = parsePropertyName(name); 56 57 String lastName = propName[propName.length - 1]; 59 String attName = null; 60 int attributeIndex = lastName.indexOf(':'); 61 if (attributeIndex >= 0) { 62 propName[propName.length - 1] = lastName.substring(0, attributeIndex); 63 attName = lastName.substring(attributeIndex + 1); 64 } 65 66 int i = propName[0].equals(element.getName()) ? 1 : 0; 68 for (; i < propName.length; i++) { 69 element = element.element(propName[i]); 70 if (element == null) { 71 break; 72 } 73 } 74 if (element != null) { 75 if (attName == null) { 76 value = element.getTextTrim(); 77 } 78 else { 79 value = element.attributeValue(attName); 80 } 81 } 82 83 return value; 84 } 85 86 93 public static boolean includesProperty(Element element, String name) { 94 String value = null; 95 96 if (value == null) { 97 String [] propName = parsePropertyName(name); 98 99 String lastName = propName[propName.length - 1]; 101 String attName = null; 102 int attributeIndex = lastName.indexOf(':'); 103 if (attributeIndex >= 0) { 104 propName[propName.length - 1] = lastName.substring(0, attributeIndex); 105 attName = lastName.substring(attributeIndex + 1); 106 } 107 108 int i = propName[0].equals(element.getName()) ? 1 : 0; 110 for (; i < propName.length; i++) { 111 element = element.element(propName[i]); 112 if (element == null) { 113 break; 114 } 115 } 116 117 if (element != null) { 118 if (attName == null){ 119 return true; 121 } else { 122 return element.attribute(attName) != null; 124 } 125 } 126 else { 127 return false; 129 } 130 } 131 return true; 132 } 133 134 156 public String [] getProperties(Element element, String name) { 157 String [] propName = parsePropertyName(name); 158 159 int i = propName[0].equals(element.getName()) ? 1 : 0; 161 for (; i < propName.length - 1; i++) { 162 element = element.element(propName[i]); 163 if (element == null) { 164 return new String []{}; 167 } 168 } 169 Iterator iter = element.elementIterator(propName[propName.length - 1]); 171 ArrayList props = new ArrayList (); 172 while (iter.hasNext()) { 173 Element e = (Element) iter.next(); 174 props.add(e.getName()); 175 } 176 String [] childrenNames = new String [props.size()]; 177 return (String []) props.toArray(childrenNames); 178 } 179 180 199 public static void setProperties(Element element, String name, String [] values) { 200 String [] propName = parsePropertyName(name); 201 setProperty(element, name, values[0]); 202 203 int i = propName[0].equals(element.getName()) ? 1 : 0; 205 for (; i < propName.length - 1; i++) { 206 element = element.element(propName[i]); 207 if (element == null) { 208 return; 211 } 212 } 213 String childName = propName[propName.length - 1]; 214 Iterator iter = element.elementIterator(childName); 216 while (iter.hasNext()) { 217 ((Node) iter.next()).detach(); 218 } 219 for (int j = 0; i < values.length; i++) { 220 if (values[j] != null) { 221 element.addElement(childName).setText(values[j]); 222 } 223 } 224 } 225 226 237 public static String [] getChildrenProperties(Element element, String parent) { 238 String [] propName = parsePropertyName(parent); 239 240 int i = propName[0].equals(element.getName()) ? 1 : 0; 242 for (; i < propName.length; i++) { 243 element = element.element(propName[i]); 244 if (element == null) { 245 return new String []{}; 248 } 249 } 250 List children = element.elements(); 252 int childCount = children.size(); 253 String [] childrenNames = new String [childCount]; 254 for (int j = 0; i < childCount; i++) { 255 childrenNames[j] = ((Element) children.get(j)).getName(); 256 } 257 return childrenNames; 258 } 259 260 269 public static String [] getRecursiveChildrenProperties(Element element, String parent) { 270 String [] properties = getChildrenProperties(element, parent); 271 if (properties.length == 0) { 272 return properties; 273 } 274 else { 275 List list = new ArrayList (15); 276 for (int i = 0; i < properties.length; i++) { 277 String propName = parent + "." + properties[i]; 278 list.add(propName); 279 list.addAll(Arrays.asList(getRecursiveChildrenProperties(element, propName))); 280 } 281 return (String []) list.toArray(new String []{}); 282 } 283 } 284 285 292 public static void setProperty(Element element, String name, String value) { 293 if (name == null || name.length() == 0) return; 294 if (value == null) value = ""; 295 296 String [] propName = parsePropertyName(name); 297 298 int i = propName[0].equals(element.getName()) ? 1 : 0; 300 for (; i < propName.length - 1; i++) { 301 if (element.element(propName[i]) == null) { 304 element.addElement(propName[i]); 305 } 306 element = element.element(propName[i]); 307 } 308 String lastName = propName[propName.length - 1]; 309 int attributeIndex = lastName.indexOf(':'); 310 if (attributeIndex >= 0) { 311 String eleName = lastName.substring(0, attributeIndex); 312 String attName = lastName.substring(attributeIndex + 1); 313 if (element.element(eleName) == null) { 316 element.addElement(eleName); 317 } 318 element.element(eleName).addAttribute(attName, value); 319 } 320 else { 321 if (element.element(lastName) == null) { 324 element.addElement(lastName); 325 } 326 element.element(lastName).setText(value); 328 } 329 } 330 331 340 public static void deleteProperty(Element element, String name) { 341 String [] propName = parsePropertyName(name); 343 344 for (int i = 0; i < propName.length - 1; i++) { 346 element = element.element(propName[i]); 347 if (element == null) { 349 return; 350 } 351 } 352 element.remove(element.element(propName[propName.length - 1])); 354 } 355 356 364 private static String [] parsePropertyName(String name) { 365 List propName = new ArrayList (5); 366 StringTokenizer tokenizer = new StringTokenizer (name, "."); 368 while (tokenizer.hasMoreTokens()) { 369 propName.add(tokenizer.nextToken()); 370 } 371 return (String []) propName.toArray(new String [propName.size()]); 372 } 373 374 375 } 376 | Popular Tags |