1. /*
  2. * Copyright 2000-2004 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. package org.apache.tools.ant.taskdefs;
  18. import org.apache.tools.ant.BuildException;
  19. import org.apache.tools.ant.Task;
  20. import java.io.IOException;
  21. /**
  22. * Call another target in the same project.
  23. *
  24. * <pre>
  25. * <target name="foo">
  26. * <antcall target="bar">
  27. * <param name="property1" value="aaaaa" />
  28. * <param name="foo" value="baz" />
  29. * </antcall>
  30. * </target>
  31. *
  32. * <target name="bar" depends="init">
  33. * <echo message="prop is ${property1} ${foo}" />
  34. * </target>
  35. * </pre>
  36. *
  37. * <p>This only works as expected if neither property1 nor foo are
  38. * defined in the project itself.
  39. *
  40. *
  41. *
  42. * @since Ant 1.2
  43. *
  44. * @ant.task name="antcall" category="control"
  45. */
  46. public class CallTarget extends Task {
  47. private Ant callee;
  48. private String subTarget;
  49. // must match the default value of Ant#inheritAll
  50. private boolean inheritAll = true;
  51. // must match the default value of Ant#inheritRefs
  52. private boolean inheritRefs = false;
  53. /**
  54. * If true, pass all properties to the new Ant project.
  55. * Defaults to true.
  56. */
  57. public void setInheritAll(boolean inherit) {
  58. inheritAll = inherit;
  59. }
  60. /**
  61. * If true, pass all references to the new Ant project.
  62. * Defaults to false
  63. * @param inheritRefs new value
  64. */
  65. public void setInheritRefs(boolean inheritRefs) {
  66. this.inheritRefs = inheritRefs;
  67. }
  68. /**
  69. * init this task by creating new instance of the ant task and
  70. * configuring it's by calling its own init method.
  71. */
  72. public void init() {
  73. callee = (Ant) getProject().createTask("ant");
  74. callee.setOwningTarget(getOwningTarget());
  75. callee.setTaskName(getTaskName());
  76. callee.setLocation(getLocation());
  77. callee.init();
  78. }
  79. /**
  80. * hand off the work to the ant task of ours, after setting it up
  81. * @throws BuildException on validation failure or if the target didn't
  82. * execute
  83. */
  84. public void execute() throws BuildException {
  85. if (callee == null) {
  86. init();
  87. }
  88. if (subTarget == null) {
  89. throw new BuildException("Attribute target is required.",
  90. getLocation());
  91. }
  92. callee.setAntfile(getProject().getProperty("ant.file"));
  93. callee.setTarget(subTarget);
  94. callee.setInheritAll(inheritAll);
  95. callee.setInheritRefs(inheritRefs);
  96. callee.execute();
  97. }
  98. /**
  99. * Property to pass to the invoked target.
  100. */
  101. public Property createParam() {
  102. if (callee == null) {
  103. init();
  104. }
  105. return callee.createProperty();
  106. }
  107. /**
  108. * Reference element identifying a data type to carry
  109. * over to the invoked target.
  110. * @since Ant 1.5
  111. */
  112. public void addReference(Ant.Reference r) {
  113. if (callee == null) {
  114. init();
  115. }
  116. callee.addReference(r);
  117. }
  118. /**
  119. * Set of properties to pass to the new project.
  120. *
  121. * @since Ant 1.6
  122. */
  123. public void addPropertyset(org.apache.tools.ant.types.PropertySet ps) {
  124. if (callee == null) {
  125. init();
  126. }
  127. callee.addPropertyset(ps);
  128. }
  129. /**
  130. * Target to execute, required.
  131. */
  132. public void setTarget(String target) {
  133. subTarget = target;
  134. }
  135. /**
  136. * Pass output sent to System.out to the new project.
  137. *
  138. * @since Ant 1.5
  139. */
  140. public void handleOutput(String output) {
  141. if (callee != null) {
  142. callee.handleOutput(output);
  143. } else {
  144. super.handleOutput(output);
  145. }
  146. }
  147. /**
  148. * @see Task#handleInput(byte[], int, int)
  149. *
  150. * @since Ant 1.6
  151. */
  152. public int handleInput(byte[] buffer, int offset, int length)
  153. throws IOException {
  154. if (callee != null) {
  155. return callee.handleInput(buffer, offset, length);
  156. } else {
  157. return super.handleInput(buffer, offset, length);
  158. }
  159. }
  160. /**
  161. * Pass output sent to System.out to the new project.
  162. *
  163. * @since Ant 1.5.2
  164. */
  165. public void handleFlush(String output) {
  166. if (callee != null) {
  167. callee.handleFlush(output);
  168. } else {
  169. super.handleFlush(output);
  170. }
  171. }
  172. /**
  173. * Pass output sent to System.err to the new project.
  174. *
  175. * @since Ant 1.5
  176. */
  177. public void handleErrorOutput(String output) {
  178. if (callee != null) {
  179. callee.handleErrorOutput(output);
  180. } else {
  181. super.handleErrorOutput(output);
  182. }
  183. }
  184. /**
  185. * Pass output sent to System.err to the new project and flush stream.
  186. *
  187. * @since Ant 1.5.2
  188. */
  189. public void handleErrorFlush(String output) {
  190. if (callee != null) {
  191. callee.handleErrorFlush(output);
  192. } else {
  193. super.handleErrorFlush(output);
  194. }
  195. }
  196. }