You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

PreSetDef.java 8.9 kB

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