Search in sources :

Example 11 with RecordCollector

use of org.apache.poi.hssf.usermodel.RecordInspector.RecordCollector in project poi by apache.

the class TestFormulaRecordAggregate method testExtraStringRecord_bug46213.

/**
	 * Sometimes a {@link StringRecord} appears after a {@link FormulaRecord} even though the
	 * formula has evaluated to a text value.  This might be more likely to occur when the formula
	 * <i>can</i> evaluate to a text value.<br/>
	 * Bug 46213 attachment 22874 has such an extra {@link StringRecord} at stream offset 0x5765.
	 * This file seems to open in Excel (2007) with no trouble.  When it is re-saved, Excel omits
	 * the extra record.  POI should do the same.
	 */
public void testExtraStringRecord_bug46213() {
    FormulaRecord fr = new FormulaRecord();
    fr.setValue(2.0);
    StringRecord sr = new StringRecord();
    sr.setString("NA");
    SharedValueManager svm = SharedValueManager.createEmpty();
    FormulaRecordAggregate fra;
    try {
        fra = new FormulaRecordAggregate(fr, sr, svm);
    } catch (RecordFormatException e) {
        if ("String record was  supplied but formula record flag is not  set".equals(e.getMessage())) {
            throw new AssertionFailedError("Identified bug 46213");
        }
        throw e;
    }
    RecordCollector rc = new RecordCollector();
    fra.visitContainedRecords(rc);
    Record[] vraRecs = rc.getRecords();
    assertEquals(1, vraRecs.length);
    assertEquals(fr, vraRecs[0]);
}
Also used : FormulaRecord(org.apache.poi.hssf.record.FormulaRecord) RecordFormatException(org.apache.poi.hssf.record.RecordFormatException) RecordCollector(org.apache.poi.hssf.usermodel.RecordInspector.RecordCollector) Record(org.apache.poi.hssf.record.Record) StringRecord(org.apache.poi.hssf.record.StringRecord) FormulaRecord(org.apache.poi.hssf.record.FormulaRecord) AssertionFailedError(junit.framework.AssertionFailedError) StringRecord(org.apache.poi.hssf.record.StringRecord)

Example 12 with RecordCollector

use of org.apache.poi.hssf.usermodel.RecordInspector.RecordCollector in project poi by apache.

the class TestPageSettingsBlock method testHeaderFooter_bug46840.

/**
	 * Bug 46840 occurred because POI failed to recognise HEADERFOOTER as part of the
	 * {@link PageSettingsBlock}.
	 */
public void testHeaderFooter_bug46840() {
    int rowIx = 5;
    int colIx = 6;
    NumberRecord nr = new NumberRecord();
    nr.setRow(rowIx);
    nr.setColumn((short) colIx);
    nr.setValue(3.0);
    Record[] recs = { BOFRecord.createSheetBOF(), new HeaderRecord("&LSales Figures"), new FooterRecord("&LJanuary"), new HeaderFooterRecord(HexRead.readFromString("9C 08 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 C4 60 00 00 00 00 00 00 00 00")), new DimensionsRecord(), new WindowTwoRecord(), new UserSViewBegin(HexRead.readFromString("ED 77 3B 86 BC 3F 37 4C A9 58 60 23 43 68 54 4B 01 00 00 00 64 00 00 00 40 00 00 00 02 00 00 00 3D 80 04 00 00 00 00 00 00 00 0C 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 F0 3F FF FF 01 00")), new HeaderRecord("&LSales Figures"), new FooterRecord("&LJanuary"), new HeaderFooterRecord(HexRead.readFromString("9C 08 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 C4 60 00 00 00 00 00 00 00 00")), new UserSViewEnd(HexRead.readFromString("01, 00")), EOFRecord.instance };
    RecordStream rs = new RecordStream(Arrays.asList(recs), 0);
    InternalSheet sheet;
    try {
        sheet = InternalSheet.createSheet(rs);
    } catch (RuntimeException e) {
        if (e.getMessage().equals("two Page Settings Blocks found in the same sheet")) {
            throw new AssertionFailedError("Identified bug 46480");
        }
        throw e;
    }
    RecordCollector rv = new RecordCollector();
    sheet.visitContainedRecords(rv, rowIx);
    Record[] outRecs = rv.getRecords();
    assertEquals(13, outRecs.length);
}
Also used : RecordCollector(org.apache.poi.hssf.usermodel.RecordInspector.RecordCollector) RecordStream(org.apache.poi.hssf.model.RecordStream) InternalSheet(org.apache.poi.hssf.model.InternalSheet) AssertionFailedError(junit.framework.AssertionFailedError)

