Debian Codegraph @

src/main/java/net/sf/antcontrib/math/MathTask.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.math;
17
18 import org.apache.tools.ant.BuildException;
19 import org.apache.tools.ant.Task;
20 import org.apache.tools.ant.DynamicConfigurator;
21
22 /**
23 * Task for mathematical operations.
24 *
25 * @author inger
26 */
27
28
29 public class MathTask
30 extends Task
31 implements DynamicConfigurator
32 {
33 // storage for result
34 private String result = null;
35 private Operation operation = null;
36 private Operation locOperation = null;
37 private String datatype = null;
38 private boolean strict = false;
39
40 public MathTask()
41 {
42 super();
43 }
44
45 public void execute()
46 throws BuildException
47 {
48 Operation op = locOperation;
49 if (op == null)
50 op = operation;
51
52 Number res = op.evaluate();
53
54 if (datatype != null)
55 res = Math.convert(res, datatype);
56 getProject().setUserProperty(result, res.toString());
57 }
58
59 public void setDynamicAttribute(String s, String s1)
60 throws BuildException {
61 throw new BuildException("No dynamic attributes for this task");
62 }
63
64 public Object createDynamicElement(String name)
65 throws BuildException {
66 Operation op = new Operation();
67 op.setOperation(name);
68 operation = op;
69 return op;
70 }
71
72 public void setResult(String result)
73 {
74 this.result = result;
75 }
76
77 public void setDatatype(String datatype)
78 {
79 this.datatype = datatype;
80 }
81
82 public void setStrict(boolean strict)
83 {
84 this.strict = strict;
85 }
86
87 private Operation getLocalOperation()
88 {
89 if (locOperation == null)
90 {
91 locOperation = new Operation();
92 locOperation.setDatatype(datatype);
93 locOperation.setStrict(strict);
94 }
95 return locOperation;
96 }
97
98 public void setOperation(String operation)
99 {
100 getLocalOperation().setOperation(operation);
101 }
102
103 public void setDataType(String dataType)
104 {
105 getLocalOperation().setDatatype(dataType);
106 }
107
108 public void setOperand1(String operand1)
109 {
110 getLocalOperation().setArg1(operand1);
111 }
112
113 public void setOperand2(String operand2)
114 {
115 getLocalOperation().setArg2(operand2);
116 }
117
118 public Operation createOperation()
119 {
120 if (locOperation != null || operation != null)
121 throw new BuildException("Only 1 operation can be specified");
122 this.operation = new Operation();
123 this.operation.setStrict(strict);
124 this.operation.setDatatype(datatype);
125 return this.operation;
126 }
127
128 // conform to old task
129 public Operation createOp()
130 {
131 return createOperation();
132 }
133 }