bug 61474, #81 on github: add @Override annotation; rename FormulaShifter shifter to FormulaShifter formulaShifter to reduce confusion with the shifter variable referring to the RowShifter

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1814255 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Javen O'Neal 2017-11-04 05:48:18 +00:00
parent 0e540db89b
commit b671199b40
3 changed files with 48 additions and 38 deletions

View File

@ -39,29 +39,34 @@ public final class HSSFRowShifter extends RowShifter {
super(sh); super(sh);
} }
@Override
@NotImplemented @NotImplemented
public void updateNamedRanges(FormulaShifter shifter) { public void updateNamedRanges(FormulaShifter formulaShifter) {
throw new NotImplementedException("HSSFRowShifter.updateNamedRanges"); throw new NotImplementedException("HSSFRowShifter.updateNamedRanges");
} }
@Override
@NotImplemented @NotImplemented
public void updateFormulas(FormulaShifter shifter) { public void updateFormulas(FormulaShifter formulaShifter) {
throw new NotImplementedException("updateFormulas"); throw new NotImplementedException("updateFormulas");
} }
@Override
@Internal @Internal
@NotImplemented @NotImplemented
public void updateRowFormulas(Row row, FormulaShifter shifter) { public void updateRowFormulas(Row row, FormulaShifter formulaShifter) {
throw new NotImplementedException("updateRowFormulas"); throw new NotImplementedException("updateRowFormulas");
} }
@Override
@NotImplemented @NotImplemented
public void updateConditionalFormatting(FormulaShifter shifter) { public void updateConditionalFormatting(FormulaShifter formulaShifter) {
throw new NotImplementedException("updateConditionalFormatting"); throw new NotImplementedException("updateConditionalFormatting");
} }
@Override
@NotImplemented @NotImplemented
public void updateHyperlinks(FormulaShifter shifter) { public void updateHyperlinks(FormulaShifter formulaShifter) {
throw new NotImplementedException("updateHyperlinks"); throw new NotImplementedException("updateHyperlinks");
} }

View File

@ -114,31 +114,31 @@ public abstract class RowShifter {
/** /**
* Updated named ranges * Updated named ranges
*/ */
public abstract void updateNamedRanges(FormulaShifter shifter); public abstract void updateNamedRanges(FormulaShifter formulaShifter);
/** /**
* Update formulas. * Update formulas.
*/ */
public abstract void updateFormulas(FormulaShifter shifter); public abstract void updateFormulas(FormulaShifter formulaShifter);
/** /**
* Update the formulas in specified row using the formula shifting policy specified by shifter * Update the formulas in specified row using the formula shifting policy specified by shifter
* *
* @param row the row to update the formulas on * @param row the row to update the formulas on
* @param shifter the formula shifting policy * @param formulaShifter the formula shifting policy
*/ */
@Internal @Internal
public abstract void updateRowFormulas(Row row, FormulaShifter shifter); public abstract void updateRowFormulas(Row row, FormulaShifter formulaShifter);
public abstract void updateConditionalFormatting(FormulaShifter shifter); public abstract void updateConditionalFormatting(FormulaShifter formulaShifter);
/** /**
* Shift the Hyperlink anchors (not the hyperlink text, even if the hyperlink * Shift the Hyperlink anchors (not the hyperlink text, even if the hyperlink
* is of type LINK_DOCUMENT and refers to a cell that was shifted). Hyperlinks * is of type LINK_DOCUMENT and refers to a cell that was shifted). Hyperlinks
* do not track the content they point to. * do not track the content they point to.
* *
* @param shifter the formula shifting policy * @param formulaShifter the formula shifting policy
*/ */
public abstract void updateHyperlinks(FormulaShifter shifter); public abstract void updateHyperlinks(FormulaShifter formulaShifter);
} }

View File

