KickJava   Java API By Example, From Geeks To Geeks.

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


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.Document JavaDoc;
22 import org.w3c.dom.DocumentFragment JavaDoc;
23 import org.w3c.dom.Element JavaDoc;
24 import org.w3c.dom.Node JavaDoc;
25 import org.w3c.dom.Text JavaDoc;
26
27 import org.apache.tools.ant.DynamicElementNS;
28 import org.apache.tools.ant.ProjectComponent;
29 import org.apache.tools.ant.DynamicConfiguratorNS;
30
31 /**
32  * Use this class as a nested element if you want to get a literal DOM
33  * fragment of something nested into your task/type.
34  *
35  * <p>This is useful for tasks that want to deal with the "real" XML
36  * from the build file instead of objects.</p>
37  *
38  * <p>Code heavily influenced by code written by Dominique Devienne.</p>
39  *
40  * @since Ant 1.7
41  */

42 public class XMLFragment extends ProjectComponent implements DynamicElementNS {
43
44     private Document JavaDoc doc;
45     private DocumentFragment JavaDoc fragment;
46
47     /**
48      * Constructor for XMLFragment object.
49      */

50     public XMLFragment() {
51         doc = JAXPUtils.getDocumentBuilder().newDocument();
52         fragment = doc.createDocumentFragment();
53     }
54
55     /**
56      * @return the DocumentFragment that corresponds to the nested
57      * structure.
58      */

59     public DocumentFragment JavaDoc getFragment() {
60         return fragment;
61     }
62
63     /**
64      * Add nested text, expanding properties as we go
65      * @param s the text to add
66      */

67     public void addText(String JavaDoc s) {
68         addText(fragment, s);
69     }
70
71     /**
72      * Creates a nested element.
73      * @param uri the uri of the nested element
74      * @param name the localname of the nested element
75      * @param qName the qualified name of the nested element
76      * @return an object that the element is applied to
77      */

78     public Object JavaDoc createDynamicElement(String JavaDoc uri, String JavaDoc name, String JavaDoc qName) {
79         Element JavaDoc e = null;
80         if (uri.equals("")) {
81             e = doc.createElement(name);
82         } else {
83             e = doc.createElementNS(uri, qName);
84         }
85         fragment.appendChild(e);
86         return new Child(e);
87     }
88
89     /**
90      * Add text to a node.
91      * @param n node
92      * @param s value
93      */

94     private void addText(Node JavaDoc n, String JavaDoc s) {
95         s = getProject().replaceProperties(s);
96         //only text nodes that are non null after property expansion are added
97
if (s != null && !s.trim().equals("")) {
98             Text JavaDoc t = doc.createTextNode(s.trim());
99             n.appendChild(t);
100         }
101     }
102
103     /**
104      * An object to handle (recursively) nested elements.
105      */

106     public class Child implements DynamicConfiguratorNS {
107         private Element JavaDoc e;
108
109         Child(Element JavaDoc e) {
110             this.e = e;
111         }
112
113         /**
114          * Add nested text.
115          * @param s the text to add
116          */

117         public void addText(String JavaDoc s) {
118             XMLFragment.this.addText(e, s);
119         }
120
121         /**
122          * Sets the attribute
123          * @param uri the uri of the attribute
124          * @param name the localname of the attribute
125          * @param qName the qualified name of the attribute
126          * @param value the value of the attribute
127          */

128         public void setDynamicAttribute(
129             String JavaDoc uri, String JavaDoc name, String JavaDoc qName, String JavaDoc value) {
130             if (uri.equals("")) {
131                 e.setAttribute(name, value);
132             } else {
133                 e.setAttributeNS(uri, qName, value);
134             }
135         }
136
137         /**
138          * Creates a nested element.
139          * @param uri the uri of the nested element
140          * @param name the localname of the nested element
141          * @param qName the qualified name of the nested element
142          * @return an object that the element is applied to
143          */

144         public Object JavaDoc createDynamicElement(String JavaDoc uri, String JavaDoc name, String JavaDoc qName) {
145             Element JavaDoc e2 = null;
146             if (uri.equals("")) {
147                 e2 = doc.createElement(name);
148             } else {
149                 e2 = doc.createElementNS(uri, qName);
150             }
151             e.appendChild(e2);
152             return new Child(e2);
153         }
154     }
155
156 }
157
Popular Tags