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    * Ptg.java
58    *
59    * Created on October 28, 2001, 6:30 PM
60    */
61   package org.apache.poi.hssf.record.formula;
62   
63   import java.util.List;
64   import java.util.ArrayList;
65   
66   /**
67    *
68    * @author  andy
69    */
70   
71   public abstract class Ptg
72   {
73   
74       /** Creates new Ptg */
75       
76       public Ptg()
77       {
78       }
79       
80       
81       
82       /*
83       private static List ptgsToList(Class [] ptgs)
84       {
85           List         result = new ArrayList();
86           Constructor constructor;
87   
88           for (int i = 0; i < ptgs.length; i++)
89           {
90               Class ptg = null;
91    
92               ptg = ptgs[ i ];
93               try
94               {
95                   
96                   constructor = ptg.getConstructor(new Class[]
97                   {
98                       byte [].class, int.class
99                   });
100              }
101              catch (Exception illegalArgumentException)
102              {
103                  throw new RuntimeException(
104                      "Now that didn't work nicely at all (couldn't do that there list of ptgs)");
105              }
106              result.add(constructor);
107          }
108          return result;
109      }*/
110      
111  
112      public static Ptg createPtg(byte [] data, int offset)
113      {
114          byte id     = data[ offset + 0 ];
115          Ptg  retval = null;
116  
117          System.out.println("PTG = " + Integer.toHexString(id) + " (" + id
118                             + ")");
119          switch (id)
120          {
121  
122              case AddPtg.sid :
123                  retval = new AddPtg(data, offset);
124                  break;
125  
126              case SubtractPtg.sid :
127                  retval = new SubtractPtg(data, offset);
128                  break;
129  
130              case IntPtg.sid :
131                  retval = new IntPtg(data, offset);
132                  break;
133  
134              case DividePtg.sid :
135                  retval = new DividePtg(data, offset);
136                  break;
137  
138              case MultiplyPtg.sid :
139                  retval = new MultiplyPtg(data, offset);
140                  break;
141  
142              case PowerPtg.sid :
143                  retval = new PowerPtg(data, offset);
144                  break;
145  
146              case AreaPtg.sid :
147                  retval = new AreaPtg(data, offset);
148                  break;
149  
150              case MemErrPtg.sid :
151                  retval = new MemErrPtg(data, offset);
152                  break;
153  
154              case AttrPtg.sid :
155                  retval = new AttrPtg(data, offset);
156                  break;
157  
158              case ValueReferencePtg.sid :
159                  retval = new ValueReferencePtg(data, offset);
160                  break;
161  
162      //        case ParenthesisPtg.sid :
163        //          retval = new ParenthesisPtg(data, offset);
164          //        break;
165  
166              case ValueVariableFunctionPtg.sid :
167                  retval = new ValueVariableFunctionPtg(data, offset);
168                  break;
169  
170              case NamePtg.sid :
171                  retval = new NamePtg(data, offset);
172                  break;
173  
174              case ExpPtg.sid :
175                  retval = new ExpPtg(data, offset);
176                  break;
177  
178              default :
179  
180                  // retval = new UnknownPtg();
181                  throw new RuntimeException("Unknown PTG = "
182                                             + Integer.toHexString(( int ) id)
183                                             + " (" + ( int ) id + ")");
184          }
185          return retval;
186      }
187  
188      public abstract int getSize();
189  
190      public final byte [] getBytes()
191      {
192          int    size  = getSize();
193          byte[] bytes = new byte[ size ];
194  
195          writeBytes(bytes, 0);
196          return bytes;
197      }
198  
199      public abstract void writeBytes(byte [] array, int offset);
200  
201      public abstract String toFormulaString();
202      
203      /**
204       * Ptg's should override this
205       */
206  //    public boolean isNextStringToken(String formula, int pos) {
207  //        return false;
208  //    }
209      
210      public int getPrecedence() {
211          return 100;
212      }
213      
214      public int getStringLength() {
215          return 0;
216      }
217      
218      
219  }
220