1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 */
18 package org.apache.tools.ant.taskdefs.optional.script;
19
20 import org.apache.tools.ant.Task;
21 import org.apache.tools.ant.MagicNames;
22 import org.apache.tools.ant.BuildException;
23 import org.apache.tools.ant.DynamicConfigurator;
24 import java.util.Map;
25 import java.util.HashMap;
26 import java.util.List;
27 import java.util.ArrayList;
28
29 /**
30 * The script execution class. This class finds the defining script task
31 * and passes control to that task's executeScript method.
32 *
33 * @since Ant 1.6
34 */
35 public class ScriptDefBase extends Task implements DynamicConfigurator {
36
37 /** Nested elements */
38 private Map nestedElementMap = new HashMap();
39
40 /** Attributes */
41 private Map attributes = new HashMap();
42
43 private String text;
44
45 /**
46 * Locate the script defining task and execute the script by passing
47 * control to it
48 */
49 public void execute() {
50 getScript().executeScript(attributes, nestedElementMap, this);
51 }
52
53 private ScriptDef getScript() {
54 String name = getTaskType();
55 Map scriptRepository
56 = (Map) getProject().getReference(MagicNames.SCRIPT_REPOSITORY);
57 if (scriptRepository == null) {
58 throw new BuildException("Script repository not found for " + name);
59 }
60
61 ScriptDef definition = (ScriptDef) scriptRepository.get(getTaskType());
62 if (definition == null) {
63 throw new BuildException("Script definition not found for " + name);
64 }
65 return definition;
66 }
67
68 /**
69 * Create a nested element
70 *
71 * @param name the nested element name
72 * @return the element to be configured
73 */
74 public Object createDynamicElement(String name) {
75 List nestedElementList = (List) nestedElementMap.get(name);
76 if (nestedElementList == null) {
77 nestedElementList = new ArrayList();
78 nestedElementMap.put(name, nestedElementList);
79 }
80 Object element = getScript().createNestedElement(name);
81 nestedElementList.add(element);
82 return element;
83 }
84
85 /**
86 * Set a task attribute
87 *
88 * @param name the attribute name.
89 * @param value the attribute's string value
90 */
91 public void setDynamicAttribute(String name, String value) {
92 ScriptDef definition = getScript();
93 if (!definition.isAttributeSupported(name)) {
94 throw new BuildException("<" + getTaskType()
95 + "> does not support the \"" + name + "\" attribute");
96 }
97
98 attributes.put(name, value);
99 }
100
101 /**
102 * Set the script text.
103 *
104 * @param text a component of the script text to be added.
105 * @since ant1.7
106 */
107 public void addText(String text) {
108 this.text = getProject().replaceProperties(text);
109 }
110
111 /**
112 * get the text of this element; may be null
113 * @return text or null for no nested text
114 * @since ant1.7
115 */
116 public String getText() {
117 return text;
118 }
119
120 /**
121 * Utility method for nested scripts; throws a BuildException
122 * with the given message.
123 * @param message text to pass to the BuildException
124 * @throws BuildException always.
125 * @since ant1.7
126 */
127 public void fail(String message) {
128 throw new BuildException(message);
129 }
130 }
131