001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.taskdefs;
020:
021: import java.io.File;
022: import org.apache.tools.ant.BuildException;
023: import org.apache.tools.ant.Task;
024: import org.apache.tools.ant.types.Resource;
025: import org.apache.tools.ant.types.ResourceCollection;
026: import org.apache.tools.ant.types.resources.FileResource;
027:
028: /**
029: * Abstract Base class for unpack tasks.
030: *
031: * @since Ant 1.5
032: */
033:
034: public abstract class Unpack extends Task {
035: // CheckStyle:VisibilityModifier OFF - bc
036: protected File source;
037: protected File dest;
038: protected Resource srcResource;
039:
040: // CheckStyle:VisibilityModifier ON
041:
042: /**
043: * @deprecated since 1.5.x.
044: * setSrc(String) is deprecated and is replaced with
045: * setSrc(File) to make Ant's Introspection
046: * mechanism do the work and also to encapsulate operations on
047: * the type in its own class.
048: * @ant.attribute ignore="true"
049: * @param src a <code>String</code> value
050: */
051: public void setSrc(String src) {
052: log("DEPRECATED - The setSrc(String) method has been deprecated."
053: + " Use setSrc(File) instead.");
054: setSrc(getProject().resolveFile(src));
055: }
056:
057: /**
058: * @deprecated since 1.5.x.
059: * setDest(String) is deprecated and is replaced with
060: * setDest(File) to make Ant's Introspection
061: * mechanism do the work and also to encapsulate operations on
062: * the type in its own class.
063: * @ant.attribute ignore="true"
064: * @param dest a <code>String</code> value
065: */
066: public void setDest(String dest) {
067: log("DEPRECATED - The setDest(String) method has been deprecated."
068: + " Use setDest(File) instead.");
069: setDest(getProject().resolveFile(dest));
070: }
071:
072: /**
073: * The file to expand; required.
074: * @param src file to expand
075: */
076: public void setSrc(File src) {
077: setSrcResource(new FileResource(src));
078: }
079:
080: /**
081: * The resource to expand; required.
082: * @param src resource to expand
083: */
084: public void setSrcResource(Resource src) {
085: if (!src.isExists()) {
086: throw new BuildException("the archive doesn't exist");
087: }
088: if (src.isDirectory()) {
089: throw new BuildException("the archive can't be a directory");
090: }
091: if (src instanceof FileResource) {
092: source = ((FileResource) src).getFile();
093: } else if (!supportsNonFileResources()) {
094: throw new BuildException("Only FileSystem resources are"
095: + " supported.");
096: }
097: srcResource = src;
098: }
099:
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(
107: "only single argument resource collections"
108: + " are supported as archives");
109: }
110: setSrcResource((Resource) a.iterator().next());
111: }
112:
113: /**
114: * The destination file or directory; optional.
115: * @param dest destination file or directory
116: */
117: public void setDest(File dest) {
118: this .dest = dest;
119: }
120:
121: private void validate() throws BuildException {
122: if (srcResource == null) {
123: throw new BuildException("No Src specified", getLocation());
124: }
125:
126: if (dest == null) {
127: dest = new File(source.getParent());
128: }
129:
130: if (dest.isDirectory()) {
131: String defaultExtension = getDefaultExtension();
132: createDestFile(defaultExtension);
133: }
134: }
135:
136: private void createDestFile(String defaultExtension) {
137: String sourceName = source.getName();
138: int len = sourceName.length();
139: if (defaultExtension != null
140: && len > defaultExtension.length()
141: && defaultExtension.equalsIgnoreCase(sourceName
142: .substring(len - defaultExtension.length()))) {
143: dest = new File(dest, sourceName.substring(0, len
144: - defaultExtension.length()));
145: } else {
146: dest = new File(dest, sourceName);
147: }
148: }
149:
150: /**
151: * Execute the task.
152: * @throws BuildException on error
153: */
154: public void execute() throws BuildException {
155: File savedDest = dest; // may be altered in validate
156: try {
157: validate();
158: extract();
159: } finally {
160: dest = savedDest;
161: }
162: }
163:
164: /**
165: * Get the extension.
166: * This is to be overridden by subclasses.
167: * @return the default extension.
168: */
169: protected abstract String getDefaultExtension();
170:
171: /**
172: * Do the uncompressing.
173: * This is to be overridden by subclasses.
174: */
175: protected abstract void extract();
176:
177: /**
178: * Whether this task can deal with non-file resources.
179: *
180: * <p>This implementation returns false.</p>
181: * @return false for this task.
182: * @since Ant 1.7
183: */
184: protected boolean supportsNonFileResources() {
185: return false;
186: }
187:
188: }
|