Search in sources :

Example 66 with Record

use of org.apache.poi.hssf.record.Record in project poi by apache.

the class TestMissingRecordAwareHSSFListener method testMulBlankHandling.

/**
	 * MulBlank records hold multiple blank cells. Check we
	 *  can handle them correctly.
	 */
public void testMulBlankHandling() {
    readRecords("45672.xls");
    // Check that we don't have any MulBlankRecords, but do
    //  have lots of BlankRecords
    Record[] rr = r;
    int eorCount = 0;
    int mbrCount = 0;
    int brCount = 0;
    for (Record record : rr) {
        if (record instanceof MulBlankRecord) {
            mbrCount++;
        }
        if (record instanceof BlankRecord) {
            brCount++;
        }
        if (record instanceof LastCellOfRowDummyRecord) {
            eorCount++;
        }
    }
    if (mbrCount > 0) {
        throw new AssertionFailedError("Identified bug 45672");
    }
    if (brCount < 20) {
        throw new AssertionFailedError("Identified bug 45672");
    }
    if (eorCount != 2) {
        throw new AssertionFailedError("Identified bug 45672");
    }
    assertEquals(2, eorCount);
}
Also used : MulBlankRecord(org.apache.poi.hssf.record.MulBlankRecord) MulBlankRecord(org.apache.poi.hssf.record.MulBlankRecord) BlankRecord(org.apache.poi.hssf.record.BlankRecord) LastCellOfRowDummyRecord(org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord) Record(org.apache.poi.hssf.record.Record) NumberRecord(org.apache.poi.hssf.record.NumberRecord) StringRecord(org.apache.poi.hssf.record.StringRecord) MissingRowDummyRecord(org.apache.poi.hssf.eventusermodel.dummyrecord.MissingRowDummyRecord) MissingCellDummyRecord(org.apache.poi.hssf.eventusermodel.dummyrecord.MissingCellDummyRecord) LastCellOfRowDummyRecord(org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord) LabelSSTRecord(org.apache.poi.hssf.record.LabelSSTRecord) MulBlankRecord(org.apache.poi.hssf.record.MulBlankRecord) RowRecord(org.apache.poi.hssf.record.RowRecord) BOFRecord(org.apache.poi.hssf.record.BOFRecord) SharedFormulaRecord(org.apache.poi.hssf.record.SharedFormulaRecord) DimensionsRecord(org.apache.poi.hssf.record.DimensionsRecord) WindowTwoRecord(org.apache.poi.hssf.record.WindowTwoRecord) BlankRecord(org.apache.poi.hssf.record.BlankRecord) FormulaRecord(org.apache.poi.hssf.record.FormulaRecord) AssertionFailedError(junit.framework.AssertionFailedError)

Example 67 with Record

use of org.apache.poi.hssf.record.Record in project poi by apache.

the class TestMissingRecordAwareHSSFListener method testNoExtraNewLines.

// Make sure we don't put in any extra new lines
//  that aren't already there
public void testNoExtraNewLines() {
    // Load a different file
    // This file has has something in lines 1-33
    readRecords("MRExtraLines.xls");
    int rowCount = 0;
    for (Record rec : r) {
        if (rec instanceof LastCellOfRowDummyRecord) {
            LastCellOfRowDummyRecord eor = (LastCellOfRowDummyRecord) rec;
            assertEquals(rowCount, eor.getRow());
            rowCount++;
        }
    }
    // Check we got the 33 rows
    assertEquals(33, rowCount);
}
Also used : LastCellOfRowDummyRecord(org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord) Record(org.apache.poi.hssf.record.Record) NumberRecord(org.apache.poi.hssf.record.NumberRecord) StringRecord(org.apache.poi.hssf.record.StringRecord) MissingRowDummyRecord(org.apache.poi.hssf.eventusermodel.dummyrecord.MissingRowDummyRecord) MissingCellDummyRecord(org.apache.poi.hssf.eventusermodel.dummyrecord.MissingCellDummyRecord) LastCellOfRowDummyRecord(org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord) LabelSSTRecord(org.apache.poi.hssf.record.LabelSSTRecord) MulBlankRecord(org.apache.poi.hssf.record.MulBlankRecord) RowRecord(org.apache.poi.hssf.record.RowRecord) BOFRecord(org.apache.poi.hssf.record.BOFRecord) SharedFormulaRecord(org.apache.poi.hssf.record.SharedFormulaRecord) DimensionsRecord(org.apache.poi.hssf.record.DimensionsRecord) WindowTwoRecord(org.apache.poi.hssf.record.WindowTwoRecord) BlankRecord(org.apache.poi.hssf.record.BlankRecord) FormulaRecord(org.apache.poi.hssf.record.FormulaRecord)

