Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
WriteStep |
|
| 2.5;2.5 |
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 | 0 | super(); |
62 | ||
63 | 0 | } |
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 | 0 | super(); |
74 | 0 | setId(id); |
75 | ||
76 | 0 | } |
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 | 0 | super(); |
89 | 0 | setId(id); |
90 | 0 | setEncoding(encoding); |
91 | 0 | setFile(file); |
92 | ||
93 | 0 | } |
94 | ||
95 | ||
96 | // ------------------------------------------------------------- Properties | |
97 | ||
98 | ||
99 | /** | |
100 | * The character encoding used to write the contents of this file. | |
101 | */ | |
102 | 0 | protected String encoding = null; |
103 | ||
104 | public String getEncoding() { | |
105 | 0 | return (this.encoding); |
106 | } | |
107 | ||
108 | public void setEncoding(String encoding) { | |
109 | 0 | this.encoding = encoding; |
110 | 0 | } |
111 | ||
112 | ||
113 | /** | |
114 | * The relative or absolute pathname of the operating system file. | |
115 | */ | |
116 | 0 | protected String file = null; |
117 | ||
118 | public String getFile() { | |
119 | 0 | return (this.file); |
120 | } | |
121 | ||
122 | public void setFile(String file) { | |
123 | 0 | this.file = file; |
124 | 0 | } |
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 | 0 | Object value = null; |
142 | try { | |
143 | 0 | value = context.pop(); |
144 | 0 | } catch (EmptyStackException e) { |
145 | 0 | throw new StepException("Evaluation stack is empty", e, this); |
146 | 0 | } |
147 | 0 | String string = null; |
148 | 0 | if (value instanceof String) |
149 | 0 | string = (String) value; |
150 | else | |
151 | 0 | string = value.toString(); |
152 | ||
153 | // Define variables we will need later | |
154 | 0 | FileOutputStream fos = null; |
155 | 0 | BufferedOutputStream bos = null; |
156 | 0 | OutputStreamWriter osw = null; |
157 | 0 | StepException se = null; |
158 | ||
159 | try { | |
160 | ||
161 | // Construct a suitable OutputStreamWriter | |
162 | 0 | fos = new FileOutputStream(file); |
163 | 0 | bos = new BufferedOutputStream(fos, 2048); |
164 | 0 | if (encoding == null) |
165 | 0 | osw = new OutputStreamWriter(bos); |
166 | else | |
167 | 0 | osw = new OutputStreamWriter(bos, encoding); |
168 | ||
169 | // Copy all characters to this file | |
170 | 0 | osw.write(string, 0, string.length()); |
171 | ||
172 | // Close the output file | |
173 | 0 | osw.flush(); |
174 | 0 | osw.close(); |
175 | 0 | osw = null; |
176 | 0 | bos = null; |
177 | 0 | fos = null; |
178 | ||
179 | 0 | } catch (IOException e) { |
180 | ||
181 | 0 | se = new StepException("IOException processing '" + file + "'", |
182 | e, this); | |
183 | ||
184 | } finally { | |
185 | ||
186 | 0 | if (osw != null) { |
187 | try { | |
188 | 0 | osw.close(); |
189 | 0 | } catch (Throwable t) { |
190 | ; | |
191 | 0 | } |
192 | 0 | } else if (bos != null) { |
193 | try { | |
194 | 0 | bos.close(); |
195 | 0 | } catch (Throwable t) { |
196 | ; | |
197 | 0 | } |
198 | 0 | } else if (fos != null) { |
199 | try { | |
200 | 0 | fos.close(); |
201 | 0 | } catch (Throwable t) { |
202 | ; | |
203 | 0 | } |
204 | } | |
205 | ||
206 | 0 | if (se != null) |
207 | 0 | (new File(file)).delete(); |
208 | ||
209 | } | |
210 | ||
211 | 0 | } |
212 | ||
213 | ||
214 | } |