Fix inconsistent indents

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1611947 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2014-07-19 19:11:54 +00:00
parent c392b4fa72
commit 0986f32d46
2 changed files with 127 additions and 128 deletions

View File

@ -25,36 +25,35 @@ import org.apache.poi.ss.formula.SheetNameFormatter;
* @author Josh Micich * @author Josh Micich
*/ */
final class ExternSheetNameResolver { final class ExternSheetNameResolver {
private ExternSheetNameResolver() {
// no instances of this class
}
private ExternSheetNameResolver() { public static String prependSheetName(FormulaRenderingWorkbook book, int field_1_index_extern_sheet, String cellRefText) {
// no instances of this class ExternalSheet externalSheet = book.getExternalSheet(field_1_index_extern_sheet);
} StringBuffer sb;
if (externalSheet != null) {
public static String prependSheetName(FormulaRenderingWorkbook book, int field_1_index_extern_sheet, String cellRefText) { String wbName = externalSheet.getWorkbookName();
ExternalSheet externalSheet = book.getExternalSheet(field_1_index_extern_sheet); String sheetName = externalSheet.getSheetName();
StringBuffer sb; if (wbName != null) {
if (externalSheet != null) { sb = new StringBuffer(wbName.length() + sheetName.length() + cellRefText.length() + 4);
String wbName = externalSheet.getWorkbookName(); SheetNameFormatter.appendFormat(sb, wbName, sheetName);
String sheetName = externalSheet.getSheetName(); } else {
if (wbName != null) {
sb = new StringBuffer(wbName.length() + sheetName.length() + cellRefText.length() + 4);
SheetNameFormatter.appendFormat(sb, wbName, sheetName);
} else {
sb = new StringBuffer(sheetName.length() + cellRefText.length() + 4); sb = new StringBuffer(sheetName.length() + cellRefText.length() + 4);
SheetNameFormatter.appendFormat(sb, sheetName); SheetNameFormatter.appendFormat(sb, sheetName);
} }
} else { } else {
String sheetName = book.getSheetNameByExternSheet(field_1_index_extern_sheet); String sheetName = book.getSheetNameByExternSheet(field_1_index_extern_sheet);
sb = new StringBuffer(sheetName.length() + cellRefText.length() + 4); sb = new StringBuffer(sheetName.length() + cellRefText.length() + 4);
if (sheetName.length() < 1) { if (sheetName.length() < 1) {
// What excel does if sheet has been deleted // What excel does if sheet has been deleted
sb.append("#REF"); // note - '!' added just once below sb.append("#REF"); // note - '!' added just once below
} else { } else {
SheetNameFormatter.appendFormat(sb, sheetName); SheetNameFormatter.appendFormat(sb, sheetName);
} }
} }
sb.append('!'); sb.append('!');
sb.append(cellRefText); sb.append(cellRefText);
return sb.toString(); return sb.toString();
} }
} }

View File

