1 | /*
|
---|
2 | * Copyright 2000-2002,2004-2005 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 |
|
---|
18 | package org.apache.tools.ant.types;
|
---|
19 |
|
---|
20 | import org.apache.tools.ant.BuildException;
|
---|
21 | import org.apache.tools.ant.Project;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Class to hold a reference to another object in the project.
|
---|
25 | *
|
---|
26 | */
|
---|
27 | public class Reference {
|
---|
28 |
|
---|
29 | private String refid;
|
---|
30 | private Project project;
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Create a reference.
|
---|
34 | * @deprecated Please use {@link Reference#Reference(Project,String)} instead.
|
---|
35 | */
|
---|
36 | public Reference() {
|
---|
37 | }
|
---|
38 |
|
---|
39 | /**
|
---|
40 | * Create a reference to a named ID.
|
---|
41 | * @param id the name of this reference
|
---|
42 | * @deprecated Please use {@link Reference#Reference(Project,String)} instead.
|
---|
43 | */
|
---|
44 | public Reference(String id) {
|
---|
45 | setRefId(id);
|
---|
46 | }
|
---|
47 |
|
---|
48 | /**
|
---|
49 | * Create a reference to a named ID in a particular project.
|
---|
50 | * @param p the project this reference is associated with
|
---|
51 | * @param id the name of this reference
|
---|
52 | * @since Ant 1.6.3
|
---|
53 | */
|
---|
54 | public Reference(Project p, String id) {
|
---|
55 | setRefId(id);
|
---|
56 | setProject(p);
|
---|
57 | }
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * Set the reference id. Should not normally be necessary;
|
---|
61 | * use {@link Reference#Reference(Project, String)}.
|
---|
62 | * @param id the reference id to use
|
---|
63 | */
|
---|
64 | public void setRefId(String id) {
|
---|
65 | refid = id;
|
---|
66 | }
|
---|
67 |
|
---|
68 | /**
|
---|
69 | * Get the reference id of this reference.
|
---|
70 | * @return the reference id
|
---|
71 | */
|
---|
72 | public String getRefId() {
|
---|
73 | return refid;
|
---|
74 | }
|
---|
75 |
|
---|
76 | /**
|
---|
77 | * Set the associated project. Should not normally be necessary;
|
---|
78 | * use {@link Reference#Reference(Project,String)}.
|
---|
79 | * @param p the project to use
|
---|
80 | * @since Ant 1.6.3
|
---|
81 | */
|
---|
82 | public void setProject(Project p) {
|
---|
83 | this.project = p;
|
---|
84 | }
|
---|
85 |
|
---|
86 | /**
|
---|
87 | * Get the associated project, if any; may be null.
|
---|
88 | * @return the associated project
|
---|
89 | * @since Ant 1.6.3
|
---|
90 | */
|
---|
91 | public Project getProject() {
|
---|
92 | return project;
|
---|
93 | }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Resolve the reference, using the associated project if
|
---|
97 | * it set, otherwise use the passed in project.
|
---|
98 | * @param fallback the fallback project to use if the project attribute of
|
---|
99 | * reference is not set.
|
---|
100 | * @return the dereferenced object.
|
---|
101 | * @throws BuildException if the reference cannot be dereferenced.
|
---|
102 | */
|
---|
103 | public Object getReferencedObject(Project fallback) throws BuildException {
|
---|
104 | if (refid == null) {
|
---|
105 | throw new BuildException("No reference specified");
|
---|
106 | }
|
---|
107 |
|
---|
108 | Object o = project == null ? fallback.getReference(refid) : project.getReference(refid);
|
---|
109 | if (o == null) {
|
---|
110 | throw new BuildException("Reference " + refid + " not found.");
|
---|
111 | }
|
---|
112 | return o;
|
---|
113 | }
|
---|
114 |
|
---|
115 | /**
|
---|
116 | * Resolve the reference, looking in the associated project.
|
---|
117 | * @see Project#getReference
|
---|
118 | * @return the dereferenced object.
|
---|
119 | * @throws BuildException if the project is null or the reference cannot be dereferenced
|
---|
120 | * @since Ant 1.6.3
|
---|
121 | */
|
---|
122 | public Object getReferencedObject() throws BuildException {
|
---|
123 | if (project == null) {
|
---|
124 | throw new BuildException("No project set on reference to " + refid);
|
---|
125 | }
|
---|
126 | return getReferencedObject(project);
|
---|
127 | }
|
---|
128 |
|
---|
129 | }
|
---|