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. * Sets a property to the base name of a specified file, optionally minus a
  23. * suffix.
  24. *
  25. * This task can accept the following attributes:
  26. * <ul>
  27. * <li>file
  28. * <li>property
  29. * <li>suffix
  30. * </ul>
  31. * The <b>file</b> and <b>property</b> attributes are required. The
  32. * <b>suffix</b> attribute can be specified either with or without
  33. * the ".", and the result will be the same (ie., the
  34. * returned file name will be minus the .suffix).
  35. * <p>
  36. * When this task executes, it will set the specified property to the
  37. * value of the last element in the specified file. If file is a
  38. * directory, the basename will be the last directory element. If file
  39. * is a full-path filename, the basename will be the simple file name.
  40. * If a suffix is specified, and the specified file ends in that suffix,
  41. * the basename will be the simple file name without the suffix.
  42. *
  43. *
  44. * @version $Revision: 1.10.2.4 $
  45. *
  46. * @since Ant 1.5
  47. *
  48. * @ant.task category="property"
  49. */
  50. public class Basename extends Task {
  51. private File file;
  52. private String property;
  53. private String suffix;
  54. /**
  55. * file or directory to get base name from
  56. * @param file file or directory to get base name from
  57. */
  58. public void setFile(File file) {
  59. this.file = file;
  60. }
  61. /**
  62. * Property to set base name to.
  63. * @param property name of property
  64. */
  65. public void setProperty(String property) {
  66. this.property = property;
  67. }
  68. /**
  69. * Optional suffix to remove from base name.
  70. * @param suffix suffix to remove from base name
  71. */
  72. public void setSuffix(String suffix) {
  73. this.suffix = suffix;
  74. }
  75. /**
  76. * do the work
  77. * @throws BuildException if required attributes are not supplied
  78. * property and attribute are required attributes
  79. */
  80. public void execute() throws BuildException {
  81. if (property == null) {
  82. throw new BuildException("property attribute required", getLocation());
  83. }
  84. if (file == null) {
  85. throw new BuildException("file attribute required", getLocation());
  86. }
  87. String value = file.getName();
  88. if (suffix != null && value.endsWith(suffix)) {
  89. // if the suffix does not starts with a '.' and the
  90. // char preceding the suffix is a '.', we assume the user
  91. // wants to remove the '.' as well (see docs)
  92. int pos = value.length() - suffix.length();
  93. if (pos > 0 && suffix.charAt(0) != '.'
  94. && value.charAt(pos - 1) == '.') {
  95. pos--;
  96. }
  97. value = value.substring(0, pos);
  98. }
  99. getProject().setNewProperty(property, value);
  100. }
  101. }