KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > Pack


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * 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
19 package org.apache.tools.ant.taskdefs;
20
21 import java.io.File JavaDoc;
22 import java.io.IOException JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.OutputStream JavaDoc;
25 import org.apache.tools.ant.BuildException;
26 import org.apache.tools.ant.Task;
27 import org.apache.tools.ant.types.Resource;
28 import org.apache.tools.ant.types.ResourceCollection;
29 import org.apache.tools.ant.types.resources.FileResource;
30
31 /**
32  * Abstract Base class for pack tasks.
33  *
34  * @since Ant 1.5
35  */

36
37 public abstract class Pack extends Task {
38
39     // CheckStyle:VisibilityModifier OFF - bc
40
protected File JavaDoc zipFile;
41     protected File JavaDoc source;
42     // CheckStyle:VisibilityModifier ON
43
private Resource src;
44
45     /**
46      * the required destination file.
47      * @param zipFile the destination file
48      */

49     public void setZipfile(File JavaDoc zipFile) {
50         this.zipFile = zipFile;
51     }
52
53     /**
54      * the required destination file.
55      * @param zipFile the destination file
56      */

57     public void setDestfile(File JavaDoc zipFile) {
58         setZipfile(zipFile);
59     }
60
61     /**
62      * the file to compress; required.
63      * @param src the source file
64      */

65     public void setSrc(File JavaDoc src) {
66         setSrcResource(new FileResource(src));
67     }
68
69     /**
70      * The resource to pack; required.
71      * @param src resource to expand
72      */

73     public void setSrcResource(Resource src) {
74         if (src.isDirectory()) {
75             throw new BuildException("the source can't be a directory");
76         }
77         if (src instanceof FileResource) {
78             source = ((FileResource) src).getFile();
79         } else if (!supportsNonFileResources()) {
80             throw new BuildException("Only FileSystem resources are"
81                                      + " supported.");
82         }
83         this.src = src;
84     }
85
86     /**
87      * Set the source resource.
88      * @param a the resource to pack as a single element Resource collection.
89      */

90     public void addConfigured(ResourceCollection a) {
91         if (a.size() != 1) {
92             throw new BuildException("only single argument resource collections"
93                                      + " are supported as archives");
94         }
95         setSrcResource((Resource) a.iterator().next());
96     }
97
98     /**
99      * validation routine
100      * @throws BuildException if anything is invalid
101      */

102     private void validate() throws BuildException {
103         if (zipFile == null) {
104             throw new BuildException("zipfile attribute is required", getLocation());
105         }
106
107         if (zipFile.isDirectory()) {
108             throw new BuildException("zipfile attribute must not "
109                                     + "represent a directory!", getLocation());
110         }
111
112         if (getSrcResource() == null) {
113             throw new BuildException("src attribute or nested resource is"
114                                      + " required", getLocation());
115         }
116     }
117
118     /**
119      * validate, then hand off to the subclass
120      * @throws BuildException on error
121      */

122     public void execute() throws BuildException {
123         validate();
124
125         Resource s = getSrcResource();
126         if (!s.isExists()) {
127             log("Nothing to do: " + s.toString()
128                 + " doesn't exist.");
129         } else if (zipFile.lastModified() < s.getLastModified()) {
130             log("Building: " + zipFile.getAbsolutePath());
131             pack();
132         } else {
133             log("Nothing to do: " + zipFile.getAbsolutePath()
134                 + " is up to date.");
135         }
136     }
137
138     /**
139      * zip a stream to an output stream
140      * @param in the stream to zip
141      * @param zOut the output stream
142      * @throws IOException
143      */

144     private void zipFile(InputStream JavaDoc in, OutputStream JavaDoc zOut)
145         throws IOException JavaDoc {
146         byte[] buffer = new byte[8 * 1024];
147         int count = 0;
148         do {
149             zOut.write(buffer, 0, count);
150             count = in.read(buffer, 0, buffer.length);
151         } while (count != -1);
152     }
153
154     /**
155      * zip a file to an output stream
156      * @param file the file to zip
157      * @param zOut the output stream
158      * @throws IOException on error
159      */

160     protected void zipFile(File JavaDoc file, OutputStream JavaDoc zOut)
161         throws IOException JavaDoc {
162         zipResource(new FileResource(file), zOut);
163     }
164
165     /**
166      * zip a resource to an output stream
167      * @param resource the resource to zip
168      * @param zOut the output stream
169      * @throws IOException on error
170      */

171     protected void zipResource(Resource resource, OutputStream JavaDoc zOut)
172         throws IOException JavaDoc {
173         InputStream JavaDoc rIn = resource.getInputStream();
174         try {
175             zipFile(rIn, zOut);
176         } finally {
177             rIn.close();
178         }
179     }
180
181     /**
182      * subclasses must implement this method to do their compression
183      */

184     protected abstract void pack();
185
186     /**
187      * The source resource.
188      * @return the source.
189      * @since Ant 1.7
190      */

191     public Resource getSrcResource() {
192         return src;
193     }
194
195     /**
196      * Whether this task can deal with non-file resources.
197      *
198      * <p>This implementation returns false.</p>
199      * @return false.
200      * @since Ant 1.7
201      */

202     protected boolean supportsNonFileResources() {
203         return false;
204     }
205 }
206
Popular Tags