1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50 public class WriteStep extends BaseStep {
51
52
53
54
55
56
57
58
59 public WriteStep() {
60
61 super();
62
63 }
64
65
66
67
68
69
70
71 public WriteStep(String id) {
72
73 super();
74 setId(id);
75
76 }
77
78
79
80
81
82
83
84
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
97
98
99
100
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
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
128
129
130
131
132
133
134
135
136
137
138 public void execute(Context context) throws StepException {
139
140
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
154 FileOutputStream fos = null;
155 BufferedOutputStream bos = null;
156 OutputStreamWriter osw = null;
157 StepException se = null;
158
159 try {
160
161
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
170 osw.write(string, 0, string.length());
171
172
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 }