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.taskdefs;
020:
021: import java.io.IOException;
022:
023: import org.apache.tools.ant.Task;
024: import org.apache.tools.ant.BuildException;
025: import org.apache.tools.ant.types.PropertySet;
026:
027: /**
028: * Call another target in the same project.
029: *
030: * <pre>
031: * <target name="foo">
032: * <antcall target="bar">
033: * <param name="property1" value="aaaaa" />
034: * <param name="foo" value="baz" />
035: * </antcall>
036: * </target>
037: *
038: * <target name="bar" depends="init">
039: * <echo message="prop is ${property1} ${foo}" />
040: * </target>
041: * </pre>
042: *
043: * <p>This only works as expected if neither property1 nor foo are
044: * defined in the project itself.
045: *
046: *
047: * @since Ant 1.2
048: *
049: * @ant.task name="antcall" category="control"
050: */
051: public class CallTarget extends Task {
052:
053: private Ant callee;
054: // must match the default value of Ant#inheritAll
055: private boolean inheritAll = true;
056: // must match the default value of Ant#inheritRefs
057: private boolean inheritRefs = false;
058:
059: private boolean targetSet = false;
060:
061: /**
062: * If true, pass all properties to the new Ant project.
063: * Defaults to true.
064: * @param inherit <code>boolean</code> flag.
065: */
066: public void setInheritAll(boolean inherit) {
067: inheritAll = inherit;
068: }
069:
070: /**
071: * If true, pass all references to the new Ant project.
072: * Defaults to false.
073: * @param inheritRefs <code>boolean</code> flag.
074: */
075: public void setInheritRefs(boolean inheritRefs) {
076: this .inheritRefs = inheritRefs;
077: }
078:
079: /**
080: * Initialize this task by creating new instance of the ant task and
081: * configuring it by calling its own init method.
082: */
083: public void init() {
084: callee = new Ant(this );
085: callee.init();
086: }
087:
088: /**
089: * Delegate the work to the ant task instance, after setting it up.
090: * @throws BuildException on validation failure or if the target didn't
091: * execute.
092: */
093: public void execute() throws BuildException {
094: if (callee == null) {
095: init();
096: }
097: if (!targetSet) {
098: throw new BuildException(
099: "Attribute target or at least one nested target is required.",
100: getLocation());
101: }
102: callee.setAntfile(getProject().getProperty("ant.file"));
103: callee.setInheritAll(inheritAll);
104: callee.setInheritRefs(inheritRefs);
105: callee.execute();
106: }
107:
108: /**
109: * Create a new Property to pass to the invoked target(s).
110: * @return a <code>Property</code> object.
111: */
112: public Property createParam() {
113: if (callee == null) {
114: init();
115: }
116: return callee.createProperty();
117: }
118:
119: /**
120: * Reference element identifying a data type to carry
121: * over to the invoked target.
122: * @param r the specified <code>Ant.Reference</code>.
123: * @since Ant 1.5
124: */
125: public void addReference(Ant.Reference r) {
126: if (callee == null) {
127: init();
128: }
129: callee.addReference(r);
130: }
131:
132: /**
133: * Set of properties to pass to the new project.
134: * @param ps the <code>PropertySet</code> to pass.
135: * @since Ant 1.6
136: */
137: public void addPropertyset(PropertySet ps) {
138: if (callee == null) {
139: init();
140: }
141: callee.addPropertyset(ps);
142: }
143:
144: /**
145: * Set target to execute.
146: * @param target the name of the target to execute.
147: */
148: public void setTarget(String target) {
149: if (callee == null) {
150: init();
151: }
152: callee.setTarget(target);
153: targetSet = true;
154: }
155:
156: /**
157: * Add a target to the list of targets to invoke.
158: * @param t <code>Ant.TargetElement</code> representing the target.
159: * @since Ant 1.6.3
160: */
161: public void addConfiguredTarget(Ant.TargetElement t) {
162: if (callee == null) {
163: init();
164: }
165: callee.addConfiguredTarget(t);
166: targetSet = true;
167: }
168:
169: /**
170: * Handles output.
171: * Send it the the new project if is present, otherwise
172: * call the super class.
173: * @param output The string output to output.
174: * @see Task#handleOutput(String)
175: * @since Ant 1.5
176: */
177: public void handleOutput(String output) {
178: if (callee != null) {
179: callee.handleOutput(output);
180: } else {
181: super .handleOutput(output);
182: }
183: }
184:
185: /**
186: * Handles input.
187: * Deleate to the created project, if present, otherwise
188: * call the super class.
189: * @param buffer the buffer into which data is to be read.
190: * @param offset the offset into the buffer at which data is stored.
191: * @param length the amount of data to read.
192: *
193: * @return the number of bytes read.
194: *
195: * @exception IOException if the data cannot be read.
196: * @see Task#handleInput(byte[], int, int)
197: * @since Ant 1.6
198: */
199: public int handleInput(byte[] buffer, int offset, int length)
200: throws IOException {
201: if (callee != null) {
202: return callee.handleInput(buffer, offset, length);
203: }
204: return super .handleInput(buffer, offset, length);
205: }
206:
207: /**
208: * Handles output.
209: * Send it the the new project if is present, otherwise
210: * call the super class.
211: * @param output The string to output.
212: * @see Task#handleFlush(String)
213: * @since Ant 1.5.2
214: */
215: public void handleFlush(String output) {
216: if (callee != null) {
217: callee.handleFlush(output);
218: } else {
219: super .handleFlush(output);
220: }
221: }
222:
223: /**
224: * Handle error output.
225: * Send it the the new project if is present, otherwise
226: * call the super class.
227: * @param output The string to output.
228: *
229: * @see Task#handleErrorOutput(String)
230: * @since Ant 1.5
231: */
232: public void handleErrorOutput(String output) {
233: if (callee != null) {
234: callee.handleErrorOutput(output);
235: } else {
236: super .handleErrorOutput(output);
237: }
238: }
239:
240: /**
241: * Handle error output.
242: * Send it the the new project if is present, otherwise
243: * call the super class.
244: * @param output The string to output.
245: * @see Task#handleErrorFlush(String)
246: * @since Ant 1.5.2
247: */
248: public void handleErrorFlush(String output) {
249: if (callee != null) {
250: callee.handleErrorFlush(output);
251: } else {
252: super.handleErrorFlush(output);
253: }
254: }
255: }
|