1 /
55
56
61 package org.apache.poi.hssf.record.formula;
62
63
67
68 public class FormulaUtil
69 {
70
71
72
73 public FormulaUtil()
74 {
75 }
76
77 public static Ptg [] parseFormula(String formula)
78 {
79 Ptg[] ptg = null;
80 StringBuffer f = new StringBuffer(formula);
81
82 if (isIntAddition(formula))
83 {
84 int loc = getLoc(formula, '+');
85
86 System.out.println(formula.substring(0, loc).trim() + ","
87 + formula.substring(loc + 1,
88 formula.length()).trim());
89 ptg = formulaAddTwoInts(Short
90 .parseShort(formula.substring(0, loc).trim()), Short
91 .parseShort(formula.substring(loc + 1, formula.length())
92 .trim()));
93 }
94 else if (isIntSubtraction(formula))
95 {
96 int loc = getLoc(formula, '-');
97
98 ptg = formulaSubtractTwoInts(Short
99 .parseShort(formula.substring(0, loc).trim()), Short
100 .parseShort(formula.substring(loc + 1, formula.length())
101 .trim()));
102 }
103 else if (isIntMultiplication(formula))
104 {
105 int loc = getLoc(formula, '*');
106
107 ptg = formulaMultiplyTwoInts(Short
108 .parseShort(formula.substring(0, loc).trim()), Short
109 .parseShort(formula.substring(loc + 1, formula.length())
110 .trim()));
111 }
112 else if (isIntDivision(formula))
113 {
114 int loc = getLoc(formula, '/');
115
116 ptg = formulaDivideTwoInts(Short
117 .parseShort(formula.substring(0, loc).trim()), Short
118 .parseShort(formula.substring(loc + 1, formula.length())
119 .trim()));
120 }
121 else if (isIntPower(formula))
122 {
123 int loc = getLoc(formula, '^');
124
125 ptg = formulaPowerTwoInts(Short
126 .parseShort(formula.substring(0, loc).trim()), Short
127 .parseShort(formula.substring(loc + 1, formula.length())
128 .trim()));
129 }
130 return ptg;
131 }
132
133 public static Ptg [] formulaAddTwoInts(short first, short second)
134 {
135 Ptg[] ptg = new Ptg[ 3 ];
136
137 ptg[ 0 ] = createInteger(first);
138 ptg[ 1 ] = createInteger(second);
139 ptg[ 2 ] = createAdd();
140 return ptg;
141 }
142
143 public static Ptg [] formulaSubtractTwoInts(short first, short second)
144 {
145 Ptg[] ptg = new Ptg[ 3 ];
146
147 ptg[ 0 ] = createInteger(first);
148 ptg[ 1 ] = createInteger(second);
149 ptg[ 2 ] = createSubtract();
150 return ptg;
151 }
152
153 public static Ptg [] formulaMultiplyTwoInts(short first, short second)
154 {
155 Ptg[] ptg = new Ptg[ 3 ];
156
157 ptg[ 0 ] = createInteger(first);
158 ptg[ 1 ] = createInteger(second);
159 ptg[ 2 ] = createMultiply();
160 return ptg;
161 }
162
163 public static Ptg [] formulaPowerTwoInts(short first, short second)
164 {
165 Ptg[] ptg = new Ptg[ 3 ];
166
167 ptg[ 0 ] = createInteger(second);
168 ptg[ 1 ] = createInteger(first);
169 ptg[ 2 ] = createPower();
170 return ptg;
171 }
172
173 public static Ptg [] formulaDivideTwoInts(short first, short second)
174 {
175 Ptg[] ptg = new Ptg[ 3 ];
176
177 ptg[ 0 ] = createInteger(first);
178 ptg[ 1 ] = createInteger(second);
179 ptg[ 2 ] = createDivide();
180 return ptg;
181 }
182
183 public static Ptg createInteger(short value)
184 {
185 IntPtg ptg = new IntPtg();
186
187 ptg.setValue(value);
188 return ptg;
189 }
190
191 public static Ptg createAdd()
192 {
193 AddPtg ptg = new AddPtg();
194
195 return ptg;
196 }
197
198 public static Ptg createSubtract()
199 {
200 SubtractPtg ptg = new SubtractPtg();
201
202 return ptg;
203 }
204
205 public static Ptg createMultiply()
206 {
207 MultiplyPtg ptg = new MultiplyPtg();
208
209 return ptg;
210 }
211
212 public static Ptg createDivide()
213 {
214 DividePtg ptg = new DividePtg();
215
216 return ptg;
217 }
218
219 public static Ptg createPower()
220 {
221 PowerPtg ptg = new PowerPtg();
222
223 return ptg;
224 }
225
226 private static boolean isIntAddition(String formula)
227 {
228 StringBuffer buffer = new StringBuffer(formula);
229
230 if (instr(formula, "+"))
231 {
232 return true;
233 }
234 return false;
235 }
236
237 private static boolean isIntSubtraction(String formula)
238 {
239 StringBuffer buffer = new StringBuffer(formula);
240
241 if (instr(formula, "-"))
242 {
243 return true;
244 }
245 return false;
246 }
247
248 private static boolean isIntMultiplication(String formula)
249 {
250 StringBuffer buffer = new StringBuffer(formula);
251
252 if (instr(formula, "*"))
253 {
254 return true;
255 }
256 return false;
257 }
258
259 private static boolean isIntDivision(String formula)
260 {
261 StringBuffer buffer = new StringBuffer(formula);
262
263 if (instr(formula, "/"))
264 {
265 return true;
266 }
267 return false;
268 }
269
270 private static boolean isIntPower(String formula)
271 {
272 StringBuffer buffer = new StringBuffer(formula);
273
274 if (instr(formula, "^"))
275 {
276 return true;
277 }
278 return false;
279 }
280
281 private static boolean instr(String matchin, String matchon)
282 {
283 int lenmatchin = matchin.length();
284 int lenmatchon = matchon.length();
285 int pos = 0;
286
287 if (lenmatchon > lenmatchin)
288 {
289 return false;
290 }
291 while (pos + lenmatchon < lenmatchin)
292 {
293 String sub = matchin.substring(pos, pos + lenmatchon);
294
295 if (sub.equals(matchon))
296 {
297 return true;
298 }
299 pos++;
300 }
301 return false;
302 }
303
304 private static int getLoc(String matchin, char matchon)
305 {
306 int retval = -1;
307
308 for (int pos = 0; pos < matchin.length(); pos++)
309 {
310 if (matchin.charAt(pos) == matchon)
311 {
312 retval = pos;
313 break;
314 }
315 }
316 return retval;
317 }
318 }
319