KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > Reference


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;
20
21 import org.apache.tools.ant.BuildException;
22 import org.apache.tools.ant.Project;
23
24 /**
25  * Class to hold a reference to another object in the project.
26  *
27  */

28 public class Reference {
29
30     private String JavaDoc refid;
31     private Project project;
32
33     /**
34      * Create a reference.
35      * @deprecated since 1.7.
36      * Please use {@link Reference#Reference(Project,String)}
37      * instead.
38      */

39     public Reference() {
40     }
41
42     /**
43      * Create a reference to a named ID.
44      * @param id the name of this reference
45      * @deprecated since 1.7.
46      * Please use {@link Reference#Reference(Project,String)}
47      * instead.
48      */

49     public Reference(String JavaDoc id) {
50         setRefId(id);
51     }
52
53     /**
54      * Create a reference to a named ID in a particular project.
55      * @param p the project this reference is associated with
56      * @param id the name of this reference
57      * @since Ant 1.6.3
58      */

59     public Reference(Project p, String JavaDoc id) {
60         setRefId(id);
61         setProject(p);
62     }
63
64     /**
65      * Set the reference id. Should not normally be necessary;
66      * use {@link Reference#Reference(Project, String)}.
67      * @param id the reference id to use
68      */

69     public void setRefId(String JavaDoc id) {
70         refid = id;
71     }
72
73     /**
74      * Get the reference id of this reference.
75      * @return the reference id
76      */

77     public String JavaDoc getRefId() {
78         return refid;
79     }
80
81     /**
82      * Set the associated project. Should not normally be necessary;
83      * use {@link Reference#Reference(Project,String)}.
84      * @param p the project to use
85      * @since Ant 1.6.3
86      */

87     public void setProject(Project p) {
88         this.project = p;
89     }
90
91     /**
92      * Get the associated project, if any; may be null.
93      * @return the associated project
94      * @since Ant 1.6.3
95      */

96     public Project getProject() {
97         return project;
98     }
99
100     /**
101      * Resolve the reference, using the associated project if
102      * it set, otherwise use the passed in project.
103      * @param fallback the fallback project to use if the project attribute of
104      * reference is not set.
105      * @return the dereferenced object.
106      * @throws BuildException if the reference cannot be dereferenced.
107      */

108     public Object JavaDoc getReferencedObject(Project fallback) throws BuildException {
109         if (refid == null) {
110             throw new BuildException("No reference specified");
111         }
112
113         Object JavaDoc o = project == null ? fallback.getReference(refid) : project.getReference(refid);
114         if (o == null) {
115             throw new BuildException("Reference " + refid + " not found.");
116         }
117         return o;
118     }
119
120     /**
121      * Resolve the reference, looking in the associated project.
122      * @see Project#getReference
123      * @return the dereferenced object.
124      * @throws BuildException if the project is null or the reference cannot be dereferenced
125      * @since Ant 1.6.3
126      */

127     public Object JavaDoc getReferencedObject() throws BuildException {
128         if (project == null) {
129             throw new BuildException("No project set on reference to " + refid);
130         }
131         return getReferencedObject(project);
132     }
133
134 }
135
Popular Tags