source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/util/DOMUtils.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 4.6 KB
Line 
1/*
2 * Copyright 2005 The Apache Software Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18package org.apache.tools.ant.util;
19
20import org.w3c.dom.CDATASection;
21import org.w3c.dom.Document;
22import org.w3c.dom.Element;
23import org.w3c.dom.Text;
24
25/**
26 * Some utility methods for common tasks when building DOM trees in memory.
27 *
28 * <p>In this documentation <code>&lt;a&gt;</code> means an {@link
29 * org.w3c.do.Element Element} instance with name <code>a</code>.</p>
30 *
31 * @since Ant 1.6.3
32 */
33public class DOMUtils {
34
35 /**
36 * Get a new Document instance,
37 *
38 * @since Ant 1.6.3
39 */
40 public static Document newDocument() {
41 return JAXPUtils.getDocumentBuilder().newDocument();
42 }
43
44 /**
45 * Creates a named Element and appends it to the given element,
46 * returns it.
47 *
48 * <p>This means
49 * <pre>createChildElement(&lt;a&gt;, "b")</pre>
50 * creates
51 * <pre>
52 * &lt;a&gt;
53 * &lt;b/&gt;
54 * &lt;/a&gt;
55 * </pre>
56 * and returns <code>&lt;b&gt;</code>.</p>
57 *
58 * @param parent element that will receive the new element as child.
59 * @param name name of the new element.
60 * @return the new element.
61 *
62 * @since Ant 1.6.3
63 */
64 public static Element createChildElement(Element parent, String name) {
65 Document doc = parent.getOwnerDocument();
66 Element e = doc.createElement(name);
67 parent.appendChild(e);
68 return e;
69 }
70
71 /**
72 * Adds nested text.
73 *
74 * <p>This means
75 * <pre>appendText(&lt;a&gt;, "b")</pre>
76 * creates
77 * <pre>
78 * &lt;a&gt;b&lt;/a&gt;
79 * </pre>
80 * </p>
81 *
82 * @param parent element that will receive the new element as child.
83 * @param content text content.
84 *
85 * @since Ant 1.6.3
86 */
87 public static void appendText(Element parent, String content) {
88 Document doc = parent.getOwnerDocument();
89 Text t = doc.createTextNode(content);
90 parent.appendChild(t);
91 }
92
93 /**
94 * Adds a nested CDATA section.
95 *
96 * <p>This means
97 * <pre>appendCDATA(&lt;a&gt;, "b")</pre>
98 * creates
99 * <pre>
100 * &lt;a&gt;&lt;[!CDATA[b]]&gt;&lt;/a&gt;
101 * </pre>
102 * </p>
103 *
104 * @param parent element that will receive the new element as child.
105 * @param content text content.
106 *
107 * @since Ant 1.6.3
108 */
109 public static void appendCDATA(Element parent, String content) {
110 Document doc = parent.getOwnerDocument();
111 CDATASection c = doc.createCDATASection(content);
112 parent.appendChild(c);
113 }
114
115 /**
116 * Adds nested text in a new child element.
117 *
118 * <p>This means
119 * <pre>appendTextElement(&lt;a&gt;, "b", "c")</pre>
120 * creates
121 * <pre>
122 * &lt;a&gt;
123 * &lt;b&gt;c&lt;/b&gt;
124 * &lt;/a&gt;
125 * </pre>
126 * </p>
127 *
128 * @param parent element that will receive the new element as child.
129 * @param name of the child element.
130 * @param content text content.
131 *
132 * @since Ant 1.6.3
133 */
134 public static void appendTextElement(Element parent, String name,
135 String content) {
136 Element e = createChildElement(parent, name);
137 appendText(e, content);
138 }
139
140 /**
141 * Adds a nested CDATA section in a new child element.
142 *
143 * <p>This means
144 * <pre>appendCDATAElement(&lt;a&gt;, "b", "c")</pre>
145 * creates
146 * <pre>
147 * &lt;a&gt;
148 * &lt;b&gt;&lt;![CDATA[c]]>&lt;/b&gt;
149 * &lt;/a&gt;
150 * </pre>
151 * </pre>
152 * </p>
153 *
154 * @param parent element that will receive the new element as child.
155 * @param name of the child element.
156 * @param content text content.
157 *
158 * @since Ant 1.6.3
159 */
160 public static void appendCDATAElement(Element parent, String name,
161 String content) {
162 Element e = createChildElement(parent, name);
163 appendCDATA(e, content);
164 }
165}
Note: See TracBrowser for help on using the repository browser.