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.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 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 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(
62: "No reference specified for isreference "
63: + "condition");
64: }
65:
66: Object o = getProject().getReference(ref.getRefId());
67:
68: if (o == null) {
69: return false;
70: } else if (type == null) {
71: return true;
72: } else {
73: Class typeClass = (Class) getProject()
74: .getDataTypeDefinitions().get(type);
75:
76: if (typeClass == null) {
77: typeClass = (Class) getProject().getTaskDefinitions()
78: .get(type);
79: }
80:
81: if (typeClass == null) {
82: // don't know the type, should throw exception instead?
83: return false;
84: }
85:
86: return typeClass.isAssignableFrom(o.getClass());
87: }
88: }
89:
90: }
|