1 | /*
|
---|
2 | * Copyright 2003-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 |
|
---|
18 | package org.apache.tools.ant.taskdefs;
|
---|
19 |
|
---|
20 | import org.apache.tools.ant.AntTypeDefinition;
|
---|
21 | import org.apache.tools.ant.BuildException;
|
---|
22 | import org.apache.tools.ant.ComponentHelper;
|
---|
23 | import org.apache.tools.ant.Project;
|
---|
24 | import org.apache.tools.ant.ProjectHelper;
|
---|
25 | import org.apache.tools.ant.Task;
|
---|
26 | import org.apache.tools.ant.TaskContainer;
|
---|
27 | import org.apache.tools.ant.UnknownElement;
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * The preset definition task generates a new definition
|
---|
31 | * based on a current definition with some attributes or
|
---|
32 | * elements preset.
|
---|
33 | * <pre>
|
---|
34 | * <presetdef name="my.javac">
|
---|
35 | * <javac deprecation="${deprecation}" debug="${debug}"/>
|
---|
36 | * </presetdef>
|
---|
37 | * <my.javac srcdir="src" destdir="classes"/>
|
---|
38 | * </pre>
|
---|
39 | *
|
---|
40 | * @since Ant 1.6
|
---|
41 | */
|
---|
42 | public class PreSetDef extends AntlibDefinition implements TaskContainer {
|
---|
43 | private UnknownElement nestedTask;
|
---|
44 | private String name;
|
---|
45 |
|
---|
46 | /**
|
---|
47 | * Name of the definition
|
---|
48 | * @param name the name of the definition
|
---|
49 | */
|
---|
50 | public void setName(String name) {
|
---|
51 | this.name = name;
|
---|
52 | }
|
---|
53 |
|
---|
54 | /**
|
---|
55 | * Add a nested task to predefine attributes and elements on
|
---|
56 | * @param nestedTask Nested task/type to extend
|
---|
57 | */
|
---|
58 | public void addTask(Task nestedTask) {
|
---|
59 | if (this.nestedTask != null) {
|
---|
60 | throw new BuildException("Only one nested element allowed");
|
---|
61 | }
|
---|
62 | if (!(nestedTask instanceof UnknownElement)) {
|
---|
63 | throw new BuildException(
|
---|
64 | "addTask called with a task that is not an unknown element");
|
---|
65 | }
|
---|
66 | this.nestedTask = (UnknownElement) nestedTask;
|
---|
67 | }
|
---|
68 |
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * make a new definition
|
---|
72 | */
|
---|
73 | public void execute() {
|
---|
74 | if (nestedTask == null) {
|
---|
75 | throw new BuildException("Missing nested element");
|
---|
76 | }
|
---|
77 | if (name == null) {
|
---|
78 | throw new BuildException("Name not specified");
|
---|
79 | }
|
---|
80 |
|
---|
81 | name = ProjectHelper.genComponentName(getURI(), name);
|
---|
82 |
|
---|
83 | ComponentHelper helper = ComponentHelper.getComponentHelper(
|
---|
84 | getProject());
|
---|
85 |
|
---|
86 | String componentName = ProjectHelper.genComponentName(
|
---|
87 | nestedTask.getNamespace(), nestedTask.getTag());
|
---|
88 |
|
---|
89 | AntTypeDefinition def = helper.getDefinition(componentName);
|
---|
90 | if (def == null) {
|
---|
91 | throw new BuildException(
|
---|
92 | "Unable to find typedef " + componentName);
|
---|
93 | }
|
---|
94 |
|
---|
95 | PreSetDefinition newDef = new PreSetDefinition(def, nestedTask);
|
---|
96 |
|
---|
97 | newDef.setName(name);
|
---|
98 |
|
---|
99 | helper.addDataTypeDefinition(newDef);
|
---|
100 | }
|
---|
101 |
|
---|
102 | /**
|
---|
103 | * This class contains the unknown element and the object
|
---|
104 | * that is predefined.
|
---|
105 | * @see AntTypeDefinition
|
---|
106 | */
|
---|
107 | public static class PreSetDefinition extends AntTypeDefinition {
|
---|
108 | private AntTypeDefinition parent;
|
---|
109 | private UnknownElement element;
|
---|
110 |
|
---|
111 | /**
|
---|
112 | * Creates a new <code>PresetDefinition</code> instance.
|
---|
113 | *
|
---|
114 | * @param parent The parent of this predefintion.
|
---|
115 | * @param el The predefined attributes, nested elements and text.
|
---|
116 | */
|
---|
117 | public PreSetDefinition(AntTypeDefinition parent, UnknownElement el) {
|
---|
118 | if (parent instanceof PreSetDefinition) {
|
---|
119 | PreSetDefinition p = (PreSetDefinition) parent;
|
---|
120 | el.applyPreSet(p.element);
|
---|
121 | parent = p.parent;
|
---|
122 | }
|
---|
123 | this.parent = parent;
|
---|
124 | this.element = el;
|
---|
125 | }
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * Override so that it is not allowed
|
---|
129 | *
|
---|
130 | * @param clazz a <code>Class</code> value
|
---|
131 | */
|
---|
132 | public void setClass(Class clazz) {
|
---|
133 | throw new BuildException("Not supported");
|
---|
134 | }
|
---|
135 |
|
---|
136 | /**
|
---|
137 | * Override so that it is not allowed
|
---|
138 | *
|
---|
139 | * @param className a <code>String</code> value
|
---|
140 | */
|
---|
141 | public void setClassName(String className) {
|
---|
142 | throw new BuildException("Not supported");
|
---|
143 | }
|
---|
144 |
|
---|
145 | /**
|
---|
146 | * get the classname of the definition
|
---|
147 | * @return the name of the class of this definition
|
---|
148 | */
|
---|
149 | public String getClassName() {
|
---|
150 | return parent.getClassName();
|
---|
151 | }
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * set the adapter class for this definition.
|
---|
155 | * NOTE Supported
|
---|
156 | * @param adapterClass the adapterClass
|
---|
157 | */
|
---|
158 | public void setAdapterClass(Class adapterClass) {
|
---|
159 | throw new BuildException("Not supported");
|
---|
160 | }
|
---|
161 |
|
---|
162 | /**
|
---|
163 | * set the assignable class for this definition.
|
---|
164 | * NOT SUPPORTED
|
---|
165 | * @param adaptToClass the assignable class
|
---|
166 | */
|
---|
167 |
|
---|
168 | public void setAdaptToClass(Class adaptToClass) {
|
---|
169 | throw new BuildException("Not supported");
|
---|
170 | }
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * set the classloader to use to create an instance
|
---|
174 | * of the definition
|
---|
175 | * @param classLoader the classLoader
|
---|
176 | */
|
---|
177 | public void setClassLoader(ClassLoader classLoader) {
|
---|
178 | throw new BuildException("Not supported");
|
---|
179 | }
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * get the classloader for this definition
|
---|
183 | * @return the classloader for this definition
|
---|
184 | */
|
---|
185 | public ClassLoader getClassLoader() {
|
---|
186 | return parent.getClassLoader();
|
---|
187 | }
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * get the exposed class for this definition.
|
---|
191 | * @param project the current project
|
---|
192 | * @return the exposed class
|
---|
193 | */
|
---|
194 | public Class getExposedClass(Project project) {
|
---|
195 | return parent.getExposedClass(project);
|
---|
196 | }
|
---|
197 |
|
---|
198 | /**
|
---|
199 | * get the definition class
|
---|
200 | * @param project the current project
|
---|
201 | * @return the type of the definition
|
---|
202 | */
|
---|
203 | public Class getTypeClass(Project project) {
|
---|
204 | return parent.getTypeClass(project);
|
---|
205 | }
|
---|
206 |
|
---|
207 |
|
---|
208 | /**
|
---|
209 | * check if the attributes are correct
|
---|
210 | * @param project the current project
|
---|
211 | */
|
---|
212 | public void checkClass(Project project) {
|
---|
213 | parent.checkClass(project);
|
---|
214 | }
|
---|
215 |
|
---|
216 | /**
|
---|
217 | * create an instance of the definition.
|
---|
218 | * The instance may be wrapped in a proxy class.
|
---|
219 | * This is a special version of create for IH and UE.
|
---|
220 | * @param project the current project
|
---|
221 | * @return the created object
|
---|
222 | */
|
---|
223 | public Object createObject(Project project) {
|
---|
224 | Object o = parent.create(project);
|
---|
225 | if (o == null) {
|
---|
226 | return null;
|
---|
227 | }
|
---|
228 | return o;
|
---|
229 | }
|
---|
230 |
|
---|
231 | /**
|
---|
232 | * @return the predefined attributes, elements and text as
|
---|
233 | * a UnknownElement
|
---|
234 | */
|
---|
235 | public UnknownElement getPreSets() {
|
---|
236 | return element;
|
---|
237 | }
|
---|
238 |
|
---|
239 | /**
|
---|
240 | * Fake create an object, used by IH and UE to see that
|
---|
241 | * this is a predefined object.
|
---|
242 | *
|
---|
243 | * @param project the current project
|
---|
244 | * @return this object
|
---|
245 | */
|
---|
246 | public Object create(Project project) {
|
---|
247 | return this;
|
---|
248 | }
|
---|
249 |
|
---|
250 | /**
|
---|
251 | * Equality method for this definition
|
---|
252 | *
|
---|
253 | * @param other another definition
|
---|
254 | * @param project the current project
|
---|
255 | * @return true if the definitions are the same
|
---|
256 | */
|
---|
257 | public boolean sameDefinition(AntTypeDefinition other, Project project) {
|
---|
258 | if (other == null) {
|
---|
259 | return false;
|
---|
260 | }
|
---|
261 | if (other.getClass() != getClass()) {
|
---|
262 | return false;
|
---|
263 | }
|
---|
264 | PreSetDefinition otherDef = (PreSetDefinition) other;
|
---|
265 | if (!parent.sameDefinition(otherDef.parent, project)) {
|
---|
266 | return false;
|
---|
267 | }
|
---|
268 | if (!element.similar(otherDef.element)) {
|
---|
269 | return false;
|
---|
270 | }
|
---|
271 | return true;
|
---|
272 | }
|
---|
273 |
|
---|
274 | /**
|
---|
275 | * Similar method for this definition
|
---|
276 | *
|
---|
277 | * @param other another definition
|
---|
278 | * @param project the current project
|
---|
279 | * @return true if the definitions are the same
|
---|
280 | */
|
---|
281 | public boolean similarDefinition(
|
---|
282 | AntTypeDefinition other, Project project) {
|
---|
283 | if (other == null) {
|
---|
284 | return false;
|
---|
285 | }
|
---|
286 | if (!other.getClass().getName().equals(getClass().getName())) {
|
---|
287 | return false;
|
---|
288 | }
|
---|
289 | PreSetDefinition otherDef = (PreSetDefinition) other;
|
---|
290 | if (!parent.similarDefinition(otherDef.parent, project)) {
|
---|
291 | return false;
|
---|
292 | }
|
---|
293 | if (!element.similar(otherDef.element)) {
|
---|
294 | return false;
|
---|
295 | }
|
---|
296 | return true;
|
---|
297 | }
|
---|
298 | }
|
---|
299 | }
|
---|