1 7 package org.jboss.cache.xml; 8 9 import org.apache.commons.logging.Log; 10 import org.apache.commons.logging.LogFactory; 11 import org.w3c.dom.Document ; 12 import org.w3c.dom.Element ; 13 import org.w3c.dom.Node ; 14 import org.w3c.dom.NodeList ; 15 import org.w3c.dom.Text ; 16 import org.xml.sax.InputSource ; 17 import org.xml.sax.SAXException ; 18 import org.xml.sax.SAXParseException ; 19 20 import javax.xml.parsers.DocumentBuilder ; 21 import javax.xml.parsers.DocumentBuilderFactory ; 22 import java.io.ByteArrayInputStream ; 23 import java.io.IOException ; 24 import java.io.InputStream ; 25 import java.util.Properties ; 26 27 32 public class XmlHelper 33 { 34 private static Log log = LogFactory.getLog(XmlHelper.class); 35 public static final String ROOT = "mbean"; 36 public static final String ATTR = "attribute"; 37 public static final String CONFIG_ATTR = "config"; 38 public static final String NAME = "name"; 39 40 41 49 public static String getTagContents(Element elem, String myName, String tagName, String attributeName) 50 { 51 NodeList list = elem.getElementsByTagName(tagName); 52 53 for (int s = 0; s < list.getLength(); s++) 54 { 55 org.w3c.dom.Node node = list.item(s); 56 if (node.getNodeType() != org.w3c.dom.Node.ELEMENT_NODE) 57 continue; 58 59 Element element = (Element ) node; 60 String name = element.getAttribute(attributeName); 61 if (name.equals(myName)) 62 { 63 return getElementContent(element, true); 64 } 65 } 66 return null; 67 } 68 69 76 public static String getAttributeValue(Element elem, String tagName, String attributeName) 77 { 78 NodeList list = elem.getElementsByTagName(tagName); 79 80 for (int s = 0; s < list.getLength(); s++) 81 { 82 org.w3c.dom.Node node = list.item(s); 83 if (node.getNodeType() != org.w3c.dom.Node.ELEMENT_NODE) 84 continue; 85 86 Element element = (Element ) node; 87 return element.getAttribute(attributeName); 88 89 } 90 return null; 91 } 92 93 public static Element getConfigSubElement(Element element) 94 { 95 return getSubElement(element, CONFIG_ATTR); 96 } 97 98 public static Element getSubElement(Element element, String subElementName) 99 { 100 NodeList nl = element.getChildNodes(); 101 for (int i = 0; i < nl.getLength(); i++) 102 { 103 Node node = nl.item(i); 104 if (node.getNodeType() == Node.ELEMENT_NODE && subElementName.equals(((Element ) node).getTagName())) 105 { 106 return (Element ) node; 107 } 108 } 109 110 if (log.isDebugEnabled()) log.debug("getSubElement(): Does not exist for " + subElementName); 111 return null; 112 } 113 114 public static String getElementContent(Element element, boolean trim) 115 { 116 NodeList nl = element.getChildNodes(); 117 String attributeText = ""; 118 for (int i = 0; i < nl.getLength(); i++) 119 { 120 Node n = nl.item(i); 121 if (n instanceof Text ) 122 { 123 attributeText += ((Text ) n).getData(); 124 } 125 } if (trim) 127 attributeText = attributeText.trim(); 128 return attributeText; 129 } 130 131 public static String readStringContents(Element element, String tagName) 132 { 133 NodeList nodes = element.getElementsByTagName(tagName); 134 if (nodes.getLength() > 0) 135 { 136 Node node = nodes.item(0); 137 Element ne = (Element ) node; 138 NodeList nl2 = ne.getChildNodes(); 139 Node node2 = nl2.item(0); 140 if (node2 != null) 141 { 142 String value = node2.getNodeValue(); 143 if (value == null) 144 return ""; 145 return value.trim(); 146 } 147 else 148 { 149 return ""; 150 } 151 } 152 else 153 { 154 return ""; 155 } 156 } 157 158 public static String escapeBackslashes(String value) 159 { 160 StringBuffer buf = new StringBuffer (value); 161 for (int looper = 0; looper < buf.length(); looper++) 162 { 163 char curr = buf.charAt(looper); 164 char next = 0; 165 if (looper + 1 < buf.length()) 166 next = buf.charAt(looper + 1); 167 168 if (curr == '\\') 169 { 170 if (next != '\\') 171 { buf.insert(looper, '\\'); } 174 looper++; } 176 } 177 return buf.toString(); 178 } 179 180 public static Properties readPropertiesContents(Element element, String tagName) throws IOException 181 { 182 String stringContents = readStringContents(element, tagName); 183 if (stringContents == null) return new Properties (); 184 stringContents = escapeBackslashes(stringContents); 186 ByteArrayInputStream is = new ByteArrayInputStream (stringContents.trim().getBytes("ISO8859_1")); 187 Properties properties = new Properties (); 188 properties.load(is); 189 is.close(); 190 return properties; 191 } 192 193 public static boolean readBooleanContents(Element element, String tagName) 194 { 195 return readBooleanContents(element, tagName, false); 196 } 197 198 public static boolean readBooleanContents(Element element, String tagName, boolean defaultValue) 199 { 200 String val = readStringContents(element, tagName); 201 if (val.equalsIgnoreCase("true") || val.equalsIgnoreCase("false")) 202 { 203 return Boolean.valueOf(val); 204 } 205 return defaultValue; 206 } 207 208 public static Element stringToElement(String xml) throws Exception 209 { 210 ByteArrayInputStream bais = new ByteArrayInputStream (xml.getBytes("utf8")); 211 DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 212 Document d = builder.parse(bais); 213 bais.close(); 214 return d.getDocumentElement(); 215 } 216 217 public static Element getDocumentRoot(InputStream is) 218 { 219 Document doc = null; 220 try 221 { 222 InputSource xmlInp = new InputSource (is); 223 224 DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); 225 DocumentBuilder parser = docBuilderFactory.newDocumentBuilder(); 226 doc = parser.parse(xmlInp); 227 Element root = doc.getDocumentElement(); 228 root.normalize(); 229 return root; 230 } 231 catch (SAXParseException err) 232 { 233 log.error("Configurator SAXParse error", err); 234 } 235 catch (SAXException e) 236 { 237 log.error("Configurator SAX error", e); 238 } 239 catch (Exception pce) 240 { 241 log.error("Configurator general error", pce); 242 } 243 return null; 244 } 245 246 254 public static boolean readBooleanAttribute(Element elem, String tagName, String attributeName, boolean defaultValue) 255 { 256 String val = getAttributeValue(elem, tagName, attributeName); 257 if (val != null) 258 { 259 if (val.equalsIgnoreCase("true") || val.equalsIgnoreCase("false")) 260 { 261 return Boolean.valueOf(val); 262 } 263 } 264 265 return defaultValue; 266 } 267 268 } 269 | Popular Tags |