1 29 30 package nextapp.echo2.webrender.util; 31 32 import java.util.ArrayList ; 33 import java.util.List ; 34 35 import javax.xml.parsers.DocumentBuilder ; 36 import javax.xml.parsers.DocumentBuilderFactory ; 37 import javax.xml.parsers.ParserConfigurationException ; 38 import javax.xml.transform.TransformerFactory ; 39 40 import org.w3c.dom.Element ; 41 import org.w3c.dom.Node ; 42 import org.w3c.dom.NodeList ; 43 import org.w3c.dom.Text ; 44 45 48 public class DomUtil { 49 50 53 private static final ThreadLocal documentBuilders = new ThreadLocal () { 54 55 58 protected Object initialValue() { 59 try { 60 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 61 factory.setNamespaceAware(true); 62 DocumentBuilder builder = factory.newDocumentBuilder(); 63 return builder; 64 } catch (ParserConfigurationException ex) { 65 throw new RuntimeException (ex); 66 } 67 } 68 }; 69 70 73 private static final ThreadLocal transformerFactories = new ThreadLocal () { 74 75 78 protected Object initialValue() { 79 TransformerFactory factory = TransformerFactory.newInstance(); 80 return factory; 81 } 82 }; 83 84 89 public static DocumentBuilder getDocumentBuilder() { 90 return (DocumentBuilder ) documentBuilders.get(); 91 } 92 93 98 public static TransformerFactory getTransformerFactory() { 99 return (TransformerFactory ) transformerFactories.get(); 100 } 101 102 110 public static boolean getBooleanAttribute(Element element, String attributeName) { 111 String value = element.getAttribute(attributeName); 112 if (value == null) { 113 return false; 114 } else if (value.equals("true")) { 115 return true; 116 } else { 117 return false; 118 } 119 } 120 121 129 public static Element getChildElementByTagName(Element parentElement, String name) { 130 NodeList nodes = parentElement.getChildNodes(); 131 int length = nodes.getLength(); 132 for (int index = 0; index < length; ++index) { 133 if (nodes.item(index).getNodeType() == Node.ELEMENT_NODE 134 && name.equals(nodes.item(index).getNodeName())) { 135 return (Element ) nodes.item(index); 136 } 137 } 138 return null; 139 } 140 141 150 public static Element getChildElementByTagNameNS(Element parentElement, String namespaceURI, String localName) { 151 NodeList nodes = parentElement.getChildNodes(); 152 int length = nodes.getLength(); 153 for (int index = 0; index < length; ++index) { 154 if (nodes.item(index).getNodeType() == Node.ELEMENT_NODE 155 && namespaceURI.equals(nodes.item(index).getNamespaceURI()) 156 && localName.equals(nodes.item(index).getNodeName())) { 157 return (Element ) nodes.item(index); 158 } 159 } 160 return null; 161 } 162 163 171 public static Element [] getChildElementsByTagName(Element parentElement, String name) { 172 List children = new ArrayList (); 173 NodeList nodes = parentElement.getChildNodes(); 174 int length = nodes.getLength(); 175 for (int index = 0; index < length; ++index) { 176 if (nodes.item(index).getNodeType() == Node.ELEMENT_NODE 177 && name.equals(nodes.item(index).getNodeName())) { 178 children.add(nodes.item(index)); 179 } 180 } 181 Element [] childElements = new Element [children.size()]; 182 return (Element []) children.toArray(childElements); 183 } 184 185 194 public static Element [] getChildElementsByTagNameNS(Element parentElement, String namespaceURI, String localName) { 195 List children = new ArrayList (); 196 NodeList nodes = parentElement.getChildNodes(); 197 int length = nodes.getLength(); 198 for (int index = 0; index < length; ++index) { 199 if (nodes.item(index).getNodeType() == Node.ELEMENT_NODE 200 && namespaceURI.equals(nodes.item(index).getNamespaceURI()) 201 && localName.equals(nodes.item(index).getNodeName())) { 202 children.add(nodes.item(index)); 203 } 204 } 205 Element [] childElements = new Element [children.size()]; 206 return (Element []) children.toArray(childElements); 207 } 208 209 217 public static int getChildElementCountByTagName(Element parentElement, String name) { 218 NodeList nodes = parentElement.getChildNodes(); 219 int length = nodes.getLength(); 220 int count = 0; 221 for (int index = 0; index < length; ++index) { 222 if (nodes.item(index).getNodeType() == Node.ELEMENT_NODE 223 && name.equals(nodes.item(index).getNodeName())) { 224 ++count; 225 } 226 } 227 return count; 228 } 229 230 239 public static int getChildElementCountByTagNameNS(Element parentElement, String namespaceURI, String localName) { 240 NodeList nodes = parentElement.getChildNodes(); 241 int length = nodes.getLength(); 242 int count = 0; 243 for (int index = 0; index < length; ++index) { 244 if (nodes.item(index).getNodeType() == Node.ELEMENT_NODE 245 && namespaceURI.equals(nodes.item(index).getNamespaceURI()) 246 && localName.equals(nodes.item(index).getNodeName())) { 247 ++count; 248 } 249 } 250 return count; 251 } 252 253 258 public static String getElementText(Element element) { 259 NodeList children = element.getChildNodes(); 260 int childCount = children.getLength(); 261 for (int index = 0; index < childCount; ++index) { 262 if (children.item(index) instanceof Text ) { 263 Text text = (Text ) children.item(index); 264 return text.getData(); 265 } 266 } 267 return null; 268 } 269 270 276 public static void setElementText(Element element, String value) { 277 NodeList children = element.getChildNodes(); 278 int childCount = children.getLength(); 279 for (int index = 0; index < childCount; ++index) { 280 if (children.item(index) instanceof Text ) { 281 Text text = (Text ) children.item(index); 282 text.setData(value); 283 return; 284 } 285 } 286 Text text = element.getOwnerDocument().createTextNode(value); 287 element.appendChild(text); 288 } 289 290 291 private DomUtil() { } 292 } 293 | Popular Tags |