Example 13 with RecordCollector

use of org.apache.poi.hssf.usermodel.RecordInspector.RecordCollector in project poi by apache.

the class TestPageSettingsBlock method testDuplicatePLS_bug47415.

/**
	 * Apparently it's OK to have more than one PLS record.
	 * Attachment 23866 from bug 47415 had a PageSettingsBlock with two PLS records.  This file
	 * seems to open OK in Excel(2007) but both PLS records are removed (perhaps because the
	 * specified printers were not available on the testing machine).  Since the example file does
	 * not upset Excel, POI will preserve multiple PLS records.</p>
	 *
	 * As of June 2009, PLS is still uninterpreted by POI
	 */
public void testDuplicatePLS_bug47415() {
    Record plsA = ur(UnknownRecord.PLS_004D, "BA AD F0 0D");
    Record plsB = ur(UnknownRecord.PLS_004D, "DE AD BE EF");
    Record contB1 = new ContinueRecord(HexRead.readFromString("FE ED"));
    Record contB2 = new ContinueRecord(HexRead.readFromString("FA CE"));
    Record[] recs = { new HeaderRecord("&LSales Figures"), new FooterRecord("&LInventory"), new HCenterRecord(), new VCenterRecord(), plsA, // make sure continuing PLS is still OK
    plsB, // make sure continuing PLS is still OK
    contB1, // make sure continuing PLS is still OK
    contB2 };
    RecordStream rs = new RecordStream(Arrays.asList(recs), 0);
    PageSettingsBlock psb;
    try {
        psb = new PageSettingsBlock(rs);
    } catch (org.apache.poi.util.RecordFormatException e) {
        if ("Duplicate PageSettingsBlock record (sid=0x4d)".equals(e.getMessage())) {
            throw new AssertionFailedError("Identified bug 47415");
        }
        throw e;
    }
    // serialize the PSB to see what records come out
    RecordCollector rc = new RecordCollector();
    psb.visitContainedRecords(rc);
    Record[] outRecs = rc.getRecords();
    // records were assembled in standard order, so this simple check is OK
    assertArrayEquals(recs, outRecs);
}
Also used : RecordCollector(org.apache.poi.hssf.usermodel.RecordInspector.RecordCollector) RecordStream(org.apache.poi.hssf.model.RecordStream) AssertionFailedError(junit.framework.AssertionFailedError)

Example 14 with RecordCollector

use of org.apache.poi.hssf.usermodel.RecordInspector.RecordCollector in project poi by apache.

the class TestSheet method testMissingDims.

/**
	 * Some apps seem to write files with missing DIMENSION records.
	 * Excel(2007) tolerates this, so POI should too.
	 */
