KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tools > ant > types > Assertions


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.types;
20
21 import org.apache.tools.ant.BuildException;
22 import org.apache.tools.ant.Project;
23
24 import java.util.ArrayList JavaDoc;
25 import java.util.Iterator JavaDoc;
26 import java.util.List JavaDoc;
27 import java.util.ListIterator JavaDoc;
28
29 /**
30  * The assertion datatype. This type describes
31  * assertion settings for the <java> task and others.
32  * One can set the system assertions, and enable/disable those in
33  * packages and classes.
34  * Assertions can only be enabled or disabled when forking Java.
35  *
36  * Example: set system assertions and all org.apache packages except
37  * for ant, and the class org.apache.tools.ant.Main.
38  * <pre>
39  * &lt;assertions enableSystemAssertions="true" &gt;
40  * &lt;enable package="org.apache" /&gt;
41  * &lt;disable package="org.apache.ant" /&gt;
42  * &lt;enable class="org.apache.tools.ant.Main"/&gt;
43  * &lt;/assertions&gt;
44  *</pre>
45  * Disable system assertions; enable those in the anonymous package
46  * <pre>
47  * &lt;assertions enableSystemAssertions="false" &gt;
48  * &lt;enable package="..." /&gt;
49  * &lt;/assertions&gt;
50  * </pre>
51  * enable assertions in a class called Test
52  * <pre>
53  * &lt;assertions &gt;
54  * &lt;enable class="Test" /&gt;
55  * &lt;/assertions&gt;
56  * </pre>
57  * This type is a datatype, so you can declare assertions and use them later
58  *
59  * <pre>
60  * &lt;assertions id="project.assertions" &gt;
61  * &lt;enable project="org.apache.test" /&gt;
62  * &lt;/assertions&gt;
63  *
64  * &lt;assertions refid="project.assertions" /&gt;
65  *
66  * </pre>
67  * @since Ant 1.6
68  */

