Fix inconsistent indents/whitespace

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1611949 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2014-07-19 19:23:13 +00:00
parent 2cd4290460
commit 84b50e845e
3 changed files with 281 additions and 282 deletions

View File

@ -46,33 +46,33 @@ import org.apache.poi.util.POILogger;
* Internal POI use only * Internal POI use only
*/ */
public final class HSSFEvaluationWorkbook implements FormulaRenderingWorkbook, EvaluationWorkbook, FormulaParsingWorkbook { public final class HSSFEvaluationWorkbook implements FormulaRenderingWorkbook, EvaluationWorkbook, FormulaParsingWorkbook {
private static POILogger logger = POILogFactory.getLogger(HSSFEvaluationWorkbook.class); private static POILogger logger = POILogFactory.getLogger(HSSFEvaluationWorkbook.class);
private final HSSFWorkbook _uBook; private final HSSFWorkbook _uBook;
private final InternalWorkbook _iBook; private final InternalWorkbook _iBook;
public static HSSFEvaluationWorkbook create(HSSFWorkbook book) { public static HSSFEvaluationWorkbook create(HSSFWorkbook book) {
if (book == null) { if (book == null) {
return null; return null;
} }
return new HSSFEvaluationWorkbook(book); return new HSSFEvaluationWorkbook(book);
} }
private HSSFEvaluationWorkbook(HSSFWorkbook book) { private HSSFEvaluationWorkbook(HSSFWorkbook book) {
_uBook = book; _uBook = book;
_iBook = book.getWorkbook(); _iBook = book.getWorkbook();
} }
public int getExternalSheetIndex(String sheetName) { public int getExternalSheetIndex(String sheetName) {
int sheetIndex = _uBook.getSheetIndex(sheetName); int sheetIndex = _uBook.getSheetIndex(sheetName);
return _iBook.checkExternSheet(sheetIndex); return _iBook.checkExternSheet(sheetIndex);
} }
public int getExternalSheetIndex(String workbookName, String sheetName) { public int getExternalSheetIndex(String workbookName, String sheetName) {
return _iBook.getExternalSheetIndex(workbookName, sheetName); return _iBook.getExternalSheetIndex(workbookName, sheetName);
} }
public Ptg get3DReferencePtg(CellReference cr, SheetIdentifier sheet) { public Ptg get3DReferencePtg(CellReference cr, SheetIdentifier sheet) {
int extIx = getSheetExtIx(sheet); int extIx = getSheetExtIx(sheet);
return new Ref3DPtg(cr, extIx); return new Ref3DPtg(cr, extIx);
} }
public Ptg get3DReferencePtg(AreaReference areaRef, SheetIdentifier sheet) { public Ptg get3DReferencePtg(AreaReference areaRef, SheetIdentifier sheet) {
int extIx = getSheetExtIx(sheet); int extIx = getSheetExtIx(sheet);
@ -81,138 +81,137 @@ public final class HSSFEvaluationWorkbook implements FormulaRenderingWorkbook, E
public NameXPtg getNameXPtg(String name, SheetIdentifier sheet) { public NameXPtg getNameXPtg(String name, SheetIdentifier sheet) {
int sheetRefIndex = getSheetExtIx(sheet); int sheetRefIndex = getSheetExtIx(sheet);
return _iBook.getNameXPtg(name, sheetRefIndex, _uBook.getUDFFinder()); return _iBook.getNameXPtg(name, sheetRefIndex, _uBook.getUDFFinder());
} }
/** /**
* Lookup a named range by its name. * Lookup a named range by its name.
* *
* @param name the name to search * @param name the name to search
* @param sheetIndex the 0-based index of the sheet this formula belongs to. * @param sheetIndex the 0-based index of the sheet this formula belongs to.
* The sheet index is required to resolve sheet-level names. <code>-1</code> means workbook-global names * The sheet index is required to resolve sheet-level names. <code>-1</code> means workbook-global names
*/ */
public EvaluationName getName(String name, int sheetIndex) { public EvaluationName getName(String name, int sheetIndex) {
for(int i=0; i < _iBook.getNumNames(); i++) { for(int i=0; i < _iBook.getNumNames(); i++) {
NameRecord nr = _iBook.getNameRecord(i); NameRecord nr = _iBook.getNameRecord(i);
if (nr.getSheetNumber() == sheetIndex+1 && name.equalsIgnoreCase(nr.getNameText())) { if (nr.getSheetNumber() == sheetIndex+1 && name.equalsIgnoreCase(nr.getNameText())) {
return new Name(nr, i); return new Name(nr, i);
} }
} }
return sheetIndex == -1 ? null : getName(name, -1); return sheetIndex == -1 ? null : getName(name, -1);
} }
public int getSheetIndex(EvaluationSheet evalSheet) { public int getSheetIndex(EvaluationSheet evalSheet) {
HSSFSheet sheet = ((HSSFEvaluationSheet)evalSheet).getHSSFSheet(); HSSFSheet sheet = ((HSSFEvaluationSheet)evalSheet).getHSSFSheet();
return _uBook.getSheetIndex(sheet); return _uBook.getSheetIndex(sheet);
} }
public int getSheetIndex(String sheetName) { public int getSheetIndex(String sheetName) {
return _uBook.getSheetIndex(sheetName); return _uBook.getSheetIndex(sheetName);
} }
public String getSheetName(int sheetIndex) { public String getSheetName(int sheetIndex) {
return _uBook.getSheetName(sheetIndex); return _uBook.getSheetName(sheetIndex);
} }
public EvaluationSheet getSheet(int sheetIndex) { public EvaluationSheet getSheet(int sheetIndex) {
return new HSSFEvaluationSheet(_uBook.getSheetAt(sheetIndex)); return new HSSFEvaluationSheet(_uBook.getSheetAt(sheetIndex));
} }
public int convertFromExternSheetIndex(int externSheetIndex) { public int convertFromExternSheetIndex(int externSheetIndex) {
return _iBook.getSheetIndexFromExternSheetIndex(externSheetIndex); return _iBook.getSheetIndexFromExternSheetIndex(externSheetIndex);
} }
public ExternalSheet getExternalSheet(int externSheetIndex) { public ExternalSheet getExternalSheet(int externSheetIndex) {
ExternalSheet sheet = _iBook.getExternalSheet(externSheetIndex); ExternalSheet sheet = _iBook.getExternalSheet(externSheetIndex);
if (sheet == null) { if (sheet == null) {
// Try to treat it as a local sheet // Try to treat it as a local sheet
int localSheetIndex = convertFromExternSheetIndex(externSheetIndex); int localSheetIndex = convertFromExternSheetIndex(externSheetIndex);
if (localSheetIndex == -1) { if (localSheetIndex == -1) {
// The sheet referenced can't be found, sorry // The sheet referenced can't be found, sorry
return null; return null;
} }
if (localSheetIndex == -2) { if (localSheetIndex == -2) {
// Not actually sheet based at all - is workbook scoped // Not actually sheet based at all - is workbook scoped
return null; return null;
} }
// Look up the local sheet // Look up the local sheet
String sheetName = getSheetName(localSheetIndex); String sheetName = getSheetName(localSheetIndex);
sheet = new ExternalSheet(null, sheetName); sheet = new ExternalSheet(null, sheetName);
} }
return sheet; return sheet;
} }
public ExternalSheet getExternalSheet(String sheetName, int externalWorkbookNumber) { public ExternalSheet getExternalSheet(String sheetName, int externalWorkbookNumber) {
throw new IllegalStateException("XSSF-style external references are not supported for HSSF"); throw new IllegalStateException("XSSF-style external references are not supported for HSSF");
} }
public ExternalName getExternalName(int externSheetIndex, int externNameIndex) { public ExternalName getExternalName(int externSheetIndex, int externNameIndex) {
return _iBook.getExternalName(externSheetIndex, externNameIndex); return _iBook.getExternalName(externSheetIndex, externNameIndex);
} }
public String resolveNameXText(NameXPtg n) { public String resolveNameXText(NameXPtg n) {
return _iBook.resolveNameXText(n.getSheetRefIndex(), n.getNameIndex()); return _iBook.resolveNameXText(n.getSheetRefIndex(), n.getNameIndex());
} }
public String getSheetNameByExternSheet(int externSheetIndex) { public String getSheetNameByExternSheet(int externSheetIndex) {
return _iBook.findSheetNameFromExternSheet(externSheetIndex); return _iBook.findSheetNameFromExternSheet(externSheetIndex);
} }
public String getNameText(NamePtg namePtg) { public String getNameText(NamePtg namePtg) {
return _iBook.getNameRecord(namePtg.getIndex()).getNameText(); return _iBook.getNameRecord(namePtg.getIndex()).getNameText();
} }
public EvaluationName getName(NamePtg namePtg) { public EvaluationName getName(NamePtg namePtg) {
int ix = namePtg.getIndex(); int ix = namePtg.getIndex();
return new Name(_iBook.getNameRecord(ix), ix); return new Name(_iBook.getNameRecord(ix), ix);
} }
@SuppressWarnings("unused") @SuppressWarnings("unused")
public Ptg[] getFormulaTokens(EvaluationCell evalCell) { public Ptg[] getFormulaTokens(EvaluationCell evalCell) {
HSSFCell cell = ((HSSFEvaluationCell)evalCell).getHSSFCell(); HSSFCell cell = ((HSSFEvaluationCell)evalCell).getHSSFCell();
if (false) { if (false) {
// re-parsing the formula text also works, but is a waste of time // re-parsing the formula text also works, but is a waste of time
// It is useful from time to time to run all unit tests with this code // It is useful from time to time to run all unit tests with this code
// to make sure that all formulas POI can evaluate can also be parsed. // to make sure that all formulas POI can evaluate can also be parsed.
try { try {
return HSSFFormulaParser.parse(cell.getCellFormula(), _uBook, FormulaType.CELL, _uBook.getSheetIndex(cell.getSheet())); return HSSFFormulaParser.parse(cell.getCellFormula(), _uBook, FormulaType.CELL, _uBook.getSheetIndex(cell.getSheet()));
} catch (FormulaParseException e) { } catch (FormulaParseException e) {
// Note - as of Bugzilla 48036 (svn r828244, r828247) POI is capable of evaluating // Note - as of Bugzilla 48036 (svn r828244, r828247) POI is capable of evaluating
// IntesectionPtg. However it is still not capable of parsing it. // IntesectionPtg. However it is still not capable of parsing it.
// So FormulaEvalTestData.xls now contains a few formulas that produce errors here. // So FormulaEvalTestData.xls now contains a few formulas that produce errors here.
logger.log( POILogger.ERROR, e.getMessage()); logger.log( POILogger.ERROR, e.getMessage());
} }
} }
FormulaRecordAggregate fra = (FormulaRecordAggregate) cell.getCellValueRecord(); FormulaRecordAggregate fra = (FormulaRecordAggregate) cell.getCellValueRecord();
return fra.getFormulaTokens(); return fra.getFormulaTokens();
} }
public UDFFinder getUDFFinder(){ public UDFFinder getUDFFinder(){
return _uBook.getUDFFinder(); return _uBook.getUDFFinder();
} }
private static final class Name implements EvaluationName { private static final class Name implements EvaluationName {
private final NameRecord _nameRecord;
private final int _index;
private final NameRecord _nameRecord; public Name(NameRecord nameRecord, int index) {
private final int _index; _nameRecord = nameRecord;
_index = index;
public Name(NameRecord nameRecord, int index) { }
_nameRecord = nameRecord; public Ptg[] getNameDefinition() {
_index = index; return _nameRecord.getNameDefinition();
} }
public Ptg[] getNameDefinition() { public String getNameText() {
return _nameRecord.getNameDefinition(); return _nameRecord.getNameText();
} }
public String getNameText() { public boolean hasFormula() {
return _nameRecord.getNameText(); return _nameRecord.hasFormula();
} }
public boolean hasFormula() { public boolean isFunctionName() {
return _nameRecord.hasFormula(); return _nameRecord.isFunctionName();
} }
public boolean isFunctionName() { public boolean isRange() {
return _nameRecord.isFunctionName(); return _nameRecord.hasFormula(); // TODO - is this right?
} }
public boolean isRange() { public NamePtg createPtg() {
return _nameRecord.hasFormula(); // TODO - is this right? return new NamePtg(_index);
} }
public NamePtg createPtg() { }
return new NamePtg(_index);
}
}
private int getSheetExtIx(SheetIdentifier sheetIden) { private int getSheetExtIx(SheetIdentifier sheetIden) {
int extIx; int extIx;
@ -229,7 +228,7 @@ public final class HSSFEvaluationWorkbook implements FormulaRenderingWorkbook, E
return extIx; return extIx;
} }
public SpreadsheetVersion getSpreadsheetVersion(){ public SpreadsheetVersion getSpreadsheetVersion(){
return SpreadsheetVersion.EXCEL97; return SpreadsheetVersion.EXCEL97;
} }
} }

View File

@ -30,75 +30,75 @@ import org.apache.poi.ss.formula.udf.UDFFinder;
* @author Josh Micich * @author Josh Micich
*/ */
public interface EvaluationWorkbook { public interface EvaluationWorkbook {
String getSheetName(int sheetIndex); String getSheetName(int sheetIndex);
/** /**
* @return -1 if the specified sheet is from a different book * @return -1 if the specified sheet is from a different book
*/ */
int getSheetIndex(EvaluationSheet sheet); int getSheetIndex(EvaluationSheet sheet);
/** /**
* Finds a sheet index by case insensitive name. * Finds a sheet index by case insensitive name.
* @return the index of the sheet matching the specified name. -1 if not found * @return the index of the sheet matching the specified name. -1 if not found
*/ */
int getSheetIndex(String sheetName); int getSheetIndex(String sheetName);
EvaluationSheet getSheet(int sheetIndex); EvaluationSheet getSheet(int sheetIndex);
/** /**
* HSSF Only - fetch the external-style sheet details * HSSF Only - fetch the external-style sheet details
* <p>Return will have no workbook set if it's actually in our own workbook</p>
*/
ExternalSheet getExternalSheet(int externSheetIndex);
/**
* XSSF Only - fetch the external-style sheet details
* <p>Return will have no workbook set if it's actually in our own workbook</p> * <p>Return will have no workbook set if it's actually in our own workbook</p>
*/ */
ExternalSheet getExternalSheet(String sheetName, int externalWorkbookNumber); ExternalSheet getExternalSheet(int externSheetIndex);
/** /**
* HSSF Only - convert an external sheet index to an internal sheet index, * XSSF Only - fetch the external-style sheet details
* for an external-style reference to one of this workbook's own sheets * <p>Return will have no workbook set if it's actually in our own workbook</p>
*/ */
int convertFromExternSheetIndex(int externSheetIndex); ExternalSheet getExternalSheet(String sheetName, int externalWorkbookNumber);
/**
* HSSF Only - convert an external sheet index to an internal sheet index,
* for an external-style reference to one of this workbook's own sheets
*/
int convertFromExternSheetIndex(int externSheetIndex);
ExternalName getExternalName(int externSheetIndex, int externNameIndex); ExternalName getExternalName(int externSheetIndex, int externNameIndex);
EvaluationName getName(NamePtg namePtg); EvaluationName getName(NamePtg namePtg);
EvaluationName getName(String name, int sheetIndex); EvaluationName getName(String name, int sheetIndex);
String resolveNameXText(NameXPtg ptg); String resolveNameXText(NameXPtg ptg);
Ptg[] getFormulaTokens(EvaluationCell cell); Ptg[] getFormulaTokens(EvaluationCell cell);
UDFFinder getUDFFinder(); UDFFinder getUDFFinder();
class ExternalSheet { class ExternalSheet {
private final String _workbookName; private final String _workbookName;
private final String _sheetName; private final String _sheetName;
public ExternalSheet(String workbookName, String sheetName) { public ExternalSheet(String workbookName, String sheetName) {
_workbookName = workbookName; _workbookName = workbookName;
_sheetName = sheetName; _sheetName = sheetName;
} }
public String getWorkbookName() { public String getWorkbookName() {
return _workbookName; return _workbookName;
} }
public String getSheetName() { public String getSheetName() {
return _sheetName; return _sheetName;
} }
} }
class ExternalName { class ExternalName {
private final String _nameName; private final String _nameName;
private final int _nameNumber; private final int _nameNumber;
private final int _ix; private final int _ix;
public ExternalName(String nameName, int nameNumber, int ix) { public ExternalName(String nameName, int nameNumber, int ix) {
_nameName = nameName; _nameName = nameName;
_nameNumber = nameNumber; _nameNumber = nameNumber;
_ix = ix; _ix = ix;
} }
public String getName() { public String getName() {
return _nameName; return _nameName;
} }
public int getNumber() { public int getNumber() {
return _nameNumber; return _nameNumber;
} }
public int getIx() { public int getIx() {
return _ix; return _ix;
} }
} }
} }

View File

@ -621,90 +621,90 @@ public final class WorkbookEvaluator {
} }
/** /**
* returns an appropriate Eval impl instance for the Ptg. The Ptg must be * returns an appropriate Eval impl instance for the Ptg. The Ptg must be
* one of: Area3DPtg, AreaPtg, ReferencePtg, Ref3DPtg, IntPtg, NumberPtg, * one of: Area3DPtg, AreaPtg, ReferencePtg, Ref3DPtg, IntPtg, NumberPtg,
* StringPtg, BoolPtg <br/>special Note: OperationPtg subtypes cannot be * StringPtg, BoolPtg <br/>special Note: OperationPtg subtypes cannot be
* passed here! * passed here!
*/ */
private ValueEval getEvalForPtg(Ptg ptg, OperationEvaluationContext ec) { private ValueEval getEvalForPtg(Ptg ptg, OperationEvaluationContext ec) {
// consider converting all these (ptg instanceof XxxPtg) expressions to (ptg.getClass() == XxxPtg.class) // consider converting all these (ptg instanceof XxxPtg) expressions to (ptg.getClass() == XxxPtg.class)
if (ptg instanceof NamePtg) { if (ptg instanceof NamePtg) {
// Named ranges, macro functions // Named ranges, macro functions
NamePtg namePtg = (NamePtg) ptg; NamePtg namePtg = (NamePtg) ptg;
EvaluationName nameRecord = _workbook.getName(namePtg); EvaluationName nameRecord = _workbook.getName(namePtg);
return getEvalForNameRecord(nameRecord, ec); return getEvalForNameRecord(nameRecord, ec);
} }
if (ptg instanceof NameXPtg) { if (ptg instanceof NameXPtg) {
// Externally defined named ranges or macro functions // Externally defined named ranges or macro functions
NameXPtg nameXPtg = (NameXPtg)ptg; NameXPtg nameXPtg = (NameXPtg)ptg;
ValueEval eval = ec.getNameXEval(nameXPtg); ValueEval eval = ec.getNameXEval(nameXPtg);
if (eval instanceof NameXEval) { if (eval instanceof NameXEval) {
// Could not be directly evaluated, so process as a name // Could not be directly evaluated, so process as a name
return getEvalForNameX(nameXPtg, ec); return getEvalForNameX(nameXPtg, ec);
} else { } else {
// Use the evaluated version // Use the evaluated version
return eval; return eval;
} }
} }
if (ptg instanceof IntPtg) { if (ptg instanceof IntPtg) {
return new NumberEval(((IntPtg)ptg).getValue()); return new NumberEval(((IntPtg)ptg).getValue());
} }
if (ptg instanceof NumberPtg) { if (ptg instanceof NumberPtg) {
return new NumberEval(((NumberPtg)ptg).getValue()); return new NumberEval(((NumberPtg)ptg).getValue());
} }
if (ptg instanceof StringPtg) { if (ptg instanceof StringPtg) {
return new StringEval(((StringPtg) ptg).getValue()); return new StringEval(((StringPtg) ptg).getValue());
} }
if (ptg instanceof BoolPtg) { if (ptg instanceof BoolPtg) {
return BoolEval.valueOf(((BoolPtg) ptg).getValue()); return BoolEval.valueOf(((BoolPtg) ptg).getValue());
} }
if (ptg instanceof ErrPtg) { if (ptg instanceof ErrPtg) {
return ErrorEval.valueOf(((ErrPtg) ptg).getErrorCode()); return ErrorEval.valueOf(((ErrPtg) ptg).getErrorCode());
} }
if (ptg instanceof MissingArgPtg) { if (ptg instanceof MissingArgPtg) {
return MissingArgEval.instance; return MissingArgEval.instance;
} }
if (ptg instanceof AreaErrPtg ||ptg instanceof RefErrorPtg if (ptg instanceof AreaErrPtg ||ptg instanceof RefErrorPtg
|| ptg instanceof DeletedArea3DPtg || ptg instanceof DeletedRef3DPtg) { || ptg instanceof DeletedArea3DPtg || ptg instanceof DeletedRef3DPtg) {
return ErrorEval.REF_INVALID; return ErrorEval.REF_INVALID;
} }
if (ptg instanceof Ref3DPtg) { if (ptg instanceof Ref3DPtg) {
return ec.getRef3DEval((Ref3DPtg)ptg); return ec.getRef3DEval((Ref3DPtg)ptg);
} }
if (ptg instanceof Ref3DPxg) { if (ptg instanceof Ref3DPxg) {
return ec.getRef3DEval((Ref3DPxg)ptg); return ec.getRef3DEval((Ref3DPxg)ptg);
} }
if (ptg instanceof Area3DPtg) { if (ptg instanceof Area3DPtg) {
Area3DPtg aptg = (Area3DPtg) ptg; Area3DPtg aptg = (Area3DPtg) ptg;
return ec.getArea3DEval(aptg.getFirstRow(), aptg.getFirstColumn(), aptg.getLastRow(), aptg.getLastColumn(), aptg.getExternSheetIndex()); return ec.getArea3DEval(aptg.getFirstRow(), aptg.getFirstColumn(), aptg.getLastRow(), aptg.getLastColumn(), aptg.getExternSheetIndex());
} }
if (ptg instanceof RefPtg) { if (ptg instanceof RefPtg) {
RefPtg rptg = (RefPtg) ptg; RefPtg rptg = (RefPtg) ptg;
return ec.getRefEval(rptg.getRow(), rptg.getColumn()); return ec.getRefEval(rptg.getRow(), rptg.getColumn());
} }
if (ptg instanceof AreaPtg) { if (ptg instanceof AreaPtg) {
AreaPtg aptg = (AreaPtg) ptg; AreaPtg aptg = (AreaPtg) ptg;
return ec.getAreaEval(aptg.getFirstRow(), aptg.getFirstColumn(), aptg.getLastRow(), aptg.getLastColumn()); return ec.getAreaEval(aptg.getFirstRow(), aptg.getFirstColumn(), aptg.getLastRow(), aptg.getLastColumn());
} }
if (ptg instanceof UnknownPtg) { if (ptg instanceof UnknownPtg) {
// POI uses UnknownPtg when the encoded Ptg array seems to be corrupted. // POI uses UnknownPtg when the encoded Ptg array seems to be corrupted.
// This seems to occur in very rare cases (e.g. unused name formulas in bug 44774, attachment 21790) // This seems to occur in very rare cases (e.g. unused name formulas in bug 44774, attachment 21790)
// In any case, formulas are re-parsed before execution, so UnknownPtg should not get here // In any case, formulas are re-parsed before execution, so UnknownPtg should not get here
throw new RuntimeException("UnknownPtg not allowed"); throw new RuntimeException("UnknownPtg not allowed");
} }
if (ptg instanceof ExpPtg) { if (ptg instanceof ExpPtg) {
// ExpPtg is used for array formulas and shared formulas. // ExpPtg is used for array formulas and shared formulas.
// it is currently unsupported, and may not even get implemented here // it is currently unsupported, and may not even get implemented here
throw new RuntimeException("ExpPtg currently not supported"); throw new RuntimeException("ExpPtg currently not supported");
} }
throw new RuntimeException("Unexpected ptg class (" + ptg.getClass().getName() + ")"); throw new RuntimeException("Unexpected ptg class (" + ptg.getClass().getName() + ")");
} }
private ValueEval getEvalForNameRecord(EvaluationName nameRecord, OperationEvaluationContext ec) { private ValueEval getEvalForNameRecord(EvaluationName nameRecord, OperationEvaluationContext ec) {
if (nameRecord.isFunctionName()) { if (nameRecord.isFunctionName()) {