You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UserDefinedFunction.java 3.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /* ====================================================================
  2. Licensed to the Apache Software Foundation (ASF) under one or more
  3. contributor license agreements. See the NOTICE file distributed with
  4. this work for additional information regarding copyright ownership.
  5. The ASF licenses this file to You under the Apache License, Version 2.0
  6. (the "License"); you may not use this file except in compliance with
  7. the License. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. ==================================================================== */
  15. package org.apache.poi.hssf.record.formula.eval;
  16. import org.apache.poi.hssf.record.formula.atp.AnalysisToolPak;
  17. import org.apache.poi.hssf.record.formula.functions.FreeRefFunction;
  18. import org.apache.poi.ss.formula.EvaluationWorkbook;
  19. import org.apache.poi.ss.formula.OperationEvaluationContext;
  20. import org.apache.poi.ss.formula.eval.NotImplementedException;
  21. /**
  22. *
  23. * Common entry point for all user-defined (non-built-in) functions (where
  24. * <tt>AbstractFunctionPtg.field_2_fnc_index</tt> == 255)
  25. *
  26. * @author Josh Micich
  27. */
  28. final class UserDefinedFunction implements FreeRefFunction {
  29. public static final FreeRefFunction instance = new UserDefinedFunction();
  30. private UserDefinedFunction() {
  31. // enforce singleton
  32. }
  33. public ValueEval evaluate(ValueEval[] args, OperationEvaluationContext ec) {
  34. int nIncomingArgs = args.length;
  35. if(nIncomingArgs < 1) {
  36. throw new RuntimeException("function name argument missing");
  37. }
  38. ValueEval nameArg = args[0];
  39. FreeRefFunction targetFunc;
  40. if (nameArg instanceof NameEval) {
  41. targetFunc = findInternalUserDefinedFunction((NameEval) nameArg);
  42. } else if (nameArg instanceof NameXEval) {
  43. targetFunc = findExternalUserDefinedFunction(ec.getWorkbook(), (NameXEval) nameArg);
  44. } else {
  45. throw new RuntimeException("First argument should be a NameEval, but got ("
  46. + nameArg.getClass().getName() + ")");
  47. }
  48. int nOutGoingArgs = nIncomingArgs -1;
  49. ValueEval[] outGoingArgs = new ValueEval[nOutGoingArgs];
  50. System.arraycopy(args, 1, outGoingArgs, 0, nOutGoingArgs);
  51. return targetFunc.evaluate(outGoingArgs, ec);
  52. }
  53. private static FreeRefFunction findExternalUserDefinedFunction(EvaluationWorkbook workbook,
  54. NameXEval n) {
  55. String functionName = workbook.resolveNameXText(n.getPtg());
  56. if(false) {
  57. System.out.println("received call to external user defined function (" + functionName + ")");
  58. }
  59. // currently only looking for functions from the 'Analysis TookPak' e.g. "YEARFRAC" or "ISEVEN"
  60. // not sure how much this logic would need to change to support other or multiple add-ins.
  61. FreeRefFunction result = AnalysisToolPak.findFunction(functionName);
  62. if (result != null) {
  63. return result;
  64. }
  65. throw new NotImplementedException(functionName);
  66. }
  67. private static FreeRefFunction findInternalUserDefinedFunction(NameEval functionNameEval) {
  68. String functionName = functionNameEval.getFunctionName();
  69. if(false) {
  70. System.out.println("received call to internal user defined function (" + functionName + ")");
  71. }
  72. // TODO find the implementation for the user defined function
  73. throw new NotImplementedException(functionName);
  74. }
  75. }