1 | /*
|
---|
2 | * Copyright 2001-2002,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 org.apache.tools.ant.BuildException;
|
---|
23 | import org.apache.tools.ant.Task;
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Abstract Base class for unpack tasks.
|
---|
27 | *
|
---|
28 | *
|
---|
29 | * @since Ant 1.5
|
---|
30 | */
|
---|
31 |
|
---|
32 | public abstract class Unpack extends Task {
|
---|
33 |
|
---|
34 | protected File source;
|
---|
35 | protected File dest;
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * @deprecated setSrc(String) is deprecated and is replaced with
|
---|
39 | * setSrc(File) to make Ant's Introspection
|
---|
40 | * mechanism do the work and also to encapsulate operations on
|
---|
41 | * the type in its own class.
|
---|
42 | * @ant.attribute ignore="true"
|
---|
43 | */
|
---|
44 | public void setSrc(String src) {
|
---|
45 | log("DEPRECATED - The setSrc(String) method has been deprecated."
|
---|
46 | + " Use setSrc(File) instead.");
|
---|
47 | setSrc(getProject().resolveFile(src));
|
---|
48 | }
|
---|
49 |
|
---|
50 | /**
|
---|
51 | * @deprecated setDest(String) is deprecated and is replaced with
|
---|
52 | * setDest(File) to make Ant's Introspection
|
---|
53 | * mechanism do the work and also to encapsulate operations on
|
---|
54 | * the type in its own class.
|
---|
55 | * @ant.attribute ignore="true"
|
---|
56 | */
|
---|
57 | public void setDest(String dest) {
|
---|
58 | log("DEPRECATED - The setDest(String) method has been deprecated."
|
---|
59 | + " Use setDest(File) instead.");
|
---|
60 | setDest(getProject().resolveFile(dest));
|
---|
61 | }
|
---|
62 |
|
---|
63 | /**
|
---|
64 | * The file to expand; required.
|
---|
65 | * @param src file to expand
|
---|
66 | */
|
---|
67 | public void setSrc(File src) {
|
---|
68 | source = src;
|
---|
69 | }
|
---|
70 |
|
---|
71 | /**
|
---|
72 | * The destination file or directory; optional.
|
---|
73 | * @param dest destination file or directory
|
---|
74 | */
|
---|
75 | public void setDest(File dest) {
|
---|
76 | this.dest = dest;
|
---|
77 | }
|
---|
78 |
|
---|
79 | private void validate() throws BuildException {
|
---|
80 | if (source == null) {
|
---|
81 | throw new BuildException("No Src specified", getLocation());
|
---|
82 | }
|
---|
83 |
|
---|
84 | if (!source.exists()) {
|
---|
85 | throw new BuildException("Src doesn't exist", getLocation());
|
---|
86 | }
|
---|
87 |
|
---|
88 | if (source.isDirectory()) {
|
---|
89 | throw new BuildException("Cannot expand a directory", getLocation());
|
---|
90 | }
|
---|
91 |
|
---|
92 | if (dest == null) {
|
---|
93 | dest = new File(source.getParent());
|
---|
94 | }
|
---|
95 |
|
---|
96 | if (dest.isDirectory()) {
|
---|
97 | String defaultExtension = getDefaultExtension();
|
---|
98 | createDestFile(defaultExtension);
|
---|
99 | }
|
---|
100 | }
|
---|
101 |
|
---|
102 | private void createDestFile(String defaultExtension) {
|
---|
103 | String sourceName = source.getName();
|
---|
104 | int len = sourceName.length();
|
---|
105 | if (defaultExtension != null
|
---|
106 | && len > defaultExtension.length()
|
---|
107 | && defaultExtension.equalsIgnoreCase(sourceName.substring(len - defaultExtension.length()))) {
|
---|
108 | dest = new File(dest, sourceName.substring(0,
|
---|
109 | len - defaultExtension.length()));
|
---|
110 | } else {
|
---|
111 | dest = new File(dest, sourceName);
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | public void execute() throws BuildException {
|
---|
116 | File savedDest = dest; // may be altered in validate
|
---|
117 | try {
|
---|
118 | validate();
|
---|
119 | extract();
|
---|
120 | } finally {
|
---|
121 | dest = savedDest;
|
---|
122 | }
|
---|
123 | }
|
---|
124 |
|
---|
125 | protected abstract String getDefaultExtension();
|
---|
126 | protected abstract void extract();
|
---|
127 | }
|
---|