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.Document;
022: import org.w3c.dom.DocumentFragment;
023: import org.w3c.dom.Element;
024: import org.w3c.dom.Node;
025: import org.w3c.dom.Text;
026:
027: import org.apache.tools.ant.DynamicElementNS;
028: import org.apache.tools.ant.ProjectComponent;
029: import org.apache.tools.ant.DynamicConfiguratorNS;
030:
031: /**
032: * Use this class as a nested element if you want to get a literal DOM
033: * fragment of something nested into your task/type.
034: *
035: * <p>This is useful for tasks that want to deal with the "real" XML
036: * from the build file instead of objects.</p>
037: *
038: * <p>Code heavily influenced by code written by Dominique Devienne.</p>
039: *
040: * @since Ant 1.7
041: */
042: public class XMLFragment extends ProjectComponent implements
043: DynamicElementNS {
044:
045: private Document doc;
046: private DocumentFragment fragment;
047:
048: /**
049: * Constructor for XMLFragment object.
050: */
051: public XMLFragment() {
052: doc = JAXPUtils.getDocumentBuilder().newDocument();
053: fragment = doc.createDocumentFragment();
054: }
055:
056: /**
057: * @return the DocumentFragment that corresponds to the nested
058: * structure.
059: */
060: public DocumentFragment getFragment() {
061: return fragment;
062: }
063:
064: /**
065: * Add nested text, expanding properties as we go
066: * @param s the text to add
067: */
068: public void addText(String s) {
069: addText(fragment, s);
070: }
071:
072: /**
073: * Creates a nested element.
074: * @param uri the uri of the nested element
075: * @param name the localname of the nested element
076: * @param qName the qualified name of the nested element
077: * @return an object that the element is applied to
078: */
079: public Object createDynamicElement(String uri, String name,
080: String qName) {
081: Element e = null;
082: if (uri.equals("")) {
083: e = doc.createElement(name);
084: } else {
085: e = doc.createElementNS(uri, qName);
086: }
087: fragment.appendChild(e);
088: return new Child(e);
089: }
090:
091: /**
092: * Add text to a node.
093: * @param n node
094: * @param s value
095: */
096: private void addText(Node n, String s) {
097: s = getProject().replaceProperties(s);
098: //only text nodes that are non null after property expansion are added
099: if (s != null && !s.trim().equals("")) {
100: Text t = doc.createTextNode(s.trim());
101: n.appendChild(t);
102: }
103: }
104:
105: /**
106: * An object to handle (recursively) nested elements.
107: */
108: public class Child implements DynamicConfiguratorNS {
109: private Element e;
110:
111: Child(Element e) {
112: this .e = e;
113: }
114:
115: /**
116: * Add nested text.
117: * @param s the text to add
118: */
119: public void addText(String s) {
120: XMLFragment.this .addText(e, s);
121: }
122:
123: /**
124: * Sets the attribute
125: * @param uri the uri of the attribute
126: * @param name the localname of the attribute
127: * @param qName the qualified name of the attribute
128: * @param value the value of the attribute
129: */
130: public void setDynamicAttribute(String uri, String name,
131: String qName, String value) {
132: if (uri.equals("")) {
133: e.setAttribute(name, value);
134: } else {
135: e.setAttributeNS(uri, qName, value);
136: }
137: }
138:
139: /**
140: * Creates a nested element.
141: * @param uri the uri of the nested element
142: * @param name the localname of the nested element
143: * @param qName the qualified name of the nested element
144: * @return an object that the element is applied to
145: */
146: public Object createDynamicElement(String uri, String name,
147: String qName) {
148: Element e2 = null;
149: if (uri.equals("")) {
150: e2 = doc.createElement(name);
151: } else {
152: e2 = doc.createElementNS(uri, qName);
153: }
154: e.appendChild(e2);
155: return new Child(e2);
156: }
157: }
158:
159: }
|