Example 68 with Record

use of org.apache.poi.hssf.record.Record in project poi by apache.

the class TestMissingRecordAwareHSSFListener method testEndOfRowRecords.

public void testEndOfRowRecords() {
    openNormal();
    // Find the cell at 0,0
    int cell00 = -1;
    for (int i = 0; i < r.length; i++) {
        if (r[i] instanceof LabelSSTRecord) {
            LabelSSTRecord lr = (LabelSSTRecord) r[i];
            if (lr.getRow() == 0 && lr.getColumn() == 0) {
                cell00 = i;
            }
        }
    }
    assertTrue(cell00 > -1);
    // We have rows 0, 1, 2, 20 and 21
    // Row 0 has 1 entry
    // Row 1 has 4 entries
    // Row 2 has 6 entries
    // Row 20 has 5 entries
    // Row 21 has 7 entries
    // Row 22 has 12 entries
    // Row 0
    assertFalse(r[cell00 + 0] instanceof LastCellOfRowDummyRecord);
    assertTrue(r[cell00 + 1] instanceof LastCellOfRowDummyRecord);
    // Row 1
    assertFalse(r[cell00 + 2] instanceof LastCellOfRowDummyRecord);
    assertFalse(r[cell00 + 3] instanceof LastCellOfRowDummyRecord);
    assertFalse(r[cell00 + 4] instanceof LastCellOfRowDummyRecord);
    assertFalse(r[cell00 + 5] instanceof LastCellOfRowDummyRecord);
    assertTrue(r[cell00 + 6] instanceof LastCellOfRowDummyRecord);
    // Row 2
    assertFalse(r[cell00 + 7] instanceof LastCellOfRowDummyRecord);
    assertFalse(r[cell00 + 8] instanceof LastCellOfRowDummyRecord);
    assertFalse(r[cell00 + 9] instanceof LastCellOfRowDummyRecord);
    assertFalse(r[cell00 + 10] instanceof LastCellOfRowDummyRecord);
    assertFalse(r[cell00 + 11] instanceof LastCellOfRowDummyRecord);
    assertFalse(r[cell00 + 12] instanceof LastCellOfRowDummyRecord);
    assertTrue(r[cell00 + 13] instanceof LastCellOfRowDummyRecord);
    // Row 3 -> 19
    assertTrue(r[cell00 + 14] instanceof LastCellOfRowDummyRecord);
    assertTrue(r[cell00 + 15] instanceof LastCellOfRowDummyRecord);
    assertTrue(r[cell00 + 16] instanceof LastCellOfRowDummyRecord);
    assertTrue(r[cell00 + 17] instanceof LastCellOfRowDummyRecord);
    assertTrue(r[cell00 + 18] instanceof LastCellOfRowDummyRecord);
    assertTrue(r[cell00 + 19] instanceof LastCellOfRowDummyRecord);
    assertTrue(r[cell00 + 20] instanceof LastCellOfRowDummyRecord);
    assertTrue(r[cell00 + 21] instanceof LastCellOfRowDummyRecord);
    assertTrue(r[cell00 + 22] instanceof LastCellOfRowDummyRecord);
    assertTrue(r[cell00 + 23] instanceof LastCellOfRowDummyRecord);
    assertTrue(r[cell00 + 24] instanceof LastCellOfRowDummyRecord);
    assertTrue(r[cell00 + 25] instanceof LastCellOfRowDummyRecord);
    assertTrue(r[cell00 + 26] instanceof LastCellOfRowDummyRecord);
    assertTrue(r[cell00 + 27] instanceof LastCellOfRowDummyRecord);
    assertTrue(r[cell00 + 28] instanceof LastCellOfRowDummyRecord);
    assertTrue(r[cell00 + 29] instanceof LastCellOfRowDummyRecord);
    assertTrue(r[cell00 + 30] instanceof LastCellOfRowDummyRecord);
    // Row 20
    assertFalse(r[cell00 + 31] instanceof LastCellOfRowDummyRecord);
    assertFalse(r[cell00 + 32] instanceof LastCellOfRowDummyRecord);
    assertFalse(r[cell00 + 33] instanceof LastCellOfRowDummyRecord);
    assertFalse(r[cell00 + 34] instanceof LastCellOfRowDummyRecord);
    assertFalse(r[cell00 + 35] instanceof LastCellOfRowDummyRecord);
    assertTrue(r[cell00 + 36] instanceof LastCellOfRowDummyRecord);
    // Row 21
    assertFalse(r[cell00 + 37] instanceof LastCellOfRowDummyRecord);
    assertFalse(r[cell00 + 38] instanceof LastCellOfRowDummyRecord);
    assertFalse(r[cell00 + 39] instanceof LastCellOfRowDummyRecord);
    assertFalse(r[cell00 + 40] instanceof LastCellOfRowDummyRecord);
    assertFalse(r[cell00 + 41] instanceof LastCellOfRowDummyRecord);
    assertFalse(r[cell00 + 42] instanceof LastCellOfRowDummyRecord);
    assertFalse(r[cell00 + 43] instanceof LastCellOfRowDummyRecord);
    assertTrue(r[cell00 + 44] instanceof LastCellOfRowDummyRecord);
    // Row 22
    assertFalse(r[cell00 + 45] instanceof LastCellOfRowDummyRecord);
    assertFalse(r[cell00 + 46] instanceof LastCellOfRowDummyRecord);
    assertFalse(r[cell00 + 47] instanceof LastCellOfRowDummyRecord);
    assertFalse(r[cell00 + 48] instanceof LastCellOfRowDummyRecord);
    assertFalse(r[cell00 + 49] instanceof LastCellOfRowDummyRecord);
    assertFalse(r[cell00 + 50] instanceof LastCellOfRowDummyRecord);
    assertFalse(r[cell00 + 51] instanceof LastCellOfRowDummyRecord);
    assertFalse(r[cell00 + 52] instanceof LastCellOfRowDummyRecord);
    assertFalse(r[cell00 + 53] instanceof LastCellOfRowDummyRecord);
    assertFalse(r[cell00 + 54] instanceof LastCellOfRowDummyRecord);
    assertFalse(r[cell00 + 55] instanceof LastCellOfRowDummyRecord);
    assertFalse(r[cell00 + 56] instanceof LastCellOfRowDummyRecord);
    assertTrue(r[cell00 + 57] instanceof LastCellOfRowDummyRecord);
    // Check the numbers of the last seen columns
    LastCellOfRowDummyRecord[] lrs = new LastCellOfRowDummyRecord[24];
    int lrscount = 0;
    for (final Record rec : r) {
        if (rec instanceof LastCellOfRowDummyRecord) {
            lrs[lrscount] = (LastCellOfRowDummyRecord) rec;
            lrscount++;
        }
    }
    assertEquals(0, lrs[0].getLastColumnNumber());
    assertEquals(0, lrs[0].getRow());
    assertEquals(3, lrs[1].getLastColumnNumber());
    assertEquals(1, lrs[1].getRow());
    assertEquals(5, lrs[2].getLastColumnNumber());
    assertEquals(2, lrs[2].getRow());
    for (int i = 3; i <= 19; i++) {
        assertEquals(-1, lrs[i].getLastColumnNumber());
        assertEquals(i, lrs[i].getRow());
    }
    assertEquals(4, lrs[20].getLastColumnNumber());
    assertEquals(20, lrs[20].getRow());
    assertEquals(6, lrs[21].getLastColumnNumber());
    assertEquals(21, lrs[21].getRow());
    assertEquals(11, lrs[22].getLastColumnNumber());
    assertEquals(22, lrs[22].getRow());
}
Also used : LastCellOfRowDummyRecord(org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord) Record(org.apache.poi.hssf.record.Record) NumberRecord(org.apache.poi.hssf.record.NumberRecord) StringRecord(org.apache.poi.hssf.record.StringRecord) MissingRowDummyRecord(org.apache.poi.hssf.eventusermodel.dummyrecord.MissingRowDummyRecord) MissingCellDummyRecord(org.apache.poi.hssf.eventusermodel.dummyrecord.MissingCellDummyRecord) LastCellOfRowDummyRecord(org.apache.poi.hssf.eventusermodel.dummyrecord.LastCellOfRowDummyRecord) LabelSSTRecord(org.apache.poi.hssf.record.LabelSSTRecord) MulBlankRecord(org.apache.poi.hssf.record.MulBlankRecord) RowRecord(org.apache.poi.hssf.record.RowRecord) BOFRecord(org.apache.poi.hssf.record.BOFRecord) SharedFormulaRecord(org.apache.poi.hssf.record.SharedFormulaRecord) DimensionsRecord(org.apache.poi.hssf.record.DimensionsRecord) WindowTwoRecord(org.apache.poi.hssf.record.WindowTwoRecord) BlankRecord(org.apache.poi.hssf.record.BlankRecord) FormulaRecord(org.apache.poi.hssf.record.FormulaRecord) LabelSSTRecord(org.apache.poi.hssf.record.LabelSSTRecord)

