KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > Basename


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  */

18
19 package org.apache.tools.ant.taskdefs;
20
21 import java.io.File JavaDoc;
22 import org.apache.tools.ant.BuildException;
23 import org.apache.tools.ant.Task;
24
25 /**
26  * Sets a property to the base name of a specified file, optionally minus a
27  * suffix.
28  *
29  * This task can accept the following attributes:
30  * <ul>
31  * <li>file
32  * <li>property
33  * <li>suffix
34  * </ul>
35  * The <b>file</b> and <b>property</b> attributes are required. The
36  * <b>suffix</b> attribute can be specified either with or without
37  * the &quot;.&quot;, and the result will be the same (ie., the
38  * returned file name will be minus the .suffix).
39  * <p>
40  * When this task executes, it will set the specified property to the
41  * value of the last element in the specified file. If file is a
42  * directory, the basename will be the last directory element. If file
43  * is a full-path filename, the basename will be the simple file name.
44  * If a suffix is specified, and the specified file ends in that suffix,
45  * the basename will be the simple file name without the suffix.
46  *
47  *
48  * @since Ant 1.5
49  *
50  * @ant.task category="property"
51  */

52
53 public class Basename extends Task {
54     private File JavaDoc file;
55     private String JavaDoc property;
56     private String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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
110
Popular Tags