KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > condition > TypeFound


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.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 JavaDoc name;
36     private String JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc typename) {
61
62         ComponentHelper helper =
63             ComponentHelper.getComponentHelper(getProject());
64         String JavaDoc componentName = ProjectHelper.genComponentName(uri, typename);
65         AntTypeDefinition def = helper.getDefinition(componentName);
66         if (def == null) {
67             return false;
68         }
69         //now verify that the class has an implementation
70
boolean found = def.getExposedClass(getProject()) != null;
71         if (!found) {
72             String JavaDoc text = helper.diagnoseCreationFailure(componentName, "type");
73             log(text, Project.MSG_VERBOSE);
74         }
75         return found;
76     }
77
78
79     /**
80      * Is this condition true?
81      * @return true if the condition is true
82      * @exception BuildException if an error occurs
83      */

84     public boolean eval() throws BuildException {
85         if (name == null) {
86             throw new BuildException("No type specified");
87         }
88         return doesTypeExist(name);
89     }
90 }
91
Popular Tags