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