Example 69 with Record

use of org.apache.poi.hssf.record.Record in project poi by apache.

the class HSSFWorkbook method encryptBytes.

@SuppressWarnings("resource")
protected void encryptBytes(byte[] buf) {
    int initialOffset = 0;
    FilePassRecord fpr = null;
    for (Record r : workbook.getRecords()) {
        initialOffset += r.getRecordSize();
        if (r instanceof FilePassRecord) {
            fpr = (FilePassRecord) r;
            break;
        }
    }
    if (fpr == null) {
        return;
    }
    // NOSONAR
    LittleEndianByteArrayInputStream plain = new LittleEndianByteArrayInputStream(buf, 0);
    // NOSONAR
    LittleEndianByteArrayOutputStream leos = new LittleEndianByteArrayOutputStream(buf, 0);
    Encryptor enc = fpr.getEncryptionInfo().getEncryptor();
    enc.setChunkSize(Biff8DecryptingStream.RC4_REKEYING_INTERVAL);
    byte[] tmp = new byte[1024];
    try {
        ChunkedCipherOutputStream os = enc.getDataStream(leos, initialOffset);
        int totalBytes = 0;
        while (totalBytes < buf.length) {
            plain.read(tmp, 0, 4);
            final int sid = LittleEndian.getUShort(tmp, 0);
            final int len = LittleEndian.getUShort(tmp, 2);
            boolean isPlain = Biff8DecryptingStream.isNeverEncryptedRecord(sid);
            os.setNextRecordSize(len, isPlain);
            os.writePlain(tmp, 0, 4);
            if (sid == BoundSheetRecord.sid) {
                // special case for the field_1_position_of_BOF (=lbPlyPos) field of
                // the BoundSheet8 record which must be unencrypted
                byte[] bsrBuf = new byte[len];
                plain.readFully(bsrBuf);
                os.writePlain(bsrBuf, 0, 4);
                os.write(bsrBuf, 4, len - 4);
            } else {
                int todo = len;
                while (todo > 0) {
                    int nextLen = Math.min(todo, tmp.length);
                    plain.readFully(tmp, 0, nextLen);
                    if (isPlain) {
                        os.writePlain(tmp, 0, nextLen);
                    } else {
                        os.write(tmp, 0, nextLen);
                    }
                    todo -= nextLen;
                }
            }
            totalBytes += 4 + len;
        }
        os.close();
    } catch (Exception e) {
        throw new EncryptedDocumentException(e);
    }
}
Also used : FilePassRecord(org.apache.poi.hssf.record.FilePassRecord) LittleEndianByteArrayOutputStream(org.apache.poi.util.LittleEndianByteArrayOutputStream) ChunkedCipherOutputStream(org.apache.poi.poifs.crypt.ChunkedCipherOutputStream) EncryptedDocumentException(org.apache.poi.EncryptedDocumentException) LittleEndianByteArrayInputStream(org.apache.poi.util.LittleEndianByteArrayInputStream) UnknownRecord(org.apache.poi.hssf.record.UnknownRecord) RecalcIdRecord(org.apache.poi.hssf.record.RecalcIdRecord) EscherBSERecord(org.apache.poi.ddf.EscherBSERecord) Record(org.apache.poi.hssf.record.Record) AbstractEscherHolderRecord(org.apache.poi.hssf.record.AbstractEscherHolderRecord) BoundSheetRecord(org.apache.poi.hssf.record.BoundSheetRecord) EscherBlipRecord(org.apache.poi.ddf.EscherBlipRecord) DrawingGroupRecord(org.apache.poi.hssf.record.DrawingGroupRecord) BackupRecord(org.apache.poi.hssf.record.BackupRecord) EscherRecord(org.apache.poi.ddf.EscherRecord) NameRecord(org.apache.poi.hssf.record.NameRecord) LabelSSTRecord(org.apache.poi.hssf.record.LabelSSTRecord) LabelRecord(org.apache.poi.hssf.record.LabelRecord) FilePassRecord(org.apache.poi.hssf.record.FilePassRecord) FontRecord(org.apache.poi.hssf.record.FontRecord) SSTRecord(org.apache.poi.hssf.record.SSTRecord) ExtendedFormatRecord(org.apache.poi.hssf.record.ExtendedFormatRecord) Encryptor(org.apache.poi.poifs.crypt.Encryptor) FileNotFoundException(java.io.FileNotFoundException) OldExcelFormatException(org.apache.poi.hssf.OldExcelFormatException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException) EncryptedDocumentException(org.apache.poi.EncryptedDocumentException)

