KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > util > DOMUtils


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.util;
20
21 import org.w3c.dom.CDATASection JavaDoc;
22 import org.w3c.dom.Document JavaDoc;
23 import org.w3c.dom.Element JavaDoc;
24 import org.w3c.dom.Text JavaDoc;
25
26 // CheckStyle:HideUtilityClassConstructorCheck OFF - bc
27

28 /**
29  * Some utility methods for common tasks when building DOM trees in memory.
30  *
31  * <p>In this documentation <code>&lt;a&gt;</code> means an {@link
32  * org.w3c.dom.Element Element} instance with name <code>a</code>.</p>
33  *
34  * @since Ant 1.6.3
35  */

36 public class DOMUtils {
37
38     /**
39      * Get a new Document instance,
40      * @return the document.
41      * @since Ant 1.6.3
42      */

43     public static Document JavaDoc newDocument() {
44         return JAXPUtils.getDocumentBuilder().newDocument();
45     }
46
47     /**
48      * Creates a named Element and appends it to the given element,
49      * returns it.
50      *
51      * <p>This means
52      * <pre>createChildElement(&lt;a&gt;, "b")</pre>
53      * creates
54      * <pre>
55      * &lt;a&gt;
56      * &lt;b/&gt;
57      * &lt;/a&gt;
58      * </pre>
59      * and returns <code>&lt;b&gt;</code>.</p>
60      *
61      * @param parent element that will receive the new element as child.
62      * @param name name of the new element.
63      * @return the new element.
64      *
65      * @since Ant 1.6.3
66      */

67     public static Element JavaDoc createChildElement(Element JavaDoc parent, String JavaDoc name) {
68         Document JavaDoc doc = parent.getOwnerDocument();
69         Element JavaDoc e = doc.createElement(name);
70         parent.appendChild(e);
71         return e;
72     }
73
74     /**
75      * Adds nested text.
76      *
77      * <p>This means
78      * <pre>appendText(&lt;a&gt;, "b")</pre>
79      * creates
80      * <pre>
81      * &lt;a&gt;b&lt;/a&gt;
82      * </pre>
83      * </p>
84      *
85      * @param parent element that will receive the new element as child.
86      * @param content text content.
87      *
88      * @since Ant 1.6.3
89      */

90     public static void appendText(Element JavaDoc parent, String JavaDoc content) {
91         Document JavaDoc doc = parent.getOwnerDocument();
92         Text JavaDoc t = doc.createTextNode(content);
93         parent.appendChild(t);
94     }
95
96     /**
97      * Adds a nested CDATA section.
98      *
99      * <p>This means
100      * <pre>appendCDATA(&lt;a&gt;, "b")</pre>
101      * creates
102      * <pre>
103      * &lt;a&gt;&lt;[!CDATA[b]]&gt;&lt;/a&gt;
104      * </pre>
105      * </p>
106      *
107      * @param parent element that will receive the new element as child.
108      * @param content text content.
109      *
110      * @since Ant 1.6.3
111      */

112     public static void appendCDATA(Element JavaDoc parent, String JavaDoc content) {
113         Document JavaDoc doc = parent.getOwnerDocument();
114         CDATASection JavaDoc c = doc.createCDATASection(content);
115         parent.appendChild(c);
116     }
117
118     /**
119      * Adds nested text in a new child element.
120      *
121      * <p>This means
122      * <pre>appendTextElement(&lt;a&gt;, "b", "c")</pre>
123      * creates
124      * <pre>
125      * &lt;a&gt;
126      * &lt;b&gt;c&lt;/b&gt;
127      * &lt;/a&gt;
128      * </pre>
129      * </p>
130      *
131      * @param parent element that will receive the new element as child.
132      * @param name of the child element.
133      * @param content text content.
134      *
135      * @since Ant 1.6.3
136      */

137     public static void appendTextElement(Element JavaDoc parent, String JavaDoc name,
138                                          String JavaDoc content) {
139         Element JavaDoc e = createChildElement(parent, name);
140         appendText(e, content);
141     }
142
143     /**
144      * Adds a nested CDATA section in a new child element.
145      *
146      * <p>This means
147      * <pre>appendCDATAElement(&lt;a&gt;, "b", "c")</pre>
148      * creates
149      * <pre>
150      * &lt;a&gt;
151      * &lt;b&gt;&lt;![CDATA[c]]>&lt;/b&gt;
152      * &lt;/a&gt;
153      * </pre>
154      * </pre>
155      * </p>
156      *
157      * @param parent element that will receive the new element as child.
158      * @param name of the child element.
159      * @param content text content.
160      *
161      * @since Ant 1.6.3
162      */

163     public static void appendCDATAElement(Element JavaDoc parent, String JavaDoc name,
164                                           String JavaDoc content) {
165         Element JavaDoc e = createChildElement(parent, name);
166         appendCDATA(e, content);
167     }
168 }
169
Popular Tags