01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * 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;
22: import org.apache.tools.ant.BuildException;
23: import org.apache.tools.ant.Task;
24:
25: /**
26: * Determines the directory name of the specified file.
27: *
28: * This task can accept the following attributes:
29: * <ul>
30: * <li>file
31: * <li>property
32: * </ul>
33: * Both <b>file</b> and <b>property</b> are required.
34: * <p>
35: * When this task executes, it will set the specified property to the
36: * value of the specified file up to, but not including, the last path
37: * element. If file is a file, the directory will be the current
38: * directory.
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 a <code>File</code> value
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 the name of the property
61: */
62: public void setProperty(String property) {
63: this .property = property;
64: }
65:
66: /**
67: * Execute this task.
68: * @throws BuildException on error
69: */
70: public void execute() throws BuildException {
71: if (property == null) {
72: throw new BuildException("property attribute required",
73: getLocation());
74: }
75: if (file == null) {
76: throw new BuildException("file attribute required",
77: getLocation());
78: } else {
79: String value = file.getParent();
80: getProject().setNewProperty(property, value);
81: }
82: }
83: }
|