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