Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
ReadStep |
|
| 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.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 | 0 | super(); |
59 | ||
60 | 0 | } |
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 | 0 | super(); |
71 | 0 | setId(id); |
72 | ||
73 | 0 | } |
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 | 0 | super(); |
86 | 0 | setId(id); |
87 | 0 | setEncoding(encoding); |
88 | 0 | setFile(file); |
89 | ||
90 | 0 | } |
91 | ||
92 | ||
93 | // ------------------------------------------------------------- Properties | |
94 | ||
95 | ||
96 | /** | |
97 | * The character encoding used to interpret the contents of this file. | |
98 | */ | |
99 | 0 | protected String encoding = null; |
100 | ||
101 | public String getEncoding() { | |
102 | 0 | return (this.encoding); |
103 | } | |
104 | ||
105 | public void setEncoding(String encoding) { | |
106 | 0 | this.encoding = encoding; |
107 | 0 | } |
108 | ||
109 | ||
110 | /** | |
111 | * The relative or absolute pathname of the operating system file. | |
112 | */ | |
113 | 0 | protected String file = null; |
114 | ||
115 | public String getFile() { | |
116 | 0 | return (this.file); |
117 | } | |
118 | ||
119 | public void setFile(String file) { | |
120 | 0 | this.file = file; |
121 | 0 | } |
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 | 0 | FileInputStream fis = null; |
139 | 0 | BufferedInputStream bis = null; |
140 | 0 | InputStreamReader isr = null; |
141 | 0 | StringBuffer sb = new StringBuffer(); |
142 | 0 | StepException se = null; |
143 | ||
144 | try { | |
145 | ||
146 | // Construct a suitable InputStreamReader | |
147 | 0 | fis = new FileInputStream(file); |
148 | 0 | bis = new BufferedInputStream(fis, 2048); |
149 | 0 | if (encoding == null) |
150 | 0 | isr = new InputStreamReader(bis); |
151 | else | |
152 | 0 | isr = new InputStreamReader(bis, encoding); |
153 | ||
154 | // Copy all characters from this file | |
155 | while (true) { | |
156 | 0 | int ch = isr.read(); |
157 | 0 | if (ch < 0) |
158 | 0 | break; |
159 | 0 | sb.append((char) ch); |
160 | 0 | } |
161 | ||
162 | // Close the input file | |
163 | 0 | isr.close(); |
164 | 0 | isr = null; |
165 | 0 | bis = null; |
166 | 0 | fis = null; |
167 | ||
168 | 0 | } catch (IOException e) { |
169 | ||
170 | 0 | se = new StepException("IOException processing '" + file + "'", |
171 | e, this); | |
172 | ||
173 | } finally { | |
174 | ||
175 | 0 | if (isr != null) { |
176 | try { | |
177 | 0 | isr.close(); |
178 | 0 | } catch (Throwable t) { |
179 | ; | |
180 | 0 | } |
181 | 0 | } else if (bis != null) { |
182 | try { | |
183 | 0 | bis.close(); |
184 | 0 | } catch (Throwable t) { |
185 | ; | |
186 | 0 | } |
187 | 0 | } else if (fis != null) { |
188 | try { | |
189 | 0 | fis.close(); |
190 | 0 | } catch (Throwable t) { |
191 | ; | |
192 | 0 | } |
193 | } | |
194 | ||
195 | } | |
196 | ||
197 | // Push results or throw exception as appropriate | |
198 | 0 | if (se != null) |
199 | 0 | throw se; |
200 | 0 | context.push(sb.toString()); |
201 | ||
202 | 0 | } |
203 | ||
204 | ||
205 | } |