Debian Codegraph @

src/main/java/net/sf/antcontrib/logic/Assert.java

raw symbols SCIP
1 /*
2 * Copyright (c) 2001-2004 Ant-Contrib project. All rights reserved.
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 package net.sf.antcontrib.logic;
17
18 import java.util.ArrayList;
19 import java.util.Iterator;
20 import java.util.List;
21
22 import net.sf.antcontrib.logic.condition.BooleanConditionBase;
23
24 import org.apache.tools.ant.BuildException;
25 import org.apache.tools.ant.Project;
26 import org.apache.tools.ant.ProjectHelper;
27 import org.apache.tools.ant.Task;
28 import org.apache.tools.ant.TaskContainer;
29 import org.apache.tools.ant.taskdefs.Exit;
30 import org.apache.tools.ant.taskdefs.Sequential;
31 import org.apache.tools.ant.taskdefs.condition.Condition;
32 import org.apache.tools.ant.taskdefs.condition.Equals;
33
34
35 /**
36 *
37 */
38 public class Assert
39 extends BooleanConditionBase {
40
41 private List tasks = new ArrayList();
42 private String message;
43 private boolean failOnError = true;
44 private boolean execute = true;
45 private Sequential sequential;
46 private String name;
47 private String value;
48
49 public Sequential createSequential() {
50 this.sequential = (Sequential) getProject().createTask("sequential");
51 return this.sequential;
52 }
53
54 public void setName(String name) {
55 this.name = name;
56 }
57
58 public void setValue(String value) {
59 this.value = value;
60 }
61
62 public void setMessage(String message) {
63 this.message = message;
64 }
65
66 public BooleanConditionBase createBool() {
67 return this;
68 }
69
70 public void setExecute(boolean execute) {
71 this.execute = execute;
72 }
73
74 public void setFailOnError(boolean failOnError) {
75 this.failOnError = failOnError;
76 }
77
78 public void execute() {
79 String use_asserts = getProject().getProperty("ant.enable.asserts");
80 boolean assertsEnabled = Project.toBoolean(use_asserts);
81
82 if (assertsEnabled) {
83 if (name != null) {
84 if (value == null) {
85 throw new BuildException("The 'value' attribute must accompany the 'name' attribute.");
86 }
87 String propVal = getProject().replaceProperties("${" + name + "}");
88 Equals e = new Equals();
89 e.setArg1(propVal);
90 e.setArg2(value);
91 addEquals(e);
92 }
93
94 if (countConditions() == 0) {
95 throw new BuildException("There is no condition specified.");
96 }
97 else if (countConditions() > 1) {
98 throw new BuildException("There must be exactly one condition specified.");
99 }
100
101 Condition c = (Condition) getConditions().nextElement();
102 if (! c.eval()) {
103 if (failOnError) {
104 Exit fail = (Exit) getProject().createTask("fail");
105 fail.setMessage(message);
106 fail.execute();
107 }
108 }
109 else {
110 if (execute && sequential != null) {
111 this.sequential.execute();
112 }
113 }
114 }
115 else {
116 if (execute && sequential != null) {
117 this.sequential.execute();
118 }
119 }
120 }
121
122
123 }