KickJava   Java API By Example, From Geeks To Geeks.

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


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
22 import java.io.File JavaDoc;
23 import org.apache.tools.ant.BuildException;
24 import org.apache.tools.ant.Task;
25 import org.apache.tools.ant.types.Resource;
26 import org.apache.tools.ant.types.ResourceCollection;
27 import org.apache.tools.ant.types.resources.FileResource;
28
29 /**
30  * Abstract Base class for unpack tasks.
31  *
32  * @since Ant 1.5
33  */

34
35 public abstract class Unpack extends Task {
36     // CheckStyle:VisibilityModifier OFF - bc
37
protected File JavaDoc source;
38     protected File JavaDoc dest;
39     protected Resource srcResource;
40     // CheckStyle:VisibilityModifier ON
41

42     /**
43      * @deprecated since 1.5.x.
44      * setSrc(String) is deprecated and is replaced with
45      * setSrc(File) to make Ant's Introspection
46      * mechanism do the work and also to encapsulate operations on
47      * the type in its own class.
48      * @ant.attribute ignore="true"
49      * @param src a <code>String</code> value
50      */

51     public void setSrc(String JavaDoc src) {
52         log("DEPRECATED - The setSrc(String) method has been deprecated."
53             + " Use setSrc(File) instead.");
54         setSrc(getProject().resolveFile(src));
55     }
56
57     /**
58      * @deprecated since 1.5.x.
59      * setDest(String) is deprecated and is replaced with
60      * setDest(File) to make Ant's Introspection
61      * mechanism do the work and also to encapsulate operations on
62      * the type in its own class.
63      * @ant.attribute ignore="true"
64      * @param dest a <code>String</code> value
65      */

66     public void setDest(String JavaDoc dest) {
67         log("DEPRECATED - The setDest(String) method has been deprecated."
68             + " Use setDest(File) instead.");
69         setDest(getProject().resolveFile(dest));
70     }
71
72     /**
73      * The file to expand; required.
74      * @param src file to expand
75      */

76     public void setSrc(File JavaDoc src) {
77         setSrcResource(new FileResource(src));
78     }
79
80     /**
81      * The resource to expand; required.
82      * @param src resource to expand
83      */

84     public void setSrcResource(Resource src) {
85         if (!src.isExists()) {
86             throw new BuildException("the archive doesn't exist");
87         }
88         if (src.isDirectory()) {
89             throw new BuildException("the archive can't be a directory");
90         }
91         if (src instanceof FileResource) {
92             source = ((FileResource) src).getFile();
93         } else if (!supportsNonFileResources()) {
94             throw new BuildException("Only FileSystem resources are"
95                                      + " supported.");
96         }
97         srcResource = src;
98     }
99
100     /**
101      * Set the source Archive resource.
102      * @param a the archive as a single element Resource collection.
103      */

104     public void addConfigured(ResourceCollection a) {
105         if (a.size() != 1) {
106             throw new BuildException("only single argument resource collections"
107                                      + " are supported as archives");
108         }
109         setSrcResource((Resource) a.iterator().next());
110     }
111
112     /**
113      * The destination file or directory; optional.
114      * @param dest destination file or directory
115      */

116     public void setDest(File JavaDoc dest) {
117         this.dest = dest;
118     }
119
120     private void validate() throws BuildException {
121         if (srcResource == null) {
122             throw new BuildException("No Src specified", getLocation());
123         }
124
125         if (dest == null) {
126             dest = new File JavaDoc(source.getParent());
127         }
128
129         if (dest.isDirectory()) {
130             String JavaDoc defaultExtension = getDefaultExtension();
131             createDestFile(defaultExtension);
132         }
133     }
134
135     private void createDestFile(String JavaDoc defaultExtension) {
136         String JavaDoc sourceName = source.getName();
137         int len = sourceName.length();
138         if (defaultExtension != null
139             && len > defaultExtension.length()
140             && defaultExtension.equalsIgnoreCase(
141                 sourceName.substring(len - defaultExtension.length()))) {
142             dest = new File JavaDoc(dest, sourceName.substring(0,
143                                                        len - defaultExtension.length()));
144         } else {
145             dest = new File JavaDoc(dest, sourceName);
146         }
147     }
148
149     /**
150      * Execute the task.
151      * @throws BuildException on error
152      */

153     public void execute() throws BuildException {
154         File JavaDoc savedDest = dest; // may be altered in validate
155
try {
156             validate();
157             extract();
158         } finally {
159             dest = savedDest;
160         }
161     }
162
163     /**
164      * Get the extension.
165      * This is to be overridden by subclasses.
166      * @return the default extension.
167      */

168     protected abstract String JavaDoc getDefaultExtension();
169
170     /**
171      * Do the uncompressing.
172      * This is to be overridden by subclasses.
173      */

174     protected abstract void extract();
175
176     /**
177      * Whether this task can deal with non-file resources.
178      *
179      * <p>This implementation returns false.</p>
180      * @return false for this task.
181      * @since Ant 1.7
182      */

183     protected boolean supportsNonFileResources() {
184         return false;
185     }
186
187 }
188
Popular Tags