01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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: package org.apache.tools.ant.taskdefs;
19:
20: import java.io.File;
21: import java.io.OutputStream;
22: import java.io.FileOutputStream;
23:
24: import org.apache.tools.ant.Project;
25: import org.apache.tools.ant.BuildException;
26: import org.apache.tools.ant.util.XMLFragment;
27: import org.apache.tools.ant.util.DOMElementWriter;
28: import org.apache.tools.ant.util.FileUtils;
29:
30: import org.w3c.dom.Node;
31: import org.w3c.dom.Element;
32:
33: /**
34: * Echo XML.
35: *
36: * Known limitations:
37: * <ol>
38: * <li>Currently no XMLNS support</li>
39: * <li>Processing Instructions get ignored</li>
40: * <li>Encoding is always UTF-8</li>
41: * </ol>
42: *
43: * @since Ant 1.7
44: */
45: public class EchoXML extends XMLFragment {
46:
47: private File file;
48: private boolean append;
49: private static final String ERROR_NO_XML = "No nested XML specified";
50:
51: /**
52: * Set the output file.
53: * @param f the output file.
54: */
55: public void setFile(File f) {
56: file = f;
57: }
58:
59: /**
60: * Set whether to append the output file.
61: * @param b boolean append flag.
62: */
63: public void setAppend(boolean b) {
64: append = b;
65: }
66:
67: /**
68: * Execute the task.
69: */
70: public void execute() {
71: DOMElementWriter writer = new DOMElementWriter(!append);
72: OutputStream os = null;
73: try {
74: if (file != null) {
75: os = new FileOutputStream(file.getAbsolutePath(),
76: append);
77: } else {
78: os = new LogOutputStream(this , Project.MSG_INFO);
79: }
80: Node n = getFragment().getFirstChild();
81: if (n == null) {
82: throw new BuildException(ERROR_NO_XML);
83: }
84: writer.write((Element) n, os);
85: } catch (BuildException e) {
86: throw e;
87: } catch (Exception e) {
88: throw new BuildException(e);
89: } finally {
90: FileUtils.close(os);
91: }
92: }
93:
94: }
|