1 26 28 package de.nava.informa.utils; 29 30 import java.util.ArrayList ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.StringTokenizer ; 34 35 import org.jdom.Element; 36 import org.jdom.Namespace; 37 38 44 public class XmlPathUtils { 45 46 private XmlPathUtils() {} 47 48 private static final String elementDelim = "/"; 49 private static final String prefixDelim = ":"; 50 51 64 public static String getElementValue(final Element parent, final String childPath) { 65 66 if (parent == null) { 67 return null; 68 } else { 69 Element child = getLeafChild(parent, childPath); 70 return (child == null) ? null : child.getTextTrim(); 71 } 72 } 73 74 92 public static String [] getElementValues(final Element parent, final String childPath, final String [] elements) { 93 94 if (parent == null) { 95 return null; 96 } else { 97 Element child = getLeafChild(parent, childPath); 98 return getElementValues(child, elements); 99 } 100 } 101 102 113 public static String [] getElementValues(final Element parent, final String [] children) { 114 if ((parent == null) || (children == null)) { 115 return null; 116 } else { 117 int numValues = children.length; 118 String [] elementValues = new String [numValues]; 119 for (int i = 0; i < numValues; ++i) { 120 Element child = getChild(parent, children[i]); 121 elementValues[i] = (child == null) ? null : child.getTextTrim(); 122 } 123 return elementValues; 124 } 125 } 126 127 145 public static String getAttributeValue(final Element parent, final String childPath, final String attribute) { 146 147 if ((parent == null) || (attribute == null)) { 148 return null; 149 } else { 150 Element child = getLeafChild(parent, childPath); 151 return getAttributeValue(child, attribute); 152 } 153 } 154 155 172 public static String [] getAttributeValues(final Element parent, final String childPath, final String [] attributes) { 173 174 if ((parent == null) || (attributes == null)) { 175 return null; 176 } else { 177 Element child = getLeafChild(parent, childPath); 178 return getAttributeValues(child, attributes); 179 } 180 } 181 182 194 public static String [] getAttributeValues(final Element element, final String [] attributes) { 195 196 if ((element == null) || (attributes == null)) { 197 return null; 198 } else { 199 int numAttributes = attributes.length; 200 String [] attributeValues = new String [numAttributes]; 201 for (int i = 0; i < numAttributes; ++i) { 202 attributeValues[i] = getAttributeValue(element, attributes[i]); 203 } 204 return attributeValues; 205 } 206 } 207 208 218 private static Element getLeafChild(final Element parent, final String childPath) { 219 220 if (childPath == null) return parent; 221 222 List elementNames = getElementNames(childPath); 223 Iterator iterator = elementNames.iterator(); 224 Element nextChild = parent; 225 226 while (iterator.hasNext() && (nextChild != null)) { 227 String elementName = (String ) iterator.next(); 228 nextChild = getChild(nextChild, elementName); 229 } 230 231 return nextChild; 232 } 233 234 244 private static Element getChild(final Element parent, final String childName) { 245 246 int prefixPos = childName.indexOf(prefixDelim); 247 if ( (prefixPos == 0) || (prefixPos >= childName.length() - 1)) { 248 return null; 249 } else { 250 if (prefixPos == -1) { 251 return parent.getChild(childName, getNamespace(parent, null)); 252 } else { 253 String prefix = childName.substring(0, prefixPos); 254 String childElementName = childName.substring(prefixPos + 1); 255 return parent.getChild(childElementName, getNamespace(parent, prefix)); 256 } 257 } 258 } 259 260 271 private static String getAttributeValue(Element element, String attribute) { 272 273 if (element == null) return null; 274 int prefixPos = attribute.indexOf(prefixDelim); 275 276 if ( (prefixPos == 0) || (prefixPos >= attribute.length() - 1)) { 277 return null; 278 } else if (prefixPos == -1) { return element.getAttributeValue(attribute); 280 } else { 281 String prefix = attribute.substring(0, prefixPos); 282 String attributeName = attribute.substring(prefixPos + 1); 283 return element.getAttributeValue(attributeName, getNamespace(element, prefix)); 284 } 285 } 286 287 295 private static List getElementNames(final String childPath) { 296 297 List strArray = new ArrayList (); 298 if (childPath != null) { 299 StringTokenizer st = new StringTokenizer (childPath, elementDelim); 300 while (st.hasMoreTokens()) { 301 String token = st.nextToken(); 302 if (token.length() > 0) { 303 strArray.add(token); 304 } 305 } 306 } 307 return strArray; 308 } 309 310 318 private static Namespace getNamespace(final Element element, final String prefix) { 319 Namespace namespace = (prefix == null) ? element.getNamespace("") : element.getNamespace(prefix); 320 return (namespace == null) ? Namespace.NO_NAMESPACE : namespace; 321 } 322 323 } 324 | Popular Tags |