Add XSSF support to ForkedEvaluator

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1747326 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2016-06-07 23:15:40 +00:00
parent d42f93cdef
commit 4cd56aefdb
4 changed files with 61 additions and 25 deletions

View File

@ -23,6 +23,9 @@ import org.apache.poi.ss.formula.eval.NumberEval;
import org.apache.poi.ss.formula.eval.StringEval; import org.apache.poi.ss.formula.eval.StringEval;
import org.apache.poi.ss.formula.eval.ValueEval; import org.apache.poi.ss.formula.eval.ValueEval;
import org.apache.poi.ss.formula.udf.UDFFinder; import org.apache.poi.ss.formula.udf.UDFFinder;
import java.lang.reflect.Method;
import org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook; import org.apache.poi.hssf.usermodel.HSSFEvaluationWorkbook;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.CollaboratingWorkbooksEnvironment; import org.apache.poi.ss.formula.CollaboratingWorkbooksEnvironment;
@ -40,8 +43,6 @@ import org.apache.poi.ss.usermodel.Workbook;
* This class enables a 'master workbook' to be loaded just once and shared between many evaluation * This class enables a 'master workbook' to be loaded just once and shared between many evaluation
* clients. Each evaluation client creates its own {@link ForkedEvaluator} and can set cell values * clients. Each evaluation client creates its own {@link ForkedEvaluator} and can set cell values
* that will be used for local evaluations (and don't disturb evaluations on other evaluators). * that will be used for local evaluations (and don't disturb evaluations on other evaluators).
*
* @author Josh Micich
*/ */
public final class ForkedEvaluator { public final class ForkedEvaluator {
@ -55,19 +56,19 @@ public final class ForkedEvaluator {
private static EvaluationWorkbook createEvaluationWorkbook(Workbook wb) { private static EvaluationWorkbook createEvaluationWorkbook(Workbook wb) {
if (wb instanceof HSSFWorkbook) { if (wb instanceof HSSFWorkbook) {
return HSSFEvaluationWorkbook.create((HSSFWorkbook) wb); return HSSFEvaluationWorkbook.create((HSSFWorkbook) wb);
} else {
try {
// TODO: check if this is Java 9 compatible ...
Class<?> evalWB = Class.forName("org.apache.poi.xssf.usermodel.XSSFEvaluationWorkbook");
Class<?> xssfWB = Class.forName("org.apache.poi.xssf.usermodel.XSSFWorkbook");
Method createM = evalWB.getDeclaredMethod("create", xssfWB);
return (EvaluationWorkbook)createM.invoke(null, wb);
} catch (Exception e) {
throw new IllegalArgumentException("Unexpected workbook type (" + wb.getClass().getName() + ") - check for poi-ooxml and poi-ooxml schemas jar in the classpath", e);
}
} }
// TODO rearrange POI build to allow this
// if (wb instanceof XSSFWorkbook) {
// return XSSFEvaluationWorkbook.create((XSSFWorkbook) wb);
// }
throw new IllegalArgumentException("Unexpected workbook type (" + wb.getClass().getName() + ")");
}
/**
* @deprecated (Sep 2009) (reduce overloading) use {@link #create(Workbook, IStabilityClassifier, UDFFinder)}
*/
public static ForkedEvaluator create(Workbook wb, IStabilityClassifier stabilityClassifier) {
return create(wb, stabilityClassifier, null);
} }
/** /**
* @param udfFinder pass <code>null</code> for default (AnalysisToolPak only) * @param udfFinder pass <code>null</code> for default (AnalysisToolPak only)
*/ */

View File

@ -17,6 +17,7 @@
package org.apache.poi.xssf.usermodel; package org.apache.poi.xssf.usermodel;
import org.apache.poi.ss.formula.eval.forked.TestForkedEvaluator;
import org.apache.poi.xssf.usermodel.extensions.TestXSSFBorder; import org.apache.poi.xssf.usermodel.extensions.TestXSSFBorder;
import org.apache.poi.xssf.usermodel.extensions.TestXSSFCellFill; import org.apache.poi.xssf.usermodel.extensions.TestXSSFCellFill;
import org.apache.poi.xssf.usermodel.extensions.TestXSSFSheetComments; import org.apache.poi.xssf.usermodel.extensions.TestXSSFSheetComments;
@ -58,7 +59,8 @@ import org.junit.runners.Suite;
TestXSSFSheetComments.class, TestXSSFSheetComments.class,
TestColumnHelper.class, TestColumnHelper.class,
TestHeaderFooterHelper.class, TestHeaderFooterHelper.class,
TestXSSFPivotTable.class TestXSSFPivotTable.class,
TestForkedEvaluator.class
}) })
public final class AllXSSFUsermodelTests { public final class AllXSSFUsermodelTests {
} }

