001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.util;
020:
021: import org.w3c.dom.CDATASection;
022: import org.w3c.dom.Document;
023: import org.w3c.dom.Element;
024: import org.w3c.dom.Text;
025:
026: // CheckStyle:HideUtilityClassConstructorCheck OFF - bc
027:
028: /**
029: * Some utility methods for common tasks when building DOM trees in memory.
030: *
031: * <p>In this documentation <code><a></code> means an {@link
032: * org.w3c.dom.Element Element} instance with name <code>a</code>.</p>
033: *
034: * @since Ant 1.6.3
035: */
036: public class DOMUtils {
037:
038: /**
039: * Get a new Document instance,
040: * @return the document.
041: * @since Ant 1.6.3
042: */
043: public static Document newDocument() {
044: return JAXPUtils.getDocumentBuilder().newDocument();
045: }
046:
047: /**
048: * Creates a named Element and appends it to the given element,
049: * returns it.
050: *
051: * <p>This means
052: * <pre>createChildElement(<a>, "b")</pre>
053: * creates
054: * <pre>
055: * <a>
056: * <b/>
057: * </a>
058: * </pre>
059: * and returns <code><b></code>.</p>
060: *
061: * @param parent element that will receive the new element as child.
062: * @param name name of the new element.
063: * @return the new element.
064: *
065: * @since Ant 1.6.3
066: */
067: public static Element createChildElement(Element parent, String name) {
068: Document doc = parent.getOwnerDocument();
069: Element e = doc.createElement(name);
070: parent.appendChild(e);
071: return e;
072: }
073:
074: /**
075: * Adds nested text.
076: *
077: * <p>This means
078: * <pre>appendText(<a>, "b")</pre>
079: * creates
080: * <pre>
081: * <a>b</a>
082: * </pre>
083: * </p>
084: *
085: * @param parent element that will receive the new element as child.
086: * @param content text content.
087: *
088: * @since Ant 1.6.3
089: */
090: public static void appendText(Element parent, String content) {
091: Document doc = parent.getOwnerDocument();
092: Text t = doc.createTextNode(content);
093: parent.appendChild(t);
094: }
095:
096: /**
097: * Adds a nested CDATA section.
098: *
099: * <p>This means
100: * <pre>appendCDATA(<a>, "b")</pre>
101: * creates
102: * <pre>
103: * <a><[!CDATA[b]]></a>
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 parent, String content) {
113: Document doc = parent.getOwnerDocument();
114: CDATASection 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(<a>, "b", "c")</pre>
123: * creates
124: * <pre>
125: * <a>
126: * <b>c</b>
127: * </a>
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 parent, String name,
138: String content) {
139: Element 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(<a>, "b", "c")</pre>
148: * creates
149: * <pre>
150: * <a>
151: * <b><![CDATA[c]]></b>
152: * </a>
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 parent, String name,
164: String content) {
165: Element e = createChildElement(parent, name);
166: appendCDATA(e, content);
167: }
168: }
|