@ -67,7 +67,8 @@ public final class XSSFRowShifter extends RowShifter {
/** /**
* Updated named ranges * Updated named ranges
*/ */
public void updateNamedRanges(FormulaShifter shifter) { @Override
public void updateNamedRanges(FormulaShifter formulaShifter) {
Workbook wb = sheet.getWorkbook(); Workbook wb = sheet.getWorkbook();
XSSFEvaluationWorkbook fpb = XSSFEvaluationWorkbook.create((XSSFWorkbook) wb); XSSFEvaluationWorkbook fpb = XSSFEvaluationWorkbook.create((XSSFWorkbook) wb);
for (Name name : wb.getAllNames()) { for (Name name : wb.getAllNames()) {
@ -76,7 +77,7 @@ public final class XSSFRowShifter extends RowShifter {
final int rowIndex = -1; //don't care, named ranges are not allowed to include structured references final int rowIndex = -1; //don't care, named ranges are not allowed to include structured references
Ptg[] ptgs = FormulaParser.parse(formula, fpb, FormulaType.NAMEDRANGE, sheetIndex, rowIndex); Ptg[] ptgs = FormulaParser.parse(formula, fpb, FormulaType.NAMEDRANGE, sheetIndex, rowIndex);
if (shifter.adjustFormula(ptgs, sheetIndex)) { if (formulaShifter.adjustFormula(ptgs, sheetIndex)) {
String shiftedFmla = FormulaRenderer.toFormulaString(fpb, ptgs); String shiftedFmla = FormulaRenderer.toFormulaString(fpb, ptgs);
name.setRefersToFormula(shiftedFmla); name.setRefersToFormula(shiftedFmla);
} }
@ -86,22 +87,23 @@ public final class XSSFRowShifter extends RowShifter {
/** /**
* Update formulas. * Update formulas.
*/ */
public void updateFormulas(FormulaShifter shifter) { @Override
public void updateFormulas(FormulaShifter formulaShifter) {
//update formulas on the parent sheet //update formulas on the parent sheet
updateSheetFormulas(sheet, shifter); updateSheetFormulas(sheet, formulaShifter);
//update formulas on other sheets //update formulas on other sheets
Workbook wb = sheet.getWorkbook(); Workbook wb = sheet.getWorkbook();
for (Sheet sh : wb) { for (Sheet sh : wb) {
if (sheet == sh) continue; if (sheet == sh) continue;
updateSheetFormulas(sh, shifter); updateSheetFormulas(sh, formulaShifter);
} }
} }
private void updateSheetFormulas(Sheet sh, FormulaShifter shifter) { private void updateSheetFormulas(Sheet sh, FormulaShifter formulashifter) {
for (Row r : sh) { for (Row r : sh) {
XSSFRow row = (XSSFRow) r; XSSFRow row = (XSSFRow) r;
updateRowFormulas(row, shifter); updateRowFormulas(row, formulashifter);
} }
} }
@ -109,10 +111,11 @@ public final class XSSFRowShifter extends RowShifter {
* Update the formulas in specified row using the formula shifting policy specified by shifter * Update the formulas in specified row using the formula shifting policy specified by shifter
* *
* @param row the row to update the formulas on * @param row the row to update the formulas on
* @param shifter the formula shifting policy * @param formulaShifter the formula shifting policy
*/ */
@Internal @Internal
public void updateRowFormulas(Row row, FormulaShifter shifter) { @Override
public void updateRowFormulas(Row row, FormulaShifter formulaShifter) {
XSSFSheet sheet = (XSSFSheet) row.getSheet(); XSSFSheet sheet = (XSSFSheet) row.getSheet();
for (Cell c : row) { for (Cell c : row) {
XSSFCell cell = (XSSFCell) c; XSSFCell cell = (XSSFCell) c;
@ -122,30 +125,30 @@ public final class XSSFRowShifter extends RowShifter {
CTCellFormula f = ctCell.getF(); CTCellFormula f = ctCell.getF();
String formula = f.getStringValue(); String formula = f.getStringValue();
if (formula.length() > 0) { if (formula.length() > 0) {
String shiftedFormula = shiftFormula(row, formula, shifter); String shiftedFormula = shiftFormula(row, formula, formulaShifter);
if (shiftedFormula != null) { if (shiftedFormula != null) {
f.setStringValue(shiftedFormula); f.setStringValue(shiftedFormula);
if(f.getT() == STCellFormulaType.SHARED){ if(f.getT() == STCellFormulaType.SHARED){
int si = (int)f.getSi(); int si = (int)f.getSi();
CTCellFormula sf = sheet.getSharedFormula(si); CTCellFormula sf = sheet.getSharedFormula(si);
sf.setStringValue(shiftedFormula); sf.setStringValue(shiftedFormula);
updateRefInCTCellFormula(row, shifter, sf); updateRefInCTCellFormula(row, formulaShifter, sf);
} }
} }
} }
//Range of cells which the formula applies to. //Range of cells which the formula applies to.
updateRefInCTCellFormula(row, shifter, f); updateRefInCTCellFormula(row, formulaShifter, f);
} }
} }
} }
private void updateRefInCTCellFormula(Row row, FormulaShifter shifter, CTCellFormula f) { private void updateRefInCTCellFormula(Row row, FormulaShifter formulaShifter, CTCellFormula f) {
if (f.isSetRef()) { //Range of cells which the formula applies to. if (f.isSetRef()) { //Range of cells which the formula applies to.
String ref = f.getRef(); String ref = f.getRef();
String shiftedRef = shiftFormula(row, ref, shifter); String shiftedRef = shiftFormula(row, ref, formulaShifter);
if (shiftedRef != null) f.setRef(shiftedRef); if (shiftedRef != null) f.setRef(shiftedRef);
} }
} }
@ -155,11 +158,11 @@ public final class XSSFRowShifter extends RowShifter {
* *
* @param row the row of the cell this formula belongs to. Used to get a reference to the parent workbook. * @param row the row of the cell this formula belongs to. Used to get a reference to the parent workbook.
* @param formula the formula to shift * @param formula the formula to shift
* @param shifter the FormulaShifter object that operates on the parsed formula tokens * @param formulaShifter the FormulaShifter object that operates on the parsed formula tokens
* @return the shifted formula if the formula was changed, * @return the shifted formula if the formula was changed,
* <code>null</code> if the formula wasn't modified * <code>null</code> if the formula wasn't modified
*/ */
private static String shiftFormula(Row row, String formula, FormulaShifter shifter) { private static String shiftFormula(Row row, String formula, FormulaShifter formulaShifter) {
Sheet sheet = row.getSheet(); Sheet sheet = row.getSheet();
Workbook wb = sheet.getWorkbook(); Workbook wb = sheet.getWorkbook();
int sheetIndex = wb.getSheetIndex(sheet); int sheetIndex = wb.getSheetIndex(sheet);
@ -169,7 +172,7 @@ public final class XSSFRowShifter extends RowShifter {
try { try {
Ptg[] ptgs = FormulaParser.parse(formula, fpb, FormulaType.CELL, sheetIndex, rowIndex); Ptg[] ptgs = FormulaParser.parse(formula, fpb, FormulaType.CELL, sheetIndex, rowIndex);
String shiftedFmla = null; String shiftedFmla = null;
if (shifter.adjustFormula(ptgs, sheetIndex)) { if (formulaShifter.adjustFormula(ptgs, sheetIndex)) {
shiftedFmla = FormulaRenderer.toFormulaString(fpb, ptgs); shiftedFmla = FormulaRenderer.toFormulaString(fpb, ptgs);
} }
return shiftedFmla; return shiftedFmla;
@ -180,7 +183,8 @@ public final class XSSFRowShifter extends RowShifter {
} }
} }
public void updateConditionalFormatting(FormulaShifter shifter) { @Override
public void updateConditionalFormatting(FormulaShifter formulaShifter) {
XSSFSheet xsheet = (XSSFSheet) sheet; XSSFSheet xsheet = (XSSFSheet) sheet;
XSSFWorkbook wb = xsheet.getWorkbook(); XSSFWorkbook wb = xsheet.getWorkbook();
int sheetIndex = wb.getSheetIndex(sheet); int sheetIndex = wb.getSheetIndex(sheet);
@ -204,7 +208,7 @@ public final class XSSFRowShifter extends RowShifter {
boolean changed = false; boolean changed = false;
List<CellRangeAddress> temp = new ArrayList<>(); List<CellRangeAddress> temp = new ArrayList<>();
for (CellRangeAddress craOld : cellRanges) { for (CellRangeAddress craOld : cellRanges) {
CellRangeAddress craNew = shiftRange(shifter, craOld, sheetIndex); CellRangeAddress craNew = shiftRange(formulaShifter, craOld, sheetIndex);
if (craNew == null) { if (craNew == null) {
changed = true; changed = true;
continue; continue;
@ -231,7 +235,7 @@ public final class XSSFRowShifter extends RowShifter {
for (int i = 0; i < formulaArray.length; i++) { for (int i = 0; i < formulaArray.length; i++) {
String formula = formulaArray[i]; String formula = formulaArray[i];
Ptg[] ptgs = FormulaParser.parse(formula, fpb, FormulaType.CELL, sheetIndex, rowIndex); Ptg[] ptgs = FormulaParser.parse(formula, fpb, FormulaType.CELL, sheetIndex, rowIndex);
if (shifter.adjustFormula(ptgs, sheetIndex)) { if (formulaShifter.adjustFormula(ptgs, sheetIndex)) {
String shiftedFmla = FormulaRenderer.toFormulaString(fpb, ptgs); String shiftedFmla = FormulaRenderer.toFormulaString(fpb, ptgs);
cfRule.setFormulaArray(i, shiftedFmla); cfRule.setFormulaArray(i, shiftedFmla);
} }
@ -245,9 +249,10 @@ public final class XSSFRowShifter extends RowShifter {
* is of type LINK_DOCUMENT and refers to a cell that was shifted). Hyperlinks * is of type LINK_DOCUMENT and refers to a cell that was shifted). Hyperlinks
* do not track the content they point to. * do not track the content they point to.
* *
* @param shifter * @param formulaShifter
*/ */
public void updateHyperlinks(FormulaShifter shifter) { @Override
public void updateHyperlinks(FormulaShifter formulaShifter) {
int sheetIndex = sheet.getWorkbook().getSheetIndex(sheet); int sheetIndex = sheet.getWorkbook().getSheetIndex(sheet);
List<? extends Hyperlink> hyperlinkList = sheet.getHyperlinkList(); List<? extends Hyperlink> hyperlinkList = sheet.getHyperlinkList();
@ -255,7 +260,7 @@ public final class XSSFRowShifter extends RowShifter {
XSSFHyperlink xhyperlink = (XSSFHyperlink) hyperlink; XSSFHyperlink xhyperlink = (XSSFHyperlink) hyperlink;
String cellRef = xhyperlink.getCellRef(); String cellRef = xhyperlink.getCellRef();
CellRangeAddress cra = CellRangeAddress.valueOf(cellRef); CellRangeAddress cra = CellRangeAddress.valueOf(cellRef);
CellRangeAddress shiftedRange = shiftRange(shifter, cra, sheetIndex); CellRangeAddress shiftedRange = shiftRange(formulaShifter, cra, sheetIndex);
if (shiftedRange != null && shiftedRange != cra) { if (shiftedRange != null && shiftedRange != cra) {
// shiftedRange should not be null. If shiftedRange is null, that means // shiftedRange should not be null. If shiftedRange is null, that means
// that a hyperlink wasn't deleted at the beginning of shiftRows when // that a hyperlink wasn't deleted at the beginning of shiftRows when
@ -265,12 +270,12 @@ public final class XSSFRowShifter extends RowShifter {
} }
} }
private static CellRangeAddress shiftRange(FormulaShifter shifter, CellRangeAddress cra, int currentExternSheetIx) { private static CellRangeAddress shiftRange(FormulaShifter formulaShifter, CellRangeAddress cra, int currentExternSheetIx) {
// FormulaShifter works well in terms of Ptgs - so convert CellRangeAddress to AreaPtg (and back) here // FormulaShifter works well in terms of Ptgs - so convert CellRangeAddress to AreaPtg (and back) here
AreaPtg aptg = new AreaPtg(cra.getFirstRow(), cra.getLastRow(), cra.getFirstColumn(), cra.getLastColumn(), false, false, false, false); AreaPtg aptg = new AreaPtg(cra.getFirstRow(), cra.getLastRow(), cra.getFirstColumn(), cra.getLastColumn(), false, false, false, false);
Ptg[] ptgs = { aptg, }; Ptg[] ptgs = { aptg, };
if (!shifter.adjustFormula(ptgs, currentExternSheetIx)) { if (!formulaShifter.adjustFormula(ptgs, currentExternSheetIx)) {
return cra; return cra;
} }
Ptg ptg0 = ptgs[0]; Ptg ptg0 = ptgs[0];