1 | /*
|
---|
2 | * Copyright 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 |
|
---|
18 | package org.apache.tools.ant.taskdefs;
|
---|
19 |
|
---|
20 |
|
---|
21 | import java.io.File;
|
---|
22 | import java.io.FileInputStream;
|
---|
23 | import java.io.IOException;
|
---|
24 | import java.io.InputStream;
|
---|
25 | import java.io.OutputStream;
|
---|
26 | import org.apache.tools.ant.BuildException;
|
---|
27 | import org.apache.tools.ant.Task;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Abstract Base class for pack tasks.
|
---|
31 | *
|
---|
32 | *
|
---|
33 | * @since Ant 1.5
|
---|
34 | */
|
---|
35 |
|
---|
36 | public abstract class Pack extends Task {
|
---|
37 |
|
---|
38 | protected File zipFile;
|
---|
39 | protected File source;
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * the required destination file.
|
---|
43 | * @param zipFile
|
---|
44 | */
|
---|
45 | public void setZipfile(File zipFile) {
|
---|
46 | this.zipFile = zipFile;
|
---|
47 | }
|
---|
48 |
|
---|
49 | /**
|
---|
50 | * the required destination file.
|
---|
51 | * @param zipFile
|
---|
52 | */
|
---|
53 | public void setDestfile(File zipFile) {
|
---|
54 | setZipfile(zipFile);
|
---|
55 | }
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * the file to compress; required.
|
---|
59 | * @param src
|
---|
60 | */
|
---|
61 | public void setSrc(File src) {
|
---|
62 | source = src;
|
---|
63 | }
|
---|
64 |
|
---|
65 |
|
---|
66 | /**
|
---|
67 | * validation routine
|
---|
68 | * @throws BuildException if anything is invalid
|
---|
69 | */
|
---|
70 | private void validate() throws BuildException {
|
---|
71 | if (zipFile == null) {
|
---|
72 | throw new BuildException("zipfile attribute is required", getLocation());
|
---|
73 | }
|
---|
74 |
|
---|
75 | if (zipFile.isDirectory()) {
|
---|
76 | throw new BuildException("zipfile attribute must not "
|
---|
77 | + "represent a directory!", getLocation());
|
---|
78 | }
|
---|
79 |
|
---|
80 | if (source == null) {
|
---|
81 | throw new BuildException("src attribute is required", getLocation());
|
---|
82 | }
|
---|
83 |
|
---|
84 | if (source.isDirectory()) {
|
---|
85 | throw new BuildException("Src attribute must not "
|
---|
86 | + "represent a directory!", getLocation());
|
---|
87 | }
|
---|
88 | }
|
---|
89 |
|
---|
90 | /**
|
---|
91 | * validate, then hand off to the subclass
|
---|
92 | * @throws BuildException
|
---|
93 | */
|
---|
94 | public void execute() throws BuildException {
|
---|
95 | validate();
|
---|
96 |
|
---|
97 | if (!source.exists()) {
|
---|
98 | log("Nothing to do: " + source.getAbsolutePath()
|
---|
99 | + " doesn't exist.");
|
---|
100 | } else if (zipFile.lastModified() < source.lastModified()) {
|
---|
101 | log("Building: " + zipFile.getAbsolutePath());
|
---|
102 | pack();
|
---|
103 | } else {
|
---|
104 | log("Nothing to do: " + zipFile.getAbsolutePath()
|
---|
105 | + " is up to date.");
|
---|
106 | }
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * zip a stream to an output stream
|
---|
111 | * @param in
|
---|
112 | * @param zOut
|
---|
113 | * @throws IOException
|
---|
114 | */
|
---|
115 | private void zipFile(InputStream in, OutputStream zOut)
|
---|
116 | throws IOException {
|
---|
117 | byte[] buffer = new byte[8 * 1024];
|
---|
118 | int count = 0;
|
---|
119 | do {
|
---|
120 | zOut.write(buffer, 0, count);
|
---|
121 | count = in.read(buffer, 0, buffer.length);
|
---|
122 | } while (count != -1);
|
---|
123 | }
|
---|
124 |
|
---|
125 | /**
|
---|
126 | * zip a file to an output stream
|
---|
127 | * @param file
|
---|
128 | * @param zOut
|
---|
129 | * @throws IOException
|
---|
130 | */
|
---|
131 | protected void zipFile(File file, OutputStream zOut)
|
---|
132 | throws IOException {
|
---|
133 | FileInputStream fIn = new FileInputStream(file);
|
---|
134 | try {
|
---|
135 | zipFile(fIn, zOut);
|
---|
136 | } finally {
|
---|
137 | fIn.close();
|
---|
138 | }
|
---|
139 | }
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * subclasses must implement this method to do their compression
|
---|
143 | */
|
---|
144 | protected abstract void pack();
|
---|
145 | }
|
---|