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.BufferedInputStream;
20 import java.io.FileInputStream;
21 import java.io.InputStreamReader;
22 import java.io.IOException;
23 import org.apache.commons.workflow.Context;
24 import org.apache.commons.workflow.StepException;
25 import org.apache.commons.workflow.base.BaseStep;
26
27
28 /**
29 * <p>Read the contents of the specified file from the filesystem, and
30 * push the contents as a String object onto the evaluation stack.</p>
31 *
32 * <p>Supported Attributes:</p>
33 * <ul>
34 * <li><strong>encoding</strong> - Character encoding in which to interpret
35 * the characters in the specified file, or omitted for the platform
36 * default encoding.</li>
37 * <li><strong>file</strong> - Relative or absolute operating system pathname
38 * whose contents are to be read.</li>
39 * </ul>
40 *
41 * <strong>DESIGN QUESTION - What about binary content?</strong>
42 *
43 * @version $Revision: 155475 $ $Date: 2005-02-26 13:31:11 +0000 (Sat, 26 Feb 2005) $
44 * @author Craig R. McClanahan
45 */
46
47 public class ReadStep extends BaseStep {
48
49
50 // ----------------------------------------------------------= Constructors
51
52
53 /**
54 * Construct a default instance of this Step.
55 */
56 public ReadStep() {
57
58 super();
59
60 }
61
62
63 /**
64 * Construct an instance of this Step with the specified identifier.
65 *
66 * @param id Step identifier
67 */
68 public ReadStep(String id) {
69
70 super();
71 setId(id);
72
73 }
74
75
76 /**
77 * Construct a fully configured instance of this Step.
78 *
79 * @param id Step identifier
80 * @param encoding Character encoding to use
81 * @param file Relative or absolute pathname
82 */
83 public ReadStep(String id, String encoding, String file) {
84
85 super();
86 setId(id);
87 setEncoding(encoding);
88 setFile(file);
89
90 }
91
92
93 // ------------------------------------------------------------- Properties
94
95
96 /**
97 * The character encoding used to interpret the contents of this file.
98 */
99 protected String encoding = null;
100
101 public String getEncoding() {
102 return (this.encoding);
103 }
104
105 public void setEncoding(String encoding) {
106 this.encoding = encoding;
107 }
108
109
110 /**
111 * The relative or absolute pathname of the operating system file.
112 */
113 protected String file = null;
114
115 public String getFile() {
116 return (this.file);
117 }
118
119 public void setFile(String file) {
120 this.file = file;
121 }
122
123
124 // --------------------------------------------------------- Public Methods
125
126
127 /**
128 * Perform the executable actions related to this Step, in the context of
129 * the specified Context.
130 *
131 * @param context The Context that is tracking our execution state
132 *
133 * @exception StepException if a processing error has occurred
134 */
135 public void execute(Context context) throws StepException {
136
137 // Define variables we will need later
138 FileInputStream fis = null;
139 BufferedInputStream bis = null;
140 InputStreamReader isr = null;
141 StringBuffer sb = new StringBuffer();
142 StepException se = null;
143
144 try {
145
146 // Construct a suitable InputStreamReader
147 fis = new FileInputStream(file);
148 bis = new BufferedInputStream(fis, 2048);
149 if (encoding == null)
150 isr = new InputStreamReader(bis);
151 else
152 isr = new InputStreamReader(bis, encoding);
153
154 // Copy all characters from this file
155 while (true) {
156 int ch = isr.read();
157 if (ch < 0)
158 break;
159 sb.append((char) ch);
160 }
161
162 // Close the input file
163 isr.close();
164 isr = null;
165 bis = null;
166 fis = null;
167
168 } catch (IOException e) {
169
170 se = new StepException("IOException processing '" + file + "'",
171 e, this);
172
173 } finally {
174
175 if (isr != null) {
176 try {
177 isr.close();
178 } catch (Throwable t) {
179 ;
180 }
181 } else if (bis != null) {
182 try {
183 bis.close();
184 } catch (Throwable t) {
185 ;
186 }
187 } else if (fis != null) {
188 try {
189 fis.close();
190 } catch (Throwable t) {
191 ;
192 }
193 }
194
195 }
196
197 // Push results or throw exception as appropriate
198 if (se != null)
199 throw se;
200 context.push(sb.toString());
201
202 }
203
204
205 }