KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejen > util > DOMUtil


1 //
2
// Ejen (code generation system)
3
// Copyright (C) 2001, 2002 François Wolff (ejen@noos.fr).
4
//
5
// This file is part of Ejen.
6
//
7
// Ejen is free software; you can redistribute it and/or modify
8
// it under the terms of the GNU General Public License as published by
9
// the Free Software Foundation; either version 2 of the License, or
10
// (at your option) any later version.
11
//
12
// Ejen is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
// GNU General Public License for more details.
16
//
17
// You should have received a copy of the GNU General Public License
18
// along with Ejen; if not, write to the Free Software
19
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
//
21
package org.ejen.util;
22
23 import org.ejen.EjenConstants;
24 import java.util.Properties JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.FileInputStream JavaDoc;
27 import java.io.ByteArrayInputStream JavaDoc;
28 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
29 import javax.xml.parsers.DocumentBuilder JavaDoc;
30 import org.xml.sax.InputSource JavaDoc;
31 import org.w3c.dom.Node JavaDoc;
32 import org.w3c.dom.Element JavaDoc;
33 import org.w3c.dom.NamedNodeMap JavaDoc;
34 import org.w3c.dom.Document JavaDoc;
35 import org.w3c.dom.DOMException JavaDoc;
36 import org.apache.xml.utils.WrappedRuntimeException;
37
38 /**
39  * DOM utility (static methods used in java code).
40  * @author F. Wolff
41  * @version 1.0
42  */

