You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Unpack.java 6.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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. package org.apache.tools.ant.taskdefs;
  19. import java.io.File;
  20. import org.apache.tools.ant.BuildException;
  21. import org.apache.tools.ant.Task;
  22. import org.apache.tools.ant.types.Resource;
  23. import org.apache.tools.ant.types.ResourceCollection;
  24. import org.apache.tools.ant.types.resources.FileProvider;
  25. import org.apache.tools.ant.types.resources.FileResource;
  26. /**
  27. * Abstract Base class for unpack tasks.
  28. *
  29. * @since Ant 1.5
  30. */
  31. public abstract class Unpack extends Task {
  32. // CheckStyle:VisibilityModifier OFF - bc
  33. protected File source;
  34. protected File dest;
  35. protected Resource srcResource;
  36. // CheckStyle:VisibilityModifier ON
  37. /**
  38. * @deprecated since 1.5.x.
  39. * setSrc(String) is deprecated and is replaced with
  40. * setSrc(File) to make Ant's Introspection
  41. * mechanism do the work and also to encapsulate operations on
  42. * the type in its own class.
  43. * @ant.attribute ignore="true"
  44. * @param src a <code>String</code> value
  45. */
  46. public void setSrc(String src) {
  47. log("DEPRECATED - The setSrc(String) method has been deprecated."
  48. + " Use setSrc(File) instead.");
  49. setSrc(getProject().resolveFile(src));
  50. }
  51. /**
  52. * @deprecated since 1.5.x.
  53. * setDest(String) is deprecated and is replaced with
  54. * setDest(File) to make Ant's Introspection
  55. * mechanism do the work and also to encapsulate operations on
  56. * the type in its own class.
  57. * @ant.attribute ignore="true"
  58. * @param dest a <code>String</code> value
  59. */
  60. public void setDest(String dest) {
  61. log("DEPRECATED - The setDest(String) method has been deprecated."
  62. + " Use setDest(File) instead.");
  63. setDest(getProject().resolveFile(dest));
  64. }
  65. /**
  66. * The file to expand; required.
  67. * @param src file to expand
  68. */
  69. public void setSrc(File src) {
  70. setSrcResource(new FileResource(src));
  71. }
  72. /**
  73. * The resource to expand; required.
  74. * @param src resource to expand
  75. */
  76. public void setSrcResource(Resource src) {
  77. if (!src.isExists()) {
  78. throw new BuildException(
  79. "the archive " + src.getName() + " doesn't exist");
  80. }
  81. if (src.isDirectory()) {
  82. throw new BuildException(
  83. "the archive " + src.getName() + " can't be a directory");
  84. }
  85. FileProvider fp = (FileProvider) src.as(FileProvider.class);
  86. if (fp != null) {
  87. source = fp.getFile();
  88. } else if (!supportsNonFileResources()) {
  89. throw new BuildException(
  90. "The source " + src.getName()
  91. + " is not a FileSystem "
  92. + "Only FileSystem resources are"
  93. + " supported.");
  94. }
  95. srcResource = src;
  96. }
  97. /**
  98. * Set the source Archive resource.
  99. * @param a the archive as a single element Resource collection.
  100. */
  101. public void addConfigured(ResourceCollection a) {
  102. if (a.size() != 1) {
  103. throw new BuildException("only single argument resource collections"
  104. + " are supported as archives");
  105. }
  106. setSrcResource((Resource) a.iterator().next());
  107. }
  108. /**
  109. * The destination file or directory; optional.
  110. * @param dest destination file or directory
  111. */
  112. public void setDest(File dest) {
  113. this.dest = dest;
  114. }
  115. private void validate() throws BuildException {
  116. if (srcResource == null) {
  117. throw new BuildException("No Src specified", getLocation());
  118. }
  119. if (dest == null) {
  120. dest = new File(source.getParent());
  121. }
  122. if (dest.isDirectory()) {
  123. String defaultExtension = getDefaultExtension();
  124. createDestFile(defaultExtension);
  125. }
  126. }
  127. private void createDestFile(String defaultExtension) {
  128. String sourceName = source.getName();
  129. int len = sourceName.length();
  130. if (defaultExtension != null
  131. && len > defaultExtension.length()
  132. && defaultExtension.equalsIgnoreCase(
  133. sourceName.substring(len - defaultExtension.length()))) {
  134. dest = new File(dest, sourceName.substring(0,
  135. len - defaultExtension.length()));
  136. } else {
  137. dest = new File(dest, sourceName);
  138. }
  139. }
  140. /**
  141. * Execute the task.
  142. * @throws BuildException on error
  143. */
  144. public void execute() throws BuildException {
  145. File savedDest = dest; // may be altered in validate
  146. try {
  147. validate();
  148. extract();
  149. } finally {
  150. dest = savedDest;
  151. }
  152. }
  153. /**
  154. * Get the extension.
  155. * This is to be overridden by subclasses.
  156. * @return the default extension.
  157. */
  158. protected abstract String getDefaultExtension();
  159. /**
  160. * Do the uncompressing.
  161. * This is to be overridden by subclasses.
  162. */
  163. protected abstract void extract();
  164. /**
  165. * Whether this task can deal with non-file resources.
  166. *
  167. * <p>This implementation returns false.</p>
  168. * @return false for this task.
  169. * @since Ant 1.7
  170. */
  171. protected boolean supportsNonFileResources() {
  172. return false;
  173. }
  174. }