1 25 26 package org.objectweb.easybeans.util.xml; 27 28 import java.util.ArrayList ; 29 import java.util.List ; 30 import java.util.Properties ; 31 32 import org.w3c.dom.Element ; 33 import org.w3c.dom.NamedNodeMap ; 34 import org.w3c.dom.Node ; 35 import org.w3c.dom.NodeList ; 36 37 40 public final class XMLUtils { 41 42 45 private XMLUtils() { 46 } 47 48 54 public static String getAttributeValue(final Element base, final String name) { 55 56 NamedNodeMap mapAttributes = base.getAttributes(); 58 Node node = mapAttributes.getNamedItem(name); 59 if (node != null) { 60 return node.getNodeValue(); 61 } 62 return null; 63 } 64 65 72 public static String getStringValueElement(final String ns, final Element base, final String name) { 73 String value = null; 74 75 NodeList list = base.getElementsByTagNameNS(ns, name); 77 if (list.getLength() == 1) { 78 Element element = (Element ) list.item(0); 79 Node node = element.getFirstChild(); 80 if (node != null) { 81 value = node.getNodeValue(); 82 } 83 } else if (list.getLength() > 1) { 84 throw new IllegalStateException ("Element '" + name + "' on '" + base + "' should be unique but there are '" 85 + list.getLength() + "' elements"); 86 } 87 88 return value; 89 } 90 91 98 public static Properties getPropertiesValueElement(final String ns, final Element base, final String name) { 99 Properties returnedProperties = new Properties (); 100 101 NodeList list = base.getElementsByTagNameNS(ns, name); 103 if (list.getLength() == 1) { 104 Element element = (Element ) list.item(0); 105 106 NodeList properties = element.getElementsByTagNameNS(ns, "property"); 108 109 if (properties.getLength() > 0) { 111 for (int i = 0; i < properties.getLength(); i++) { 112 Element elemProperty = (Element ) properties.item(i); 113 String pName = getAttributeValue(elemProperty, "name"); 114 String pValue = getAttributeValue(elemProperty, "value"); 115 if (pName != null && pValue != null) { 116 returnedProperties.setProperty(pName, pValue); 117 } 118 119 } 120 } 121 } else if (list.getLength() > 1) { 122 throw new IllegalStateException ("Element '" + name + "' on '" + base + "' should be unique but there are '" 123 + list.getLength() + "' elements"); 124 } 125 126 return returnedProperties; 127 } 128 129 136 public static List <String > getStringListValueElement(final String ns, final Element base, final String name) { 137 List <String > returnedlist = new ArrayList <String >(); 138 139 NodeList list = base.getElementsByTagNameNS(ns, name); 141 int length = list.getLength(); 142 143 if (length > 0) { 145 for (int i = 0; i < length; i++) { 146 Element element = (Element ) list.item(i); 147 Node node = element.getFirstChild(); 148 if (node != null) { 149 returnedlist.add(node.getNodeValue()); 150 } 151 } 152 } 153 return returnedlist; 154 } 155 156 } 157 | Popular Tags |