KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > record > formula > Ptg


1 /* ====================================================================
2    Copyright 2003-2004 Apache Software Foundation
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
17 package org.apache.poi.hssf.record.formula;
18
19 import java.util.List JavaDoc;
20 import java.util.ArrayList JavaDoc;
21
22 import org.apache.poi.hssf.model.Workbook;
23
24 /**
25  *
26  * @author andy
27  * @author avik
28  * @author Jason Height (jheight at chariot dot net dot au)
29  */

30
31 public abstract class Ptg
32 {
33
34         
35     /* convert infix order ptg list to rpn order ptg list
36      * @return List ptgs in RPN order
37      * @param infixPtgs List of ptgs in infix order
38      */

39     
40     /* DO NOT REMOVE
41      *we keep this method in case we wish to change the way we parse
42      *It needs a getPrecedence in OperationsPtg
43     
44     public static List ptgsToRpn(List infixPtgs) {
45         java.util.Stack operands = new java.util.Stack();
46         java.util.List retval = new java.util.Stack();
47         
48         java.util.ListIterator i = infixPtgs.listIterator();
49         Object p;
50         OperationPtg o ;
51         boolean weHaveABracket = false;
52         while (i.hasNext()) {
53             p=i.next();
54             if (p instanceof OperationPtg) {
55                 if (p instanceof ParenthesisPtg) {
56                     if (!weHaveABracket) {
57                         operands.push(p);
58                         weHaveABracket = true;
59                     } else {
60                         o = (OperationPtg) operands.pop();
61                         while (!(o instanceof ParenthesisPtg)) {
62                             retval.add(o);
63                         }
64                         weHaveABracket = false;
65                     }
66                 } else {
67                     
68                     while (!operands.isEmpty() && ((OperationPtg) operands.peek()).getPrecedence() >= ((OperationPtg) p).getPrecedence() ) { //TODO handle ^ since it is right associative
69                         retval.add(operands.pop());
70                     }
71                     operands.push(p);
72                 }
73             } else {
74                 retval.add(p);
75             }
76         }
77         while (!operands.isEmpty()) {
78             if (operands.peek() instanceof ParenthesisPtg ){
79                 //throw some error
80             } else {
81                 retval.add(operands.pop());
82             }
83         }
84         return retval;
85     }
86     */

87     
88     public static Ptg createPtg(byte [] data, int offset)
89     {
90         byte id = data[ offset + 0 ];
91         Ptg retval = null;
92
93         final byte valueRef = ReferencePtg.sid + 0x20;
94         final byte arrayRef = ReferencePtg.sid + 0x40;
95         final byte valueFunc = FuncPtg.sid + 0x20;
96         final byte arrayFunc = FuncPtg.sid + 0x40;
97         final byte valueFuncVar = FuncVarPtg.sid +0x20;
98         final byte arrayFuncVar = FuncVarPtg.sid+0x40;
99         final byte valueArea = AreaPtg.sid + 0x20;
100         final byte arrayArea = AreaPtg.sid + 0x40;
101
102         switch (id)
103         {
104             case AddPtg.sid :
105                 retval = new AddPtg(data, offset);
106                 break;
107
108             case SubtractPtg.sid :
109                 retval = new SubtractPtg(data, offset);
110                 break;
111
112             case BoolPtg.sid:
113                retval = new BoolPtg(data, offset);
114                break;
115
116             case IntPtg.sid :
117                 retval = new IntPtg(data, offset);
118                 break;
119
120             case DividePtg.sid :
121                 retval = new DividePtg(data, offset);
122                 break;
123
124             case MultiplyPtg.sid :
125                 retval = new MultiplyPtg(data, offset);
126                 break;
127
128             case PowerPtg.sid :
129                 retval = new PowerPtg(data, offset);
130                 break;
131  
132             case EqualPtg.sid:
133                 retval = new EqualPtg(data, offset);
134                 break;
135                 
136             case GreaterThanPtg.sid:
137                 retval = new GreaterThanPtg(data, offset);
138                 break;
139                 
140             case LessThanPtg.sid:
141                 retval = new LessThanPtg(data, offset);
142                 break;
143
144                case LessEqualPtg.sid:
145                    retval = new LessEqualPtg(data, offset);
146                    break;
147                             
148                case GreaterEqualPtg.sid:
149                    retval = new GreaterEqualPtg(data, offset);
150                    break;
151                    
152                case NotEqualPtg.sid:
153                  retval = new NotEqualPtg(data, offset);
154                  break;
155                    
156             case ConcatPtg.sid :
157                 retval = new ConcatPtg(data, offset);
158                 break;
159
160             case AreaPtg.sid :
161                 retval = new AreaPtg(data, offset);
162                 break;
163             case valueArea:
164                 retval = new AreaPtg(data, offset);
165                 break;
166             case arrayArea:
167                 retval = new AreaPtg(data, offset);
168                 break;
169             case MemErrPtg.sid : // 0x27 These 3 values
170
case MemErrPtg.sid+0x20 : // 0x47 documented in
171
case MemErrPtg.sid+0x40 : // 0x67 openOffice.org doc.
172
retval = new MemErrPtg(data, offset);
173                 break;
174
175             case AttrPtg.sid :
176                 retval = new AttrPtg(data, offset);
177                 break;
178                 
179             case ReferencePtg.sid :
180                 retval = new ReferencePtg(data, offset);
181                 break;
182             case valueRef :
183                 retval = new ReferencePtg(data, offset);
184                 break;
185             case arrayRef :
186                 retval = new ReferencePtg(data, offset);
187                 break;
188
189             case ParenthesisPtg.sid :
190                 retval = new ParenthesisPtg(data, offset);
191                 break;
192
193             case MemFuncPtg.sid :
194                 retval = new MemFuncPtg(data, offset);
195                 break;
196
197             case UnionPtg.sid :
198                 retval = new UnionPtg(data, offset);
199                 break;
200
201             case FuncPtg.sid :
202                 retval = new FuncPtg(data, offset);
203                 break;
204                 
205             case valueFunc :
206                 retval = new FuncPtg(data, offset);
207                 break;
208             case arrayFunc :
209                 retval = new FuncPtg(data, offset);
210                 break;
211
212             case FuncVarPtg.sid :
213                 retval = new FuncVarPtg(data, offset);
214                 break;
215                 
216             case valueFuncVar :
217                 retval = new FuncVarPtg(data, offset);
218                 break;
219             case arrayFuncVar :
220                 retval = new FuncVarPtg(data, offset);
221                 break;
222                 
223             case NumberPtg.sid :
224                retval = new NumberPtg(data, offset);
225                break;
226
227             case StringPtg.sid :
228                retval = new StringPtg(data, offset);
229                break;
230
231             case NamePtg.sid : // 0x23 These 3 values
232
case NamePtg.sid+0x20 : // 0x43 documented in
233
case NamePtg.sid+0x40 : // 0x63 openOffice.org doc.
234

235                 retval = new NamePtg(data, offset);
236                 break;
237                 
238             case NameXPtg.sid : // 0x39
239
case NameXPtg.sid+0x20 : // 0x45
240
case NameXPtg.sid+0x40 : // 0x79
241

242                 retval = new NameXPtg(data, offset);
243                 break;
244
245             case ExpPtg.sid :
246                 retval = new ExpPtg(data, offset);
247                 break;
248
249             case Area3DPtg.sid : // 0x3b These 3 values
250
case Area3DPtg.sid+0x20 : // 0x5b documented in
251
case Area3DPtg.sid+0x40 : // 0x7b openOffice.org doc.
252

253                 retval = new Area3DPtg(data, offset);
254                 break;
255
256             case Ref3DPtg.sid: // 0x3a These 3 values
257
case Ref3DPtg.sid+0x20: // 0x5a documented in
258
case Ref3DPtg.sid+0x40: // 0x7a openOffice.org doc.
259

260                 retval = new Ref3DPtg(data, offset);
261                 break;
262                 
263             case MissingArgPtg.sid:
264                 retval = new MissingArgPtg(data,offset);
265                 break;
266             case UnaryPlusPtg.sid:
267                 retval=new UnaryPlusPtg(data,offset);
268                 break;
269             case UnaryMinusPtg.sid:
270                 retval=new UnaryMinusPtg(data,offset);
271                 break;
272
273             default :
274
275                  //retval = new UnknownPtg();
276
throw new java.lang.UnsupportedOperationException JavaDoc(
277                         Integer.toHexString(( int ) id) + " (" + ( int ) id + ")");
278         }
279         
280         if (id > 0x60) {
281             retval.setClass(CLASS_ARRAY);
282         } else if (id > 0x40) {
283             retval.setClass(CLASS_VALUE);
284         } else
285             retval.setClass(CLASS_REF);
286        return retval;
287         
288     }
289
290     public abstract int getSize();
291
292     public final byte [] getBytes()
293     {
294         int size = getSize();
295         byte[] bytes = new byte[ size ];
296
297         writeBytes(bytes, 0);
298         return bytes;
299     }
300     /** write this Ptg to a byte array*/
301     public abstract void writeBytes(byte [] array, int offset);
302     
303     /**
304      * return a string representation of this token alone
305      */

306     public abstract String JavaDoc toFormulaString(Workbook book);
307     /**
308      * dump a debug representation (hexdump) to a string
309      */

310     public String JavaDoc toDebugString() {
311         byte[] ba = new byte[getSize()];
312         String JavaDoc retval=null;
313         writeBytes(ba,0);
314         try {
315             retval = org.apache.poi.util.HexDump.dump(ba,0,0);
316         } catch (Exception JavaDoc e) {
317             e.printStackTrace();
318         }
319         return retval;
320     }
321     
322     /** Overridden toString method to ensure object hash is not printed.
323      * This helps get rid of gratuitous diffs when comparing two dumps
324      * Subclasses may output more relevant information by overriding this method
325      **/

326     public String JavaDoc toString(){
327         return this.getClass().toString();
328     }
329     
330     public static final byte CLASS_REF = 0x00;
331     public static final byte CLASS_VALUE = 0x20;
332     public static final byte CLASS_ARRAY = 0x40;
333     
334     protected byte ptgClass = CLASS_REF; //base ptg
335

336     public void setClass(byte thePtgClass) {
337         ptgClass = thePtgClass;
338     }
339     
340     /** returns the class (REF/VALUE/ARRAY) for this Ptg */
341     public byte getPtgClass() {
342         return ptgClass;
343     }
344     
345     public abstract byte getDefaultOperandClass();
346
347     public abstract Object JavaDoc clone();
348
349     
350     
351 }
352
Popular Tags