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.

MacroDef.java 17 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566
  1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. * Copyright (c) 2003 The Apache Software Foundation. All rights
  5. * reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. *
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in
  16. * the documentation and/or other materials provided with the
  17. * distribution.
  18. *
  19. * 3. The end-user documentation included with the redistribution, if
  20. * any, must include the following acknowlegement:
  21. * "This product includes software developed by the
  22. * Apache Software Foundation (http://www.apache.org/)."
  23. * Alternately, this acknowlegement may appear in the software itself,
  24. * if and wherever such third-party acknowlegements normally appear.
  25. *
  26. * 4. The names "Ant" and "Apache Software
  27. * Foundation" must not be used to endorse or promote products derived
  28. * from this software without prior written permission. For written
  29. * permission, please contact apache@apache.org.
  30. *
  31. * 5. Products derived from this software may not be called "Apache"
  32. * nor may "Apache" appear in their names without prior written
  33. * permission of the Apache Group.
  34. *
  35. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  36. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  37. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  38. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  39. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  40. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  42. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  43. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  44. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  45. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  46. * SUCH DAMAGE.
  47. * ====================================================================
  48. *
  49. * This software consists of voluntary contributions made by many
  50. * individuals on behalf of the Apache Software Foundation. For more
  51. * information on the Apache Software Foundation, please see
  52. * <http://www.apache.org/>.
  53. */
  54. package org.apache.tools.ant.taskdefs;
  55. import java.util.ArrayList;
  56. import java.util.List;
  57. import java.util.Map;
  58. import java.util.Locale;
  59. import java.util.HashMap;
  60. import org.apache.tools.ant.AntTypeDefinition;
  61. import org.apache.tools.ant.BuildException;
  62. import org.apache.tools.ant.ComponentHelper;
  63. import org.apache.tools.ant.Project;
  64. import org.apache.tools.ant.ProjectHelper;
  65. import org.apache.tools.ant.RuntimeConfigurable;
  66. import org.apache.tools.ant.Task;
  67. import org.apache.tools.ant.TaskContainer;
  68. import org.apache.tools.ant.UnknownElement;
  69. /**
  70. * Describe class <code>MacroDef</code> here.
  71. *
  72. * @author Peter Reilly
  73. * @since Ant 1.6
  74. */
  75. public class MacroDef extends AntlibDefinition {
  76. private NestedSequential nestedSequential;
  77. private String name;
  78. private List attributes = new ArrayList();
  79. private Map elements = new HashMap();
  80. /**
  81. * Name of the definition
  82. * @param name the name of the definition
  83. */
  84. public void setName(String name) {
  85. this.name = name;
  86. }
  87. /**
  88. * This is the sequential nested element of the macrodef.
  89. *
  90. * @return a sequential element to be configured.
  91. */
  92. public NestedSequential createSequential() {
  93. if (this.nestedSequential != null) {
  94. throw new BuildException("Only one sequential allowed");
  95. }
  96. this.nestedSequential = new NestedSequential();
  97. return this.nestedSequential;
  98. }
  99. /**
  100. * The class corresponding to the sequential nested element.
  101. * This is a simple task container.
  102. */
  103. public static class NestedSequential implements TaskContainer {
  104. private List nested = new ArrayList();
  105. /**
  106. * Add a task or type to the container.
  107. *
  108. * @param task an unknown element.
  109. */
  110. public void addTask(Task task) {
  111. nested.add(task);
  112. }
  113. /**
  114. * @return the list of unknown elements
  115. */
  116. public List getNested() {
  117. return nested;
  118. }
  119. /**
  120. * A compare function to compare this with another
  121. * NestedSequential.
  122. * It calls similar on the nested unknown elements.
  123. *
  124. * @param other the nested sequential to compare with.
  125. * @return true if they are similar, false otherwise
  126. */
  127. public boolean similar(NestedSequential other) {
  128. if (nested.size() != other.nested.size()) {
  129. return false;
  130. }
  131. for (int i = 0; i < nested.size(); ++i) {
  132. UnknownElement me = (UnknownElement) nested.get(i);
  133. UnknownElement o = (UnknownElement) other.nested.get(i);
  134. if (!me.similar(o)) {
  135. return false;
  136. }
  137. }
  138. return true;
  139. }
  140. }
  141. /**
  142. * Convert the nested sequential to an unknown element
  143. * @return the nested sequential as an unknown element.
  144. */
  145. public UnknownElement getNestedTask() {
  146. UnknownElement ret = new UnknownElement("sequential");
  147. ret.setTaskName("sequential");
  148. ret.setNamespace("");
  149. ret.setQName("sequential");
  150. new RuntimeConfigurable(ret, "sequential");
  151. for (int i = 0; i < nestedSequential.getNested().size(); ++i) {
  152. UnknownElement e =
  153. (UnknownElement) nestedSequential.getNested().get(i);
  154. ret.addChild(e);
  155. ret.getWrapper().addChild(e.getWrapper());
  156. }
  157. return ret;
  158. }
  159. /**
  160. * @return the nested Attributes
  161. */
  162. public List getAttributes() {
  163. return attributes;
  164. }
  165. /**
  166. * @return the nested elements
  167. */
  168. public Map getElements() {
  169. return elements;
  170. }
  171. /**
  172. * Check if a character is a valid character for an element or
  173. * attribute name
  174. * @param c the character to check
  175. * @return true if the character is a letter or digit or '.' or '-'
  176. * attribute name
  177. */
  178. public static boolean isValidNameCharacter(char c) {
  179. // ? is there an xml api for this ?
  180. return Character.isLetterOrDigit(c) || c == '.' || c == '-';
  181. }
  182. /**
  183. * Check if a string is a valid name for an element or
  184. * attribute
  185. * @param name the string to check
  186. * @return true if the name consists of valid name characters
  187. */
  188. private static boolean isValidName(String name) {
  189. if (name.length() == 0) {
  190. return false;
  191. }
  192. for (int i = 0; i < name.length(); ++i) {
  193. if (!isValidNameCharacter(name.charAt(i))) {
  194. return false;
  195. }
  196. }
  197. return true;
  198. }
  199. /**
  200. * Add an attribute element.
  201. *
  202. * @param attribute an attribute nested element.
  203. */
  204. public void addConfiguredAttribute(Attribute attribute) {
  205. if (attribute.getName() == null) {
  206. throw new BuildException(
  207. "the attribute nested element needed a \"name\" attribute");
  208. }
  209. for (int i = 0; i < attributes.size(); ++i) {
  210. if (((Attribute) attributes.get(i)).getName().equals(
  211. attribute.getName())) {
  212. throw new BuildException(
  213. "the attribute " + attribute.getName()
  214. + " has already been specified");
  215. }
  216. }
  217. attributes.add(attribute);
  218. }
  219. /**
  220. * Add an element element.
  221. *
  222. * @param element an element nested element.
  223. */
  224. public void addConfiguredElement(TemplateElement element) {
  225. if (element.getName() == null) {
  226. throw new BuildException(
  227. "the element nested element needed a \"name\" attribute");
  228. }
  229. if (elements.get(element.getName()) != null) {
  230. throw new BuildException(
  231. "the element " + element.getName()
  232. + " has already been specified");
  233. }
  234. elements.put(element.getName(), element);
  235. }
  236. /**
  237. * Create a new ant type based on the embedded tasks and types.
  238. *
  239. */
  240. public void execute() {
  241. if (nestedSequential == null) {
  242. throw new BuildException("Missing sequential element");
  243. }
  244. if (name == null) {
  245. throw new BuildException("Name not specified");
  246. }
  247. name = ProjectHelper.genComponentName(getURI(), name);
  248. MyAntTypeDefinition def = new MyAntTypeDefinition(this);
  249. def.setName(name);
  250. def.setClass(MacroInstance.class);
  251. ComponentHelper helper = ComponentHelper.getComponentHelper(
  252. getProject());
  253. helper.addDataTypeDefinition(def);
  254. }
  255. /**
  256. * A nested element for the MacroDef task.
  257. *
  258. */
  259. public static class Attribute {
  260. private String name;
  261. private String defaultValue;
  262. /**
  263. * The name of the attribute.
  264. *
  265. * @param name the name of the attribute
  266. */
  267. public void setName(String name) {
  268. if (!isValidName(name)) {
  269. throw new BuildException(
  270. "Illegal name [" + name + "] for attribute");
  271. }
  272. this.name = name.toLowerCase(Locale.US);
  273. }
  274. /**
  275. * @return the name of the attribute
  276. */
  277. public String getName() {
  278. return name;
  279. }
  280. /**
  281. * The default value to use if the parameter is not
  282. * used in the templated instance.
  283. *
  284. * @param defaultValue the default value
  285. */
  286. public void setDefault(String defaultValue) {
  287. this.defaultValue = defaultValue;
  288. }
  289. /**
  290. * @return the default value, null if not set
  291. */
  292. public String getDefault() {
  293. return defaultValue;
  294. }
  295. /**
  296. * equality method
  297. *
  298. * @param obj an <code>Object</code> value
  299. * @return a <code>boolean</code> value
  300. */
  301. public boolean equals(Object obj) {
  302. if (obj == null) {
  303. return false;
  304. }
  305. if (obj.getClass() != getClass()) {
  306. return false;
  307. }
  308. Attribute other = (Attribute) obj;
  309. if (name == null) {
  310. if (other.name != null) {
  311. return false;
  312. }
  313. } else if (!name.equals(other.name)) {
  314. return false;
  315. }
  316. if (defaultValue == null) {
  317. if (other.defaultValue != null) {
  318. return false;
  319. }
  320. } else if (!defaultValue.equals(other.defaultValue)) {
  321. return false;
  322. }
  323. return true;
  324. }
  325. /**
  326. * @return a hash code value for this object.
  327. */
  328. public int hashCode() {
  329. return objectHashCode(defaultValue) + objectHashCode(name);
  330. }
  331. }
  332. /**
  333. * A nested element for the MacroDef task.
  334. *
  335. */
  336. public static class TemplateElement {
  337. private String name;
  338. private boolean optional = false;
  339. /**
  340. * The name of the element.
  341. *
  342. * @param name the name of the element.
  343. */
  344. public void setName(String name) {
  345. if (!isValidName(name)) {
  346. throw new BuildException(
  347. "Illegal name [" + name + "] for attribute");
  348. }
  349. this.name = name;
  350. }
  351. /**
  352. * @return the name of the element.
  353. */
  354. public String getName() {
  355. return name;
  356. }
  357. /**
  358. * is this element optional ?
  359. *
  360. * @param optional if true this element may be left out, default
  361. * is false.
  362. */
  363. public void setOptional(boolean optional) {
  364. this.optional = optional;
  365. }
  366. /**
  367. * @return the optional attribute
  368. */
  369. public boolean isOptional() {
  370. return optional;
  371. }
  372. /**
  373. * equality method
  374. *
  375. * @param obj an <code>Object</code> value
  376. * @return a <code>boolean</code> value
  377. */
  378. public boolean equals(Object obj) {
  379. if (obj == null) {
  380. return false;
  381. }
  382. if (obj.getClass() != getClass()) {
  383. return false;
  384. }
  385. TemplateElement other = (TemplateElement) obj;
  386. if (name == null) {
  387. if (other.name != null) {
  388. return false;
  389. }
  390. } else if (!name.equals(other.name)) {
  391. return false;
  392. }
  393. return optional == other.optional;
  394. }
  395. /**
  396. * @return a hash code value for this object.
  397. */
  398. public int hashCode() {
  399. return objectHashCode(name) + (optional ? 1 : 0);
  400. }
  401. }
  402. /**
  403. * equality method for macrodef, ignores project and
  404. * runtime info.
  405. *
  406. * @param obj an <code>Object</code> value
  407. * @return a <code>boolean</code> value
  408. */
  409. public boolean equals(Object obj) {
  410. if (obj == null) {
  411. return false;
  412. }
  413. if (!obj.getClass().equals(getClass())) {
  414. return false;
  415. }
  416. MacroDef other = (MacroDef) obj;
  417. if (name == null) {
  418. return other.name == null;
  419. }
  420. if (!name.equals(other.name)) {
  421. return false;
  422. }
  423. if (getURI() == null || getURI().equals("")
  424. || getURI().equals(ProjectHelper.ANT_CORE_URI)) {
  425. if (!(other.getURI() == null || other.getURI().equals("")
  426. || other.getURI().equals(ProjectHelper.ANT_CORE_URI))) {
  427. return false;
  428. }
  429. } else {
  430. if (!getURI().equals(other.getURI())) {
  431. return false;
  432. }
  433. }
  434. if (!nestedSequential.similar(other.nestedSequential)) {
  435. return false;
  436. }
  437. if (!attributes.equals(other.attributes)) {
  438. return false;
  439. }
  440. if (!elements.equals(other.elements)) {
  441. return false;
  442. }
  443. return true;
  444. }
  445. /**
  446. * @return a hash code value for this object.
  447. */
  448. public int hashCode() {
  449. return objectHashCode(name)
  450. + objectHashCode(getURI())
  451. + objectHashCode(nestedSequential)
  452. + objectHashCode(attributes)
  453. + objectHashCode(elements);
  454. }
  455. /**
  456. * extends AntTypeDefinition, on create
  457. * of the object, the template macro definition
  458. * is given.
  459. */
  460. private static class MyAntTypeDefinition extends AntTypeDefinition {
  461. private MacroDef macroDef;
  462. /**
  463. * Creates a new <code>MyAntTypeDefinition</code> instance.
  464. *
  465. * @param macroDef a <code>MacroDef</code> value
  466. */
  467. public MyAntTypeDefinition(MacroDef macroDef) {
  468. this.macroDef = macroDef;
  469. }
  470. /**
  471. * create an instance of the definition.
  472. * The instance may be wrapped in a proxy class.
  473. * @param project the current project
  474. * @return the created object
  475. */
  476. public Object create(Project project) {
  477. Object o = super.create(project);
  478. if (o == null) {
  479. return null;
  480. }
  481. ((MacroInstance) o).setMacroDef(macroDef);
  482. return o;
  483. }
  484. /**
  485. * Equality method for this definition
  486. *
  487. * @param other another definition
  488. * @param project the current project
  489. * @return true if the definitions are the same
  490. */
  491. public boolean sameDefinition(AntTypeDefinition other, Project project) {
  492. if (!super.sameDefinition(other, project)) {
  493. return false;
  494. }
  495. MyAntTypeDefinition otherDef = (MyAntTypeDefinition) other;
  496. return macroDef.equals(otherDef.macroDef);
  497. }
  498. /**
  499. * Similar method for this definition
  500. *
  501. * @param other another definition
  502. * @param project the current project
  503. * @return true if the definitions are the same
  504. */
  505. public boolean similarDefinition(
  506. AntTypeDefinition other, Project project) {
  507. if (!super.similarDefinition(other, project)) {
  508. return false;
  509. }
  510. MyAntTypeDefinition otherDef = (MyAntTypeDefinition) other;
  511. return macroDef.equals(otherDef.macroDef);
  512. }
  513. }
  514. private static int objectHashCode(Object o) {
  515. if (o == null) {
  516. return 0;
  517. } else {
  518. return o.hashCode();
  519. }
  520. }
  521. }