1    
2    /* ====================================================================
3     * The Apache Software License, Version 1.1
4     *
5     * Copyright (c) 2002 The Apache Software Foundation.  All rights
6     * reserved.
7     *
8     * Redistribution and use in source and binary forms, with or without
9     * modification, are permitted provided that the following conditions
10    * are met:
11    *
12    * 1. Redistributions of source code must retain the above copyright
13    *    notice, this list of conditions and the following disclaimer.
14    *
15    * 2. Redistributions in binary form must reproduce the above copyright
16    *    notice, this list of conditions and the following disclaimer in
17    *    the documentation and/or other materials provided with the
18    *    distribution.
19    *
20    * 3. The end-user documentation included with the redistribution,
21    *    if any, must include the following acknowledgment:
22    *       "This product includes software developed by the
23    *        Apache Software Foundation (http://www.apache.org/)."
24    *    Alternately, this acknowledgment may appear in the software itself,
25    *    if and wherever such third-party acknowledgments normally appear.
26    *
27    * 4. The names "Apache" and "Apache Software Foundation" and
28    *    "Apache POI" must not be used to endorse or promote products
29    *    derived from this software without prior written permission. For
30    *    written permission, please contact apache@apache.org.
31    *
32    * 5. Products derived from this software may not be called "Apache",
33    *    "Apache POI", nor may "Apache" appear in their name, without
34    *    prior written permission of the Apache Software Foundation.
35    *
36    * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37    * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38    * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39    * DISCLAIMED.  IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40    * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41    * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42    * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43    * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44    * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45    * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46    * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47    * SUCH DAMAGE.
48    * ====================================================================
49    *
50    * This software consists of voluntary contributions made by many
51    * individuals on behalf of the Apache Software Foundation.  For more
52    * information on the Apache Software Foundation, please see
53    * <http://www.apache.org/>.
54    */
55   
56   /*
57    * FormulaUtil.java
58    *
59    * Created on November 4, 2001, 5:50 AM
60    */
61   package org.apache.poi.hssf.record.formula;
62   
63   /**
64    *
65    * @author  andy
66    */
67   
68   public class FormulaUtil
69   {
70   
71       /** Creates new FormulaUtil */
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