You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ni...@apache.org on 2008/09/07 22:11:34 UTC

svn commit: r692932 [2/4] - in /poi/branches/ooxml: ./ src/documentation/content/xdocs/ src/java/org/apache/poi/ddf/ src/java/org/apache/poi/hssf/model/ src/java/org/apache/poi/hssf/record/ src/java/org/apache/poi/hssf/record/formula/ src/java/org/apac...

Modified: poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/GreaterThanEval.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/GreaterThanEval.java?rev=692932&r1=692931&r2=692932&view=diff
==============================================================================
--- poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/GreaterThanEval.java (original)
+++ poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/GreaterThanEval.java Sun Sep  7 13:11:32 2008
@@ -1,67 +1,34 @@
-/*
-* 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.
-*/
-/*
- * Created on May 8, 2005
- *
- */
-package org.apache.poi.hssf.record.formula.eval;
+/* ====================================================================
+   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.
+==================================================================== */
 
-import org.apache.poi.hssf.record.formula.GreaterThanPtg;
-import org.apache.poi.hssf.record.formula.Ptg;
+package org.apache.poi.hssf.record.formula.eval;
 
 /**
  * @author Amol S. Deshmukh < amolweb at ya hoo dot com >
  *  
  */
-public class GreaterThanEval extends RelationalOperationEval {
-
-    private GreaterThanPtg delegate;
-
-    public GreaterThanEval(Ptg ptg) {
-        this.delegate = (GreaterThanPtg) ptg;
-    }
-
-    public Eval evaluate(Eval[] operands, int srcRow, short srcCol) {
-        ValueEval retval = null;
-        
-        RelationalValues rvs = super.doEvaluate(operands, srcRow, srcCol);
-        retval = rvs.ee;
-        int result = 0;
-        if (retval == null) {
-            result = doComparison(rvs.bs);
-            if (result == 0) {
-                result = doComparison(rvs.ss);
-            }
-            if (result == 0) {
-                result = doComparison(rvs.ds);
-            }
-
-            retval = (result > 0) ? BoolEval.TRUE : BoolEval.FALSE;;
-        }
-
-        return retval;
-    }
-
-    public int getNumberOfOperands() {
-        return delegate.getNumberOfOperands();
-    }
-
-    public int getType() {
-        return delegate.getType();
-    }
+public final class GreaterThanEval extends RelationalOperationEval {
 
+	public static final OperationEval instance = new GreaterThanEval();
+	
+	private GreaterThanEval() {
+	}
+
+	protected boolean convertComparisonResult(int cmpResult) {
+		return cmpResult > 0;
+	}
 }

Modified: poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/LazyAreaEval.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/LazyAreaEval.java?rev=692932&r1=692931&r2=692932&view=diff
==============================================================================
--- poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/LazyAreaEval.java (original)
+++ poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/LazyAreaEval.java Sun Sep  7 13:11:32 2008
@@ -1,19 +1,19 @@
-/*
-* 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.
-*/
+/* ====================================================================
+   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.eval;
 
@@ -24,20 +24,21 @@
 import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.ss.util.CellReference;
 
 /**
- * @author Amol S. Deshmukh < amolweb at ya hoo dot com >
- * 
+ *
+ * @author Josh Micich 
  */
 public final class LazyAreaEval extends AreaEvalBase {
 
 	private final Sheet _sheet;
-	private Workbook _workbook;
+	private FormulaEvaluator _evaluator;
 
-	public LazyAreaEval(AreaI ptg, Sheet sheet, Workbook workbook) {
+	public LazyAreaEval(AreaI ptg, Sheet sheet, FormulaEvaluator evaluator) {
 		super(ptg);
 		_sheet = sheet;
-		_workbook = workbook;
+		_evaluator = evaluator;
 	}
 
 	public ValueEval getRelativeValue(int relativeRowIndex, int relativeColumnIndex) { 
@@ -53,13 +54,27 @@
 		if (cell == null) {
 			return BlankEval.INSTANCE;
 		}
-		return FormulaEvaluator.getEvalForCell(cell, _sheet, _workbook);
+		return _evaluator.getEvalForCell(cell, _sheet);
 	}
 
 	public AreaEval offset(int relFirstRowIx, int relLastRowIx, int relFirstColIx, int relLastColIx) {
 		AreaI area = new OffsetArea(getFirstRow(), getFirstColumn(),
 				relFirstRowIx, relLastRowIx, relFirstColIx, relLastColIx);
 
-		return new LazyAreaEval(area, _sheet, _workbook);
+		return new LazyAreaEval(area, _sheet, _evaluator);
+	}
+	public String toString() {
+		CellReference crA = new CellReference(getFirstRow(), getFirstColumn());
+		CellReference crB = new CellReference(getLastRow(), getLastColumn());
+		StringBuffer sb = new StringBuffer();
+		sb.append(getClass().getName()).append("[");
+		String sheetName = _evaluator.getSheetName(_sheet);
+		sb.append(sheetName);
+		sb.append('!');
+		sb.append(crA.formatAsString());
+		sb.append(':');
+		sb.append(crB.formatAsString());
+		sb.append("]");
+		return sb.toString();
 	}
 }

Modified: poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/LazyRefEval.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/LazyRefEval.java?rev=692932&r1=692931&r2=692932&view=diff
==============================================================================
--- poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/LazyRefEval.java (original)
+++ poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/LazyRefEval.java Sun Sep  7 13:11:32 2008
@@ -1,30 +1,51 @@
+/* ====================================================================
+   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.eval;
 
 import org.apache.poi.hssf.record.formula.AreaI;
-import org.apache.poi.hssf.record.formula.AreaI.OffsetArea;
 import org.apache.poi.hssf.record.formula.Ref3DPtg;
 import org.apache.poi.hssf.record.formula.RefPtg;
+import org.apache.poi.hssf.record.formula.AreaI.OffsetArea;
 import org.apache.poi.ss.usermodel.Cell;
 import org.apache.poi.ss.usermodel.FormulaEvaluator;
 import org.apache.poi.ss.usermodel.Row;
 import org.apache.poi.ss.usermodel.Sheet;
 import org.apache.poi.ss.usermodel.Workbook;
+import org.apache.poi.ss.util.CellReference;
 
+/**
+*
+* @author Josh Micich 
+*/
 public final class LazyRefEval extends RefEvalBase {
-
 	private final Sheet _sheet;
-	private final Workbook _workbook;
+	private final FormulaEvaluator _evaluator;
 
 
-	public LazyRefEval(RefPtg ptg, Sheet sheet, Workbook workbook) {
+	public LazyRefEval(RefPtg ptg, Sheet sheet, FormulaEvaluator evaluator) {
 		super(ptg.getRow(), ptg.getColumn());
 		_sheet = sheet;
-		_workbook = workbook;
+		_evaluator = evaluator;
 	}
-	public LazyRefEval(Ref3DPtg ptg, Sheet sheet, Workbook workbook) {
+	public LazyRefEval(Ref3DPtg ptg, Sheet sheet, FormulaEvaluator evaluator) {
 		super(ptg.getRow(), ptg.getColumn());
 		_sheet = sheet;
-		_workbook = workbook;
+		_evaluator = evaluator;
 	}
 
 	public ValueEval getInnerValueEval() {
@@ -39,7 +60,7 @@
 		if (cell == null) {
 			return BlankEval.INSTANCE;
 		}
-		return FormulaEvaluator.getEvalForCell(cell, _sheet, _workbook);
+		return _evaluator.getEvalForCell(cell, _sheet);
 	}
 	
 	public AreaEval offset(int relFirstRowIx, int relLastRowIx, int relFirstColIx, int relLastColIx) {
@@ -47,6 +68,18 @@
 		AreaI area = new OffsetArea(getRow(), getColumn(),
 				relFirstRowIx, relLastRowIx, relFirstColIx, relLastColIx);
 
-		return new LazyAreaEval(area, _sheet, _workbook);
+		return new LazyAreaEval(area, _sheet, _evaluator);
+	}
+	
+	public String toString() {
+		CellReference cr = new CellReference(getRow(), getColumn());
+		StringBuffer sb = new StringBuffer();
+		sb.append(getClass().getName()).append("[");
+		String sheetName = _evaluator.getSheetName(_sheet);
+		sb.append(sheetName);
+		sb.append('!');
+		sb.append(cr.formatAsString());
+		sb.append("]");
+		return sb.toString();
 	}
 }

Modified: poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/LessEqualEval.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/LessEqualEval.java?rev=692932&r1=692931&r2=692932&view=diff
==============================================================================
--- poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/LessEqualEval.java (original)
+++ poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/LessEqualEval.java Sun Sep  7 13:11:32 2008
@@ -1,67 +1,34 @@
-/*
-* 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.
-*/
-/*
- * Created on May 8, 2005
- *
- */
-package org.apache.poi.hssf.record.formula.eval;
+/* ====================================================================
+   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.
+==================================================================== */
 
-import org.apache.poi.hssf.record.formula.LessEqualPtg;
-import org.apache.poi.hssf.record.formula.Ptg;
+package org.apache.poi.hssf.record.formula.eval;
 
 /**
  * @author Amol S. Deshmukh < amolweb at ya hoo dot com >
  *  
  */
-public class LessEqualEval extends RelationalOperationEval {
-
-    private LessEqualPtg delegate;
-
-    public LessEqualEval(Ptg ptg) {
-        this.delegate = (LessEqualPtg) ptg;
-    }
-
-    public Eval evaluate(Eval[] operands, int srcRow, short srcCol) {
-        ValueEval retval = null;
-        
-        RelationalValues rvs = super.doEvaluate(operands, srcRow, srcCol);
-        retval = rvs.ee;
-        int result = 0;
-        if (retval == null) {
-            result = doComparison(rvs.bs);
-            if (result == 0) {
-                result = doComparison(rvs.ss);
-            }
-            if (result == 0) {
-                result = doComparison(rvs.ds);
-            }
-
-            retval = (result <= 0) ? BoolEval.TRUE : BoolEval.FALSE;;
-        }
-
-        return retval;
-    }
-
-    public int getNumberOfOperands() {
-        return delegate.getNumberOfOperands();
-    }
-
-    public int getType() {
-        return delegate.getType();
-    }
+public final class LessEqualEval extends RelationalOperationEval {
 
+	public static final OperationEval instance = new LessEqualEval();
+	
+	private LessEqualEval() {
+	}
+
+	protected boolean convertComparisonResult(int cmpResult) {
+		return cmpResult <= 0;
+	}
 }

Modified: poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/LessThanEval.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/LessThanEval.java?rev=692932&r1=692931&r2=692932&view=diff
==============================================================================
--- poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/LessThanEval.java (original)
+++ poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/LessThanEval.java Sun Sep  7 13:11:32 2008
@@ -1,68 +1,34 @@
-/*
-* 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.
-*/
-/*
- * Created on May 8, 2005
- *
- */
-package org.apache.poi.hssf.record.formula.eval;
+/* ====================================================================
+   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.
+==================================================================== */
 
-import org.apache.poi.hssf.record.formula.LessThanPtg;
-import org.apache.poi.hssf.record.formula.Ptg;
+package org.apache.poi.hssf.record.formula.eval;
 
 /**
  * @author Amol S. Deshmukh &lt; amolweb at ya hoo dot com &gt;
  *  
  */
-public class LessThanEval extends RelationalOperationEval {
-
-    private LessThanPtg delegate;
-
-    public LessThanEval(Ptg ptg) {
-        this.delegate = (LessThanPtg) ptg;
-    }
-
-
-    public Eval evaluate(Eval[] operands, int srcRow, short srcCol) {
-        ValueEval retval = null;
-        
-        RelationalValues rvs = super.doEvaluate(operands, srcRow, srcCol);
-        retval = rvs.ee;
-        int result = 0;
-        if (retval == null) {
-            result = doComparison(rvs.bs);
-            if (result == 0) {
-                result = doComparison(rvs.ss);
-            }
-            if (result == 0) {
-                result = doComparison(rvs.ds);
-            }
-
-            retval = (result < 0) ? BoolEval.TRUE : BoolEval.FALSE;;
-        }
-
-        return retval;
-    }
-
-    public int getNumberOfOperands() {
-        return delegate.getNumberOfOperands();
-    }
-
-    public int getType() {
-        return delegate.getType();
-    }
+public final class LessThanEval extends RelationalOperationEval {
 
+	public static final OperationEval instance = new LessThanEval();
+	
+	private LessThanEval() {
+	}
+
+	protected boolean convertComparisonResult(int cmpResult) {
+		return cmpResult < 0;
+	}
 }

Modified: poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/MultiplyEval.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/MultiplyEval.java?rev=692932&r1=692931&r2=692932&view=diff
==============================================================================
--- poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/MultiplyEval.java (original)
+++ poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/MultiplyEval.java Sun Sep  7 13:11:32 2008
@@ -1,89 +1,33 @@
-/*
-* 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.
-*/
+/* ====================================================================
+   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.eval;
 
-import org.apache.poi.hssf.record.formula.Ptg;
-import org.apache.poi.hssf.record.formula.MultiplyPtg;
-
 /**
  * @author Amol S. Deshmukh &lt; amolweb at ya hoo dot com &gt;
- *  
  */
-public final class MultiplyEval extends NumericOperationEval {
+public final class MultiplyEval extends TwoOperandNumericOperation {
+
+	public static final OperationEval instance = new MultiplyEval();
 
-    private MultiplyPtg delegate;
+	private MultiplyEval() {
+	}
 
-    private static final ValueEvalToNumericXlator NUM_XLATOR = 
-        new ValueEvalToNumericXlator((short)
-                ( ValueEvalToNumericXlator.BOOL_IS_PARSED 
-                | ValueEvalToNumericXlator.REF_BOOL_IS_PARSED
-                | ValueEvalToNumericXlator.STRING_IS_PARSED
-                | ValueEvalToNumericXlator.REF_STRING_IS_PARSED
-                ));
-
-    public MultiplyEval(Ptg ptg) {
-        delegate = (MultiplyPtg) ptg;
-    }
-    
-    protected ValueEvalToNumericXlator getXlator() {
-        return NUM_XLATOR;
-    }
-
-    public Eval evaluate(Eval[] args, int srcRow, short srcCol) {
-    	if(args.length != 2) {
-    		return ErrorEval.VALUE_INVALID;
-    	}
-    	
-        double d0 = 0;
-        double d1 = 0;
-        ValueEval ve = singleOperandEvaluate(args[0], srcRow, srcCol);
-        if (ve instanceof NumericValueEval) {
-            d0 = ((NumericValueEval) ve).getNumberValue();
-        }
-        else if (ve instanceof BlankEval) {
-            // do nothing
-        }
-        else {
-            return ErrorEval.VALUE_INVALID;
-        }
-        
-        ve = singleOperandEvaluate(args[1], srcRow, srcCol);
-        if (ve instanceof NumericValueEval) {
-            d1 = ((NumericValueEval) ve).getNumberValue();
-        }
-        else if (ve instanceof BlankEval) {
-        // do nothing
-        }
-        else {
-        	return ErrorEval.VALUE_INVALID;
-        }
- 
-        if (Double.isNaN(d0) || Double.isNaN(d1)) {
-			return ErrorEval.NUM_ERROR;
-		}
-		return new NumberEval(d0 * d1);
-    }
-
-    public int getNumberOfOperands() {
-        return delegate.getNumberOfOperands();
-    }
-
-    public int getType() {
-        return delegate.getType();
-    }
+	protected double evaluate(double d0, double d1) {
+		return d0 * d1;
+	}
 }

Modified: poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/NameEval.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/NameEval.java?rev=692932&r1=692931&r2=692932&view=diff
==============================================================================
--- poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/NameEval.java (original)
+++ poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/NameEval.java Sun Sep  7 13:11:32 2008
@@ -22,26 +22,24 @@
  */
 public final class NameEval implements Eval {
 
-	private final int _index;
+	private final String _functionName;
 
 	/**
-	 * @param index zero based index to a defined name record
+	 * Creates a NameEval representing a function name
 	 */
-	public NameEval(int index) {
-		_index = index;
+	public NameEval(String functionName) {
+		_functionName = functionName;
 	}
 
-	/**
-	 * @return zero based index to a defined name record
-	 */
-	public int getIndex() {
-		return _index;
+
+	public String getFunctionName() {
+		return _functionName;
 	}
 
 	public String toString() {
 		StringBuffer sb = new StringBuffer(64);
 		sb.append(getClass().getName()).append(" [");
-		sb.append(_index);
+		sb.append(_functionName);
 		sb.append("]");
 		return sb.toString();
 	}

Modified: poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/NotEqualEval.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/NotEqualEval.java?rev=692932&r1=692931&r2=692932&view=diff
==============================================================================
--- poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/NotEqualEval.java (original)
+++ poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/NotEqualEval.java Sun Sep  7 13:11:32 2008
@@ -1,68 +1,34 @@
-/*
-* 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.
-*/
-/*
- * Created on May 8, 2005
- *
- */
-package org.apache.poi.hssf.record.formula.eval;
+/* ====================================================================
+   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.
+==================================================================== */
 
-import org.apache.poi.hssf.record.formula.NotEqualPtg;
-import org.apache.poi.hssf.record.formula.Ptg;
+package org.apache.poi.hssf.record.formula.eval;
 
 /**
  * @author Amol S. Deshmukh &lt; amolweb at ya hoo dot com &gt;
  *  
  */
-public class NotEqualEval extends RelationalOperationEval {
-
-    private NotEqualPtg delegate;
-
-    public NotEqualEval(Ptg ptg) {
-        this.delegate = (NotEqualPtg) ptg;
-    }
-
-
-    public Eval evaluate(Eval[] operands, int srcRow, short srcCol) {
-        ValueEval retval = null;
-        
-        RelationalValues rvs = super.doEvaluate(operands, srcRow, srcCol);
-        retval = rvs.ee;
-        int result = 0;
-        if (retval == null) {
-            result = doComparison(rvs.bs);
-            if (result == 0) {
-                result = doComparison(rvs.ss);
-            }
-            if (result == 0) {
-                result = doComparison(rvs.ds);
-            }
-
-            retval = (result != 0) ? BoolEval.TRUE : BoolEval.FALSE;
-        }
-
-        return retval;
-    }
-
-    public int getNumberOfOperands() {
-        return delegate.getNumberOfOperands();
-    }
-
-    public int getType() {
-        return delegate.getType();
-    }
+public final class NotEqualEval extends RelationalOperationEval {
 
+	public static final OperationEval instance = new NotEqualEval();
+	
+	private NotEqualEval() {
+	}
+
+	protected boolean convertComparisonResult(int cmpResult) {
+		return cmpResult != 0;
+	}
 }

Modified: poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/PercentEval.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/PercentEval.java?rev=692932&r1=692931&r2=692932&view=diff
==============================================================================
--- poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/PercentEval.java (original)
+++ poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/PercentEval.java Sun Sep  7 13:11:32 2008
@@ -17,55 +17,40 @@
 
 package org.apache.poi.hssf.record.formula.eval;
 
-import org.apache.poi.hssf.record.formula.PercentPtg;
-import org.apache.poi.hssf.record.formula.Ptg;
 
 /**
  * Implementation of Excel formula token '%'. <p/>
  * @author Josh Micich
  */
-public final class PercentEval extends NumericOperationEval {
+public final class PercentEval implements OperationEval {
 
-	private PercentPtg _delegate;
+	public static final OperationEval instance = new PercentEval();
 
-	private static final ValueEvalToNumericXlator NUM_XLATOR = new ValueEvalToNumericXlator(
-			(short) (ValueEvalToNumericXlator.BOOL_IS_PARSED
-					| ValueEvalToNumericXlator.REF_BOOL_IS_PARSED
-					| ValueEvalToNumericXlator.STRING_IS_PARSED | ValueEvalToNumericXlator.REF_STRING_IS_PARSED));
-
-	public PercentEval(Ptg ptg) {
-		_delegate = (PercentPtg) ptg;
-	}
-
-	protected ValueEvalToNumericXlator getXlator() {
-		return NUM_XLATOR;
+	private PercentEval() {
 	}
 
 	public Eval evaluate(Eval[] args, int srcRow, short srcCol) {
 		if (args.length != 1) {
 			return ErrorEval.VALUE_INVALID;
 		}
-
-		ValueEval ve = singleOperandEvaluate(args[0], srcRow, srcCol);
-		if (ve instanceof NumericValueEval) {
-			double d0 = ((NumericValueEval) ve).getNumberValue();
-			return new NumberEval(d0 / 100);
+    	double d0;
+		try {
+			ValueEval ve = OperandResolver.getSingleValue(args[0], srcRow, srcCol);
+			if (ve instanceof BlankEval) {
+				return NumberEval.ZERO;
+			}
+			d0 = OperandResolver.coerceValueToDouble(ve);
+		} catch (EvaluationException e) {
+			return e.getErrorEval();
 		}
-
-		if (ve instanceof BlankEval) {
-			return NumberEval.ZERO;
-		}
-		if (ve instanceof ErrorEval) {
-			return ve;
-		}
-		return ErrorEval.VALUE_INVALID;
+		return new NumberEval(d0 / 100);
 	}
 
 	public int getNumberOfOperands() {
-		return _delegate.getNumberOfOperands();
-	}
-
-	public int getType() {
-		return _delegate.getType();
+		return 1;
 	}
+	public final int getType() {
+    	// TODO - remove
+        throw new RuntimeException("obsolete code should not be called");
+    }
 }

Modified: poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/PowerEval.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/PowerEval.java?rev=692932&r1=692931&r2=692932&view=diff
==============================================================================
--- poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/PowerEval.java (original)
+++ poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/PowerEval.java Sun Sep  7 13:11:32 2008
@@ -1,90 +1,33 @@
-/*
-* 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.
-*/
+/* ====================================================================
+   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.eval;
 
-import org.apache.poi.hssf.record.formula.Ptg;
-import org.apache.poi.hssf.record.formula.PowerPtg;
-
 /**
  * @author Amol S. Deshmukh &lt; amolweb at ya hoo dot com &gt;
- *  
  */
-public final class PowerEval extends NumericOperationEval {
+public final class PowerEval extends TwoOperandNumericOperation {
+
+	public static final OperationEval instance = new PowerEval();
 
-    private PowerPtg delegate;
+	private PowerEval() {
+	}
 
-    private static final ValueEvalToNumericXlator NUM_XLATOR = 
-        new ValueEvalToNumericXlator((short)
-                ( ValueEvalToNumericXlator.BOOL_IS_PARSED 
-                | ValueEvalToNumericXlator.REF_BOOL_IS_PARSED
-                | ValueEvalToNumericXlator.STRING_IS_PARSED
-                | ValueEvalToNumericXlator.REF_STRING_IS_PARSED
-                ));
-
-    public PowerEval(Ptg ptg) {
-        delegate = (PowerPtg) ptg;
-    }
-    
-    protected ValueEvalToNumericXlator getXlator() {
-        return NUM_XLATOR;
-    }
-
-    public Eval evaluate(Eval[] args, int srcRow, short srcCol) {
-    	if(args.length != 2) {
-    		return ErrorEval.VALUE_INVALID;
-    	}
-        double d0 = 0;
-        double d1 = 0;
-        
-        ValueEval ve = singleOperandEvaluate(args[0], srcRow, srcCol);
-        if (ve instanceof NumericValueEval) {
-            d0 = ((NumericValueEval) ve).getNumberValue();
-        }
-        else if (ve instanceof BlankEval) {
-            // do nothing
-        }
-        else {
-            return ErrorEval.VALUE_INVALID;
-        }
-        
-        ve = singleOperandEvaluate(args[1], srcRow, srcCol);
-        if (ve instanceof NumericValueEval) {
-            d1 = ((NumericValueEval) ve).getNumberValue();
-        }
-        else if (ve instanceof BlankEval) {
-        // do nothing
-        }
-        else {
-            return ErrorEval.VALUE_INVALID;
-        }
-
-        double p = Math.pow(d0, d1);
-        if (Double.isNaN(p)) {
-			return ErrorEval.VALUE_INVALID;
-		}
-		return new NumberEval(p);
-    }
-
-    public int getNumberOfOperands() {
-        return delegate.getNumberOfOperands();
-    }
-
-    public int getType() {
-        return delegate.getType();
-    }
+	protected double evaluate(double d0, double d1) {
+		return Math.pow(d0, d1);
+	}
 }

Modified: poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/RelationalOperationEval.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/RelationalOperationEval.java?rev=692932&r1=692931&r2=692932&view=diff
==============================================================================
--- poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/RelationalOperationEval.java (original)
+++ poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/RelationalOperationEval.java Sun Sep  7 13:11:32 2008
@@ -1,216 +1,145 @@
-/*
-* 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.
-*/
-/*
- * Created on May 10, 2005
- *
- */
+/* ====================================================================
+   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.eval;
 
 /**
+ * Base class for all comparison operator evaluators
+ * 
  * @author Amol S. Deshmukh &lt; amolweb at ya hoo dot com &gt;
- *
  */
 public abstract class RelationalOperationEval implements OperationEval {
 
-    protected class RelationalValues {
-        public Double[] ds = new Double[2];
-        public Boolean[] bs = new Boolean[2];
-        public String[] ss = new String[3];
-        public ErrorEval ee = null;
-    }
-
-    
-    /*
-     * This is a description of how the relational operators apply in MS Excel.
-     * Use this as a guideline when testing/implementing the evaluate methods 
-     * for the relational operators Evals.
-     * 
-     * Bool > any number. ALWAYS
-     * Bool > any string. ALWAYS
-     * Bool.TRUE > Bool.FALSE
-     * 
-     * String > any number. ALWAYS
-     * String > Blank. ALWAYS
-     * String are sorted dictionary wise
-     * 
-     * Blank == 0 (numeric)
-     */
-    public RelationalValues doEvaluate(Eval[] operands, int srcRow, short srcCol) {
-        RelationalValues retval = new RelationalValues();
-        
-        switch (operands.length) {
-        default:
-            retval.ee = ErrorEval.VALUE_INVALID;
-            break;
-        case 2:
-            internalDoEvaluate(operands, srcRow, srcCol, retval, 0);
-            internalDoEvaluate(operands, srcRow, srcCol, retval, 1);
-        } // end switch
-        return retval;
-    }
-    
-    /**
-     * convenience method to avoid code duplication for multiple operands
-     * @param operands
-     * @param srcRow
-     * @param srcCol
-     * @param retval
-     * @param index
-     */
-    private void internalDoEvaluate(Eval[] operands, int srcRow, short srcCol, RelationalValues retval, int index) {
-        if (operands[index] instanceof BoolEval) {
-            BoolEval be = (BoolEval) operands[index];
-            retval.bs[index] = Boolean.valueOf(be.getBooleanValue());
-        }
-        else if (operands[index] instanceof NumericValueEval) {
-            NumericValueEval ne = (NumericValueEval) operands[index];
-            retval.ds[index] = new Double(ne.getNumberValue());
-        }
-        else if (operands[index] instanceof StringValueEval) {
-            StringValueEval se = (StringValueEval) operands[index];
-            retval.ss[index] = se.getStringValue();
-        }
-        else if (operands[index] instanceof RefEval) {
-            RefEval re = (RefEval) operands[index];
-            ValueEval ve = re.getInnerValueEval();
-            if (ve instanceof BoolEval) {
-                BoolEval be = (BoolEval) ve;
-                retval.bs[index] = Boolean.valueOf(be.getBooleanValue());
-            }
-            else if (ve instanceof BlankEval) {
-                retval.ds[index] = new Double(0);
-            }
-            else if (ve instanceof NumericValueEval) {
-                NumericValueEval ne = (NumericValueEval) ve;
-                retval.ds[index] = new Double(ne.getNumberValue());
-            }
-            else if (ve instanceof StringValueEval) {
-                StringValueEval se = (StringValueEval) ve;
-                retval.ss[index] = se.getStringValue();
-            }
-        }
-        else if (operands[index] instanceof AreaEval) {
-            AreaEval ae = (AreaEval) operands[index];
-            if (ae.isRow()) {
-                if (ae.containsColumn(srcCol)) {
-                    ValueEval ve = ae.getValueAt(ae.getFirstRow(), srcCol);
-                    if (ve instanceof BoolEval) {
-                        BoolEval be = (BoolEval) ve;
-                        retval.bs[index] = Boolean.valueOf(be.getBooleanValue());
-                    }
-                    else if (ve instanceof BlankEval) {
-                        retval.ds[index] = new Double(0);
-                    }
-                    else if (ve instanceof NumericValueEval) {
-                        NumericValueEval ne = (NumericValueEval) ve;
-                        retval.ds[index] = new Double(ne.getNumberValue());
-                    }
-                    else if (ve instanceof StringValueEval) {
-                        StringValueEval se = (StringValueEval) ve;
-                        retval.ss[index] = se.getStringValue();
-                    }
-                    else {
-                        retval.ee = ErrorEval.VALUE_INVALID;
-                    }
-                }
-                else {
-                    retval.ee = ErrorEval.VALUE_INVALID;
-                }
-            }
-            else if (ae.isColumn()) {
-                if (ae.containsRow(srcRow)) {
-                    ValueEval ve = ae.getValueAt(srcRow, ae.getFirstColumn());
-                    if (ve instanceof BoolEval) {
-                        BoolEval be = (BoolEval) ve;
-                        retval.bs[index] = Boolean.valueOf(be.getBooleanValue());
-                    }
-                    else if (ve instanceof BlankEval) {
-                        retval.ds[index] = new Double(0);
-                    }
-                    else if (ve instanceof NumericValueEval) {
-                        NumericValueEval ne = (NumericValueEval) ve;
-                        retval.ds[index] = new Double(ne.getNumberValue());
-                    }
-                    else if (ve instanceof StringValueEval) {
-                        StringValueEval se = (StringValueEval) ve;
-                        retval.ss[index] = se.getStringValue();
-                    }
-                    else {
-                        retval.ee = ErrorEval.VALUE_INVALID;
-                    }
-                }
-                else {
-                    retval.ee = ErrorEval.VALUE_INVALID;
-                }
-            }
-            else {
-                retval.ee = ErrorEval.VALUE_INVALID;
-            }
-        }
-    }
-    
-    // if both null return 0, else non null wins, else TRUE wins
-    protected int doComparison(Boolean[] bs) {
-        int retval = 0;
-        if (bs[0] != null || bs[1] != null) {
-            retval = bs[0] != null
-                    ? bs[1] != null
-                            ? bs[0].booleanValue()
-                                    ? bs[1].booleanValue()
-                                            ? 0
-                                            : 1
-                                    : bs[1].booleanValue()
-                                            ? -1
-                                            : 0
-                            : 1
-                    : bs[1] != null
-                            ? -1
-                            : 0;
-        }
-        return retval;
-    }
-
-    // if both null return 0, else non null wins, else string compare
-    protected int doComparison(String[] ss) {
-        int retval = 0;
-        if (ss[0] != null || ss[1] != null) {
-            retval = ss[0] != null
-                    ? ss[1] != null
-                            ? ss[0].compareTo(ss[1])
-                            : 1
-                    : ss[1] != null
-                            ? -1
-                            : 0;
-        }
-        return retval;
-    }
-
-    // if both null return 0, else non null wins, else doublevalue compare
-    protected int doComparison(Double[] ds) {
-        int retval = 0;
-        if (ds[0] != null || ds[1] != null) {
-            retval = ds[0] != null
-                    ? ds[1] != null
-                            ? ds[0].compareTo(ds[1])
-                            : 1
-                    : ds[1] != null
-                            ? -1
-                            : 0;
-        }
-        return retval;
-    }
+	/**
+	 * Converts a standard compare result (-1, 0, 1) to <code>true</code> or <code>false</code>
+	 * according to subclass' comparison type.
+	 */
+	protected abstract boolean convertComparisonResult(int cmpResult);
+
+	/**
+	 * This is a description of how the relational operators apply in MS Excel.
+	 * Use this as a guideline when testing/implementing the evaluate methods
+	 * for the relational operators Evals.
+	 *
+	 * <pre>
+	 * Bool.TRUE > any number.
+	 * Bool > any string. ALWAYS
+	 * Bool.TRUE > Bool.FALSE
+	 * Bool.FALSE == Blank
+	 *
+	 * Strings are never converted to numbers or booleans
+	 * String > any number. ALWAYS
+	 * Non-empty String > Blank
+	 * Empty String == Blank
+	 * String are sorted dictionary wise
+	 *
+	 * Blank > Negative numbers
+	 * Blank == 0
+	 * Blank < Positive numbers
+	 * </pre>
+	 */
+	public final Eval evaluate(Eval[] operands, int srcRow, short srcCol) {
+		if (operands.length != 2) {
+			return ErrorEval.VALUE_INVALID;
+		}
+
+		ValueEval vA;
+		ValueEval vB;
+		try {
+			vA = OperandResolver.getSingleValue(operands[0], srcRow, srcCol);
+			vB = OperandResolver.getSingleValue(operands[1], srcRow, srcCol);
+		} catch (EvaluationException e) {
+			return e.getErrorEval();
+		}
+		int cmpResult = doCompare(vA, vB);
+		boolean result = convertComparisonResult(cmpResult);
+		return BoolEval.valueOf(result);
+	}
+
+	private static int doCompare(ValueEval va, ValueEval vb) {
+		// special cases when one operand is blank
+		if (va == BlankEval.INSTANCE) {
+			return compareBlank(vb);
+		}
+		if (vb == BlankEval.INSTANCE) {
+			return -compareBlank(va);
+		}
+
+		if (va instanceof BoolEval) {
+			if (vb instanceof BoolEval) {
+				BoolEval bA = (BoolEval) va;
+				BoolEval bB = (BoolEval) vb;
+				if (bA.getBooleanValue() == bB.getBooleanValue()) {
+					return 0;
+				}
+				return bA.getBooleanValue() ? 1 : -1;
+			}
+			return 1;
+		}
+		if (vb instanceof BoolEval) {
+			return -1;
+		}
+		if (va instanceof StringEval) {
+			if (vb instanceof StringEval) {
+				StringEval sA = (StringEval) va;
+				StringEval sB = (StringEval) vb;
+				return sA.getStringValue().compareTo(sB.getStringValue());
+			}
+			return 1;
+		}
+		if (vb instanceof StringEval) {
+			return -1;
+		}
+		if (va instanceof NumberEval) {
+			if (vb instanceof NumberEval) {
+				NumberEval nA = (NumberEval) va;
+				NumberEval nB = (NumberEval) vb;
+				return Double.compare(nA.getNumberValue(), nB.getNumberValue());
+			}
+		}
+		throw new IllegalArgumentException("Bad operand types (" + va.getClass().getName() + "), ("
+				+ vb.getClass().getName() + ")");
+	}
+
+	private static int compareBlank(ValueEval v) {
+		if (v == BlankEval.INSTANCE) {
+			return 0;
+		}
+		if (v instanceof BoolEval) {
+			BoolEval boolEval = (BoolEval) v;
+			return boolEval.getBooleanValue() ? -1 : 0;
+		}
+		if (v instanceof NumberEval) {
+			NumberEval ne = (NumberEval) v;
+			return Double.compare(0, ne.getNumberValue());
+		}
+		if (v instanceof StringEval) {
+			StringEval se = (StringEval) v;
+			return se.getStringValue().length() < 1 ? 0 : -1;
+		}
+		throw new IllegalArgumentException("bad value class (" + v.getClass().getName() + ")");
+	}
+
+	public final int getNumberOfOperands() {
+		return 2;
+	}
+
+	public final int getType() {
+		// TODO - get rid of this method
+		throw new RuntimeException("Obsolete code - should not be called");
+	}
 }

Modified: poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/SubtractEval.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/SubtractEval.java?rev=692932&r1=692931&r2=692932&view=diff
==============================================================================
--- poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/SubtractEval.java (original)
+++ poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/SubtractEval.java Sun Sep  7 13:11:32 2008
@@ -1,93 +1,33 @@
-/*
-* 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.
-*/
+/* ====================================================================
+   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.eval;
 
-import org.apache.poi.hssf.record.formula.Ptg;
-import org.apache.poi.hssf.record.formula.SubtractPtg;
-
 /**
  * @author Amol S. Deshmukh &lt; amolweb at ya hoo dot com &gt;
- *  
  */
-public final class SubtractEval extends NumericOperationEval {
+public final class SubtractEval extends TwoOperandNumericOperation {
+
+	public static final OperationEval instance = new SubtractEval();
 
-    private SubtractPtg delegate;
+	private SubtractEval() {
+	}
 
-    private static final ValueEvalToNumericXlator NUM_XLATOR = 
-        new ValueEvalToNumericXlator((short)
-                ( ValueEvalToNumericXlator.BOOL_IS_PARSED 
-                | ValueEvalToNumericXlator.REF_BOOL_IS_PARSED
-                | ValueEvalToNumericXlator.STRING_IS_PARSED
-                | ValueEvalToNumericXlator.REF_STRING_IS_PARSED
-                ));
-
-    public SubtractEval(Ptg ptg) {
-        delegate = (SubtractPtg) ptg;
-    }
-    
-    protected ValueEvalToNumericXlator getXlator() {
-        return NUM_XLATOR;
-    }
-
-    public Eval evaluate(Eval[] args, int srcRow, short srcCol) {
-    	if(args.length != 2) {
-    		return ErrorEval.VALUE_INVALID;
-    	}
-        Eval retval = null;
-        double d0 = 0;
-        double d1 = 0;
-        ValueEval ve = singleOperandEvaluate(args[0], srcRow, srcCol);
-        if (ve instanceof NumericValueEval) {
-            d0 = ((NumericValueEval) ve).getNumberValue();
-        }
-        else if (ve instanceof BlankEval) {
-            // do nothing
-        }
-        else {
-            retval = ErrorEval.VALUE_INVALID;
-        }
-        
-        if (retval == null) { // no error yet
-            ve = singleOperandEvaluate(args[1], srcRow, srcCol);
-            if (ve instanceof NumericValueEval) {
-                d1 = ((NumericValueEval) ve).getNumberValue();
-            }
-            else if (ve instanceof BlankEval) {
-                // do nothing
-            }
-            else {
-                retval = ErrorEval.VALUE_INVALID;
-            }
-        }
- 
-        if (retval == null) {
-            retval = (Double.isNaN(d0) || Double.isNaN(d1)) 
-                    ? (ValueEval) ErrorEval.VALUE_INVALID 
-                    : new NumberEval(d0 - d1);
-        }
-        return retval;
-    }
-
-    public int getNumberOfOperands() {
-        return delegate.getNumberOfOperands();
-    }
-
-    public int getType() {
-        return delegate.getType();
-    }
+	protected double evaluate(double d0, double d1) {
+		return d0 - d1;
+	}
 }

Modified: poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/UnaryMinusEval.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/UnaryMinusEval.java?rev=692932&r1=692931&r2=692932&view=diff
==============================================================================
--- poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/UnaryMinusEval.java (original)
+++ poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/UnaryMinusEval.java Sun Sep  7 13:11:32 2008
@@ -1,75 +1,56 @@
-/*
-* 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.
-*/
+/* ====================================================================
+   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.eval;
 
-import org.apache.poi.hssf.record.formula.Ptg;
-import org.apache.poi.hssf.record.formula.UnaryMinusPtg;
 
 /**
  * @author Amol S. Deshmukh &lt; amolweb at ya hoo dot com &gt;
  *  
  */
-public final class UnaryMinusEval extends NumericOperationEval {
+public final class UnaryMinusEval implements OperationEval {
 
-    private UnaryMinusPtg delegate;
-    private static final ValueEvalToNumericXlator NUM_XLATOR = 
-        new ValueEvalToNumericXlator((short)
-                ( ValueEvalToNumericXlator.BOOL_IS_PARSED 
-                | ValueEvalToNumericXlator.REF_BOOL_IS_PARSED
-                | ValueEvalToNumericXlator.STRING_IS_PARSED
-                | ValueEvalToNumericXlator.REF_STRING_IS_PARSED
-                ));
+	public static final OperationEval instance = new UnaryMinusEval();
 
+	private UnaryMinusEval() {
+	}
 
-    public UnaryMinusEval(Ptg ptg) {
-        this.delegate = (UnaryMinusPtg) ptg;
+	public Eval evaluate(Eval[] args, int srcRow, short srcCol) {
+		if (args.length != 1) {
+			return ErrorEval.VALUE_INVALID;
+		}
+    	double d;
+		try {
+			ValueEval ve = OperandResolver.getSingleValue(args[0], srcRow, srcCol);
+			if (ve instanceof BlankEval) {
+				return NumberEval.ZERO;
+			}
+			d = OperandResolver.coerceValueToDouble(ve);
+		} catch (EvaluationException e) {
+			return e.getErrorEval();
+		}
+		return new NumberEval(-d);
+	}
+
+	public int getNumberOfOperands() {
+		return 1;
+	}
+	public final int getType() {
+    	// TODO - remove
+        throw new RuntimeException("obsolete code should not be called");
     }
-    
-    protected ValueEvalToNumericXlator getXlator() {
-        return NUM_XLATOR;
-    }
-
-    public Eval evaluate(Eval[] args, int srcRow, short srcCol) {
-    	if(args.length != 1) {
-    		return ErrorEval.VALUE_INVALID;
-    	}
-        double d = 0;
-        
-        ValueEval ve = singleOperandEvaluate(args[0], srcRow, srcCol);
-        if (ve instanceof NumericValueEval) {
-            d = ((NumericValueEval) ve).getNumberValue();
-        }
-        else if (ve instanceof BlankEval) {
-            // do nothing
-        }
-        else if (ve instanceof ErrorEval) {
-            return ve;
-        }
-        
-        return new NumberEval(-d);
-    }
-
-    public int getNumberOfOperands() {
-        return delegate.getNumberOfOperands();
-    }
-
-    public int getType() {
-        return delegate.getType();
-    }
-
 }

Modified: poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/UnaryPlusEval.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/UnaryPlusEval.java?rev=692932&r1=692931&r2=692932&view=diff
==============================================================================
--- poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/UnaryPlusEval.java (original)
+++ poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/UnaryPlusEval.java Sun Sep  7 13:11:32 2008
@@ -17,8 +17,6 @@
 
 package org.apache.poi.hssf.record.formula.eval;
 
-import org.apache.poi.hssf.record.formula.Ptg;
-import org.apache.poi.hssf.record.formula.UnaryPlusPtg;
 
 /**
  * @author Amol S. Deshmukh &lt; amolweb at ya hoo dot com &gt;
@@ -26,13 +24,9 @@
  */
 public final class UnaryPlusEval implements OperationEval {
 
-    private UnaryPlusPtg delegate;
+    public static final OperationEval instance = new UnaryPlusEval();
     
-    /**
-     * called by reflection
-     */
-    public UnaryPlusEval(Ptg ptg) {
-        this.delegate = (UnaryPlusPtg) ptg;
+    private UnaryPlusEval() {
     }
 
     public Eval evaluate(Eval[] args, int srcCellRow, short srcCellCol) {
@@ -59,10 +53,10 @@
     }
 
     public int getNumberOfOperands() {
-        return delegate.getNumberOfOperands();
+        return 1;
     }
 
     public int getType() {
-        return delegate.getType();
+        throw new RuntimeException("obsolete code should not be called");
     }
 }

Modified: poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/ValueEvalToNumericXlator.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/ValueEvalToNumericXlator.java?rev=692932&r1=692931&r2=692932&view=diff
==============================================================================
--- poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/ValueEvalToNumericXlator.java (original)
+++ poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/eval/ValueEvalToNumericXlator.java Sun Sep  7 13:11:32 2008
@@ -55,49 +55,40 @@
      * @param eval
      */
     public ValueEval attemptXlateToNumeric(ValueEval eval) {
-        ValueEval retval = null;
         
         if (eval == null) {
-            retval = BlankEval.INSTANCE;
+            throw new IllegalArgumentException("eval must not be null");
         }
         
         // most common case - least worries :)
-        else if (eval instanceof NumberEval) {
-            retval = eval; 
+        if (eval instanceof NumberEval) {
+            return eval; 
         }
         
-        // booleval
-        else if (eval instanceof BoolEval) {
-            retval = ((flags & BOOL_IS_PARSED) > 0)
+        if (eval instanceof BoolEval) {
+            return ((flags & BOOL_IS_PARSED) > 0)
                 ? (NumericValueEval) eval
                 : xlateBlankEval(BLANK_IS_PARSED);
         } 
         
-        // stringeval 
-        else if (eval instanceof StringEval) {
-            retval = xlateStringEval((StringEval) eval); // TODO: recursive call needed
+        if (eval instanceof StringEval) {
+            return xlateStringEval((StringEval) eval); // TODO: recursive call needed
         }
         
-        // refeval
-        else if (eval instanceof RefEval) {
-            retval = xlateRefEval((RefEval) eval);
+        if (eval instanceof RefEval) {
+            return xlateRefEval((RefEval) eval);
         }
         
-        // erroreval
-        else if (eval instanceof ErrorEval) {
-            retval = eval;
+        if (eval instanceof ErrorEval) {
+            return eval;
         }
         
-        else if (eval instanceof BlankEval) {
-            retval = xlateBlankEval(BLANK_IS_PARSED);
+        if (eval instanceof BlankEval) {
+            return xlateBlankEval(BLANK_IS_PARSED);
         }
         
         // probably AreaEval? then not acceptable.
-        else {
-            throw new RuntimeException("Invalid ValueEval type passed for conversion: " + eval.getClass());
-        }
-        
-        return retval;
+        throw new RuntimeException("Invalid ValueEval type passed for conversion: " + eval.getClass());
     }
     
     /**

Modified: poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/functions/Hlookup.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/functions/Hlookup.java?rev=692932&r1=692931&r2=692932&view=diff
==============================================================================
--- poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/functions/Hlookup.java (original)
+++ poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/functions/Hlookup.java Sun Sep  7 13:11:32 2008
@@ -60,8 +60,7 @@
 			AreaEval tableArray = LookupUtils.resolveTableArrayArg(args[1]);
 			boolean isRangeLookup = LookupUtils.resolveRangeLookupArg(arg3, srcCellRow, srcCellCol);
 			int colIndex = LookupUtils.lookupIndexOfValue(lookupValue, LookupUtils.createRowVector(tableArray, 0), isRangeLookup);
-			ValueEval veColIndex = OperandResolver.getSingleValue(args[2], srcCellRow, srcCellCol);
-			int rowIndex = LookupUtils.resolveRowOrColIndexArg(veColIndex);
+			int rowIndex = LookupUtils.resolveRowOrColIndexArg(args[2], srcCellRow, srcCellCol);
 			ValueVector resultCol = createResultColumnVector(tableArray, rowIndex);
 			return resultCol.getItem(colIndex);
 		} catch (EvaluationException e) {
@@ -73,15 +72,14 @@
 	/**
 	 * Returns one column from an <tt>AreaEval</tt>
 	 * 
-	 * @throws EvaluationException (#VALUE!) if colIndex is negative, (#REF!) if colIndex is too high
+	 * @param rowIndex assumed to be non-negative
+	 * 
+	 * @throws EvaluationException (#REF!) if colIndex is too high
 	 */
-	private ValueVector createResultColumnVector(AreaEval tableArray, int colIndex) throws EvaluationException {
-		if(colIndex < 0) {
-			throw EvaluationException.invalidValue();
-		}
-		if(colIndex >= tableArray.getWidth()) {
+	private ValueVector createResultColumnVector(AreaEval tableArray, int rowIndex) throws EvaluationException {
+		if(rowIndex >= tableArray.getHeight()) {
 			throw EvaluationException.invalidRef();
 		}
-		return LookupUtils.createRowVector(tableArray, colIndex);
+		return LookupUtils.createRowVector(tableArray, rowIndex);
 	}
 }

Modified: poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/functions/LookupUtils.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/functions/LookupUtils.java?rev=692932&r1=692931&r2=692932&view=diff
==============================================================================
--- poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/functions/LookupUtils.java (original)
+++ poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/functions/LookupUtils.java Sun Sep  7 13:11:32 2008
@@ -322,30 +322,45 @@
 	 *      <tr><td>&lt;blank&gt;</td><td>&nbsp;</td><td>#VALUE!</td></tr>
 	 *    </table><br/>
 	 *
-	 *  * Note - out of range errors (both too high and too low) are handled by the caller.
-	 * @return column or row index as a zero-based value
-	 *
+	 * Note - out of range errors (result index too high) are handled by the caller.
+	 * @return column or row index as a zero-based value, never negative.
+	 * @throws EvaluationException when the specified arg cannot be coerced to a non-negative integer
 	 */
-	public static int resolveRowOrColIndexArg(ValueEval veRowColIndexArg) throws EvaluationException {
-		if(veRowColIndexArg == null) {
+	public static int resolveRowOrColIndexArg(Eval rowColIndexArg, int srcCellRow, int srcCellCol) throws EvaluationException {
+		if(rowColIndexArg == null) {
 			throw new IllegalArgumentException("argument must not be null");
 		}
-		if(veRowColIndexArg instanceof BlankEval) {
-			throw EvaluationException.invalidValue();
+		
+		ValueEval veRowColIndexArg;
+		try {
+			veRowColIndexArg = OperandResolver.getSingleValue(rowColIndexArg, srcCellRow, (short)srcCellCol);
+		} catch (EvaluationException e) {
+			// All errors get translated to #REF!
+			throw EvaluationException.invalidRef();
 		}
-		if(veRowColIndexArg instanceof StringEval) {
-			StringEval se = (StringEval) veRowColIndexArg;
-			String strVal = se.getStringValue();
-			Double dVal = OperandResolver.parseDouble(strVal);
-			if(dVal == null) {
-				// String does not resolve to a number. Raise #VALUE! error.
-				throw EvaluationException.invalidRef();
-				// This includes text booleans "TRUE" and "FALSE".  They are not valid.
+		int oneBasedIndex;
+		if(veRowColIndexArg instanceof BlankEval) {
+			oneBasedIndex = 0;
+		} else {
+			if(veRowColIndexArg instanceof StringEval) {
+				StringEval se = (StringEval) veRowColIndexArg;
+				String strVal = se.getStringValue();
+				Double dVal = OperandResolver.parseDouble(strVal);
+				if(dVal == null) {
+					// String does not resolve to a number. Raise #REF! error.
+					throw EvaluationException.invalidRef();
+					// This includes text booleans "TRUE" and "FALSE".  They are not valid.
+				}
+				// else - numeric value parses OK
 			}
-			// else - numeric value parses OK
+			// actual BoolEval values get interpreted as FALSE->0 and TRUE->1
+			oneBasedIndex = OperandResolver.coerceValueToInt(veRowColIndexArg);
+		}
+		if (oneBasedIndex < 1) {
+			// note this is asymmetric with the errors when the index is too large (#REF!)  
+			throw EvaluationException.invalidValue();
 		}
-		// actual BoolEval values get interpreted as FALSE->0 and TRUE->1
-		return OperandResolver.coerceValueToInt(veRowColIndexArg) - 1;
+		return oneBasedIndex - 1; // convert to zero based
 	}
 
 
@@ -583,11 +598,13 @@
 		return maxIx - 1;
 	}
 
-	public static LookupValueComparer createLookupComparer(ValueEval lookupValue) throws EvaluationException {
+	public static LookupValueComparer createLookupComparer(ValueEval lookupValue) {
 
-		if (lookupValue instanceof BlankEval) {
-			// blank eval can never be found in a lookup array
-			throw new EvaluationException(ErrorEval.NA);
+		if (lookupValue == BlankEval.INSTANCE) {
+			// blank eval translates to zero
+			// Note - a blank eval in the lookup column/row never matches anything
+			// empty string in the lookup column/row can only be matched by explicit emtpty string
+			return new NumberLookupComparer(NumberEval.ZERO);
 		}
 		if (lookupValue instanceof StringEval) {
 			return new StringLookupComparer((StringEval) lookupValue);

Modified: poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/functions/Vlookup.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/functions/Vlookup.java?rev=692932&r1=692931&r2=692932&view=diff
==============================================================================
--- poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/functions/Vlookup.java (original)
+++ poi/branches/ooxml/src/java/org/apache/poi/hssf/record/formula/functions/Vlookup.java Sun Sep  7 13:11:32 2008
@@ -60,8 +60,7 @@
 			AreaEval tableArray = LookupUtils.resolveTableArrayArg(args[1]);
 			boolean isRangeLookup = LookupUtils.resolveRangeLookupArg(arg3, srcCellRow, srcCellCol);
 			int rowIndex = LookupUtils.lookupIndexOfValue(lookupValue, LookupUtils.createColumnVector(tableArray, 0), isRangeLookup);
-			ValueEval veColIndex = OperandResolver.getSingleValue(args[2], srcCellRow, srcCellCol);
-			int colIndex = LookupUtils.resolveRowOrColIndexArg(veColIndex);
+			int colIndex = LookupUtils.resolveRowOrColIndexArg(args[2], srcCellRow, srcCellCol);
 			ValueVector resultCol = createResultColumnVector(tableArray, colIndex);
 			return resultCol.getItem(rowIndex);
 		} catch (EvaluationException e) {
@@ -73,12 +72,11 @@
 	/**
 	 * Returns one column from an <tt>AreaEval</tt>
 	 * 
-	 * @throws EvaluationException (#VALUE!) if colIndex is negative, (#REF!) if colIndex is too high
+	 * @param colIndex assumed to be non-negative
+	 * 
+	 * @throws EvaluationException (#REF!) if colIndex is too high
 	 */
 	private ValueVector createResultColumnVector(AreaEval tableArray, int colIndex) throws EvaluationException {
-		if(colIndex < 0) {
-			throw EvaluationException.invalidValue();
-		}
 		if(colIndex >= tableArray.getWidth()) {
 			throw EvaluationException.invalidRef();
 		}

Modified: poi/branches/ooxml/src/java/org/apache/poi/hssf/usermodel/HSSFName.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/java/org/apache/poi/hssf/usermodel/HSSFName.java?rev=692932&r1=692931&r2=692932&view=diff
==============================================================================
--- poi/branches/ooxml/src/java/org/apache/poi/hssf/usermodel/HSSFName.java (original)
+++ poi/branches/ooxml/src/java/org/apache/poi/hssf/usermodel/HSSFName.java Sun Sep  7 13:11:32 2008
@@ -23,15 +23,15 @@
 import org.apache.poi.ss.usermodel.Name;
 
 /**
- * High Level Representation of a 'defined name' which could be a 'built-in' name, 
+ * High Level Representation of a 'defined name' which could be a 'built-in' name,
  * 'named range' or name of a user defined function.
- * 
+ *
  * @author Libin Roman (Vista Portal LDT. Developer)
  */
 public class HSSFName implements Name {
     private HSSFWorkbook _book;
     private NameRecord _definedNameRec;
-    
+
     /** Creates new HSSFName   - called by HSSFWorkbook to create a sheet from
      * scratch.
      *
@@ -43,58 +43,57 @@
         _book = book;
         _definedNameRec = name;
     }
-    
+
     /** Get the sheets name which this named range is referenced to
      * @return sheet name, which this named range referred to
-     */    
+     */
     public String getSheetName() {
         short indexToExternSheet = _definedNameRec.getExternSheetNumber();
-        
+
         return _book.getWorkbook().findSheetNameFromExternSheet(indexToExternSheet);
     }
-    
-    /** 
+
+    /**
      * @return text name of this defined name
-     */    
+     */
     public String getNameName(){
         return _definedNameRec.getNameText();
     }
-    
-    /** 
+
+    /**
      * sets the name of the named range
      * @param nameName named range name to set
-     */    
+     */
     public void setNameName(String nameName){
         _definedNameRec.setNameText(nameName);
-        _definedNameRec.setNameTextLength((byte)nameName.length());
         Workbook wb = _book.getWorkbook();
-        
+
         //Check to ensure no other names have the same case-insensitive name
         for ( int i = wb.getNumNames()-1; i >=0; i-- )
         {
-        	NameRecord rec = wb.getNameRecord(i);
-        	if (rec != _definedNameRec) {
-        		if (rec.getNameText().equalsIgnoreCase(getNameName()))
-        			throw new IllegalArgumentException("The workbook already contains this name (case-insensitive)");
-        	}
+            NameRecord rec = wb.getNameRecord(i);
+            if (rec != _definedNameRec) {
+                if (rec.getNameText().equalsIgnoreCase(getNameName()))
+                    throw new IllegalArgumentException("The workbook already contains this name (case-insensitive)");
+            }
         }
     }
 
-    /** 
+    /**
      * Note - this method only applies to named ranges
      * @return the formula text defining the named range
-     */    
+     */
     public String getReference() {
-    	if (_definedNameRec.isFunctionName()) {
-    		throw new IllegalStateException("Only applicable to named ranges");
-    	}
+        if (_definedNameRec.isFunctionName()) {
+            throw new IllegalStateException("Only applicable to named ranges");
+        }
         return _definedNameRec.getAreaReference(_book);
     }
 
-    /** 
+    /**
      * sets the sheet name which this named range referenced to
      * @param sheetName the sheet name of the reference
-     */    
+     */
     private void setSheetName(String sheetName){
         int sheetNumber = _book.getSheetIndex(sheetName);
         short externSheetNumber = (short)
@@ -102,11 +101,11 @@
         _definedNameRec.setExternSheetNumber(externSheetNumber);
     }
 
-  
-    /** 
+
+    /**
      * sets the reference of this named range
      * @param ref the reference to set
-     */    
+     */
     public void setReference(String ref){
 
         RangeAddress ra = new RangeAddress(ref);
@@ -117,7 +116,7 @@
             setSheetName(sheetName);
         }
 
-		//allow the poi utilities to parse it out
+        //allow the poi utilities to parse it out
         _definedNameRec.setAreaReference(ref);
     }
 
@@ -131,7 +130,7 @@
         return "#REF!".endsWith(ref);
     }
     public boolean isFunctionName() {
-    	return _definedNameRec.isFunctionName();
+        return _definedNameRec.isFunctionName();
     }
     public String toString() {
         StringBuffer sb = new StringBuffer(64);

Modified: poi/branches/ooxml/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java
URL: http://svn.apache.org/viewvc/poi/branches/ooxml/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java?rev=692932&r1=692931&r2=692932&view=diff
==============================================================================
--- poi/branches/ooxml/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java (original)
+++ poi/branches/ooxml/src/java/org/apache/poi/hssf/usermodel/HSSFWorkbook.java Sun Sep  7 13:11:32 2008
@@ -27,7 +27,6 @@
 import java.util.Hashtable;
 import java.util.Iterator;
 import java.util.List;
-import java.util.Stack;
 
 import org.apache.poi.POIDocument;
 import org.apache.poi.ddf.EscherBSERecord;
@@ -36,6 +35,7 @@
 import org.apache.poi.ddf.EscherRecord;
 import org.apache.poi.hssf.model.Sheet;
 import org.apache.poi.hssf.model.Workbook;
+import org.apache.poi.hssf.model.DrawingManager2;
 import org.apache.poi.hssf.record.AbstractEscherHolderRecord;
 import org.apache.poi.hssf.record.BackupRecord;
 import org.apache.poi.hssf.record.DrawingGroupRecord;
@@ -55,6 +55,8 @@
 import org.apache.poi.hssf.record.formula.Area3DPtg;
 import org.apache.poi.hssf.record.formula.MemFuncPtg;
 import org.apache.poi.hssf.record.formula.NameXPtg;
+import org.apache.poi.hssf.record.formula.Ptg;
+import org.apache.poi.hssf.record.formula.Ref3DPtg;
 import org.apache.poi.hssf.record.formula.UnionPtg;
 import org.apache.poi.hssf.util.CellReference;
 import org.apache.poi.hssf.util.SheetReferences;
@@ -667,33 +669,81 @@
      * @return HSSFSheet representing the cloned sheet.
      */
 
-    public HSSFSheet cloneSheet(int sheetNum) {
-        validateSheetIndex(sheetNum);
-        HSSFSheet srcSheet = (HSSFSheet) _sheets.get(sheetNum);
-        String srcName = workbook.getSheetName(sheetNum);
+    public HSSFSheet cloneSheet(int sheetIndex) {
+        validateSheetIndex(sheetIndex);
+        HSSFSheet srcSheet = (HSSFSheet) _sheets.get(sheetIndex);
+        String srcName = workbook.getSheetName(sheetIndex);
         HSSFSheet clonedSheet = srcSheet.cloneSheet(this);
         clonedSheet.setSelected(false);
         clonedSheet.setActive(false);
 
+        String name = getUniqueSheetName(srcName);
+        int newSheetIndex = _sheets.size(); 
         _sheets.add(clonedSheet);
-        int i = 1;
+        workbook.setSheetName(newSheetIndex, name);
+        
+        // Check this sheet has an autofilter, (which has a built-in NameRecord at workbook level)
+        int filterDbNameIndex = findExistingBuiltinNameRecordIdx(sheetIndex, NameRecord.BUILTIN_FILTER_DB);
+        if (filterDbNameIndex >=0) {
+            NameRecord origNameRecord = workbook.getNameRecord(filterDbNameIndex);
+            // copy original formula but adjust 3D refs to the new external sheet index
+            int newExtSheetIx = getExternalSheetIndex(newSheetIndex);
+            Ptg[] ptgs = origNameRecord.getNameDefinition();
+            for (int i=0; i< ptgs.length; i++) {
+                Ptg ptg = ptgs[i];
+                ptg = ptg.copy();
+                
+                if (ptg instanceof Area3DPtg) {
+                    Area3DPtg a3p = (Area3DPtg) ptg;
+                    a3p.setExternSheetIndex(newExtSheetIx);
+                } else if (ptg instanceof Ref3DPtg) {
+                    Ref3DPtg r3p = (Ref3DPtg) ptg;
+                    r3p.setExternSheetIndex(newExtSheetIx);
+                }
+                ptgs[i] = ptg;
+            }
+            NameRecord newNameRecord = workbook.createBuiltInName(NameRecord.BUILTIN_FILTER_DB, newSheetIndex+1);
+            newNameRecord.setNameDefinition(ptgs);
+            newNameRecord.setHidden(true);
+            HSSFName newName = new HSSFName(this, newNameRecord);
+            names.add(newName);
+
+            workbook.cloneDrawings(clonedSheet.getSheet());
+        }
+        // TODO - maybe same logic required for other/all built-in name records
+        
+        return clonedSheet;
+    }
+
+    private String getUniqueSheetName(String srcName) {
+        int uniqueIndex = 2;
+        String baseName = srcName;
+        int bracketPos = srcName.lastIndexOf('(');
+        if (bracketPos > 0 && srcName.endsWith(")")) {
+            String suffix = srcName.substring(bracketPos + 1, srcName.length() - ")".length());
+            try {
+                uniqueIndex = Integer.parseInt(suffix.trim());
+                uniqueIndex++;
+                baseName=srcName.substring(0, bracketPos).trim();
+            } catch (NumberFormatException e) {
+                // contents of brackets not numeric
+            }
+        }
         while (true) {
             // Try and find the next sheet name that is unique
-            String name = srcName;
-            String index = Integer.toString(i++);
-            if (name.length() + index.length() + 2 < 31) {
-                name = name + "(" + index + ")";
+            String index = Integer.toString(uniqueIndex++);
+            String name;
+            if (baseName.length() + index.length() + 2 < 31) {
+                name = baseName + " (" + index + ")";
             } else {
-                name = name.substring(0, 31 - index.length() - 2) + "(" + index + ")";
+                name = baseName.substring(0, 31 - index.length() - 2) + "(" + index + ")";
             }
 
             //If the sheet name is unique, then set it otherwise move on to the next number.
             if (workbook.getSheetIndex(name) == -1) {
-              workbook.setSheetName(_sheets.size()-1, name);
-              break;
+              return name;
             }
         }
-        return clonedSheet;
     }
 
     /**
@@ -907,7 +957,7 @@
         boolean removingRange =
                 startColumn == -1 && endColumn == -1 && startRow == -1 && endRow == -1;
 
-        int rowColHeaderNameIndex = findExistingRowColHeaderNameRecordIdx(sheetIndex);
+        int rowColHeaderNameIndex = findExistingBuiltinNameRecordIdx(sheetIndex, NameRecord.BUILTIN_PRINT_TITLE);
         if (removingRange) {
             if (rowColHeaderNameIndex >= 0) {
                 workbook.removeName(rowColHeaderNameIndex);
@@ -925,29 +975,27 @@
             isNewRecord = false;
         }
 
-        short definitionTextLength = settingRowAndColumn ? (short)0x001a : (short)0x000b;
-        nameRecord.setDefinitionTextLength(definitionTextLength); // TODO - remove
-
-        Stack ptgs = new Stack();
+        List temp = new ArrayList();
 
         if (settingRowAndColumn) {
             final int exprsSize = 2 * 11 + 1; // 2 * Area3DPtg.SIZE + UnionPtg.SIZE
-            ptgs.add(new MemFuncPtg(exprsSize));
+            temp.add(new MemFuncPtg(exprsSize));
         }
         if (startColumn >= 0) {
             Area3DPtg colArea = new Area3DPtg(0, MAX_ROW, startColumn, endColumn, 
                     false, false, false, false, externSheetIndex);
-            ptgs.add(colArea);
+            temp.add(colArea);
         }
         if (startRow >= 0) {
             Area3DPtg rowArea = new Area3DPtg(startRow, endRow, 0, MAX_COLUMN, 
                     false, false, false, false, externSheetIndex);
-            ptgs.add(rowArea);
+            temp.add(rowArea);
         }
-        if (settingRowAndColumn)
-        {
-            ptgs.add(UnionPtg.instance);
+        if (settingRowAndColumn) {
+            temp.add(UnionPtg.instance);
         }
+        Ptg[] ptgs = new Ptg[temp.size()];
+        temp.toArray(ptgs);
         nameRecord.setNameDefinition(ptgs);
 
         if (isNewRecord)
@@ -963,13 +1011,13 @@
     }
 
 
-    private int findExistingRowColHeaderNameRecordIdx(int sheetIndex) {
+    private int findExistingBuiltinNameRecordIdx(int sheetIndex, byte builtinCode) {
         for(int defNameIndex =0; defNameIndex<names.size(); defNameIndex++) {
             NameRecord r = workbook.getNameRecord(defNameIndex);
             if (r == null) {
                 throw new RuntimeException("Unable to find all defined names to iterate over");
             }
-            if (!isRowColHeaderRecord( r )) {
+            if (!r.isBuiltInName() || r.getBuiltInName() != builtinCode) {
                 continue;
             }
             if(r.getSheetNumber() == 0) {
@@ -985,10 +1033,6 @@
         return -1;
     }
 
-    private static boolean isRowColHeaderRecord(NameRecord r) {
-        return r.isBuiltInName() && r.getBuiltInName() == NameRecord.BUILTIN_PRINT_TITLE;
-    }
-
     /**
      * create a new Font and add it to the workbook's font table
      * @return new font object
@@ -1288,6 +1332,9 @@
 
         return result;
     }
+	public NameRecord getNameRecord(int nameIndex) {
+		return getWorkbook().getNameRecord(nameIndex);
+	}
 
     /** gets the named range name
      * @param index the named range index (0 based)



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org