View | Details | Raw Unified | Return to bug 52575
Collapse All | Expand All

(-)src/java/org/apache/poi/ss/formula/WorkbookEvaluator.java (-5 / +46 lines)
Lines 67-72 Link Here
67
import org.apache.poi.ss.formula.CollaboratingWorkbooksEnvironment.WorkbookNotFoundException;
67
import org.apache.poi.ss.formula.CollaboratingWorkbooksEnvironment.WorkbookNotFoundException;
68
import org.apache.poi.ss.formula.eval.NotImplementedException;
68
import org.apache.poi.ss.formula.eval.NotImplementedException;
69
import org.apache.poi.ss.usermodel.Cell;
69
import org.apache.poi.ss.usermodel.Cell;
70
import org.apache.poi.util.POILogFactory;
71
import org.apache.poi.util.POILogger;
70
72
71
/**
73
/**
72
 * Evaluates formula cells.<p/>
74
 * Evaluates formula cells.<p/>
Lines 80-86 Link Here
80
 * @author Josh Micich
82
 * @author Josh Micich
81
 */
83
 */
82
public final class WorkbookEvaluator {
84
public final class WorkbookEvaluator {
83
85
	
86
	private static final POILogger LOG = POILogFactory.getLogger(WorkbookEvaluator.class);
87
	private static final String IGNORE_MISSING_WORKBOOKS = WorkbookEvaluator.class.getName() + ".IGNORE_MISSING_WORKBOOKS";
84
	private final EvaluationWorkbook _workbook;
88
	private final EvaluationWorkbook _workbook;
85
	private EvaluationCache _cache;
89
	private EvaluationCache _cache;
86
	/** part of cache entry key (useful when evaluating multiple workbooks) */
90
	/** part of cache entry key (useful when evaluating multiple workbooks) */
Lines 144-154 Link Here
144
	}
148
	}
145
149
146
	private static boolean isDebugLogEnabled() {
150
	private static boolean isDebugLogEnabled() {
147
		return false;
151
		return LOG.check(POILogger.DEBUG);
152
	}
153
	private static boolean isInfoLogEnabled() {
154
		return LOG.check(POILogger.INFO);
148
	}
155
	}
149
	private static void logDebug(String s) {
156
	private static void logDebug(String s) {
150
		if (isDebugLogEnabled()) {
157
		if (isDebugLogEnabled()) {
151
			System.out.println(s);
158
			LOG.log(POILogger.DEBUG, s);
159
		}
160
	}
161
	private static void logInfo(String s) {
162
		if (isInfoLogEnabled()) {
163
			LOG.log(POILogger.INFO, s);
152
		}
164
		}
153
	}
165
	}
154
	/* package */ void attachToEnvironment(CollaboratingWorkbooksEnvironment collaboratingWorkbooksEnvironment, EvaluationCache cache, int workbookIx) {
166
	/* package */ void attachToEnvironment(CollaboratingWorkbooksEnvironment collaboratingWorkbooksEnvironment, EvaluationCache cache, int workbookIx) {
Lines 288-296 Link Here
288
				}
300
				}
289
301
290
				tracker.updateCacheResult(result);
302
				tracker.updateCacheResult(result);
291
			} catch (NotImplementedException e) {
303
			}
304
			 catch (NotImplementedException e) {
292
				throw addExceptionInfo(e, sheetIndex, rowIndex, columnIndex);
305
				throw addExceptionInfo(e, sheetIndex, rowIndex, columnIndex);
293
			} finally {
306
			 } catch (RuntimeException re) {
307
				 if (re.getCause() instanceof WorkbookNotFoundException 
308
						 //To be replaced by configuration infrastructure
309
						 && Boolean.valueOf(System.getProperty(IGNORE_MISSING_WORKBOOKS))) {
310
 					logInfo(re.getCause().getMessage() + " - Continuing with cached value!");
311
 					switch(srcCell.getCachedFormulaResultType()) {
312
	 					case Cell.CELL_TYPE_NUMERIC:
313
	 						result = new NumberEval(srcCell.getNumericCellValue());
314
	 						break;
315
	 					case Cell.CELL_TYPE_STRING:
316
	 						result =  new StringEval(srcCell.getStringCellValue());
317
	 						break;
318
	 					case Cell.CELL_TYPE_BLANK:
319
	 						result = BlankEval.instance;
320
	 						break;
321
	 					case Cell.CELL_TYPE_BOOLEAN:
322
	 						result =  BoolEval.valueOf(srcCell.getBooleanCellValue());
323
	 						break;
324
	 					case Cell.CELL_TYPE_ERROR:
325
							result =  ErrorEval.valueOf(srcCell.getErrorCellValue());
326
							break;
327
	 					case Cell.CELL_TYPE_FORMULA:
328
						default:
329
							throw new RuntimeException("Unexpected cell type '" + srcCell.getCellType()+"' found!");
330
 					}
331
				 } else {
332
					 throw re;
333
				 }
334
			 } finally {
294
				tracker.endEvaluate(cce);
335
				tracker.endEvaluate(cce);
295
			}
