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.script;
  18. import org.apache.tools.ant.Task;
  19. import org.apache.tools.ant.MagicNames;
  20. import org.apache.tools.ant.BuildException;
  21. import org.apache.tools.ant.DynamicConfigurator;
  22. import java.util.Map;
  23. import java.util.HashMap;
  24. import java.util.List;
  25. import java.util.ArrayList;
  26. /**
  27. * The script execution class. This class finds the defining script task
  28. * and passes control to that task's executeScript method.
  29. *
  30. * @since Ant 1.6
  31. */
  32. public class ScriptDefBase extends Task implements DynamicConfigurator {
  33. /** Nested elements */
  34. private Map nestedElementMap = new HashMap();
  35. /** Attributes */
  36. private Map attributes = new HashMap();
  37. /**
  38. * Locate the script defining task and execute the script by passing
  39. * control to it
  40. */
  41. public void execute() {
  42. getScript().executeScript(attributes, nestedElementMap);
  43. }
  44. private ScriptDef getScript() {
  45. String name = getTaskType();
  46. Map scriptRepository
  47. = (Map) getProject().getReference(MagicNames.SCRIPT_REPOSITORY);
  48. if (scriptRepository == null) {
  49. throw new BuildException("Script repository not found for " + name);
  50. }
  51. ScriptDef definition = (ScriptDef) scriptRepository.get(getTaskType());
  52. if (definition == null) {
  53. throw new BuildException("Script definition not found for " + name);
  54. }
  55. return definition;
  56. }
  57. /**
  58. * Create a nested element
  59. *
  60. * @param name the nested element name
  61. * @return the element to be configured
  62. */
  63. public Object createDynamicElement(String name) {
  64. List nestedElementList = (List) nestedElementMap.get(name);
  65. if (nestedElementList == null) {
  66. nestedElementList = new ArrayList();
  67. nestedElementMap.put(name, nestedElementList);
  68. }
  69. Object element = getScript().createNestedElement(name);
  70. nestedElementList.add(element);
  71. return element;
  72. }
  73. /**
  74. * Set a task attribute
  75. *
  76. * @param name the attribute name.
  77. * @param value the attribute's string value
  78. */
  79. public void setDynamicAttribute(String name, String value) {
  80. ScriptDef definition = getScript();
  81. if (!definition.isAttributeSupported(name)) {
  82. throw new BuildException("<" + getTaskType()
  83. + "> does not support the \"" + name + "\" attribute");
  84. }
  85. attributes.put(name, value);
  86. }
  87. }