KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > petals > util > XMLUtil


1 /**
2  * PETALS - PETALS Services Platform.
3  * Copyright (c) 2006 EBM Websourcing
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * -------------------------------------------------------------------------
19  * $Id: XMLUtil.java 98 2006-02-24 16:18:48Z alouis $
20  * -------------------------------------------------------------------------
21  */

22 package org.objectweb.petals.util;
23
24 import java.io.ByteArrayInputStream JavaDoc;
25 import java.io.StringWriter JavaDoc;
26 import java.util.ArrayList JavaDoc;
27 import java.util.List JavaDoc;
28
29 import javax.xml.parsers.DocumentBuilder JavaDoc;
30 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
31 import javax.xml.transform.Result JavaDoc;
32 import javax.xml.transform.Source JavaDoc;
33 import javax.xml.transform.Transformer JavaDoc;
34 import javax.xml.transform.TransformerException JavaDoc;
35 import javax.xml.transform.TransformerFactory JavaDoc;
36 import javax.xml.transform.dom.DOMSource JavaDoc;
37 import javax.xml.transform.stream.StreamResult JavaDoc;
38
39 import org.apache.xpath.XPathAPI;
40 import org.w3c.dom.Document JavaDoc;
41 import org.w3c.dom.NamedNodeMap JavaDoc;
42 import org.w3c.dom.Node JavaDoc;
43 import org.w3c.dom.NodeList JavaDoc;
44 import org.xml.sax.InputSource JavaDoc;
45
46 /**
47  * Contains utilities methods for XML operations.
48  *
49  * @version $Rev: 98 $ $Date: 2006-02-24 17:18:48 +0100 (ven., 24 févr. 2006) $
50  * @since Petals 1.0
51  * @author Adrien LOUIS
52  * @author ddesjardins - eBMWebsourcing
53  */

54 public final class XMLUtil {
55
56     private XMLUtil() {
57         // Do nothing
58
}
59
60     /**
61      * Create an attribute node with the specified value
62      *
63      * @param document
64      * XML document
65      * @param att
66      * attribute name
67      * @param value
68      * attribute value
69      * @return an attribute, null if the document is null
70      */

71     public static Node JavaDoc createAttribute(Document JavaDoc document, String JavaDoc att,
72         String JavaDoc value) {
73         Node JavaDoc element = null;
74         if (document != null) {
75             element = document.createAttribute(att);
76             element.setNodeValue(value);
77         }
78         return element;
79     }
80
81     /**
82      * Create a node with the specified name, attributes and values. attVal is a
83      * suite : att1,val1,att2,val2,...
84      *
85      * @param document
86      * xlm document
87      * @param nodeName
88      * node name
89      * @param attVal
90      * attribute values
91      * @return the node with its attribute, null if the array is not correct or
92      * the document is null.
93      */

94     public static Node JavaDoc createNode(Document JavaDoc document, String JavaDoc nodeName,
95         String JavaDoc... attVal) {
96         Node JavaDoc 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     /**
108      * Return the value of the attribute in the node
109      *
110      * @param n
111      * the node
112      * @param attName
113      * the name of the attribute
114      * @return the value of the attribute, null if not found
115      */

116     public static String JavaDoc getAttributeValue(Node JavaDoc n, String JavaDoc attName) {
117         if (n != null) {
118             NamedNodeMap JavaDoc atts = n.getAttributes();
119             Node JavaDoc att = atts.getNamedItem(attName);
120             if (att != null) {
121                 return att.getNodeValue();
122             }
123         }
124         return null;
125     }
126
127     /**
128      * Return the first child of a node, regardless <i>text</i> node
129      *
130      * @param node
131      * @return
132      */

133     public static Node JavaDoc getFirstChild(Node JavaDoc node) {
134         Node JavaDoc 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     /**
144      * Return the next sibling of a node, regardless <i>text</i> node
145      *
146      * @param node
147      * @return
148      */

149     public static Node JavaDoc getNextSibling(Node JavaDoc node) {
150         Node JavaDoc 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     /**
160      * return the node corresponding to the given path for the Document.
161      *
162      * @param path
163      * @param document
164      * @return the first matching node, null otherwise, or if document/path is
165      * null
166      */

167     public static Node JavaDoc getNode(Document JavaDoc document, String JavaDoc path) {
168         Node JavaDoc result = null;
169         if (document != null && path != null) {
170             try {
171                 result = XPathAPI.selectSingleNode(document, path);
172             } catch (TransformerException JavaDoc e) {
173                 return null;
174             }
175         }
176         return result;
177     }
178
179     /**
180      * Return the <i>text</i> element of a node
181      *
182      * @param node
183      * @return
184      */

185     public static String JavaDoc getTextContent(Node JavaDoc node) {
186         NodeList JavaDoc 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     /**
196      * Create a String result from a DOM document
197      *
198      * @param document
199      * the DOM Document
200      * @return a String representation of the DOM Document
201      * @throws TransformerException
202      */

203     public static String JavaDoc createStringFromDOMDocument(Document JavaDoc document)
204         throws TransformerException JavaDoc {
205         document.normalize();
206         Source JavaDoc source = new DOMSource JavaDoc(document);
207         StringWriter JavaDoc out = new StringWriter JavaDoc();
208         Result JavaDoc resultStream = new StreamResult JavaDoc(out);
209         TransformerFactory JavaDoc tFactory = TransformerFactory.newInstance();
210         Transformer JavaDoc transformer;
211         transformer = tFactory.newTransformer();
212         transformer.transform(source, resultStream);
213         String JavaDoc result = out.toString();
214
215         return result;
216     }
217
218     /**
219      * Create a document from a String
220      *
221      * @param xml
222      * xml string
223      * @return document representing the document
224      */

225     public static Document JavaDoc createDocumentFromString(String JavaDoc xml) {
226         Document JavaDoc doc = null;
227         try {
228             DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory
229                 .newInstance();
230             DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
231             byte[] msgByte = xml.getBytes();
232             ByteArrayInputStream JavaDoc in = new ByteArrayInputStream JavaDoc(msgByte);
233             InputSource JavaDoc inputSource = new InputSource JavaDoc(in);
234             doc = builder.parse(inputSource);
235         } catch (Exception JavaDoc e) {
236             // Do nothing
237
}
238         return doc;
239     }
240
241     /**
242      * Return an array of String representing each Text element of the nodes
243      * which are in the list.
244      *
245      * @param list
246      * @return
247      */

248     public static List JavaDoc<String JavaDoc> getTextContents(NodeList JavaDoc list) {
249         List JavaDoc<String JavaDoc> result = null;
250         if (list != null) {
251             result = new ArrayList JavaDoc<String JavaDoc>();
252             for (int i = 0; i < list.getLength(); i++) {
253                 Node JavaDoc pathElement = list.item(i);
254                 result.add(getTextContent(pathElement));
255             }
256         }
257         return result;
258     }
259
260 }
261
Popular Tags