Example 70 with Record

use of org.apache.poi.hssf.record.Record in project poi by apache.

the class TestLinkTable method testMissingExternSheetRecord_bug47001b.

/**
	 * This problem was visible in POI svn r763332
	 * when reading the workbook of attachment 23468 from bugzilla 47001
	 */
public void testMissingExternSheetRecord_bug47001b() {
    Record[] recs = { SupBookRecord.createAddInFunctions(), new SSTRecord() };
    List<Record> recList = Arrays.asList(recs);
    WorkbookRecordList wrl = new WorkbookRecordList();
    LinkTable lt;
    try {
        lt = new LinkTable(recList, 0, wrl, Collections.<String, NameCommentRecord>emptyMap());
    } catch (RuntimeException e) {
        if (e.getMessage().equals("Expected an EXTERNSHEET record but got (org.apache.poi.hssf.record.SSTRecord)")) {
            throw new AssertionFailedError("Identified bug 47001b");
        }
        throw e;
    }
    assertNotNull(lt);
}
Also used : Record(org.apache.poi.hssf.record.Record) SupBookRecord(org.apache.poi.hssf.record.SupBookRecord) SSTRecord(org.apache.poi.hssf.record.SSTRecord) EOFRecord(org.apache.poi.hssf.record.EOFRecord) ExternalNameRecord(org.apache.poi.hssf.record.ExternalNameRecord) NameRecord(org.apache.poi.hssf.record.NameRecord) CountryRecord(org.apache.poi.hssf.record.CountryRecord) NameCommentRecord(org.apache.poi.hssf.record.NameCommentRecord) BOFRecord(org.apache.poi.hssf.record.BOFRecord) ExternSheetRecord(org.apache.poi.hssf.record.ExternSheetRecord) NameCommentRecord(org.apache.poi.hssf.record.NameCommentRecord) AssertionFailedError(junit.framework.AssertionFailedError) SSTRecord(org.apache.poi.hssf.record.SSTRecord)