336
			}
296
		} else {
337
		} else {
(-)src/java/org/apache/poi/ss/formula/EvaluationCell.java (+2 lines)
Lines 41-44 Link Here
41
	String getStringCellValue();
41
	String getStringCellValue();
42
	boolean getBooleanCellValue();
42
	boolean getBooleanCellValue();
43
	int getErrorCellValue();
43
	int getErrorCellValue();
44
45
	int getCachedFormulaResultType();
44
}
46
}
(-)src/testcases/org/apache/poi/ss/formula/TestMissingWorkbook.java (+119 lines)
Line 0 Link Here
1
package org.apache.poi.ss.formula;
2
3
import java.io.File;
4
import java.io.FileInputStream;
5
import java.io.IOException;
6
import java.io.InputStream;
7
8
import org.apache.poi.hssf.usermodel.HSSFCell;
9
import org.apache.poi.hssf.usermodel.HSSFFormulaEvaluator;
10
import org.apache.poi.hssf.usermodel.HSSFRow;
11
import org.apache.poi.hssf.usermodel.HSSFSheet;
12
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
13
import org.apache.poi.ss.usermodel.Cell;
14
import org.apache.poi.ss.usermodel.FormulaEvaluator;
15
16
import junit.framework.TestCase;
17
18
public class TestMissingWorkbook extends TestCase {
19
	private static final String MAIN_WORKBOOK_FILENAME = "main.xls";
20
	private static final String SOURCE_DUMMY_WORKBOOK_FILENAME = "source_dummy.xls";
21
	private static final String SOURCE_WORKBOOK_FILENAME = "source.xls";
22
	
23
	private static final String propertyKey = WorkbookEvaluator.class.getName() + ".IGNORE_MISSING_WORKBOOKS";
24
	
25
	private HSSFWorkbook mainWorkbook;
26
	private HSSFWorkbook sourceWorkbook;
27
	
28
	@Override
29
	protected void setUp() throws Exception {
30
		mainWorkbook = openWorkbook("test-data/spreadsheet/missing_workbook/"+MAIN_WORKBOOK_FILENAME);
31
		sourceWorkbook = openWorkbook("test-data/spreadsheet/missing_workbook/"+SOURCE_WORKBOOK_FILENAME);
32
		
33
		assertNotNull(mainWorkbook);
34
		assertNotNull(sourceWorkbook);
35
	}
36
37
	private HSSFWorkbook openWorkbook(String pFileName) throws IOException {
38
		File lSourceFile = new File(pFileName);
39
		assertTrue(pFileName + " does not exist!", lSourceFile.exists());
40
		
41
		InputStream lSourceFileInputStream = null;
42
		try {
43
			lSourceFileInputStream = new FileInputStream(lSourceFile);
44
			
45
			HSSFWorkbook lWorrkbook = new HSSFWorkbook(lSourceFileInputStream);
46
			
47
			return lWorrkbook;
48
		} finally {
49
			if (lSourceFileInputStream != null)  {
50
				lSourceFileInputStream.close();
51
			}
52
		}
53
	}
54
	
55
	public void testMissingWorkbookMissing() throws IOException {
56
		FormulaEvaluator evaluator = mainWorkbook.getCreationHelper().createFormulaEvaluator();
57
		
58
		HSSFSheet lSheet = mainWorkbook.getSheetAt(0);
59
		HSSFRow lARow = lSheet.getRow(0);
60
		HSSFCell lA1Cell = lARow.getCell(0);
61
		
62
		assertEquals(Cell.CELL_TYPE_FORMULA, lA1Cell.getCellType());
63
		try {
64
			evaluator.evaluateFormulaCell(lA1Cell);
65
			fail("Missing external workbook reference exception expected!");
66
		}catch(RuntimeException re) {
67
			assertTrue("Unexpected exception: " + re, re.getMessage().indexOf(SOURCE_DUMMY_WORKBOOK_FILENAME) != -1);
68
		}
69
	}
70
	
71
	public void testMissingWorkbookMissingOverride() throws IOException {
72
		HSSFSheet lSheet = mainWorkbook.getSheetAt(0);
73
		HSSFCell lA1Cell = lSheet.getRow(0).getCell(0);
74
		HSSFCell lB1Cell = lSheet.getRow(1).getCell(0);
75
		HSSFCell lC1Cell = lSheet.getRow(2).getCell(0);
76
		
77
		assertEquals(Cell.CELL_TYPE_FORMULA, lA1Cell.getCellType());
78
		assertEquals(Cell.CELL_TYPE_FORMULA, lB1Cell.getCellType());
79
		assertEquals(Cell.CELL_TYPE_FORMULA, lC1Cell.getCellType());
80
		
81
		FormulaEvaluator evaluator = mainWorkbook.getCreationHelper().createFormulaEvaluator();
82
		
83
		System.setProperty(propertyKey, Boolean.toString(true));
84
		assertEquals(Cell.CELL_TYPE_NUMERIC, evaluator.evaluateFormulaCell(lA1Cell));
85
		assertEquals(Cell.CELL_TYPE_STRING, evaluator.evaluateFormulaCell(lB1Cell));
86
		assertEquals(Cell.CELL_TYPE_BOOLEAN, evaluator.evaluateFormulaCell(lC1Cell));
87
88
		assertEquals(10.0d, lA1Cell.getNumericCellValue(), 0.00001d);
89
		assertEquals("POI rocks!", lB1Cell.getStringCellValue());
90
		assertEquals(true, lC1Cell.getBooleanCellValue());
91
	}
92
	
93
94
	public void testExistingWorkbook() throws IOException {
95
		HSSFSheet lSheet = mainWorkbook.getSheetAt(0);
96
		HSSFCell lA1Cell = lSheet.getRow(0).getCell(0);
97
		HSSFCell lB1Cell = lSheet.getRow(1).getCell(0);
98
		HSSFCell lC1Cell = lSheet.getRow(2).getCell(0);
99
		
100
		assertEquals(Cell.CELL_TYPE_FORMULA, lA1Cell.getCellType());
101
		assertEquals(Cell.CELL_TYPE_FORMULA, lB1Cell.getCellType());
102
		assertEquals(Cell.CELL_TYPE_FORMULA, lC1Cell.getCellType());
103
104
		HSSFFormulaEvaluator lMainWorkbookEvaluator = new HSSFFormulaEvaluator(mainWorkbook);
105
		HSSFFormulaEvaluator lSourceEvaluator = new HSSFFormulaEvaluator(sourceWorkbook);
106
		HSSFFormulaEvaluator.setupEnvironment(
107
				new String[]{MAIN_WORKBOOK_FILENAME, SOURCE_DUMMY_WORKBOOK_FILENAME}, 
108
				new HSSFFormulaEvaluator[] {lMainWorkbookEvaluator, lSourceEvaluator});
109
		
110
		assertEquals(Cell.CELL_TYPE_NUMERIC, lMainWorkbookEvaluator.evaluateFormulaCell(lA1Cell));
111
		assertEquals(Cell.CELL_TYPE_STRING, lMainWorkbookEvaluator.evaluateFormulaCell(lB1Cell));
112
		assertEquals(Cell.CELL_TYPE_BOOLEAN, lMainWorkbookEvaluator.evaluateFormulaCell(lC1Cell));
113
114
		assertEquals(20.0d, lA1Cell.getNumericCellValue(), 0.00001d);
115
		assertEquals("Apache rocks!", lB1Cell.getStringCellValue());
116
		assertEquals(false, lC1Cell.getBooleanCellValue());
117
	}
118
119
}
(-)src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFEvaluationCell.java (+3 lines)
Lines 72-75 Link Here
72
	public String getStringCellValue() {
72
	public String getStringCellValue() {
73
		return _cell.getRichStringCellValue().getString();
73
		return _cell.getRichStringCellValue().getString();
74
	}
74
	}
