2002-01-30 21:22:28 -05:00
/ * = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
2004-08-23 04:52:54 -04:00
Copyright 2003 - 2004 Apache Software Foundation
2004-04-09 09:05:39 -04:00
Licensed under the Apache License , Version 2 . 0 ( the " License " ) ;
you may not use this file except in compliance with the License .
You may obtain a copy of the License at
http : //www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing , software
distributed under the License is distributed on an " AS IS " BASIS ,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND , either express or implied .
See the License for the specific language governing permissions and
limitations under the License .
= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = * /
2002-01-30 21:22:28 -05:00
package org.apache.poi.hssf.record.formula ;
2002-04-18 08:00:53 -04:00
import java.util.List ;
import java.util.ArrayList ;
2003-08-31 02:16:57 -04:00
import org.apache.poi.hssf.model.Workbook ;
2002-07-14 20:14:40 -04:00
2002-01-30 21:22:28 -05:00
/ * *
*
* @author andy
2002-05-27 17:00:12 -04:00
* @author avik
2002-09-04 20:26:28 -04:00
* @author Jason Height ( jheight at chariot dot net dot au )
2002-01-30 21:22:28 -05:00
* /
public abstract class Ptg
{
2004-08-23 04:52:54 -04:00
/ * * convert infix order ptg list to rpn order ptg list
* @return List ptgs in RPN order
* @param infixPtgs List of ptgs in infix order
* /
2002-04-29 20:18:29 -04:00
2002-04-30 19:22:27 -04:00
/ * DO NOT REMOVE
2002-04-29 20:18:29 -04:00
* we keep this method in case we wish to change the way we parse
* It needs a getPrecedence in OperationsPtg
2002-04-27 10:07:53 -04:00
public static List ptgsToRpn ( List infixPtgs ) {
java . util . Stack operands = new java . util . Stack ( ) ;
java . util . List retval = new java . util . Stack ( ) ;
java . util . ListIterator i = infixPtgs . listIterator ( ) ;
Object p ;
OperationPtg o ;
boolean weHaveABracket = false ;
while ( i . hasNext ( ) ) {
p = i . next ( ) ;
if ( p instanceof OperationPtg ) {
if ( p instanceof ParenthesisPtg ) {
if ( ! weHaveABracket ) {
operands . push ( p ) ;
weHaveABracket = true ;
} else {
o = ( OperationPtg ) operands . pop ( ) ;
while ( ! ( o instanceof ParenthesisPtg ) ) {
retval . add ( o ) ;
}
weHaveABracket = false ;
}
} else {
while ( ! operands . isEmpty ( ) & & ( ( OperationPtg ) operands . peek ( ) ) . getPrecedence ( ) > = ( ( OperationPtg ) p ) . getPrecedence ( ) ) { //TODO handle ^ since it is right associative
retval . add ( operands . pop ( ) ) ;
}
operands . push ( p ) ;
}
} else {
retval . add ( p ) ;
}
}
while ( ! operands . isEmpty ( ) ) {
if ( operands . peek ( ) instanceof ParenthesisPtg ) {
//throw some error
} else {
retval . add ( operands . pop ( ) ) ;
}
}
return retval ;
}
2002-04-29 20:18:29 -04:00
* /
2002-05-06 14:23:24 -04:00
2002-04-20 19:42:10 -04:00
public static Ptg createPtg ( byte [ ] data , int offset )
2002-01-30 21:22:28 -05:00
{
byte id = data [ offset + 0 ] ;
Ptg retval = null ;
2002-05-27 17:00:12 -04:00
final byte valueRef = ReferencePtg . sid + 0x20 ;
final byte arrayRef = ReferencePtg . sid + 0x40 ;
2002-06-11 16:18:28 -04:00
final byte valueFunc = FuncPtg . sid + 0x20 ;
final byte arrayFunc = FuncPtg . sid + 0x40 ;
final byte valueFuncVar = FuncVarPtg . sid + 0x20 ;
final byte arrayFuncVar = FuncVarPtg . sid + 0x40 ;
2002-05-27 17:00:12 -04:00
final byte valueArea = AreaPtg . sid + 0x20 ;
final byte arrayArea = AreaPtg . sid + 0x40 ;
2002-04-28 21:25:28 -04:00
2002-01-30 21:22:28 -05:00
switch ( id )
{
case AddPtg . sid :
retval = new AddPtg ( data , offset ) ;
break ;
case SubtractPtg . sid :
retval = new SubtractPtg ( data , offset ) ;
break ;
2002-10-01 13:37:47 -04:00
case BoolPtg . sid :
retval = new BoolPtg ( data , offset ) ;
break ;
2002-01-30 21:22:28 -05:00
case IntPtg . sid :
retval = new IntPtg ( data , offset ) ;
break ;
case DividePtg . sid :
retval = new DividePtg ( data , offset ) ;
break ;
case MultiplyPtg . sid :
retval = new MultiplyPtg ( data , offset ) ;
break ;
case PowerPtg . sid :
retval = new PowerPtg ( data , offset ) ;
break ;
2002-09-02 17:16:29 -04:00
case EqualPtg . sid :
retval = new EqualPtg ( data , offset ) ;
break ;
2002-04-28 12:33:57 -04:00
2003-05-04 14:22:09 -04:00
case GreaterThanPtg . sid :
retval = new GreaterThanPtg ( data , offset ) ;
break ;
case LessThanPtg . sid :
retval = new LessThanPtg ( data , offset ) ;
break ;
2003-05-06 23:40:52 -04:00
case LessEqualPtg . sid :
retval = new LessEqualPtg ( data , offset ) ;
break ;
case GreaterEqualPtg . sid :
retval = new GreaterEqualPtg ( data , offset ) ;
break ;
case NotEqualPtg . sid :
retval = new NotEqualPtg ( data , offset ) ;
break ;
2002-04-28 12:33:57 -04:00
case ConcatPtg . sid :
retval = new ConcatPtg ( data , offset ) ;
break ;
2002-01-30 21:22:28 -05:00
case AreaPtg . sid :
retval = new AreaPtg ( data , offset ) ;
break ;
2002-05-27 17:00:12 -04:00
case valueArea :
retval = new AreaPtg ( data , offset ) ;
break ;
case arrayArea :
retval = new AreaPtg ( data , offset ) ;
break ;
2002-06-12 14:06:52 -04:00
case MemErrPtg . sid : // 0x27 These 3 values
case MemErrPtg . sid + 0x20 : // 0x47 documented in
case MemErrPtg . sid + 0x40 : // 0x67 openOffice.org doc.
2002-01-30 21:22:28 -05:00
retval = new MemErrPtg ( data , offset ) ;
break ;
case AttrPtg . sid :
retval = new AttrPtg ( data , offset ) ;
break ;
2002-05-27 17:00:12 -04:00
2002-04-28 17:35:57 -04:00
case ReferencePtg . sid :
retval = new ReferencePtg ( data , offset ) ;
2002-05-27 17:00:12 -04:00
break ;
case valueRef :
2002-04-28 18:31:55 -04:00
retval = new ReferencePtg ( data , offset ) ;
2002-05-27 17:00:12 -04:00
break ;
2002-04-28 18:31:55 -04:00
case arrayRef :
retval = new ReferencePtg ( data , offset ) ;
2002-05-27 17:00:12 -04:00
break ;
2002-01-30 21:22:28 -05:00
2002-04-28 12:40:37 -04:00
case ParenthesisPtg . sid :
retval = new ParenthesisPtg ( data , offset ) ;
break ;
2002-01-30 21:22:28 -05:00
2003-02-06 05:29:45 -05:00
case MemFuncPtg . sid :
retval = new MemFuncPtg ( data , offset ) ;
break ;
case UnionPtg . sid :
retval = new UnionPtg ( data , offset ) ;
break ;
2002-06-11 16:18:28 -04:00
case FuncPtg . sid :
retval = new FuncPtg ( data , offset ) ;
2002-04-28 21:25:28 -04:00
break ;
2002-04-29 23:05:07 -04:00
case valueFunc :
2002-06-11 16:18:28 -04:00
retval = new FuncPtg ( data , offset ) ;
2002-04-28 21:25:28 -04:00
break ;
case arrayFunc :
2002-06-11 16:18:28 -04:00
retval = new FuncPtg ( data , offset ) ;
break ;
2003-02-06 05:29:45 -05:00
2002-06-11 16:18:28 -04:00
case FuncVarPtg . sid :
retval = new FuncVarPtg ( data , offset ) ;
break ;
case valueFuncVar :
retval = new FuncVarPtg ( data , offset ) ;
break ;
case arrayFuncVar :
retval = new FuncVarPtg ( data , offset ) ;
2002-01-30 21:22:28 -05:00
break ;
2002-04-29 23:05:07 -04:00
2002-05-26 19:19:58 -04:00
case NumberPtg . sid :
retval = new NumberPtg ( data , offset ) ;
break ;
2002-04-29 20:18:29 -04:00
2002-05-26 19:19:58 -04:00
case StringPtg . sid :
retval = new StringPtg ( data , offset ) ;
break ;
2002-01-30 21:22:28 -05:00
2002-06-12 14:06:52 -04:00
case NamePtg . sid : // 0x23 These 3 values
case NamePtg . sid + 0x20 : // 0x43 documented in
case NamePtg . sid + 0x40 : // 0x63 openOffice.org doc.
2002-01-30 21:22:28 -05:00
retval = new NamePtg ( data , offset ) ;
break ;
2003-05-05 11:35:39 -04:00
case NameXPtg . sid : // 0x39
case NameXPtg . sid + 0x20 : // 0x45
case NameXPtg . sid + 0x40 : // 0x79
retval = new NameXPtg ( data , offset ) ;
break ;
2002-01-30 21:22:28 -05:00
case ExpPtg . sid :
retval = new ExpPtg ( data , offset ) ;
break ;
2002-06-12 14:06:52 -04:00
case Area3DPtg . sid : // 0x3b These 3 values
case Area3DPtg . sid + 0x20 : // 0x5b documented in
case Area3DPtg . sid + 0x40 : // 0x7b openOffice.org doc.
2002-04-23 18:24:41 -04:00
retval = new Area3DPtg ( data , offset ) ;
break ;
2002-06-12 14:06:52 -04:00
case Ref3DPtg . sid : // 0x3a These 3 values
case Ref3DPtg . sid + 0x20 : // 0x5a documented in
case Ref3DPtg . sid + 0x40 : // 0x7a openOffice.org doc.
2002-04-23 18:24:41 -04:00
retval = new Ref3DPtg ( data , offset ) ;
break ;
2002-06-11 16:18:28 -04:00
case MissingArgPtg . sid :
retval = new MissingArgPtg ( data , offset ) ;
break ;
2003-07-25 16:11:27 -04:00
case UnaryPlusPtg . sid :
retval = new UnaryPlusPtg ( data , offset ) ;
break ;
case UnaryMinusPtg . sid :
retval = new UnaryMinusPtg ( data , offset ) ;
break ;
2002-04-23 18:24:41 -04:00
2002-01-30 21:22:28 -05:00
default :
2002-06-11 16:18:28 -04:00
//retval = new UnknownPtg();
throw new java . lang . UnsupportedOperationException (
Integer . toHexString ( ( int ) id ) + " ( " + ( int ) id + " ) " ) ;
2002-01-30 21:22:28 -05:00
}
2002-05-27 17:00:12 -04:00
if ( id > 0x60 ) {
retval . setClass ( CLASS_ARRAY ) ;
} else if ( id > 0x40 ) {
retval . setClass ( CLASS_VALUE ) ;
} else
retval . setClass ( CLASS_REF ) ;
return retval ;
2002-04-20 19:42:10 -04:00
}
2002-01-30 21:22:28 -05:00
public abstract int getSize ( ) ;
public final byte [ ] getBytes ( )
{
int size = getSize ( ) ;
byte [ ] bytes = new byte [ size ] ;
writeBytes ( bytes , 0 ) ;
return bytes ;
}
2002-04-30 19:22:27 -04:00
/** write this Ptg to a byte array*/
2002-01-30 21:22:28 -05:00
public abstract void writeBytes ( byte [ ] array , int offset ) ;
2002-04-18 08:00:53 -04:00
2002-04-30 19:22:27 -04:00
/ * *
* return a string representation of this token alone
* /
2003-08-31 02:16:57 -04:00
public abstract String toFormulaString ( Workbook book ) ;
2002-04-29 21:05:49 -04:00
/ * *
2002-08-21 07:56:49 -04:00
* dump a debug representation ( hexdump ) to a string
2002-04-29 21:05:49 -04:00
* /
2002-04-29 23:05:07 -04:00
public String toDebugString ( ) {
2002-04-29 21:05:49 -04:00
byte [ ] ba = new byte [ getSize ( ) ] ;
String retval = null ;
writeBytes ( ba , 0 ) ;
try {
2002-08-21 07:56:49 -04:00
retval = org . apache . poi . util . HexDump . dump ( ba , 0 , 0 ) ;
2002-04-29 21:05:49 -04:00
} catch ( Exception e ) {
e . printStackTrace ( ) ;
}
2002-04-29 23:05:07 -04:00
return retval ;
2002-04-29 21:05:49 -04:00
}
2003-07-27 14:24:33 -04:00
/ * * Overridden toString method to ensure object hash is not printed .
* This helps get rid of gratuitous diffs when comparing two dumps
* Subclasses may output more relevant information by overriding this method
* * /
public String toString ( ) {
return this . getClass ( ) . toString ( ) ;
}
2002-05-27 17:00:12 -04:00
public static final byte CLASS_REF = 0x00 ;
public static final byte CLASS_VALUE = 0x20 ;
public static final byte CLASS_ARRAY = 0x40 ;
protected byte ptgClass = CLASS_REF ; //base ptg
public void setClass ( byte thePtgClass ) {
ptgClass = thePtgClass ;
}
2002-06-11 16:18:28 -04:00
/** returns the class (REF/VALUE/ARRAY) for this Ptg */
public byte getPtgClass ( ) {
return ptgClass ;
}
2002-05-27 17:00:12 -04:00
public abstract byte getDefaultOperandClass ( ) ;
2002-09-04 20:26:28 -04:00
public abstract Object clone ( ) ;
2002-05-27 17:00:12 -04:00
2002-04-18 08:00:53 -04:00
2002-01-30 21:22:28 -05:00
}