1 22 package org.objectweb.petals.util; 23 24 import java.io.ByteArrayInputStream ; 25 import java.io.StringWriter ; 26 import java.util.ArrayList ; 27 import java.util.List ; 28 29 import javax.xml.parsers.DocumentBuilder ; 30 import javax.xml.parsers.DocumentBuilderFactory ; 31 import javax.xml.transform.Result ; 32 import javax.xml.transform.Source ; 33 import javax.xml.transform.Transformer ; 34 import javax.xml.transform.TransformerException ; 35 import javax.xml.transform.TransformerFactory ; 36 import javax.xml.transform.dom.DOMSource ; 37 import javax.xml.transform.stream.StreamResult ; 38 39 import org.apache.xpath.XPathAPI; 40 import org.w3c.dom.Document ; 41 import org.w3c.dom.NamedNodeMap ; 42 import org.w3c.dom.Node ; 43 import org.w3c.dom.NodeList ; 44 import org.xml.sax.InputSource ; 45 46 54 public final class XMLUtil { 55 56 private XMLUtil() { 57 } 59 60 71 public static Node createAttribute(Document document, String att, 72 String value) { 73 Node element = null; 74 if (document != null) { 75 element = document.createAttribute(att); 76 element.setNodeValue(value); 77 } 78 return element; 79 } 80 81 94 public static Node createNode(Document document, String nodeName, 95 String ... attVal) { 96 Node element = null; 97 if (document != null && attVal.length % 2 == 0) { 98 element = document.createElement(nodeName); 99 for (int i = 0; i < attVal.length; i = i + 2) { 100 element.getAttributes().setNamedItem( 101 createAttribute(document, attVal[i], attVal[i + 1])); 102 } 103 } 104 return element; 105 } 106 107 116 public static String getAttributeValue(Node n, String attName) { 117 if (n != null) { 118 NamedNodeMap atts = n.getAttributes(); 119 Node att = atts.getNamedItem(attName); 120 if (att != null) { 121 return att.getNodeValue(); 122 } 123 } 124 return null; 125 } 126 127 133 public static Node getFirstChild(Node node) { 134 Node result = node.getFirstChild(); 135 if (result != null) { 136 while (result.getNodeType() == Node.TEXT_NODE) { 137 result = result.getNextSibling(); 138 } 139 } 140 return result; 141 } 142 143 149 public static Node getNextSibling(Node node) { 150 Node result = node.getNextSibling(); 151 if (result != null) { 152 while (result.getNodeType() == Node.TEXT_NODE) { 153 result = result.getNextSibling(); 154 } 155 } 156 return result; 157 } 158 159 167 public static Node getNode(Document document, String path) { 168 Node result = null; 169 if (document != null && path != null) { 170 try { 171 result = XPathAPI.selectSingleNode(document, path); 172 } catch (TransformerException e) { 173 return null; 174 } 175 } 176 return result; 177 } 178 179 185 public static String getTextContent(Node node) { 186 NodeList list = node.getChildNodes(); 187 for (int i = 0; i < list.getLength(); i++) { 188 if (list.item(i).getNodeType() == Node.TEXT_NODE) { 189 return list.item(i).getNodeValue(); 190 } 191 } 192 return null; 193 } 194 195 203 public static String createStringFromDOMDocument(Document document) 204 throws TransformerException { 205 document.normalize(); 206 Source source = new DOMSource (document); 207 StringWriter out = new StringWriter (); 208 Result resultStream = new StreamResult (out); 209 TransformerFactory tFactory = TransformerFactory.newInstance(); 210 Transformer transformer; 211 transformer = tFactory.newTransformer(); 212 transformer.transform(source, resultStream); 213 String result = out.toString(); 214 215 return result; 216 } 217 218 225 public static Document createDocumentFromString(String xml) { 226 Document doc = null; 227 try { 228 DocumentBuilderFactory factory = DocumentBuilderFactory 229 .newInstance(); 230 DocumentBuilder builder = factory.newDocumentBuilder(); 231 byte[] msgByte = xml.getBytes(); 232 ByteArrayInputStream in = new ByteArrayInputStream (msgByte); 233 InputSource inputSource = new InputSource (in); 234 doc = builder.parse(inputSource); 235 } catch (Exception e) { 236 } 238 return doc; 239 } 240 241 248 public static List <String > getTextContents(NodeList list) { 249 List <String > result = null; 250 if (list != null) { 251 result = new ArrayList <String >(); 252 for (int i = 0; i < list.getLength(); i++) { 253 Node pathElement = list.item(i); 254 result.add(getTextContent(pathElement)); 255 } 256 } 257 return result; 258 } 259 260 } 261 | Popular Tags |