View File

@ -0,0 +1,28 @@
/* ====================================================================
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.xssf.usermodel;
import org.apache.poi.ss.formula.eval.forked.TestForkedEvaluator;
import org.apache.poi.ss.usermodel.Workbook;
public class TestXSSFForkedEvaluator extends TestForkedEvaluator {
protected Workbook newWorkbook() {
return new XSSFWorkbook();
}
}

View File

@ -21,29 +21,34 @@ import static org.junit.Assert.assertEquals;
import java.io.IOException; import java.io.IOException;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook; import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.formula.IStabilityClassifier; import org.apache.poi.ss.formula.IStabilityClassifier;
import org.apache.poi.ss.formula.eval.NumberEval; import org.apache.poi.ss.formula.eval.NumberEval;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
public final class TestForkedEvaluator { public class TestForkedEvaluator {
@Rule @Rule
public ExpectedException expectedEx = ExpectedException.none(); public ExpectedException expectedEx = ExpectedException.none();
protected Workbook newWorkbook() {
return new HSSFWorkbook();
}
/** /**
* set up a calculation workbook with input cells nicely segregated on a * set up a calculation workbook with input cells nicely segregated on a
* sheet called "Inputs" * sheet called "Inputs"
*/ */
private static HSSFWorkbook createWorkbook() { protected Workbook createWorkbook() {
HSSFWorkbook wb = new HSSFWorkbook(); Workbook wb = newWorkbook();
HSSFSheet sheet1 = wb.createSheet("Inputs"); Sheet sheet1 = wb.createSheet("Inputs");
HSSFSheet sheet2 = wb.createSheet("Calculations"); Sheet sheet2 = wb.createSheet("Calculations");
HSSFRow row; Row row;
row = sheet2.createRow(0); row = sheet2.createRow(0);
row.createCell(0).setCellFormula("B1*Inputs!A1-Inputs!B1"); row.createCell(0).setCellFormula("B1*Inputs!A1-Inputs!B1");
row.createCell(1).setCellValue(5.0); // Calculations!B1 row.createCell(1).setCellValue(5.0); // Calculations!B1
@ -60,7 +65,7 @@ public final class TestForkedEvaluator {
*/ */
@Test @Test
public void testBasic() throws IOException { public void testBasic() throws IOException {
HSSFWorkbook wb = createWorkbook(); Workbook wb = createWorkbook();
// The stability classifier is useful to reduce memory consumption of caching logic // The stability classifier is useful to reduce memory consumption of caching logic
IStabilityClassifier stabilityClassifier = new IStabilityClassifier() { IStabilityClassifier stabilityClassifier = new IStabilityClassifier() {
@ -99,11 +104,11 @@ public final class TestForkedEvaluator {
* <i>read-only</i> with respect to the ForkedEvaluator. * <i>read-only</i> with respect to the ForkedEvaluator.
*/ */
@Test @Test
public void testMissingInputCell() throws IOException { public void testMissingInputCellH() throws IOException {
expectedEx.expect(UnsupportedOperationException.class); expectedEx.expect(UnsupportedOperationException.class);
expectedEx.expectMessage("Underlying cell 'A2' is missing in master sheet."); expectedEx.expectMessage("Underlying cell 'A2' is missing in master sheet.");
HSSFWorkbook wb = createWorkbook(); Workbook wb = createWorkbook();
try { try {
ForkedEvaluator fe = ForkedEvaluator.create(wb, null, null); ForkedEvaluator fe = ForkedEvaluator.create(wb, null, null);