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 4.3 kB

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