KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ejen > EjenSaveNode


1 //
2
// Ejen (code generation system)
3
// Copyright (C) 2001, 2002 François Wolff (ejen@noos.fr).
4
//
5
// This file is part of Ejen.
6
//
7
// Ejen is free software; you can redistribute it and/or modify
8
// it under the terms of the GNU General Public License as published by
9
// the Free Software Foundation; either version 2 of the License, or
10
// (at your option) any later version.
11
//
12
// Ejen is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
// GNU General Public License for more details.
16
//
17
// You should have received a copy of the GNU General Public License
18
// along with Ejen; if not, write to the Free Software
19
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20
//
21
package org.ejen;
22
23 import java.util.Properties JavaDoc;
24 import java.io.File JavaDoc;
25 import java.io.OutputStream JavaDoc;
26 import java.io.FileOutputStream JavaDoc;
27 import javax.xml.transform.Transformer JavaDoc;
28 import javax.xml.transform.dom.DOMSource JavaDoc;
29 import javax.xml.transform.stream.StreamResult JavaDoc;
30 import javax.xml.transform.OutputKeys JavaDoc;
31 import org.apache.xalan.processor.TransformerFactoryImpl;
32 import org.apache.xalan.templates.OutputProperties;
33
34 /**
35  * Save node class.
36  * <p>
37  * A Save node saves the current in memory DOM tree to file.
38  * <p>
39  * <table class="usage">
40  * <tr><th class="usage">Usage (ant build file)</th></tr>
41  * <tr><td class="usage"><pre><code>
42  * &lt;?xml version="1.0" encoding="UTF-8"?&gt;
43  *
44  * &lt;project name="generate" default="build"&gt;
45  *
46  * &lt;taskdef name="ejen" classname="org.ejen.EjenTask"/&gt;
47  *
48  * &lt;target name="build"&gt;
49  * &lt;{@link org.ejen.EjenTask ejen} ...&gt;
50  * ...
51  * <b>&lt;save {@link #setFile(String) file}="saved.xml"
52  * [{@link #setEncoding(String) encoding}="iso-8859-1"]
53  * [{@link #setIndent(String) indent}="(yes|no)"]
54  * [{@link #setAmount(String) amount}="2"]
55  * /&gt;</b>
56  * ...
57  * &lt;/ejen&gt;
58  * &lt;/target&gt;
59  *
60  * &lt;/project&gt;
61  * </code></pre></td></tr></table>
62  * <p>
63  * <b>Windows users</b>: an understandable (but stupid) problem with Windows platforms comes
64  * with carriage return conversion on saving. If the DOM tree to be saved contains a Node
65  * with the <code>"\r\n"</code> (<code>0D0A</code>) String (alone or not), the saved file
66  * will contain the <code>"\r\r\n"</code> (<code>0D0D0A</code>) String instead.
67  * <p>
68  * <b>Parent nodes</b>:
69  * <ul>
70  * <li>{@link org.ejen.EjenTask ejen}
71  * </ul>
72  * @author F. Wolff
73  * @version 1.0
74  */

75 public class EjenSaveNode extends EjenChildNode {
76     protected String JavaDoc _file = null;
77     protected String JavaDoc _encoding = "iso-8859-1";
78     protected String JavaDoc _indent = "yes";
79     protected String JavaDoc _amount = "2";
80
81     /**
82      * Returns the name of this EjenSaveNode (always "save").
83      * @return the name of this EjenSaveNode.
84      */

85     public String JavaDoc nodeName() {
86         return "save";
87     }
88
89     /**
90      * Returns all non null attributes of this EjenSaveNode.
91      * @return non null attributes of this EjenSaveNode.
92      */

93     public Properties JavaDoc getAttributes() {
94         Properties JavaDoc attrs = super.getAttributes();
95
96         if (_file != null) {
97             attrs.setProperty("file", _file);
98         }
99         if (_encoding != null) {
100             attrs.setProperty("encoding", _encoding);
101         }
102         if (_indent != null) {
103             attrs.setProperty("indent", _indent);
104         }
105         if (_amount != null) {
106             attrs.setProperty("amount", _amount);
107         }
108         return attrs;
109     }
110
111     /**
112      * <b>[mandatory/AVT]</b> - sets the file attribute.
113      * @param file name of the XML file to created.
114      */

115     public void setFile(String JavaDoc file) {
116         _file = file;
117     }
118
119     /**
120      * <b>[optional/AVT]</b> - sets the encoding attribute.
121      * @param encoding encoding String (default is "iso-8859-1").
122      */

123     public void setEncoding(String JavaDoc encoding) {
124         _encoding = encoding;
125     }
126
127     /**
128      * <b>[optional/AVT]</b> - sets the indent attribute.
129      * @param indent "yes" or "no" (default is "yes").
130      */

131     public void setIndent(String JavaDoc indent) {
132         _indent = indent;
133     }
134
135     /**
136      * <b>[optional/AVT]</b> - sets the (indent) amount attribute.
137      * @param indent positive integer (default is "2").
138      */

139     public void setAmount(String JavaDoc amount) {
140         _amount = amount;
141     }
142
143     /**
144      * Checks this EjenSaveNode for mandatory attributes.
145      * @throws org.ejen.EjenException if file attribute is not set.
146      */

147     public void check() {
148         super.check();
149         if (_file == null) {
150             throw new EjenException(this, "No 'file' attribute");
151         }
152     }
153     
154     /**
155      * Executes this EjenSaveNode.
156      * @throws org.ejen.EjenException if something goes wrong...
157      */

158     public void process() {
159         super.process();
160         TransformerFactoryImpl tfi = null;
161         DOMSource JavaDoc src = null;
162
163         try {
164             tfi = (TransformerFactoryImpl) (getFromGlobalContext(CTX_TRANSFORMER_FACTORY_IMPL));
165             src = (DOMSource JavaDoc) (getFromGlobalContext(CTX_DOM_SOURCE));
166         } catch (Exception JavaDoc e) {
167             throw new EjenException(this, null, e);
168         }
169         if (tfi == null) {
170             throw new EjenException(this,
171                     "no '" + CTX_TRANSFORMER_FACTORY_IMPL
172                     + "' in global context");
173         }
174         if (src == null) {
175             throw new EjenException(this,
176                     "no '" + CTX_DOM_SOURCE + "' in global context");
177         }
178         OutputStream JavaDoc outputs = null;
179
180         try {
181             File JavaDoc f = new File JavaDoc(evaluateAVT(_file));
182             File JavaDoc pf = f.getParentFile();
183
184             if (pf != null) {
185                 pf.mkdirs();
186             }
187             outputs = new FileOutputStream JavaDoc(f.getPath());
188             Transformer JavaDoc serializer = tfi.newTransformer();
189
190             serializer.setOutputProperty(OutputKeys.INDENT, evaluateAVT(_indent));
191             serializer.setOutputProperty(OutputKeys.METHOD, "xml");
192             serializer.setOutputProperty(OutputKeys.ENCODING,
193                     evaluateAVT(_encoding));
194             serializer.setOutputProperty(OutputProperties.S_KEY_INDENT_AMOUNT,
195                     evaluateAVT(_amount));
196             serializer.transform(src, new StreamResult JavaDoc(outputs));
197         } catch (Exception JavaDoc e) {
198             throw new EjenException(this, _file, e);
199         }
200         finally {
201             if (outputs != null) {
202                 try {
203                     outputs.close();
204                 } catch (Exception JavaDoc e) {}
205                 finally {
206                     outputs = null;
207                 }
208             }
209         }
210     }
211 }
212
Popular Tags