001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: */
018:
019: package org.apache.tools.ant.types;
020:
021: import org.apache.tools.ant.BuildException;
022: import org.apache.tools.ant.Project;
023:
024: /**
025: * Class to hold a reference to another object in the project.
026: *
027: */
028: public class Reference {
029:
030: private String refid;
031: private Project project;
032:
033: /**
034: * Create a reference.
035: * @deprecated since 1.7.
036: * Please use {@link Reference#Reference(Project,String)}
037: * instead.
038: */
039: public Reference() {
040: }
041:
042: /**
043: * Create a reference to a named ID.
044: * @param id the name of this reference
045: * @deprecated since 1.7.
046: * Please use {@link Reference#Reference(Project,String)}
047: * instead.
048: */
049: public Reference(String id) {
050: setRefId(id);
051: }
052:
053: /**
054: * Create a reference to a named ID in a particular project.
055: * @param p the project this reference is associated with
056: * @param id the name of this reference
057: * @since Ant 1.6.3
058: */
059: public Reference(Project p, String id) {
060: setRefId(id);
061: setProject(p);
062: }
063:
064: /**
065: * Set the reference id. Should not normally be necessary;
066: * use {@link Reference#Reference(Project, String)}.
067: * @param id the reference id to use
068: */
069: public void setRefId(String id) {
070: refid = id;
071: }
072:
073: /**
074: * Get the reference id of this reference.
075: * @return the reference id
076: */
077: public String getRefId() {
078: return refid;
079: }
080:
081: /**
082: * Set the associated project. Should not normally be necessary;
083: * use {@link Reference#Reference(Project,String)}.
084: * @param p the project to use
085: * @since Ant 1.6.3
086: */
087: public void setProject(Project p) {
088: this .project = p;
089: }
090:
091: /**
092: * Get the associated project, if any; may be null.
093: * @return the associated project
094: * @since Ant 1.6.3
095: */
096: public Project getProject() {
097: return project;
098: }
099:
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 getReferencedObject(Project fallback)
109: throws BuildException {
110: if (refid == null) {
111: throw new BuildException("No reference specified");
112: }
113:
114: Object o = project == null ? fallback.getReference(refid)
115: : project.getReference(refid);
116: if (o == null) {
117: throw new BuildException("Reference " + refid
118: + " not found.");
119: }
120: return o;
121: }
122:
123: /**
124: * Resolve the reference, looking in the associated project.
125: * @see Project#getReference
126: * @return the dereferenced object.
127: * @throws BuildException if the project is null or the reference cannot be dereferenced
128: * @since Ant 1.6.3
129: */
130: public Object getReferencedObject() throws BuildException {
131: if (project == null) {
132: throw new BuildException("No project set on reference to "
133: + refid);
134: }
135: return getReferencedObject(project);
136: }
137:
138: }
|