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 |
|
---|
18 | package org.apache.tools.ant.util;
|
---|
19 |
|
---|
20 | import org.w3c.dom.CDATASection;
|
---|
21 | import org.w3c.dom.Document;
|
---|
22 | import org.w3c.dom.Element;
|
---|
23 | import 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><a></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 | */
|
---|
33 | public 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(<a>, "b")</pre>
|
---|
50 | * creates
|
---|
51 | * <pre>
|
---|
52 | * <a>
|
---|
53 | * <b/>
|
---|
54 | * </a>
|
---|
55 | * </pre>
|
---|
56 | * and returns <code><b></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(<a>, "b")</pre>
|
---|
76 | * creates
|
---|
77 | * <pre>
|
---|
78 | * <a>b</a>
|
---|
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(<a>, "b")</pre>
|
---|
98 | * creates
|
---|
99 | * <pre>
|
---|
100 | * <a><[!CDATA[b]]></a>
|
---|
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(<a>, "b", "c")</pre>
|
---|
120 | * creates
|
---|
121 | * <pre>
|
---|
122 | * <a>
|
---|
123 | * <b>c</b>
|
---|
124 | * </a>
|
---|
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(<a>, "b", "c")</pre>
|
---|
145 | * creates
|
---|
146 | * <pre>
|
---|
147 | * <a>
|
---|
148 | * <b><![CDATA[c]]></b>
|
---|
149 | * </a>
|
---|
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 | }
|
---|