75
	public int getCachedFormulaResultType() {
76
		return _cell.getCachedFormulaResultType();
77
	}
75
}
78
}
(-)src/java/org/apache/poi/hssf/usermodel/HSSFEvaluationCell.java (+3 lines)
Lines 69-72 Link Here
69
	public String getStringCellValue() {
69
	public String getStringCellValue() {
70
		return _cell.getRichStringCellValue().getString();
70
		return _cell.getRichStringCellValue().getString();
71
	}
71
	}
72
	public int getCachedFormulaResultType() {
73
		return _cell.getCachedFormulaResultType();
74
	}
72
}
75
}
(-)src/java/org/apache/poi/ss/formula/OperationEvaluationContext.java (-1 / +1 lines)
Lines 86-92 Link Here
86
			try {
86
			try {
87
				targetEvaluator = _bookEvaluator.getOtherWorkbookEvaluator(workbookName);
87
				targetEvaluator = _bookEvaluator.getOtherWorkbookEvaluator(workbookName);
88
			} catch (WorkbookNotFoundException e) {
88
			} catch (WorkbookNotFoundException e) {
89
				throw new RuntimeException(e.getMessage());
89
				throw new RuntimeException(e.getMessage(), e);
90
			}
90
			}
91
			otherSheetIndex = targetEvaluator.getSheetIndex(externalSheet.getSheetName());
91
			otherSheetIndex = targetEvaluator.getSheetIndex(externalSheet.getSheetName());
92
			if (otherSheetIndex < 0) {
92
			if (otherSheetIndex < 0) {

Return to bug 52575