1. /*
  2. * Copyright 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. * Determines the directory name of the specified file.
  23. *
  24. * This task can accept the following attributes:
  25. * <ul>
  26. * <li>file
  27. * <li>property
  28. * </ul>
  29. * Both <b>file</b> and <b>property</b> are required.
  30. * <p>
  31. * When this task executes, it will set the specified property to the
  32. * value of the specified file up to, but not including, the last path
  33. * element. If file is a file, the directory will be the current
  34. * directory.
  35. *
  36. *
  37. * @version $Revision: 1.7.2.4 $
  38. *
  39. * @since Ant 1.5
  40. *
  41. * @ant.task category="property"
  42. */
  43. public class Dirname extends Task {
  44. private File file;
  45. private String property;
  46. /**
  47. * Path to take the dirname of.
  48. * @param file
  49. */
  50. public void setFile(File file) {
  51. this.file = file;
  52. }
  53. /**
  54. * The name of the property to set.
  55. * @param property
  56. */
  57. public void setProperty(String property) {
  58. this.property = property;
  59. }
  60. // The method executing the task
  61. public void execute() throws BuildException {
  62. if (property == null) {
  63. throw new BuildException("property attribute required", getLocation());
  64. }
  65. if (file == null) {
  66. throw new BuildException("file attribute required", getLocation());
  67. } else {
  68. String value = file.getParent();
  69. getProject().setNewProperty(property, value);
  70. }
  71. }
  72. }