KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > EchoXML


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 package org.apache.tools.ant.taskdefs;
19
20 import java.io.File JavaDoc;
21 import java.io.OutputStream JavaDoc;
22 import java.io.FileOutputStream JavaDoc;
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 JavaDoc;
31 import org.w3c.dom.Element JavaDoc;
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 JavaDoc file;
48     private boolean append;
49     private static final String JavaDoc 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 JavaDoc 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 JavaDoc os = null;
73         try {
74             if (file != null) {
75                 os = new FileOutputStream JavaDoc(file.getAbsolutePath(), append);
76             } else {
77                 os = new LogOutputStream(this, Project.MSG_INFO);
78             }
79             Node JavaDoc n = getFragment().getFirstChild();
80             if (n == null) {
81                 throw new BuildException(ERROR_NO_XML);
82             }
83             writer.write((Element JavaDoc) n, os);
84         } catch (BuildException e) {
85             throw e;
86         } catch (Exception JavaDoc e) {
87             throw new BuildException(e);
88         } finally {
89             FileUtils.close(os);
90         }
91     }
92
93 }
94
Popular Tags