When throwing an exception during formula evaluation, if this is due to an unimplemented function, have a more specific exception type

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1607588 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2014-07-03 11:20:32 +00:00
parent 7bc0767bf5
commit dd6819aa13
6 changed files with 91 additions and 24 deletions

View File

@ -19,9 +19,9 @@ package org.apache.poi.ss.formula;
import org.apache.poi.ss.formula.eval.NameEval;
import org.apache.poi.ss.formula.eval.NameXEval;
import org.apache.poi.ss.formula.eval.NotImplementedFunctionException;
import org.apache.poi.ss.formula.eval.ValueEval;
import org.apache.poi.ss.formula.functions.FreeRefFunction;
import org.apache.poi.ss.formula.eval.NotImplementedException;
/**
*
* Common entry point for all user-defined (non-built-in) functions (where
@ -56,7 +56,7 @@ final class UserDefinedFunction implements FreeRefFunction {
}
FreeRefFunction targetFunc = ec.findUserDefinedFunction(functionName);
if (targetFunc == null) {
throw new NotImplementedException(functionName);
throw new NotImplementedFunctionException(functionName);
}
int nOutGoingArgs = nIncomingArgs -1;
ValueEval[] outGoingArgs = new ValueEval[nOutGoingArgs];

View File

@ -10,16 +10,35 @@
package org.apache.poi.ss.formula.atp;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.TreeSet;
import org.apache.poi.ss.formula.OperationEvaluationContext;
import org.apache.poi.ss.formula.eval.NotImplementedException;
import org.apache.poi.ss.formula.eval.NotImplementedFunctionException;
import org.apache.poi.ss.formula.eval.ValueEval;
import org.apache.poi.ss.formula.function.FunctionMetadata;
import org.apache.poi.ss.formula.function.FunctionMetadataRegistry;
import org.apache.poi.ss.formula.functions.*;
import org.apache.poi.ss.formula.functions.Bin2Dec;
import org.apache.poi.ss.formula.functions.Complex;
import org.apache.poi.ss.formula.functions.Countifs;
import org.apache.poi.ss.formula.functions.Dec2Bin;
import org.apache.poi.ss.formula.functions.Dec2Hex;
import org.apache.poi.ss.formula.functions.Delta;
import org.apache.poi.ss.formula.functions.EDate;
import org.apache.poi.ss.formula.functions.FactDouble;
import org.apache.poi.ss.formula.functions.FreeRefFunction;
import org.apache.poi.ss.formula.functions.Hex2Dec;
import org.apache.poi.ss.formula.functions.ImReal;
import org.apache.poi.ss.formula.functions.Imaginary;
import org.apache.poi.ss.formula.functions.Oct2Dec;
import org.apache.poi.ss.formula.functions.Quotient;
import org.apache.poi.ss.formula.functions.Sumifs;
import org.apache.poi.ss.formula.functions.WeekNum;
import org.apache.poi.ss.formula.udf.UDFFinder;
import java.util.*;
/**
* Analysis Toolpack Function Definitions
*/
@ -35,7 +54,7 @@ public final class AnalysisToolPak implements UDFFinder {
}
public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
throw new NotImplementedException(_functionName);
throw new NotImplementedFunctionException(_functionName);
}
}

View File

@ -20,12 +20,16 @@ package org.apache.poi.ss.formula.eval;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
/**
* An exception thrown by implementors of {@link FormulaEvaluator} when attempting to evaluate
* a formula which requires features that POI does not (yet) support.
* An exception thrown by implementors of {@link FormulaEvaluator},
* when attempting to evaluate a formula which requires features
* that POI does not (yet) support.
*
* @author Josh Micich
* <p>Where possible, a subclass of this should be thrown, to provide
* more detail of what part of the formula couldn't be processed due
* to a missing implementation
*/
public final class NotImplementedException extends RuntimeException {
public class NotImplementedException extends RuntimeException {
private static final long serialVersionUID = -5840703336495141301L;
public NotImplementedException(String message) {
super(message);

View File

@ -0,0 +1,44 @@
/* ====================================================================
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.ss.formula.eval;
import org.apache.poi.ss.usermodel.FormulaEvaluator;
/**
* An exception thrown by implementors of {@link FormulaEvaluator} when
* attempting to evaluate a formula which requires a function that POI
* does not (yet) support.
*/
public final class NotImplementedFunctionException extends NotImplementedException {
private static final long serialVersionUID = 1208119411557559057L;
private String functionName;
public NotImplementedFunctionException(String functionName) {
super(functionName);
this.functionName = functionName;
}
public NotImplementedFunctionException(String functionName, NotImplementedException cause) {
super(functionName, cause);
this.functionName = functionName;
}
public String getFunctionName() {
return functionName;
}
}

View File

@ -17,15 +17,13 @@
package org.apache.poi.ss.formula.functions;
import org.apache.poi.ss.formula.eval.NotImplementedFunctionException;
import org.apache.poi.ss.formula.eval.ValueEval;
import org.apache.poi.ss.formula.eval.NotImplementedException;
/**
*
* @author Amol S. Deshmukh &lt; amolweb at ya hoo dot com &gt;
* This is the default implementation of a Function class.
* The default behaviour is to raise a POI internal error
* ({@link NotImplementedException}). This error should alert
* ({@link NotImplementedFunctionException}). This error should alert
* the user that the formula contained a function that is not
* yet implemented.
*/
@ -39,7 +37,7 @@ public final class NotImplementedFunction implements Function {
}
public ValueEval evaluate(ValueEval[] operands, int srcRow, int srcCol) {
throw new NotImplementedException(_functionName);
throw new NotImplementedFunctionException(_functionName);
}
public String getFunctionName() {
return _functionName;

View File

@ -17,12 +17,14 @@
package org.apache.poi.ss.formula.functions;
import static org.apache.poi.ss.formula.functions.AggregateFunction.subtotalInstance;
import org.apache.poi.ss.formula.eval.ErrorEval;
import org.apache.poi.ss.formula.eval.EvaluationException;
import org.apache.poi.ss.formula.eval.NotImplementedException;
import org.apache.poi.ss.formula.eval.NotImplementedFunctionException;
import org.apache.poi.ss.formula.eval.OperandResolver;
import org.apache.poi.ss.formula.eval.ValueEval;
import org.apache.poi.ss.formula.eval.NotImplementedException;
import static org.apache.poi.ss.formula.functions.AggregateFunction.subtotalInstance;
/**
* Implementation for the Excel function SUBTOTAL<p>
@ -68,10 +70,10 @@ public class Subtotal implements Function {
case 5: return subtotalInstance(AggregateFunction.MIN);
case 6: return subtotalInstance(AggregateFunction.PRODUCT);
case 7: return subtotalInstance(AggregateFunction.STDEV);
case 8: throw new NotImplementedException("STDEVP");
case 8: throw new NotImplementedFunctionException("STDEVP");
case 9: return subtotalInstance(AggregateFunction.SUM);
case 10: throw new NotImplementedException("VAR");
case 11: throw new NotImplementedException("VARP");
case 10: throw new NotImplementedFunctionException("VAR");
case 11: throw new NotImplementedFunctionException("VARP");
}
if (functionCode > 100 && functionCode < 112) {
throw new NotImplementedException("SUBTOTAL - with 'exclude hidden values' option");