Aggregations

Record (org.apache.poi.hssf.record.Record)78 EOFRecord (org.apache.poi.hssf.record.EOFRecord)42 BOFRecord (org.apache.poi.hssf.record.BOFRecord)36 WindowTwoRecord (org.apache.poi.hssf.record.WindowTwoRecord)36 FormulaRecord (org.apache.poi.hssf.record.FormulaRecord)30 Test (org.junit.Test)25 DimensionsRecord (org.apache.poi.hssf.record.DimensionsRecord)24 NumberRecord (org.apache.poi.hssf.record.NumberRecord)24 RowRecord (org.apache.poi.hssf.record.RowRecord)24 EscherDggRecord (org.apache.poi.ddf.EscherDggRecord)23 NameRecord (org.apache.poi.hssf.record.NameRecord)23 ArrayList (java.util.ArrayList)21 EscherRecord (org.apache.poi.ddf.EscherRecord)21 BlankRecord (org.apache.poi.hssf.record.BlankRecord)21 DrawingRecord (org.apache.poi.hssf.record.DrawingRecord)21 UnknownRecord (org.apache.poi.hssf.record.UnknownRecord)19 BoundSheetRecord (org.apache.poi.hssf.record.BoundSheetRecord)18 MulBlankRecord (org.apache.poi.hssf.record.MulBlankRecord)18 StringRecord (org.apache.poi.hssf.record.StringRecord)18 NoteRecord (org.apache.poi.hssf.record.NoteRecord)17