KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > taskdefs > PreSetDef


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
19 package org.apache.tools.ant.taskdefs;
20
21 import org.apache.tools.ant.AntTypeDefinition;
22 import org.apache.tools.ant.BuildException;
23 import org.apache.tools.ant.ComponentHelper;
24 import org.apache.tools.ant.Project;
25 import org.apache.tools.ant.ProjectHelper;
26 import org.apache.tools.ant.Task;
27 import org.apache.tools.ant.TaskContainer;
28 import org.apache.tools.ant.UnknownElement;
29
30 /**
31  * The preset definition task generates a new definition
32  * based on a current definition with some attributes or
33  * elements preset.
34  * <pre>
35  * &lt;presetdef name="my.javac"&gt;
36  * &lt;javac deprecation="${deprecation}" debug="${debug}"/&gt;
37  * &lt;/presetdef&gt;
38  * &lt;my.javac srcdir="src" destdir="classes"/&gt;
39  * </pre>
40  *
41  * @since Ant 1.6
42  */

43 public class PreSetDef extends AntlibDefinition implements TaskContainer {
44     private UnknownElement nestedTask;
45     private String JavaDoc name;
46
47     /**
48      * Set the name of this definition.
49      * @param name the name of the definition.
50      */

51      public void setName(String JavaDoc name) {
52         this.name = name;
53     }
54
55     /**
56      * Add a nested task to predefine attributes and elements on.
57      * @param nestedTask Nested task/type to extend.
58      */

59     public void addTask(Task nestedTask) {
60         if (this.nestedTask != null) {
61             throw new BuildException("Only one nested element allowed");
62         }
63         if (!(nestedTask instanceof UnknownElement)) {
64             throw new BuildException(
65                 "addTask called with a task that is not an unknown element");
66         }
67         this.nestedTask = (UnknownElement) nestedTask;
68     }
69
70
71     /**
72      * Make a new definition.
73      */

74     public void execute() {
75         if (nestedTask == null) {
76             throw new BuildException("Missing nested element");
77         }
78         if (name == null) {
79             throw new BuildException("Name not specified");
80         }
81         name = ProjectHelper.genComponentName(getURI(), name);
82
83         ComponentHelper helper = ComponentHelper.getComponentHelper(
84             getProject());
85
86         String JavaDoc 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         PreSetDefinition newDef = new PreSetDefinition(def, nestedTask);
95
96         newDef.setName(name);
97
98         helper.addDataTypeDefinition(newDef);
99         log("defining preset " + name, Project.MSG_VERBOSE);
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 predefinition.
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 JavaDoc 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 JavaDoc 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 JavaDoc getClassName() {
150             return parent.getClassName();
151         }
152
153         /**
154          * Set the adapter class for this definition.
155          * NOT Supported
156          * @param adapterClass the adapterClass.
157          */

158         public void setAdapterClass(Class JavaDoc 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         public void setAdaptToClass(Class JavaDoc adaptToClass) {
168             throw new BuildException("Not supported");
169         }
170
171         /**
172          * Set the classloader to use to create an instance
173          * of the definition.
174          * NOT SUPPORTED
175          * @param classLoader the classLoader.
176          */

177         public void setClassLoader(ClassLoader JavaDoc 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 JavaDoc 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 JavaDoc 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 JavaDoc 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. The instance may be wrapped
218          * in a proxy class. This is a special version of create for
219          * IntrospectionHelper and UnknownElement.
220          * @param project the current project.
221          * @return the created object.
222          */

223         public Object JavaDoc createObject(Project project) {
224             return parent.create(project);
225         }
226
227         /**
228          * Get the preset values.
229          * @return the predefined attributes, elements and text as
230          * an UnknownElement.
231          */

232         public UnknownElement getPreSets() {
233             return element;
234         }
235
236         /**
237          * Fake create an object, used by IntrospectionHelper and UnknownElement
238          * to see that this is a predefined object.
239          *
240          * @param project the current project.
241          * @return this object.
242          */

243         public Object JavaDoc create(Project project) {
244             return this;
245         }
246
247         /**
248          * Equality method for this definition.
249          *
250          * @param other another definition.
251          * @param project the current project.
252          * @return true if the definitions are the same.
253          */

254         public boolean sameDefinition(AntTypeDefinition other, Project project) {
255             return (other != null && other.getClass() == getClass() && parent != null
256                 && parent.sameDefinition(((PreSetDefinition) other).parent, project)
257                 && element.similar(((PreSetDefinition) other).element));
258         }
259
260         /**
261          * Similar method for this definition.
262          *
263          * @param other another definition.
264          * @param project the current project.
265          * @return true if the definitions are similar.
266          */

267         public boolean similarDefinition(
268             AntTypeDefinition other, Project project) {
269             return (other != null && other.getClass().getName().equals(
270                 getClass().getName()) && parent != null
271                 && parent.similarDefinition(((PreSetDefinition) other).parent, project)
272                 && element.similar(((PreSetDefinition) other).element));
273         }
274     }
275 }
276
Popular Tags