@Test
public void testMissingDims() {
    int rowIx = 5;
    int colIx = 6;
    NumberRecord nr = new NumberRecord();
    nr.setRow(rowIx);
    nr.setColumn((short) colIx);
    nr.setValue(3.0);
    List<Record> inRecs = new ArrayList<Record>();
    inRecs.add(BOFRecord.createSheetBOF());
    inRecs.add(new RowRecord(rowIx));
    inRecs.add(nr);
    inRecs.add(createWindow2Record());
    inRecs.add(EOFRecord.instance);
    InternalSheet sheet;
    try {
        sheet = createSheet(inRecs);
    } catch (RuntimeException e) {
        if ("DimensionsRecord was not found".equals(e.getMessage())) {
            throw new AssertionFailedError("Identified bug 46206");
        }
        throw e;
    }
    RecordCollector rv = new RecordCollector();
    sheet.visitContainedRecords(rv, rowIx);
    Record[] outRecs = rv.getRecords();
    assertEquals(8, outRecs.length);
    DimensionsRecord dims = (DimensionsRecord) outRecs[5];
    assertEquals(rowIx, dims.getFirstRow());
    assertEquals(rowIx, dims.getLastRow());
    assertEquals(colIx, dims.getFirstCol());
    assertEquals(colIx, dims.getLastCol());
}
Also used : RecordCollector(org.apache.poi.hssf.usermodel.RecordInspector.RecordCollector) RowRecord(org.apache.poi.hssf.record.RowRecord) ArrayList(java.util.ArrayList) StringRecord(org.apache.poi.hssf.record.StringRecord) GutsRecord(org.apache.poi.hssf.record.GutsRecord) MergeCellsRecord(org.apache.poi.hssf.record.MergeCellsRecord) DrawingRecord(org.apache.poi.hssf.record.DrawingRecord) NoteRecord(org.apache.poi.hssf.record.NoteRecord) Record(org.apache.poi.hssf.record.Record) ObjRecord(org.apache.poi.hssf.record.ObjRecord) EOFRecord(org.apache.poi.hssf.record.EOFRecord) RowRecord(org.apache.poi.hssf.record.RowRecord) ColumnInfoRecord(org.apache.poi.hssf.record.ColumnInfoRecord) WindowTwoRecord(org.apache.poi.hssf.record.WindowTwoRecord) EscherDggRecord(org.apache.poi.ddf.EscherDggRecord) TextObjectRecord(org.apache.poi.hssf.record.TextObjectRecord) IndexRecord(org.apache.poi.hssf.record.IndexRecord) BOFRecord(org.apache.poi.hssf.record.BOFRecord) NumberRecord(org.apache.poi.hssf.record.NumberRecord) MulBlankRecord(org.apache.poi.hssf.record.MulBlankRecord) UncalcedRecord(org.apache.poi.hssf.record.UncalcedRecord) DimensionsRecord(org.apache.poi.hssf.record.DimensionsRecord) BlankRecord(org.apache.poi.hssf.record.BlankRecord) FormulaRecord(org.apache.poi.hssf.record.FormulaRecord) DimensionsRecord(org.apache.poi.hssf.record.DimensionsRecord) NumberRecord(org.apache.poi.hssf.record.NumberRecord) AssertionFailedError(junit.framework.AssertionFailedError) Test(org.junit.Test)

Aggregations

RecordCollector (org.apache.poi.hssf.usermodel.RecordInspector.RecordCollector)14 AssertionFailedError (junit.framework.AssertionFailedError)10 RecordStream (org.apache.poi.hssf.model.RecordStream)8 InternalSheet (org.apache.poi.hssf.model.InternalSheet)5 Record (org.apache.poi.hssf.record.Record)5 Test (org.junit.Test)5 FormulaRecord (org.apache.poi.hssf.record.FormulaRecord)4 DimensionsRecord (org.apache.poi.hssf.record.DimensionsRecord)3 NumberRecord (org.apache.poi.hssf.record.NumberRecord)3 ObjRecord (org.apache.poi.hssf.record.ObjRecord)3 RowRecord (org.apache.poi.hssf.record.RowRecord)3 StringRecord (org.apache.poi.hssf.record.StringRecord)3 WindowTwoRecord (org.apache.poi.hssf.record.WindowTwoRecord)3 EscherDggRecord (org.apache.poi.ddf.EscherDggRecord)2 BOFRecord (org.apache.poi.hssf.record.BOFRecord)2 BlankRecord (org.apache.poi.hssf.record.BlankRecord)2 ColumnInfoRecord (org.apache.poi.hssf.record.ColumnInfoRecord)2 DrawingRecord (org.apache.poi.hssf.record.DrawingRecord)2 EOFRecord (org.apache.poi.hssf.record.EOFRecord)2 GutsRecord (org.apache.poi.hssf.record.GutsRecord)2