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