@ -39,130 +39,130 @@ import org.apache.poi.ss.formula.ptg.Ref3DPtg;
* Tests for {@link EventWorkbookBuilder} * Tests for {@link EventWorkbookBuilder}
*/ */
public final class TestEventWorkbookBuilder extends TestCase { public final class TestEventWorkbookBuilder extends TestCase {
private MockHSSFListener mockListen; private MockHSSFListener mockListen;
private SheetRecordCollectingListener listener; private SheetRecordCollectingListener listener;
@Override @Override
public void setUp() { public void setUp() {
HSSFRequest req = new HSSFRequest(); HSSFRequest req = new HSSFRequest();
mockListen = new MockHSSFListener(); mockListen = new MockHSSFListener();
listener = new SheetRecordCollectingListener(mockListen); listener = new SheetRecordCollectingListener(mockListen);
req.addListenerForAllRecords(listener); req.addListenerForAllRecords(listener);
HSSFEventFactory factory = new HSSFEventFactory(); HSSFEventFactory factory = new HSSFEventFactory();
try { try {
InputStream is = HSSFTestDataSamples.openSampleFileStream("3dFormulas.xls"); InputStream is = HSSFTestDataSamples.openSampleFileStream("3dFormulas.xls");
POIFSFileSystem fs = new POIFSFileSystem(is); POIFSFileSystem fs = new POIFSFileSystem(is);
factory.processWorkbookEvents(req, fs); factory.processWorkbookEvents(req, fs);
} catch (IOException e) { } catch (IOException e) {
throw new RuntimeException(e); throw new RuntimeException(e);
} }
} }
public void testBasics() { public void testBasics() {
assertNotNull(listener.getSSTRecord()); assertNotNull(listener.getSSTRecord());
assertNotNull(listener.getBoundSheetRecords()); assertNotNull(listener.getBoundSheetRecords());
assertNotNull(listener.getExternSheetRecords()); assertNotNull(listener.getExternSheetRecords());
} }
public void testGetStubWorkbooks() { public void testGetStubWorkbooks() {
assertNotNull(listener.getStubWorkbook()); assertNotNull(listener.getStubWorkbook());
assertNotNull(listener.getStubHSSFWorkbook()); assertNotNull(listener.getStubHSSFWorkbook());
} }
public void testContents() { public void testContents() {
assertEquals(2, listener.getSSTRecord().getNumStrings()); assertEquals(2, listener.getSSTRecord().getNumStrings());
assertEquals(3, listener.getBoundSheetRecords().length); assertEquals(3, listener.getBoundSheetRecords().length);
assertEquals(1, listener.getExternSheetRecords().length); assertEquals(1, listener.getExternSheetRecords().length);
assertEquals(3, listener.getStubWorkbook().getNumSheets()); assertEquals(3, listener.getStubWorkbook().getNumSheets());
InternalWorkbook ref = listener.getStubWorkbook(); InternalWorkbook ref = listener.getStubWorkbook();
assertEquals("Sh3", ref.findSheetNameFromExternSheet(0)); assertEquals("Sh3", ref.findSheetNameFromExternSheet(0));
assertEquals("Sheet1", ref.findSheetNameFromExternSheet(1)); assertEquals("Sheet1", ref.findSheetNameFromExternSheet(1));
assertEquals("S2", ref.findSheetNameFromExternSheet(2)); assertEquals("S2", ref.findSheetNameFromExternSheet(2));
} }
public void testFormulas() { public void testFormulas() {
FormulaRecord[] fRecs = mockListen.getFormulaRecords(); FormulaRecord[] fRecs = mockListen.getFormulaRecords();
// Check our formula records // Check our formula records
assertEquals(6, fRecs.length); assertEquals(6, fRecs.length);
InternalWorkbook stubWB = listener.getStubWorkbook(); InternalWorkbook stubWB = listener.getStubWorkbook();
assertNotNull(stubWB); assertNotNull(stubWB);
HSSFWorkbook stubHSSF = listener.getStubHSSFWorkbook(); HSSFWorkbook stubHSSF = listener.getStubHSSFWorkbook();
assertNotNull(stubHSSF); assertNotNull(stubHSSF);
// Check these stubs have the right stuff on them // Check these stubs have the right stuff on them
assertEquals("Sheet1", stubWB.getSheetName(0)); assertEquals("Sheet1", stubWB.getSheetName(0));
assertEquals("Sheet1", stubHSSF.getSheetName(0)); assertEquals("Sheet1", stubHSSF.getSheetName(0));
assertEquals("S2", stubWB.getSheetName(1)); assertEquals("S2", stubWB.getSheetName(1));
assertEquals("S2", stubHSSF.getSheetName(1)); assertEquals("S2", stubHSSF.getSheetName(1));
assertEquals("Sh3", stubWB.getSheetName(2)); assertEquals("Sh3", stubWB.getSheetName(2));
assertEquals("Sh3", stubHSSF.getSheetName(2)); assertEquals("Sh3", stubHSSF.getSheetName(2));
// Check we can get the formula without breaking // Check we can get the formula without breaking
for(int i=0; i<fRecs.length; i++) { for(int i=0; i<fRecs.length; i++) {
HSSFFormulaParser.toFormulaString(stubHSSF, fRecs[i].getParsedExpression()); HSSFFormulaParser.toFormulaString(stubHSSF, fRecs[i].getParsedExpression());
} }
// Peer into just one formula, and check that // Peer into just one formula, and check that
// all the ptgs give back the right things // all the ptgs give back the right things
Ptg[] ptgs = fRecs[0].getParsedExpression(); Ptg[] ptgs = fRecs[0].getParsedExpression();
assertEquals(1, ptgs.length); assertEquals(1, ptgs.length);
assertTrue(ptgs[0] instanceof Ref3DPtg); assertTrue(ptgs[0] instanceof Ref3DPtg);
Ref3DPtg ptg = (Ref3DPtg)ptgs[0]; Ref3DPtg ptg = (Ref3DPtg)ptgs[0];
HSSFEvaluationWorkbook book = HSSFEvaluationWorkbook.create(stubHSSF); HSSFEvaluationWorkbook book = HSSFEvaluationWorkbook.create(stubHSSF);
assertEquals("Sheet1!A1", ptg.toFormulaString(book)); assertEquals("Sheet1!A1", ptg.toFormulaString(book));
// Now check we get the right formula back for // Now check we get the right formula back for
// a few sample ones // a few sample ones
FormulaRecord fr; FormulaRecord fr;
// Sheet 1 A2 is on same sheet // Sheet 1 A2 is on same sheet
fr = fRecs[0]; fr = fRecs[0];
assertEquals(1, fr.getRow()); assertEquals(1, fr.getRow());
assertEquals(0, fr.getColumn()); assertEquals(0, fr.getColumn());
assertEquals("Sheet1!A1", HSSFFormulaParser.toFormulaString(stubHSSF, fr.getParsedExpression())); assertEquals("Sheet1!A1", HSSFFormulaParser.toFormulaString(stubHSSF, fr.getParsedExpression()));
// Sheet 1 A5 is to another sheet // Sheet 1 A5 is to another sheet
fr = fRecs[3]; fr = fRecs[3];
assertEquals(4, fr.getRow()); assertEquals(4, fr.getRow());
assertEquals(0, fr.getColumn()); assertEquals(0, fr.getColumn());
assertEquals("'S2'!A1", HSSFFormulaParser.toFormulaString(stubHSSF, fr.getParsedExpression())); assertEquals("'S2'!A1", HSSFFormulaParser.toFormulaString(stubHSSF, fr.getParsedExpression()));
// Sheet 1 A7 is to another sheet, range // Sheet 1 A7 is to another sheet, range
fr = fRecs[5]; fr = fRecs[5];
assertEquals(6, fr.getRow()); assertEquals(6, fr.getRow());
assertEquals(0, fr.getColumn()); assertEquals(0, fr.getColumn());
assertEquals("SUM(Sh3!A1:A4)", HSSFFormulaParser.toFormulaString(stubHSSF, fr.getParsedExpression())); assertEquals("SUM(Sh3!A1:A4)", HSSFFormulaParser.toFormulaString(stubHSSF, fr.getParsedExpression()));
// Now, load via Usermodel and re-check // Now, load via Usermodel and re-check
HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("3dFormulas.xls"); HSSFWorkbook wb = HSSFTestDataSamples.openSampleWorkbook("3dFormulas.xls");
assertEquals("Sheet1!A1", wb.getSheetAt(0).getRow(1).getCell(0).getCellFormula()); assertEquals("Sheet1!A1", wb.getSheetAt(0).getRow(1).getCell(0).getCellFormula());
assertEquals("SUM(Sh3!A1:A4)", wb.getSheetAt(0).getRow(6).getCell(0).getCellFormula()); assertEquals("SUM(Sh3!A1:A4)", wb.getSheetAt(0).getRow(6).getCell(0).getCellFormula());
} }
private static final class MockHSSFListener implements HSSFListener { private static final class MockHSSFListener implements HSSFListener {
public MockHSSFListener() {} public MockHSSFListener() {}
private final List<Record> _records = new ArrayList<Record>(); private final List<Record> _records = new ArrayList<Record>();
private final List<Record> _frecs = new ArrayList<Record>(); private final List<Record> _frecs = new ArrayList<Record>();
public void processRecord(Record record) { public void processRecord(Record record) {
_records.add(record); _records.add(record);
if(record instanceof FormulaRecord) { if(record instanceof FormulaRecord) {
_frecs.add(record); _frecs.add(record);
} }
} }
public FormulaRecord[] getFormulaRecords() { public FormulaRecord[] getFormulaRecords() {
FormulaRecord[] result = new FormulaRecord[_frecs.size()]; FormulaRecord[] result = new FormulaRecord[_frecs.size()];
_frecs.toArray(result); _frecs.toArray(result);
return result; return result;
} }
} }
} }