1 package org.hibernate.ejb.packaging; 2 3 import java.util.ArrayList ; 4 import java.util.Iterator ; 5 6 import org.w3c.dom.Element ; 7 import org.w3c.dom.Node ; 8 import org.w3c.dom.NodeList ; 9 10 16 public final class XmlHelper { 17 private XmlHelper() {} 18 26 public static Iterator getChildrenByTagName( 27 Element element, 28 String tagName 29 ) { 30 if ( element == null ) return null; 31 34 NodeList children = element.getChildNodes(); 35 ArrayList goodChildren = new ArrayList (); 36 for ( int i = 0 ; i < children.getLength() ; i++ ) { 37 Node currentChild = children.item( i ); 38 if ( currentChild.getNodeType() == Node.ELEMENT_NODE && 39 ( (Element ) currentChild ).getTagName().equals( tagName ) ) { 40 goodChildren.add( (Element ) currentChild ); 41 } 42 } 43 return goodChildren.iterator(); 44 } 45 46 56 public static Element getUniqueChild(Element element, String tagName) 57 throws Exception { 58 Iterator goodChildren = getChildrenByTagName( element, tagName ); 59 60 if ( goodChildren != null && goodChildren.hasNext() ) { 61 Element child = (Element ) goodChildren.next(); 62 if ( goodChildren.hasNext() ) { 63 throw new Exception 64 ( "expected only one " + tagName + " tag" ); 65 } 66 return child; 67 } 68 else { 69 throw new Exception 70 ( "expected one " + tagName + " tag" ); 71 } 72 } 73 74 83 public static Element getOptionalChild(Element element, String tagName) 84 throws Exception { 85 return getOptionalChild( element, tagName, null ); 86 } 87 88 99 public static Element getOptionalChild( 100 Element element, 101 String tagName, 102 Element defaultElement 103 ) 104 throws Exception { 105 Iterator goodChildren = getChildrenByTagName( element, tagName ); 106 107 if ( goodChildren != null && goodChildren.hasNext() ) { 108 Element child = (Element ) goodChildren.next(); 109 if ( goodChildren.hasNext() ) { 110 throw new Exception 111 ( "expected only one " + tagName + " tag" ); 112 } 113 return child; 114 } 115 else { 116 return defaultElement; 117 } 118 } 119 120 126 public static String getElementContent(final Element element) 127 throws Exception { 128 return getElementContent( element, null ); 129 } 130 131 138 public static String getElementContent(Element element, String defaultStr) 139 throws Exception { 140 if ( element == null ) { 141 return defaultStr; 142 } 143 144 NodeList children = element.getChildNodes(); 145 String result = ""; 146 for ( int i = 0 ; i < children.getLength() ; i++ ) { 147 if ( children.item( i ).getNodeType() == Node.TEXT_NODE || 148 children.item( i ).getNodeType() == Node.CDATA_SECTION_NODE ) { 149 result += children.item( i ).getNodeValue(); 150 } 151 else if ( children.item( i ).getNodeType() == Node.COMMENT_NODE ) { 152 } 154 } 155 return result.trim(); 156 } 157 158 165 public static String getUniqueChildContent( 166 Element element, 167 String tagName 168 ) 169 throws Exception { 170 return getElementContent( getUniqueChild( element, tagName ) ); 171 } 172 173 180 public static String getOptionalChildContent( 181 Element element, 182 String tagName 183 ) 184 throws Exception { 185 return getElementContent( getOptionalChild( element, tagName ) ); 186 } 187 188 public static boolean getOptionalChildBooleanContent(Element element, String name) throws Exception { 189 Element child = getOptionalChild( element, name ); 190 if ( child != null ) { 191 String value = getElementContent( child ).toLowerCase(); 192 return value.equals( "true" ) || value.equals( "yes" ); 193 } 194 195 return false; 196 } 197 198 199 } 200 201 202 | Popular Tags |