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

ConditionalTask.java 5.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /* *******************************************************************
  2. * Copyright (c) 1999-2001 Xerox Corporation,
  3. * 2002 Palo Alto Research Center, Incorporated (PARC).
  4. * All rights reserved.
  5. * This program and the accompanying materials are made available
  6. * under the terms of the Eclipse Public License v1.0
  7. * which accompanies this distribution and is available at
  8. * http://www.eclipse.org/legal/epl-v10.html
  9. *
  10. * Contributors:
  11. * Xerox/PARC initial implementation
  12. * ******************************************************************/
  13. package org.aspectj.internal.tools.ant.taskdefs;
  14. import java.util.Iterator;
  15. import java.util.List;
  16. import java.util.StringTokenizer;
  17. import java.util.Vector;
  18. import org.apache.tools.ant.BuildException;
  19. import org.apache.tools.ant.Task;
  20. public abstract class ConditionalTask extends Task {
  21. public final static String TRUE = "true";
  22. private List<If> ifs;
  23. protected List<If> ifs() {
  24. return ifs != null ? ifs : (ifs = new Vector<If>());
  25. }
  26. public If createIf() {
  27. If i = new If();
  28. ifs().add(i);
  29. return i;
  30. }
  31. public If createIf(String name, String equals, boolean strict) {
  32. If i = createIf();
  33. i.setName(name);
  34. i.setEquals(equals);
  35. i.setStrict(strict);
  36. return i;
  37. }
  38. public If createIf(String name, String equals) {
  39. return createIf(name, equals, false);
  40. }
  41. public If createIf(String name) {
  42. return createIf(name, TRUE, false);
  43. }
  44. public If createIf(String name, boolean strict) {
  45. return createIf(name, TRUE, strict);
  46. }
  47. public void setIfs(String ifs) {
  48. StringTokenizer tok = new StringTokenizer(ifs, ",;: ", false);
  49. while (tok.hasMoreTokens()) {
  50. String next = tok.nextToken();
  51. int iequals = next.lastIndexOf("=");
  52. String equals;
  53. String name;
  54. boolean strict;
  55. If i = createIf();
  56. if (iequals != -1) {
  57. name = next.substring(0, iequals);
  58. equals = next.substring(iequals + 1);
  59. strict = true;
  60. } else {
  61. name = next.substring(0);
  62. equals = TRUE;
  63. strict = false;
  64. }
  65. i.setName(name);
  66. i.setEquals(equals);
  67. i.setStrict(strict);
  68. }
  69. }
  70. public void setIf(String ifStr) {
  71. setIfs(ifStr);
  72. }
  73. public class If {
  74. public If() {
  75. this(null, null);
  76. }
  77. public If(String name) {
  78. this(name, TRUE);
  79. }
  80. public If(String name, String equals) {
  81. setName(name);
  82. setEquals(equals);
  83. }
  84. private String name;
  85. public void setName(String name) {
  86. this.name = name;
  87. }
  88. public String getName() {
  89. return name;
  90. }
  91. private String equals;
  92. public void setEquals(String equals) {
  93. this.equals = equals;
  94. }
  95. public String getEquals() {
  96. return equals;
  97. }
  98. private boolean strict = false;
  99. public void setStrict(boolean strict) {
  100. this.strict = strict;
  101. }
  102. public boolean isStrict() {
  103. return strict;
  104. }
  105. public boolean isOk(String prop) {
  106. return isOk(prop, isStrict());
  107. }
  108. //XXX Need a better boolean parser
  109. public boolean isOk(String prop, boolean isStrict) {
  110. if (isStrict) {
  111. return prop != null && prop.equals(getEquals());
  112. } else {
  113. if (isOk(prop, true)) {
  114. return true;
  115. }
  116. if (prop == null || isFalse(getEquals())) {
  117. return true;
  118. }
  119. if ( (isTrue(getEquals()) && isTrue(prop)) ||
  120. (isFalse(getEquals()) && isFalse(prop)) ) {
  121. return true;
  122. }
  123. return false;
  124. }
  125. }
  126. private boolean isFalse(String prop) {
  127. return isOneOf(prop, falses) || isOneOf(prop, complement(trues));
  128. }
  129. private boolean isTrue(String prop) {
  130. return isOneOf(prop, trues) || isOneOf(prop, complement(falses));
  131. }
  132. private boolean isOneOf(String prop, String[] strings) {
  133. for (int i = 0; i < strings.length; i++) {
  134. if (strings[i].equals(prop)) {
  135. return true;
  136. }
  137. }
  138. return false;
  139. }
  140. private String[] complement(String[] strings) {
  141. for (int i = 0; i < strings.length; i++) {
  142. strings[i] = "!" + strings[i];
  143. }
  144. return strings;
  145. }
  146. }
  147. final static String[] falses = { "false", "no" };
  148. final static String[] trues = { "true", "yes" };
  149. protected boolean checkIfs() {
  150. return getFalses().size() == 0;
  151. }
  152. protected List<String> getFalses() {
  153. Iterator<If> iter = ifs().iterator();
  154. List<String> result = new Vector<String>();
  155. while (iter.hasNext()) {
  156. If next = (If) iter.next();
  157. String name = next.getName();
  158. String prop = project.getProperty(name);
  159. if (prop == null) {
  160. prop = project.getUserProperty(name);
  161. }
  162. if (!next.isOk(prop)) {
  163. result.add(name);
  164. }
  165. }
  166. return result;
  167. }
  168. public abstract void execute() throws BuildException;
  169. }