1 /*
2 * Copyright 1999-2001,2004 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 package org.apache.commons.workflow.io;
18
19 import java.io.BufferedOutputStream;
20 import java.io.File;
21 import java.io.FileOutputStream;
22 import java.io.IOException;
23 import java.io.OutputStreamWriter;
24 import java.util.EmptyStackException;
25 import org.apache.commons.workflow.Context;
26 import org.apache.commons.workflow.StepException;
27 import org.apache.commons.workflow.base.BaseStep;
28
29
30 /**
31 * <p>Pop the top value from the evaluation stack, and write its contents
32 * as a string to the specified file in the filesystem (replacing any
33 * previous contents in that file).</p>
34 *
35 * <p>Supported Attributes:</p>
36 * <ul>
37 * <li><strong>encoding</strong> - Character encoding in which to write
38 * the characters to the specified file, or omitted for the platform
39 * default encoding.</li>
40 * <li><strong>file</strong> - Relative or absolute operating system pathname
41 * whose contents are to be written.</li>
42 * </ul>
43 *
44 * <strong>DESIGN QUESTION - What about binary content?</strong>
45 *
46 * @version $Revision: 155475 $ $Date: 2005-02-26 13:31:11 +0000 (Sat, 26 Feb 2005) $
47 * @author Craig R. McClanahan
48 */
49
50 public class WriteStep extends BaseStep {
51
52
53 // ----------------------------------------------------------= Constructors
54
55
56 /**
57 * Construct a default instance of this Step.
58 */
59 public WriteStep() {
60
61 super();
62
63 }
64
65
66 /**
67 * Construct an instance of this Step with the specified identifier.
68 *
69 * @param id Step identifier
70 */
71 public WriteStep(String id) {
72
73 super();
74 setId(id);
75
76 }
77
78
79 /**
80 * Construct a fully configured instance of this Step.
81 *
82 * @param id Step identifier
83 * @param encoding Character encoding to use
84 * @param file Relative or absolute pathname
85 */
86 public WriteStep(String id, String encoding, String file) {
87
88 super();
89 setId(id);
90 setEncoding(encoding);
91 setFile(file);
92
93 }
94
95
96 // ------------------------------------------------------------- Properties
97
98
99 /**
100 * The character encoding used to write the contents of this file.
101 */
102 protected String encoding = null;
103
104 public String getEncoding() {
105 return (this.encoding);
106 }
107
108 public void setEncoding(String encoding) {
109 this.encoding = encoding;
110 }
111
112
113 /**
114 * The relative or absolute pathname of the operating system file.
115 */
116 protected String file = null;
117
118 public String getFile() {
119 return (this.file);
120 }
121
122 public void setFile(String file) {
123 this.file = file;
124 }
125
126
127 // --------------------------------------------------------- Public Methods
128
129
130 /**
131 * Perform the executable actions related to this Step, in the context of
132 * the specified Context.
133 *
134 * @param context The Context that is tracking our execution state
135 *
136 * @exception StepException if a processing error has occurred
137 */
138 public void execute(Context context) throws StepException {
139
140 // Pop the top value from the evaluation stack
141 Object value = null;
142 try {
143 value = context.pop();
144 } catch (EmptyStackException e) {
145 throw new StepException("Evaluation stack is empty", e, this);
146 }
147 String string = null;
148 if (value instanceof String)
149 string = (String) value;
150 else
151 string = value.toString();
152
153 // Define variables we will need later
154 FileOutputStream fos = null;
155 BufferedOutputStream bos = null;
156 OutputStreamWriter osw = null;
157 StepException se = null;
158
159 try {
160
161 // Construct a suitable OutputStreamWriter
162 fos = new FileOutputStream(file);
163 bos = new BufferedOutputStream(fos, 2048);
164 if (encoding == null)
165 osw = new OutputStreamWriter(bos);
166 else
167 osw = new OutputStreamWriter(bos, encoding);
168
169 // Copy all characters to this file
170 osw.write(string, 0, string.length());
171
172 // Close the output file
173 osw.flush();
174 osw.close();
175 osw = null;
176 bos = null;
177 fos = null;
178
179 } catch (IOException e) {
180
181 se = new StepException("IOException processing '" + file + "'",
182 e, this);
183
184 } finally {
185
186 if (osw != null) {
187 try {
188 osw.close();
189 } catch (Throwable t) {
190 ;
191 }
192 } else if (bos != null) {
193 try {
194 bos.close();
195 } catch (Throwable t) {
196 ;
197 }
198 } else if (fos != null) {
199 try {
200 fos.close();
201 } catch (Throwable t) {
202 ;
203 }
204 }
205
206 if (se != null)
207 (new File(file)).delete();
208
209 }
210
211 }
212
213
214 }