01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: *
17: */
18: package task;
19:
20: import org.apache.tools.ant.Task;
21: import org.apache.tools.ant.BuildException;
22: import org.apache.tools.ant.util.FileUtils;
23: import java.io.File;
24: import java.io.BufferedInputStream;
25: import java.io.FileInputStream;
26: import java.io.FileOutputStream;
27: import java.io.InputStream;
28: import java.io.OutputStream;
29:
30: /**
31: * Base class for the uuencode/decode test tasks.
32: */
33: abstract public class BaseTask extends Task {
34: private final static FileUtils FILE_UTILS = FileUtils
35: .getFileUtils();
36: private File inFile;
37: private File outFile;
38:
39: public void setInFile(File inFile) {
40: this .inFile = inFile;
41: }
42:
43: protected File getInFile() {
44: return inFile;
45: }
46:
47: public void setOutFile(File outFile) {
48: this .outFile = outFile;
49: }
50:
51: protected File getOutFile() {
52: return outFile;
53: }
54:
55: public void execute() {
56: assertAttribute(inFile, "inFile");
57: assertAttribute(outFile, "outFile");
58: InputStream inputStream = null;
59: OutputStream outputStream = null;
60: try {
61: inputStream = new BufferedInputStream(new FileInputStream(
62: getInFile()));
63: outputStream = new FileOutputStream(getOutFile());
64: doit(inputStream, outputStream);
65: } catch (Exception ex) {
66: throw new BuildException(ex);
67: } finally {
68: FILE_UTILS.close(inputStream);
69: FILE_UTILS.close(outputStream);
70: }
71: }
72:
73: abstract protected void doit(InputStream is, OutputStream os)
74: throws Exception;
75:
76: private void assertAttribute(File file, String attributeName) {
77: if (file == null) {
78: throw new BuildException("Required attribute "
79: + attributeName + " not set");
80: }
81: }
82: }
|