source: other-projects/trunk/gs3-release-maker/apache-ant-1.6.5/src/main/org/apache/tools/ant/types/Assertions.java@ 14627

Last change on this file since 14627 was 14627, checked in by oranfry, 17 years ago

initial import of the gs3-release-maker

File size: 11.4 KB
Line 
1/*
2 * Copyright 2000-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
18package org.apache.tools.ant.types;
19
20import org.apache.tools.ant.BuildException;
21import org.apache.tools.ant.Project;
22
23import java.util.ArrayList;
24import java.util.Iterator;
25import java.util.List;
26import java.util.ListIterator;
27
28/**
29 * The assertion datatype. This type describes
30 * assertion settings for the <java> task and others.
31 * One can set the system assertions, and enable/disable those in
32 * packages and classes.
33 * Assertions can only be enabled or disabled when forking Java.
34 *
35 * Example: set system assertions and all org.apache packages except
36 * for ant, and the class org.apache.tools.ant.Main.
37 * <pre>
38 * &lt;assertions enableSystemAssertions="true" &gt;
39 * &lt;enable package="org.apache" /&gt;
40 * &lt;disable package="org.apache.ant" /&gt;
41 * &lt;enable class="org.apache.tools.ant.Main"/&gt;
42 * &lt;/assertions&gt;
43 *</pre>
44 * Disable system assertions; enable those in the anonymous package
45 * <pre>
46 * &lt;assertions enableSystemAssertions="false" &gt;
47 * &lt;enable package="..." /&gt;
48 * &lt;/assertions&gt;
49 * </pre>
50 * enable assertions in a class called Test
51 * <pre>
52 * &lt;assertions &gt;
53 * &lt;enable class="Test" /&gt;
54 * &lt;/assertions&gt;
55 * </pre>
56 * This type is a datatype, so you can declare assertions and use them later
57 *
58 * <pre>
59 * &lt;assertions id="project.assertions" &gt;
60 * &lt;enable project="org.apache.test" /&gt;
61 * &lt;/assertions&gt;
62 *
63 * &lt;assertions refid="project.assertions" /&gt;
64 *
65 * </pre>
66 * @since Ant 1.6
67 */
68public class Assertions extends DataType implements Cloneable {
69
70 /**
71 * enable/disable sys assertions; null means undefined
72 */
73 private Boolean enableSystemAssertions;
74
75 /**
76 * list of type BaseAssertion
77 */
78 private ArrayList assertionList = new ArrayList();
79
80
81 /**
82 * enable assertions
83 * @param assertion
84 */
85 public void addEnable(EnabledAssertion assertion) {
86 checkChildrenAllowed();
87 assertionList.add(assertion);
88 }
89
90 /**
91 * disable assertions
92 * @param assertion
93 */
94 public void addDisable(DisabledAssertion assertion) {
95 checkChildrenAllowed();
96 assertionList.add(assertion);
97 }
98
99 /**
100 * enable or disable system assertions
101 * @param enableSystemAssertions
102 */
103 public void setEnableSystemAssertions(Boolean enableSystemAssertions) {
104 checkAttributesAllowed();
105 this.enableSystemAssertions = enableSystemAssertions;
106 }
107
108 /**
109 * Set the value of the refid attribute.
110 *
111 * <p>Subclasses may need to check whether any other attributes
112 * have been set as well or child elements have been created and
113 * thus override this method. if they do the must call
114 * <code>super.setRefid</code>.</p>
115 */
116 public void setRefid(Reference ref) {
117 if (assertionList.size() > 0 || enableSystemAssertions != null) {
118 throw tooManyAttributes();
119 }
120 super.setRefid(ref);
121 }
122
123 /**
124 * get whatever we are referencing to. This could be ourself.
125 * @return the object that contains the assertion info
126 */
127 private Assertions getFinalReference() {
128 if (getRefid() == null) {
129 return this;
130 } else {
131 Object o = getRefid().getReferencedObject(getProject());
132 if (!(o instanceof Assertions)) {
133 throw new BuildException("reference is of wrong type");
134 }
135 return (Assertions) o;
136 }
137 }
138
139 /**
140 * how many assertions are made...will resolve references before returning
141 * @return total # of commands to make
142 */
143 public int size() {
144 Assertions clause = getFinalReference();
145 return clause.getFinalSize();
146 }
147
148
149 /**
150 * what is the final size of this object
151 * @return
152 */
153 private int getFinalSize() {
154 return assertionList.size() + (enableSystemAssertions != null ? 1 : 0);
155 }
156
157 /**
158 * add the assertions to a list in a format suitable
159 * for adding to a command line
160 * @param commandList
161 */
162 public void applyAssertions(List commandList) {
163 getProject().log("Applying assertions",Project.MSG_DEBUG);
164 Assertions clause = getFinalReference();
165 //do the system assertions
166 if (Boolean.TRUE.equals(clause.enableSystemAssertions)) {
167 getProject().log("Enabling system assertions", Project.MSG_DEBUG);
168 commandList.add("-enablesystemassertions");
169 } else if (Boolean.FALSE.equals(clause.enableSystemAssertions)) {
170 getProject().log("disabling system assertions", Project.MSG_DEBUG);
171 commandList.add("-disablesystemassertions");
172 }
173
174 //now any inner assertions
175 Iterator it = clause.assertionList.iterator();
176 while (it.hasNext()) {
177 BaseAssertion assertion = (BaseAssertion) it.next();
178 String arg = assertion.toCommand();
179 getProject().log("adding assertion "+arg, Project.MSG_DEBUG);
180 commandList.add(arg);
181 }
182 }
183
184 /**
185 * apply all the assertions to the command.
186 * @param command
187 */
188 public void applyAssertions(CommandlineJava command) {
189 Assertions clause = getFinalReference();
190 //do the system assertions
191 if (Boolean.TRUE.equals(clause.enableSystemAssertions)) {
192 addVmArgument(command, "-enablesystemassertions");
193 } else if (Boolean.FALSE.equals(clause.enableSystemAssertions)) {
194 addVmArgument(command, "-disablesystemassertions");
195 }
196
197 //now any inner assertions
198 Iterator it = clause.assertionList.iterator();
199 while (it.hasNext()) {
200 BaseAssertion assertion = (BaseAssertion) it.next();
201 String arg = assertion.toCommand();
202 addVmArgument(command, arg);
203 }
204 }
205
206 /**
207 * add the assertions to a list in a format suitable
208 * for adding to a command line
209 * @param commandIterator list of commands
210 */
211 public void applyAssertions(final ListIterator commandIterator) {
212 getProject().log("Applying assertions", Project.MSG_DEBUG);
213 Assertions clause = getFinalReference();
214 //do the system assertions
215 if (Boolean.TRUE.equals(clause.enableSystemAssertions)) {
216 getProject().log("Enabling system assertions", Project.MSG_DEBUG);
217 commandIterator.add("-enablesystemassertions");
218 } else if (Boolean.FALSE.equals(clause.enableSystemAssertions)) {
219 getProject().log("disabling system assertions", Project.MSG_DEBUG);
220 commandIterator.add("-disablesystemassertions");
221 }
222
223 //now any inner assertions
224 Iterator it = clause.assertionList.iterator();
225 while (it.hasNext()) {
226 BaseAssertion assertion = (BaseAssertion) it.next();
227 String arg = assertion.toCommand();
228 getProject().log("adding assertion "+arg, Project.MSG_DEBUG);
229 commandIterator.add(arg);
230 }
231 }
232
233 /**
234 * helper method to add a string JVM argument to a command
235 * @param command
236 * @param arg
237 */
238 private static void addVmArgument(CommandlineJava command, String arg) {
239 Commandline.Argument argument;
240 argument = command.createVmArgument();
241 argument.setValue(arg);
242 }
243
244 /**
245 * clone the objects.
246 * This is not a full depth clone; the list of assertions is cloned,
247 * but it does not clone the underlying assertions.
248 * @return a cli
249 * @throws CloneNotSupportedException
250 */
251 public Object clone() throws CloneNotSupportedException {
252 Assertions that = (Assertions) super.clone();
253 that.assertionList = (ArrayList) assertionList.clone();
254 return that;
255 }
256
257 /**
258 * base class for our assertion elements.
259 */
260
261 public abstract static class BaseAssertion {
262 private String packageName;
263 private String className;
264
265 /**
266 * name a class
267 * @param className
268 */
269 public void setClass(String className) {
270 this.className = className;
271 }
272
273 /**
274 * name a package
275 * @param packageName
276 */
277 public void setPackage(String packageName) {
278 this.packageName = packageName;
279 }
280
281 /**
282 * what is the class name?
283 * @return classname or null
284 * @see #setClass
285 */
286 protected String getClassName() {
287 return className;
288 }
289
290 /**
291 * what is the package name?
292 * @return package name or null
293 * @see #setPackage
294 */
295 protected String getPackageName() {
296 return packageName;
297 }
298
299 /**
300 * get the prefix used to begin the command; -ea or -da.
301 * @return prefix
302 */
303 public abstract String getCommandPrefix();
304
305 /**
306 * create a full command string from this class
307 * @throws BuildException in case of trouble
308 * @return The command string
309 */
310 public String toCommand() {
311 //catch invalidness
312 if (getPackageName() != null && getClassName() != null) {
313 throw new BuildException("Both package and class have been set");
314 }
315 StringBuffer command = new StringBuffer(getCommandPrefix());
316 //see if it is a package or a class
317 if (getPackageName() != null) {
318 //packages get a ... prefix
319 command.append(':');
320 command.append(getPackageName());
321 if (!command.toString().endsWith("...")) {
322 //append the ... suffix if not there already
323 command.append("...");
324 }
325 } else if (getClassName() != null) {
326 //classes just get the classname
327 command.append(':');
328 command.append(getClassName());
329 }
330 return command.toString();
331 }
332 }
333
334
335 /**
336 * an enabled assertion enables things
337 */
338 public static class EnabledAssertion extends BaseAssertion {
339 /**
340 * get the prefix used to begin the command; -ea or -da.
341 * @return prefix
342 */
343 public String getCommandPrefix() {
344 return "-ea";
345 }
346
347 }
348
349 /**
350 * A disabled assertion disables things
351 */
352 public static class DisabledAssertion extends BaseAssertion {
353 /**
354 * get the prefix used to begin the command; -ea or -da.
355 * @return prefix
356 */
357 public String getCommandPrefix() {
358 return "-da";
359 }
360
361 }
362}
Note: See TracBrowser for help on using the repository browser.