/* ====================================================================
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You 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.
==================================================================== */
package org.apache.poi.hssf.record.formula;
import java.util.List;
import java.util.ArrayList;
import java.util.Stack;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.hssf.record.RecordInputStream;
/**
*
* @author andy
* @author avik
* @author Jason Height (jheight at chariot dot net dot au)
*/
public abstract class Ptg
{
/* convert infix order ptg list to rpn order ptg list
* @return List ptgs in RPN order
* @param infixPtgs List of ptgs in infix order
*/
/* DO NOT REMOVE
*we keep this method in case we wish to change the way we parse
*It needs a getPrecedence in OperationsPtg
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;
}
*/
/**
* Reads size bytes of the input stream, to create an array of Ptgs.
* Extra data (beyond size) may be read if and ArrayPtgs are present.
*/
public static Stack createParsedExpressionTokens(short size, RecordInputStream in )
{
Stack stack = new Stack();
int pos = 0;
List arrayPtgs = null;
while ( pos < size )
{
Ptg ptg = Ptg.createPtg( in );
if (ptg instanceof ArrayPtg) {
if (arrayPtgs == null)
arrayPtgs = new ArrayList(5);
arrayPtgs.add(ptg);
pos += 8;
} else pos += ptg.getSize();
stack.push( ptg );
}
if(pos != size) {
throw new RuntimeException("Ptg array size mismatch");
}
if (arrayPtgs != null) {
for (int i=0;i 0x60) {
retval.setClass(CLASS_ARRAY);
} else if (id > 0x40) {
retval.setClass(CLASS_VALUE);
} else {
retval.setClass(CLASS_REF);
}
return retval;
}
public static int serializePtgStack(Stack expression, byte[] array, int offset) {
int pos = 0;
int size = 0;
if (expression != null)
size = expression.size();
List arrayPtgs = null;
for (int k = 0; k < size; k++) {
Ptg ptg = ( Ptg ) expression.get(k);
ptg.writeBytes(array, pos + offset);
if (ptg instanceof ArrayPtg) {
if (arrayPtgs == null)
arrayPtgs = new ArrayList(5);
arrayPtgs.add(ptg);
pos += 8;
} else pos += ptg.getSize();
}
if (arrayPtgs != null) {
for (int i=0;i