1 package org.ejen.util; 22 23 import org.ejen.EjenConstants; 24 import java.util.Properties ; 25 import java.io.InputStream ; 26 import java.io.FileInputStream ; 27 import java.io.ByteArrayInputStream ; 28 import javax.xml.parsers.DocumentBuilderFactory ; 29 import javax.xml.parsers.DocumentBuilder ; 30 import org.xml.sax.InputSource ; 31 import org.w3c.dom.Node ; 32 import org.w3c.dom.Element ; 33 import org.w3c.dom.NamedNodeMap ; 34 import org.w3c.dom.Document ; 35 import org.w3c.dom.DOMException ; 36 import org.apache.xml.utils.WrappedRuntimeException; 37 38 43 public class DOMUtil { 44 public static final String S_PY_PROPERTY_NODE_NAME = "property"; 45 public static final String S_PY_NAME = "name"; 46 public static final String S_PY_VALUE = "value"; 47 48 54 public static String getAttribute(Node n, String name) { 55 if (n != null && name != null) { 56 NamedNodeMap nnm = n.getAttributes(); 57 58 if (nnm != null) { 59 Node attr = nnm.getNamedItem(name); 60 61 if (attr != null) { 62 return attr.getNodeValue(); 63 } 64 } 65 } 66 return null; 67 } 68 69 84 public static String getProperty(Node n, String name) { 85 if (n != null && name != null) { 86 String [] prop = getProperty(n); 87 88 if (prop != null && name.equals(prop[0])) { 89 return prop[1]; 90 } 91 } 92 return null; 93 } 94 95 110 public static String [] getProperty(Node n) { 111 if (n != null) { 112 if (S_PY_PROPERTY_NODE_NAME.equals(n.getNodeName())) { 113 NamedNodeMap nnm = n.getAttributes(); 114 115 if (nnm != null) { 116 Node name = nnm.getNamedItem(S_PY_NAME); 117 Node value = nnm.getNamedItem(S_PY_VALUE); 118 119 if (name != null && value != null) { 120 return new String [] {name.getNodeValue(), 121 value.getNodeValue()}; 122 } 123 } 124 } 125 } 126 return null; 127 } 128 129 144 public static Properties getChildProperties(Node n) { 145 return getChildProperties(n, new Properties ()); 146 } 147 148 164 public static Properties getChildProperties(Node parent, Properties props) { 165 if (props != null) { 166 for (Node n = parent.getFirstChild(); n != null; n = n.getNextSibling()) { 167 String [] prop = getProperty(n); 168 169 if (prop != null) { 170 props.setProperty(prop[0], prop[1]); 171 } 172 } 173 } 174 return props; 175 } 176 177 182 public static Document newDocument() { 183 try { 184 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 185 186 dbf.setNamespaceAware(true); 187 DocumentBuilder db = dbf.newDocumentBuilder(); 188 189 return db.newDocument(); 190 } catch (Exception e) { 191 throw new WrappedRuntimeException(e); 192 } 193 } 194 195 201 public static Document parseXMLFile(String name) { 202 InputStream inputs = null; 203 204 try { 205 inputs = new FileInputStream (name); 206 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 207 208 dbf.setNamespaceAware(true); 209 return dbf.newDocumentBuilder().parse(new InputSource (inputs)); 210 } catch (Exception e) { 211 throw new WrappedRuntimeException(e); 212 } 213 finally { 214 if (inputs != null) { 215 try { 216 inputs.close(); 217 } catch (Exception e) {} 218 finally { 219 inputs = null; 220 } 221 } 222 } 223 } 224 225 233 public static Document parseXMLString(String xmlString) { 234 InputStream inputs = null; 235 236 try { 237 if (xmlString == null) { 238 inputs = new ByteArrayInputStream (EjenConstants.DEFAULT_XML_DATA.getBytes(EjenConstants.DEFAULT_XML_DATA_ENCODING)); 239 } else { 240 inputs = new ByteArrayInputStream (xmlString.getBytes(EjenConstants.DEFAULT_XML_DATA_ENCODING)); 241 } 242 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 243 244 dbf.setNamespaceAware(true); 245 return dbf.newDocumentBuilder().parse(new InputSource (inputs)); 246 } catch (Exception e) { 247 throw new WrappedRuntimeException(e); 248 } 249 } 250 251 259 public static Node createNode(Document doc, Node parent, String nodeName) { 260 try { 261 Element elt = doc.createElement(nodeName); 262 263 parent.appendChild(elt); 264 return elt; 265 } catch (DOMException e) { 266 throw new WrappedRuntimeException(e); 267 } 268 } 269 270 280 public static Node createCDATANode(Document doc, Node parent, String nodeName, String nodeValue) { 281 try { 282 Element elt = doc.createElement(nodeName); 283 284 elt.appendChild(doc.createCDATASection(nodeValue)); 285 parent.appendChild(elt); 286 return elt; 287 } catch (DOMException e) { 288 throw new WrappedRuntimeException(e); 289 } 290 } 291 292 301 public static Node createCDATANode(Document doc, String nodeName, String nodeValue) { 302 try { 303 Element elt = doc.createElement(nodeName); 304 305 elt.appendChild(doc.createCDATASection(nodeValue)); 306 return elt; 307 } catch (DOMException e) { 308 throw new WrappedRuntimeException(e); 309 } 310 } 311 312 321 public static Node createTextNode(Document doc, String nodeName, String nodeValue) { 322 try { 323 Element elt = doc.createElement(nodeName); 324 325 elt.appendChild(doc.createTextNode(nodeValue)); 326 return elt; 327 } catch (DOMException e) { 328 throw new WrappedRuntimeException(e); 329 } 330 } 331 } 332 | Popular Tags |