KickJava   Java API By Example, From Geeks To Geeks.

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


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.ProjectComponent;
23 import org.apache.tools.ant.types.Reference;
24
25 /**
26  * Condition that tests whether a given reference has been defined.
27  *
28  * <p>Optionally tests whether it is of a given type/class.</p>
29  *
30  * @since Ant 1.6
31  */

32 public class IsReference extends ProjectComponent implements Condition {
33     private Reference ref;
34     private String JavaDoc type;
35
36     /**
37      * Set the refid attribute.
38      *
39      * @param r a Reference value
40      */

41     public void setRefid(Reference r) {
42         ref = r;
43     }
44
45     /**
46      * Set the type attribute. This is optional attribute.
47      *
48      * @param type an ant component type name
49      */

50     public void setType(String JavaDoc type) {
51         this.type = type;
52     }
53
54     /**
55      * @return true if the reference exists and if type is set, if
56      * the reference is the same type
57      * @exception BuildException if an error occurs
58      */

59     public boolean eval() throws BuildException {
60         if (ref == null) {
61             throw new BuildException("No reference specified for isreference "
62                                      + "condition");
63         }
64
65         Object JavaDoc o = getProject().getReference(ref.getRefId());
66
67         if (o == null) {
68             return false;
69         } else if (type == null) {
70             return true;
71         } else {
72             Class JavaDoc typeClass =
73                 (Class JavaDoc) getProject().getDataTypeDefinitions().get(type);
74
75             if (typeClass == null) {
76                 typeClass =
77                     (Class JavaDoc) getProject().getTaskDefinitions().get(type);
78             }
79
80             if (typeClass == null) {
81                 // don't know the type, should throw exception instead?
82
return false;
83             }
84
85             return typeClass.isAssignableFrom(o.getClass());
86         }
87     }
88
89 }
90
Popular Tags