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