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.optional;
  18. import java.io.File;
  19. import org.apache.tools.ant.BuildException;
  20. import org.apache.tools.ant.Task;
  21. import org.apache.tools.ant.util.ScriptRunner;
  22. /**
  23. * Executes a script.
  24. *
  25. * @ant.task name="script"
  26. */
  27. public class Script extends Task {
  28. private String language;
  29. private File src;
  30. private String text;
  31. /**
  32. * Do the work.
  33. *
  34. * @exception BuildException if something goes wrong with the build
  35. */
  36. public void execute() throws BuildException {
  37. ScriptRunner runner = new ScriptRunner();
  38. if (language != null) {
  39. runner.setLanguage(language);
  40. }
  41. if (src != null) {
  42. runner.setSrc(src);
  43. }
  44. if (text != null) {
  45. runner.addText(text);
  46. }
  47. runner.addBeans(getProject().getProperties());
  48. runner.addBeans(getProject().getUserProperties());
  49. runner.addBeans(getProject().getTargets());
  50. runner.addBeans(getProject().getReferences());
  51. runner.addBean("project", getProject());
  52. runner.addBean("self", this);
  53. runner.executeScript("ANT");
  54. }
  55. /**
  56. * Defines the language (required).
  57. *
  58. * @param language the scripting language name for the script.
  59. */
  60. public void setLanguage(String language) {
  61. this.language = language;
  62. }
  63. /**
  64. * Load the script from an external file ; optional.
  65. *
  66. * @param fileName the name of the file containing the script source.
  67. */
  68. public void setSrc(String fileName) {
  69. this.src = new File(fileName);
  70. }
  71. /**
  72. * Set the script text.
  73. *
  74. * @param text a component of the script text to be added.
  75. */
  76. public void addText(String text) {
  77. this.text = text;
  78. }
  79. }