KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > resources > selectors > InstanceOf


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.types.resources.selectors;
20
21 import org.apache.tools.ant.Project;
22 import org.apache.tools.ant.ProjectHelper;
23 import org.apache.tools.ant.BuildException;
24 import org.apache.tools.ant.ComponentHelper;
25 import org.apache.tools.ant.AntTypeDefinition;
26 import org.apache.tools.ant.types.Resource;
27
28 /**
29  * InstanceOf ResourceSelector.
30  * @since Ant 1.7
31  */

32 public class InstanceOf implements ResourceSelector {
33     private static final String JavaDoc ONE_ONLY = "Exactly one of class|type must be set.";
34
35     private Project project;
36     private Class JavaDoc clazz;
37     private String JavaDoc type;
38     private String JavaDoc uri;
39
40     /**
41      * Set the Project instance for this InstanceOf selector.
42      * @param p the Project instance used for type comparisons.
43      */

44     public void setProject(Project p) {
45         project = p;
46     }
47
48     /**
49      * Set the class to compare against.
50      * @param c the class.
51      */

52     public void setClass(Class JavaDoc c) {
53         if (clazz != null) {
54             throw new BuildException("The class attribute has already been set.");
55         }
56         clazz = c;
57     }
58
59     /**
60      * Set the Ant type to compare against.
61      * @param s the type name.
62      */

63     public void setType(String JavaDoc s) {
64         type = s;
65     }
66
67     /**
68      * Set the URI in which the Ant type, if specified, should be defined.
69      * @param u the URI.
70      */

71     public void setURI(String JavaDoc u) {
72         uri = u;
73     }
74
75     /**
76      * Get the comparison class.
77      * @return the Class object.
78      */

79     public Class JavaDoc getCheckClass() {
80         return clazz;
81     }
82
83     /**
84      * Get the comparison type.
85      * @return the String typename.
86      */

87     public String JavaDoc getType() {
88         return type;
89     }
90
91     /**
92      * Get the type's URI.
93      * @return the String URI.
94      */

95     public String JavaDoc getURI() {
96         return uri;
97     }
98
99     /**
100      * Return true if this Resource is selected.
101      * @param r the Resource to check.
102      * @return whether the Resource was selected.
103      * @throws BuildException if an error occurs.
104      */

105     public boolean isSelected(Resource r) {
106         if ((clazz == null) == (type == null)) {
107             throw new BuildException(ONE_ONLY);
108         }
109         Class JavaDoc c = clazz;
110         if (type != null) {
111             if (project == null) {
112                 throw new BuildException(
113                     "No project set for InstanceOf ResourceSelector; "
114                     + "the type attribute is invalid.");
115             }
116             AntTypeDefinition d = ComponentHelper.getComponentHelper(
117                 project).getDefinition(ProjectHelper.genComponentName(uri, type));
118             if (d == null) {
119                 throw new BuildException("type " + type + " not found.");
120             }
121             try {
122                 c = d.innerGetTypeClass();
123             } catch (ClassNotFoundException JavaDoc e) {
124                 throw new BuildException(e);
125             }
126         }
127         return c.isAssignableFrom(r.getClass());
128     }
129
130 }
131
Popular Tags