1 22 package org.jboss.aop.util; 23 24 import org.w3c.dom.Element ; 25 import org.w3c.dom.Node ; 26 import org.w3c.dom.NodeList ; 27 28 import java.util.ArrayList ; 29 import java.util.Iterator ; 30 31 37 public class XmlHelper 38 { 39 47 public static Iterator getChildrenByTagName(Element element, 48 String tagName) 49 { 50 if (element == null) return null; 51 54 NodeList children = element.getChildNodes(); 55 ArrayList goodChildren = new ArrayList (); 56 for (int i=0; i<children.getLength(); i++) { 57 Node currentChild = children.item(i); 58 if (currentChild.getNodeType() == Node.ELEMENT_NODE && 59 ((Element )currentChild).getTagName().equals(tagName)) { 60 goodChildren.add(currentChild); 61 } 62 } 63 return goodChildren.iterator(); 64 } 65 66 77 public static Element getUniqueChild(Element element, String tagName) 78 throws Exception 79 { 80 Iterator goodChildren = getChildrenByTagName(element, tagName); 81 82 if (goodChildren != null && goodChildren.hasNext()) { 83 Element child = (Element )goodChildren.next(); 84 if (goodChildren.hasNext()) { 85 throw new Exception 86 ("expected only one " + tagName + " tag"); 87 } 88 return child; 89 } else { 90 throw new Exception 91 ("expected one " + tagName + " tag"); 92 } 93 } 94 95 104 public static Element getOptionalChild(Element element, String tagName) 105 throws Exception 106 { 107 return getOptionalChild(element, tagName, null); 108 } 109 110 121 public static Element getOptionalChild(Element element, 122 String tagName, 123 Element defaultElement) 124 throws Exception 125 { 126 Iterator goodChildren = getChildrenByTagName(element, tagName); 127 128 if (goodChildren != null && goodChildren.hasNext()) { 129 Element child = (Element )goodChildren.next(); 130 if (goodChildren.hasNext()) { 131 throw new Exception 132 ("expected only one " + tagName + " tag"); 133 } 134 return child; 135 } else { 136 return defaultElement; 137 } 138 } 139 140 146 public static String getElementContent(final Element element) 147 throws Exception 148 { 149 return getElementContent(element, null); 150 } 151 152 159 public static String getElementContent(Element element, String defaultStr) 160 throws Exception 161 { 162 if (element == null) 163 return defaultStr; 164 165 NodeList children = element.getChildNodes(); 166 String result = ""; 167 for (int i = 0; i < children.getLength(); i++) 168 { 169 if (children.item(i).getNodeType() == Node.TEXT_NODE || 170 children.item(i).getNodeType() == Node.CDATA_SECTION_NODE) 171 { 172 result += children.item(i).getNodeValue(); 173 } 174 else if( children.item(i).getNodeType() == Node.COMMENT_NODE ) 175 { 176 } 178 } 179 return result.trim(); 180 } 181 182 189 public static String getUniqueChildContent(Element element, 190 String tagName) 191 throws Exception 192 { 193 return getElementContent(getUniqueChild(element, tagName)); 194 } 195 196 203 public static String getOptionalChildContent(Element element, 204 String tagName) 205 throws Exception 206 { 207 return getElementContent(getOptionalChild(element, tagName)); 208 } 209 210 public static boolean getOptionalChildBooleanContent(Element element, String name) throws Exception 211 { 212 Element child = getOptionalChild(element, name); 213 if(child != null) 214 { 215 String value = getElementContent(child).toLowerCase(); 216 return value.equals("true") || value.equals("yes"); 217 } 218 219 return false; 220 } 221 222 223 } 224 225 226 | Popular Tags |