43 public class DOMUtil {
44     public static final String JavaDoc S_PY_PROPERTY_NODE_NAME = "property";
45     public static final String JavaDoc S_PY_NAME = "name";
46     public static final String JavaDoc S_PY_VALUE = "value";
47
48     /**
49      * Returns a Node attribute value.
50      * @param n the <code>Node</code> from which the attribute must be retreived.
51      * @param name the name of the attribute.
52      * @return the attribute value or <code>null</code> if there is no such attribute.
53      */

54     public static String JavaDoc getAttribute(Node JavaDoc n, String JavaDoc name) {
55         if (n != null && name != null) {
56             NamedNodeMap JavaDoc nnm = n.getAttributes();
57
58             if (nnm != null) {
59                 Node JavaDoc attr = nnm.getNamedItem(name);
60
61                 if (attr != null) {
62                     return attr.getNodeValue();
63                 }
64             }
65         }
66         return null;
67     }
68
69     /**
70      * Returns a property value.
71      * <p>
72      * The <code>n</code> parameter must have the following format:
73      * <p>
74      * <table class="usage"><tr><td class="usage"><pre>
75      *
76      * &lt;property name="..." value="..."&gt;
77      * </pre></td></tr></table>
78      * <p>
79      * @param n the <code>Node</code> from which the property must be retreived.
80      * @param name the name of the property (<i>ie: the value of the "name" attribute</i>).
81      * @return the property value (<i>ie: the value of the "value" attribute</i>)
82      * or <code>null</code> if there is no such property.
83      */

84     public static String JavaDoc getProperty(Node JavaDoc n, String JavaDoc name) {
85         if (n != null && name != null) {
86             String JavaDoc[] prop = getProperty(n);
87
88             if (prop != null && name.equals(prop[0])) {
89                 return prop[1];
90             }
91         }
92         return null;
93     }
94
95     /**
96      * Returns a property as an array of two Strings.
97      * <p>
98      * The <code>n</code> parameter must have the following format:
99      * <p>
100      * <table class="usage"><tr><td class="usage"><pre>
101      *
102      * &lt;property name="..." value="..."&gt;
103      * </pre></td></tr></table>
104      * <p>
105      * @param n the <code>Node</code> from which the property must be retreived.
106      * @return the property name/value pair (<i>ie: the value of the "name" attribute and
107      * the value of the "value" attribute</i>)
108      * or <code>null</code> if there is no such property.
109      */

110     public static String JavaDoc[] getProperty(Node JavaDoc n) {
111         if (n != null) {
112             if (S_PY_PROPERTY_NODE_NAME.equals(n.getNodeName())) {
113                 NamedNodeMap JavaDoc nnm = n.getAttributes();
114
115                 if (nnm != null) {
116                     Node JavaDoc name = nnm.getNamedItem(S_PY_NAME);
117                     Node JavaDoc value = nnm.getNamedItem(S_PY_VALUE);
118
119                     if (name != null && value != null) {
120                         return new String JavaDoc[] {name.getNodeValue(),
121                             value.getNodeValue()};
122                     }
123                 }
124             }
125         }
126         return null;
127     }
128
129     /**
130      * Returns a set of properties from a given parent <code>Node</code>.
131      * <p>
132      * Each child property must have the following format:
133      * <p>
134      * <table class="usage"><tr><td class="usage"><pre>
135      *
136      * &lt;property name="..." value="..."&gt;
137      * </pre></td></tr></table>
138      * <p>
139      * Other kind of child nodes are ignored.
140      * <p>
141      * @param n the <code>Node</code> from which the child properties must be retreived.
142      * @return the set of child properties (empty if there is no child property).
143      */

144     public static Properties JavaDoc getChildProperties(Node JavaDoc n) {
145         return getChildProperties(n, new Properties JavaDoc());
146     }
147
148     /**
149      * Appends to an existing set of properties those found in a given parent <code>Node</code>.
150      * <p>
151      * Each child property must have the following format:
152      * <p>
153      * <table class="usage"><tr><td class="usage"><pre>
154      *
155      * &lt;property name="..." value="..."&gt;
156      * </pre></td></tr></table>
157      * <p>
158      * Other kind of child nodes are ignored.
159      * <p>
160      * @param n the <code>Node</code> from which the child properties must be retreived.
161      * @param props the existing properties.
162      * @return the <code>props</code> parameter (with appended child properties).
163      */

164     public static Properties JavaDoc getChildProperties(Node JavaDoc parent, Properties JavaDoc props) {
165         if (props != null) {
166             for (Node JavaDoc n = parent.getFirstChild(); n != null; n = n.getNextSibling()) {
167                 String JavaDoc[] prop = getProperty(n);
168
169                 if (prop != null) {
170                     props.setProperty(prop[0], prop[1]);
171                 }
172             }
173         }
174         return props;
175     }
176
177     /**
178      * Create a new <code>Document</code> object.
179      * @return the new <code>Document</code>.
180      * @throws org.apache.xml.utils.WrappedRuntimeException creation error.
181      */

182     public static Document JavaDoc newDocument() {
183         try {
184             DocumentBuilderFactory JavaDoc dbf = DocumentBuilderFactory.newInstance();
185
186             dbf.setNamespaceAware(true);
187             DocumentBuilder JavaDoc db = dbf.newDocumentBuilder();
188
189             return db.newDocument();
190         } catch (Exception JavaDoc e) {
191             throw new WrappedRuntimeException(e);
192         }
193     }
194
195     /**
196      * Parse a XML document file.
197      * @param name the XML file name.
198      * @return the parsed <code>Document</code> object.
199      * @throws org.apache.xml.utils.WrappedRuntimeException error (no such file, parse error).
200      */

201     public static Document JavaDoc parseXMLFile(String JavaDoc name) {
202         InputStream JavaDoc inputs = null;
203
204         try {
205             inputs = new FileInputStream JavaDoc(name);
206             DocumentBuilderFactory JavaDoc dbf = DocumentBuilderFactory.newInstance();
207
208             dbf.setNamespaceAware(true);
209             return dbf.newDocumentBuilder().parse(new InputSource JavaDoc(inputs));
210         } catch (Exception JavaDoc e) {
211             throw new WrappedRuntimeException(e);
212         }
213         finally {
214             if (inputs != null) {
215                 try {
216                     inputs.close();
217                 } catch (Exception JavaDoc e) {}
218                 finally {
219                     inputs = null;
220                 }
221             }
222         }
223     }
224
225     /**
226      * Parse a XML document <code>String</code> (using <code>DEFAULT_XML_DATA_ENCODING</code>
227      * encoding).
228      * @param xmlString the XML content <code>String</code>. If <code>xmlString</code>
229      * is <code>null</code>, the <code>DEFAULT_XML_DATA</code> is used.
230      * @return the parsed <code>Document</code> object.
231      * @throws org.apache.xml.utils.WrappedRuntimeException error (parse error).
232      */

233     public static Document JavaDoc parseXMLString(String JavaDoc xmlString) {
234         InputStream JavaDoc inputs = null;
235
236         try {
237             if (xmlString == null) {
238                 inputs = new ByteArrayInputStream JavaDoc(EjenConstants.DEFAULT_XML_DATA.getBytes(EjenConstants.DEFAULT_XML_DATA_ENCODING));
239             } else {
240                 inputs = new ByteArrayInputStream JavaDoc(xmlString.getBytes(EjenConstants.DEFAULT_XML_DATA_ENCODING));
241             }
242             DocumentBuilderFactory JavaDoc dbf = DocumentBuilderFactory.newInstance();
243
244             dbf.setNamespaceAware(true);
245             return dbf.newDocumentBuilder().parse(new InputSource JavaDoc(inputs));
246         } catch (Exception JavaDoc e) {
247             throw new WrappedRuntimeException(e);
248         }
249     }
250
251     /**
252      * Creates a new Node of name 'nodeName', in the 'doc' Document, child of the 'parent' Node.
253      * @param doc the owning Document of the new Node.
254      * @param parent the parent Node of the new Node.
255      * @param nodeName name of the <code>Node</code> to be created.
256      * @return the created Node.
257      * @throws org.apache.xml.utils.WrappedRuntimeException DOM errors.
258      */

259     public static Node JavaDoc createNode(Document JavaDoc doc, Node JavaDoc parent, String JavaDoc nodeName) {
260         try {
261             Element JavaDoc elt = doc.createElement(nodeName);
262
263             parent.appendChild(elt);
264             return elt;
265         } catch (DOMException JavaDoc e) {
266             throw new WrappedRuntimeException(e);
267         }
268     }
269
270     /**
271      * Creates a new Node of name 'nodeName', in the 'doc' Document, child of the 'parent' Node,
272      * and fills it with a CDATA Node whose content is 'nodeValue'.
273      * @param doc the owning Document of the new Node.
274      * @param parent the parent Node of the new Node.
275      * @param nodeName name of the <code>Node</code> to be created.
276      * @param nodeValue value of the <code>CDATA Node</code> to be created.
277      * @return the created Node (parent of the CDATA Node).
278      * @throws org.apache.xml.utils.WrappedRuntimeException DOM errors.
279      */

280     public static Node JavaDoc createCDATANode(Document JavaDoc doc, Node JavaDoc parent, String JavaDoc nodeName, String JavaDoc nodeValue) {
281         try {
282             Element JavaDoc elt = doc.createElement(nodeName);
283
284             elt.appendChild(doc.createCDATASection(nodeValue));
285             parent.appendChild(elt);
286             return elt;
287         } catch (DOMException JavaDoc e) {
288             throw new WrappedRuntimeException(e);
289         }
290     }
291
292     /**
293      * Creates a new Node of name 'nodeName', in the 'doc' Document,
294      * and fills it with a CDATA Node whose content is 'nodeValue'.
295      * @param doc the owning Document of the new Node.
296      * @param nodeName name of the <code>Node</code> to be created.
297      * @param nodeValue value of the <code>CDATA Node</code> to be created.
298      * @return the created Node (parent of the CDATA Node).
299      * @throws org.apache.xml.utils.WrappedRuntimeException DOM errors.
300      */

301     public static Node JavaDoc createCDATANode(Document JavaDoc doc, String JavaDoc nodeName, String JavaDoc nodeValue) {
302         try {
303             Element JavaDoc elt = doc.createElement(nodeName);
304
305             elt.appendChild(doc.createCDATASection(nodeValue));
306             return elt;
307         } catch (DOMException JavaDoc e) {
308             throw new WrappedRuntimeException(e);
309         }
310     }
311
312     /**
313      * Creates a new Node of name 'nodeName', in the 'doc' Document,
314      * and fills it with a text Node whose content is 'nodeValue'.
315      * @param doc the owning Document of the new Node.
316      * @param nodeName name of the <code>Node</code> to be created.
317      * @param nodeValue value of the text <code>Node</code> to be created.
318      * @return the created Node (parent of the text Node).
319      * @throws org.apache.xml.utils.WrappedRuntimeException DOM errors.
320      */

321     public static Node JavaDoc createTextNode(Document JavaDoc doc, String JavaDoc nodeName, String JavaDoc nodeValue) {
322         try {
323             Element JavaDoc elt = doc.createElement(nodeName);
324
325             elt.appendChild(doc.createTextNode(nodeValue));
326             return elt;
327         } catch (DOMException JavaDoc e) {
328             throw new WrappedRuntimeException(e);
329         }
330     }
331 }
332
Popular Tags