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.condition;
20:
21: import org.apache.tools.ant.BuildException;
22: import org.apache.tools.ant.ComponentHelper;
23: import org.apache.tools.ant.ProjectComponent;
24: import org.apache.tools.ant.ProjectHelper;
25: import org.apache.tools.ant.AntTypeDefinition;
26: import org.apache.tools.ant.Project;
27:
28: /**
29: * looks for a task or other Ant type that exists. Existence is defined as
30: * the type is defined, and its implementation class is present. This
31: * will work for datatypes and preset, script and macro definitions.
32: */
33: public class TypeFound extends ProjectComponent implements Condition {
34:
35: private String name;
36: private String uri;
37:
38: /**
39: * the task or other type to look for
40: * @param name the name of the type
41: */
42: public void setName(String name) {
43: this .name = name;
44: }
45:
46: /**
47: * The URI for this definition.
48: * @param uri the namespace URI. If this is not set, use the
49: * default ant namespace.
50: */
51: public void setURI(String uri) {
52: this .uri = uri;
53: }
54:
55: /**
56: * test for a task or other ant type existing in the current project
57: * @param typename the name of the type
58: * @return true if the typename exists
59: */
60: protected boolean doesTypeExist(String typename) {
61:
62: ComponentHelper helper = ComponentHelper
63: .getComponentHelper(getProject());
64: String componentName = ProjectHelper.genComponentName(uri,
65: typename);
66: AntTypeDefinition def = helper.getDefinition(componentName);
67: if (def == null) {
68: return false;
69: }
70: //now verify that the class has an implementation
71: boolean found = def.getExposedClass(getProject()) != null;
72: if (!found) {
73: String text = helper.diagnoseCreationFailure(componentName,
74: "type");
75: log(text, Project.MSG_VERBOSE);
76: }
77: return found;
78: }
79:
80: /**
81: * Is this condition true?
82: * @return true if the condition is true
83: * @exception BuildException if an error occurs
84: */
85: public boolean eval() throws BuildException {
86: if (name == null) {
87: throw new BuildException("No type specified");
88: }
89: return doesTypeExist(name);
90: }
91: }
|