use of org.apache.poi.hssf.record.Record in project poi by apache.
the class TestSanityChecker method testCheckRecordOrder.
@Test
public void testCheckRecordOrder() {
final SanityChecker c = new SanityChecker();
List<Record> records = new ArrayList<Record>();
records.add(new BOFRecord());
records.add(INTERFACEHDR);
records.add(createBoundSheetRec());
records.add(EOFRecord.instance);
CheckRecord[] check = { new CheckRecord(BOFRecord.class, '1'), new CheckRecord(InterfaceHdrRecord.class, '0'), new CheckRecord(BoundSheetRecord.class, 'M'), new CheckRecord(NameRecord.class, '*'), new CheckRecord(EOFRecord.class, '1') };
// check pass
c.checkRecordOrder(records, check);
records.add(2, createBoundSheetRec());
c.checkRecordOrder(records, check);
// optional record missing
records.remove(1);
c.checkRecordOrder(records, check);
records.add(3, new NameRecord());
// optional multiple record occurs more than one time
records.add(3, new NameRecord());
c.checkRecordOrder(records, check);
// check fail
confirmBadRecordOrder(check, new Record[] { new BOFRecord(), createBoundSheetRec(), INTERFACEHDR, EOFRecord.instance });
confirmBadRecordOrder(check, new Record[] { new BOFRecord(), INTERFACEHDR, createBoundSheetRec(), INTERFACEHDR, EOFRecord.instance });
confirmBadRecordOrder(check, new Record[] { new BOFRecord(), createBoundSheetRec(), new NameRecord(), EOFRecord.instance, new NameRecord() });
confirmBadRecordOrder(check, new Record[] { INTERFACEHDR, createBoundSheetRec(), EOFRecord.instance });
confirmBadRecordOrder(check, new Record[] { new BOFRecord(), INTERFACEHDR, EOFRecord.instance });
confirmBadRecordOrder(check, new Record[] { INTERFACEHDR, createBoundSheetRec(), new BOFRecord(), EOFRecord.instance });
confirmBadRecordOrder(check, new Record[] { new BOFRecord(), createBoundSheetRec(), INTERFACEHDR, EOFRecord.instance });
}
use of org.apache.poi.hssf.record.Record in project poi by apache.
the class InternalWorkbook method getSize.
public int getSize() {
int retval = 0;
SSTRecord lSST = null;
for (Record record : records) {
if (record instanceof SSTRecord) {
lSST = (SSTRecord) record;
}
if (record.getSid() == ExtSSTRecord.sid && lSST != null) {
retval += lSST.calcExtSSTRecordSize();
} else {
retval += record.getRecordSize();
}
}
return retval;
}
use of org.apache.poi.hssf.record.Record in project poi by apache.
the class InternalWorkbook method createStyleRecord.
/**
* Creates a new StyleRecord, for the given Extended
* Format index, and adds it onto the end of the
* records collection
*
* @param xfIndex the extended format index
*
* @return a new StyleRecord
*/
public StyleRecord createStyleRecord(int xfIndex) {
// Style records always follow after
// the ExtendedFormat records
StyleRecord newSR = new StyleRecord();
newSR.setXFIndex(xfIndex);
// Find the spot
int addAt = -1;
for (int i = records.getXfpos(); i < records.size() && addAt == -1; i++) {
Record r = records.get(i);
if (r instanceof ExtendedFormatRecord || r instanceof StyleRecord) {
// Keep going
} else {
addAt = i;
}
}
if (addAt == -1) {
throw new IllegalStateException("No XF Records found!");
}
records.add(addAt, newSR);
return newSR;
}
use of org.apache.poi.hssf.record.Record in project poi by apache.
the class InternalWorkbook method getCustomPalette.
/**
* Returns the custom palette in use for this workbook; if a custom palette record
* does not exist, then it is created.
*
* @return the custom palette
*/
public PaletteRecord getCustomPalette() {
PaletteRecord palette;
int palettePos = records.getPalettepos();
if (palettePos != -1) {
Record rec = records.get(palettePos);
if (rec instanceof PaletteRecord) {
palette = (PaletteRecord) rec;
} else {
throw new RuntimeException("InternalError: Expected PaletteRecord but got a '" + rec + "'");
}
} else {
palette = createPalette();
//Add the palette record after the bof which is always the first record
records.add(1, palette);
records.setPalettepos(1);
}
return palette;
}
use of org.apache.poi.hssf.record.Record in project poi by apache.
the class InternalWorkbook method createWorkbook.
/**
* read support for low level
* API. Pass in an array of Record objects, A Workbook
* object is constructed and passed back with all of its initialization set
* to the passed in records and references to those records held. Unlike Sheet
* workbook does not use an offset (its assumed to be 0) since its first in a file.
* If you need an offset then construct a new array with a 0 offset or write your
* own ;-p.
*
* @param recs an array of Record objects
* @return Workbook object
*/
public static InternalWorkbook createWorkbook(List<Record> recs) {
LOG.log(DEBUG, "Workbook (readfile) created with reclen=", recs.size());
InternalWorkbook retval = new InternalWorkbook();
List<Record> records = new ArrayList<Record>(recs.size() / 3);
retval.records.setRecords(records);
boolean eofPassed = false;
for (int k = 0; k < recs.size(); k++) {
Record rec = recs.get(k);
String logObj;
switch(rec.getSid()) {
case EOFRecord.sid:
logObj = "workbook eof";
break;
case BoundSheetRecord.sid:
logObj = "boundsheet";
retval.boundsheets.add((BoundSheetRecord) rec);
retval.records.setBspos(k);
break;
case SSTRecord.sid:
logObj = "sst";
retval.sst = (SSTRecord) rec;
break;
case FontRecord.sid:
logObj = "font";
retval.records.setFontpos(k);
retval.numfonts++;
break;
case ExtendedFormatRecord.sid:
logObj = "XF";
retval.records.setXfpos(k);
retval.numxfs++;
break;
case TabIdRecord.sid:
logObj = "tabid";
retval.records.setTabpos(k);
break;
case ProtectRecord.sid:
logObj = "protect";
retval.records.setProtpos(k);
break;
case BackupRecord.sid:
logObj = "backup";
retval.records.setBackuppos(k);
break;
case ExternSheetRecord.sid:
throw new RecordFormatException("Extern sheet is part of LinkTable");
case NameRecord.sid:
case SupBookRecord.sid:
// LinkTable can start with either of these
LOG.log(DEBUG, "found SupBook record at " + k);
retval.linkTable = new LinkTable(recs, k, retval.records, retval.commentRecords);
k += retval.linkTable.getRecordCount() - 1;
continue;
case FormatRecord.sid:
logObj = "format";
FormatRecord fr = (FormatRecord) rec;
retval.formats.add(fr);
retval.maxformatid = retval.maxformatid >= fr.getIndexCode() ? retval.maxformatid : fr.getIndexCode();
break;
case DateWindow1904Record.sid:
logObj = "datewindow1904";
retval.uses1904datewindowing = ((DateWindow1904Record) rec).getWindowing() == 1;
break;
case PaletteRecord.sid:
logObj = "palette";
retval.records.setPalettepos(k);
break;
case WindowOneRecord.sid:
logObj = "WindowOneRecord";
retval.windowOne = (WindowOneRecord) rec;
break;
case WriteAccessRecord.sid:
logObj = "WriteAccess";
retval.writeAccess = (WriteAccessRecord) rec;
break;
case WriteProtectRecord.sid:
logObj = "WriteProtect";
retval.writeProtect = (WriteProtectRecord) rec;
break;
case FileSharingRecord.sid:
logObj = "FileSharing";
retval.fileShare = (FileSharingRecord) rec;
break;
case NameCommentRecord.sid:
logObj = "NameComment";
final NameCommentRecord ncr = (NameCommentRecord) rec;
retval.commentRecords.put(ncr.getNameText(), ncr);
break;
case HyperlinkRecord.sid:
// Look for other interesting values that follow the EOFRecord
logObj = "Hyperlink";
retval.hyperlinks.add((HyperlinkRecord) rec);
break;
default:
logObj = "(sid=" + rec.getSid() + ")";
break;
}
if (!eofPassed) {
records.add(rec);
}
LOG.log(DEBUG, "found " + logObj + " record at " + k);
if (rec.getSid() == EOFRecord.sid) {
eofPassed = true;
}
}
if (retval.windowOne == null) {
retval.windowOne = createWindowOne();
}
LOG.log(DEBUG, "exit create workbook from existing file function");
return retval;
}
Aggregations