69 public class Assertions extends DataType implements Cloneable JavaDoc {
70
71     /**
72      * enable/disable sys assertions; null means undefined
73      */

74     private Boolean JavaDoc enableSystemAssertions;
75
76     /**
77      * list of type BaseAssertion
78      */

79     private ArrayList JavaDoc assertionList = new ArrayList JavaDoc();
80
81
82     /**
83      * enable assertions
84      * @param assertion an enable assertion nested element
85      */

86     public void addEnable(EnabledAssertion assertion) {
87         checkChildrenAllowed();
88         assertionList.add(assertion);
89     }
90
91     /**
92      * disable assertions
93      * @param assertion a disable assertion nested element
94      */

95     public void addDisable(DisabledAssertion assertion) {
96         checkChildrenAllowed();
97         assertionList.add(assertion);
98     }
99
100     /**
101      * enable or disable system assertions.
102      * Default is not set (neither -enablesystemassersions or -disablesytemassertions
103      * are used on the command line).
104      * @param enableSystemAssertions if true enable system assertions
105      */

106     public void setEnableSystemAssertions(Boolean JavaDoc enableSystemAssertions) {
107         checkAttributesAllowed();
108         this.enableSystemAssertions = enableSystemAssertions;
109     }
110
111     /**
112      * Set the value of the refid attribute.
113      *
114      * <p>Subclasses may need to check whether any other attributes
115      * have been set as well or child elements have been created and
116      * thus override this method. if they do the must call
117      * <code>super.setRefid</code>.</p>
118      * @param ref the reference to use
119      */

120     public void setRefid(Reference ref) {
121         if (assertionList.size() > 0 || enableSystemAssertions != null) {
122             throw tooManyAttributes();
123         }
124         super.setRefid(ref);
125     }
126
127     /**
128      * get whatever we are referencing to. This could be ourself.
129      * @return the object that contains the assertion info
130      */

131     private Assertions getFinalReference() {
132         if (getRefid() == null) {
133             return this;
134         } else {
135             Object JavaDoc o = getRefid().getReferencedObject(getProject());
136             if (!(o instanceof Assertions)) {
137                 throw new BuildException("reference is of wrong type");
138             }
139             return (Assertions) o;
140         }
141     }
142
143     /**
144      * how many assertions are made...will resolve references before returning
145      * @return total # of commands to make
146      */

147     public int size() {
148         Assertions clause = getFinalReference();
149         return clause.getFinalSize();
150     }
151
152
153     /**
154      * what is the final size of this object
155      * @return
156      */

157     private int getFinalSize() {
158         return assertionList.size() + (enableSystemAssertions != null ? 1 : 0);
159     }
160
161     /**
162      * add the assertions to a list in a format suitable
163      * for adding to a command line
164      * @param commandList the command line to format
165      */

166     public void applyAssertions(List JavaDoc commandList) {
167         getProject().log("Applying assertions", Project.MSG_DEBUG);
168         Assertions clause = getFinalReference();
169         //do the system assertions
170
if (Boolean.TRUE.equals(clause.enableSystemAssertions)) {
171             getProject().log("Enabling system assertions", Project.MSG_DEBUG);
172             commandList.add("-enablesystemassertions");
173         } else if (Boolean.FALSE.equals(clause.enableSystemAssertions)) {
174             getProject().log("disabling system assertions", Project.MSG_DEBUG);
175             commandList.add("-disablesystemassertions");
176         }
177
178         //now any inner assertions
179
Iterator JavaDoc it = clause.assertionList.iterator();
180         while (it.hasNext()) {
181             BaseAssertion assertion = (BaseAssertion) it.next();
182             String JavaDoc arg = assertion.toCommand();
183             getProject().log("adding assertion " + arg, Project.MSG_DEBUG);
184             commandList.add(arg);
185         }
186     }
187
188     /**
189      * apply all the assertions to the command.
190      * @param command the command line to format
191      */

192     public void applyAssertions(CommandlineJava command) {
193         Assertions clause = getFinalReference();
194         //do the system assertions
195
if (Boolean.TRUE.equals(clause.enableSystemAssertions)) {
196             addVmArgument(command, "-enablesystemassertions");
197         } else if (Boolean.FALSE.equals(clause.enableSystemAssertions)) {
198             addVmArgument(command, "-disablesystemassertions");
199         }
200
201         //now any inner assertions
202
Iterator JavaDoc it = clause.assertionList.iterator();
203         while (it.hasNext()) {
204             BaseAssertion assertion = (BaseAssertion) it.next();
205             String JavaDoc arg = assertion.toCommand();
206             addVmArgument(command, arg);
207         }
208     }
209
210     /**
211      * add the assertions to a list in a format suitable
212      * for adding to a command line
213      * @param commandIterator list of commands
214      */

215     public void applyAssertions(final ListIterator JavaDoc commandIterator) {
216         getProject().log("Applying assertions", Project.MSG_DEBUG);
217         Assertions clause = getFinalReference();
218         //do the system assertions
219
if (Boolean.TRUE.equals(clause.enableSystemAssertions)) {
220             getProject().log("Enabling system assertions", Project.MSG_DEBUG);
221             commandIterator.add("-enablesystemassertions");
222         } else if (Boolean.FALSE.equals(clause.enableSystemAssertions)) {
223             getProject().log("disabling system assertions", Project.MSG_DEBUG);
224             commandIterator.add("-disablesystemassertions");
225         }
226
227         //now any inner assertions
228
Iterator JavaDoc it = clause.assertionList.iterator();
229         while (it.hasNext()) {
230             BaseAssertion assertion = (BaseAssertion) it.next();
231             String JavaDoc arg = assertion.toCommand();
232             getProject().log("adding assertion " + arg, Project.MSG_DEBUG);
233             commandIterator.add(arg);
234         }
235     }
236
237     /**
238      * helper method to add a string JVM argument to a command
239      * @param command
240      * @param arg
241      */

242     private static void addVmArgument(CommandlineJava command, String JavaDoc arg) {
243         Commandline.Argument argument;
244         argument = command.createVmArgument();
245         argument.setValue(arg);
246     }
247
248     /**
249      * clone the objects.
250      * This is not a full depth clone; the list of assertions is cloned,
251      * but it does not clone the underlying assertions.
252      * @return a cli
253      * @throws CloneNotSupportedException if the super class does not support cloning
254      */

255     public Object JavaDoc clone() throws CloneNotSupportedException JavaDoc {
256         Assertions that = (Assertions) super.clone();
257         that.assertionList = (ArrayList JavaDoc) assertionList.clone();
258         return that;
259     }
260
261     /**
262      * base class for our assertion elements.
263      */

264
265     public abstract static class BaseAssertion {
266         private String JavaDoc packageName;
267         private String JavaDoc className;
268
269         /**
270          * name a class
271          * @param className a class name
272          */

273         public void setClass(String JavaDoc className) {
274             this.className = className;
275         }
276
277         /**
278          * name a package
279          * @param packageName a package name
280          */

281         public void setPackage(String JavaDoc packageName) {
282             this.packageName = packageName;
283         }
284
285         /**
286          * what is the class name?
287          * @return classname or null
288          * @see #setClass
289          */

290         protected String JavaDoc getClassName() {
291             return className;
292         }
293
294         /**
295          * what is the package name?
296          * @return package name or null
297          * @see #setPackage
298          */

299         protected String JavaDoc getPackageName() {
300             return packageName;
301         }
302
303         /**
304          * get the prefix used to begin the command; -ea or -da.
305          * @return prefix
306          */

307         public abstract String JavaDoc getCommandPrefix();
308
309         /**
310          * create a full command string from this class
311          * @throws BuildException in case of trouble
312          * @return The command string
313          */

314         public String JavaDoc toCommand() {
315             //catch invalidness
316
if (getPackageName() != null && getClassName() != null) {
317                 throw new BuildException("Both package and class have been set");
318             }
319             StringBuffer JavaDoc command = new StringBuffer JavaDoc(getCommandPrefix());
320             //see if it is a package or a class
321
if (getPackageName() != null) {
322                 //packages get a ... prefix
323
command.append(':');
324                 command.append(getPackageName());
325                 if (!command.toString().endsWith("...")) {
326                     //append the ... suffix if not there already
327
command.append("...");
328                 }
329             } else if (getClassName() != null) {
330                 //classes just get the classname
331
command.append(':');
332                 command.append(getClassName());
333             }
334             return command.toString();
335         }
336     }
337
338
339     /**
340      * an enabled assertion enables things
341      */

342     public static class EnabledAssertion extends BaseAssertion {
343         /**
344          * get the prefix used to begin the command; -ea or -da.
345          * @return prefix
346          */

347         public String JavaDoc getCommandPrefix() {
348             return "-ea";
349         }
350
351     }
352
353     /**
354      * A disabled assertion disables things
355      */

356     public static class DisabledAssertion extends BaseAssertion {
357         /**
358          * get the prefix used to begin the command; -ea or -da.
359          * @return prefix
360          */

361         public String JavaDoc getCommandPrefix() {
362             return "-da";
363         }
364
365     }
366 }
367
Popular Tags