mirror of
https://github.com/apache/poi.git
synced 2025-02-08 02:58:18 +00:00
Bug 63294: Add some more methods to allow to use CellType everywhere
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1876948 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
52320e3213
commit
4abe6ef9ef
@ -50,10 +50,10 @@ import org.apache.poi.util.IOUtils;
|
||||
|
||||
/**
|
||||
* A text extractor for old Excel files, which are too old for
|
||||
* HSSFWorkbook to handle. This includes Excel 95, and very old
|
||||
* HSSFWorkbook to handle. This includes Excel 95, and very old
|
||||
* (pre-OLE2) Excel files, such as Excel 4 files.
|
||||
* <p>
|
||||
* Returns much (but not all) of the textual content of the file,
|
||||
* Returns much (but not all) of the textual content of the file,
|
||||
* suitable for indexing by something like Apache Lucene, or used
|
||||
* by Apache Tika, but not really intended for display to the user.
|
||||
* </p>
|
||||
@ -113,7 +113,7 @@ public class OldExcelExtractor implements Closeable {
|
||||
}
|
||||
|
||||
private void open(InputStream biffStream) throws IOException {
|
||||
BufferedInputStream bis = (biffStream instanceof BufferedInputStream)
|
||||
BufferedInputStream bis = (biffStream instanceof BufferedInputStream)
|
||||
? (BufferedInputStream)biffStream
|
||||
: new BufferedInputStream(biffStream, 8);
|
||||
|
||||
@ -150,7 +150,7 @@ public class OldExcelExtractor implements Closeable {
|
||||
if (book == null) {
|
||||
throw new IOException("No Excel 5/95 Book stream found");
|
||||
}
|
||||
|
||||
|
||||
ris = new RecordInputStream(directory.createDocumentInputStream(book));
|
||||
prepare();
|
||||
}
|
||||
@ -165,13 +165,13 @@ public class OldExcelExtractor implements Closeable {
|
||||
System.out.println(extractor.getText());
|
||||
extractor.close();
|
||||
}
|
||||
|
||||
|
||||
private void prepare() {
|
||||
if (! ris.hasNextRecord()) {
|
||||
throw new IllegalArgumentException("File contains no records!");
|
||||
}
|
||||
}
|
||||
ris.nextRecord();
|
||||
|
||||
|
||||
// Work out what version we're dealing with
|
||||
int bofSid = ris.getSid();
|
||||
switch (bofSid) {
|
||||
@ -188,9 +188,9 @@ public class OldExcelExtractor implements Closeable {
|
||||
biffVersion = 5;
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("File does not begin with a BOF, found sid of " + bofSid);
|
||||
throw new IllegalArgumentException("File does not begin with a BOF, found sid of " + bofSid);
|
||||
}
|
||||
|
||||
|
||||
// Get the type
|
||||
BOFRecord bof = new BOFRecord(ris);
|
||||
fileType = bof.getType();
|
||||
@ -198,18 +198,18 @@ public class OldExcelExtractor implements Closeable {
|
||||
|
||||
/**
|
||||
* The Biff version, largely corresponding to the Excel version
|
||||
*
|
||||
*
|
||||
* @return the Biff version
|
||||
*/
|
||||
public int getBiffVersion() {
|
||||
return biffVersion;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* The kind of the file, one of {@link BOFRecord#TYPE_WORKSHEET},
|
||||
* {@link BOFRecord#TYPE_CHART}, {@link BOFRecord#TYPE_EXCEL_4_MACRO}
|
||||
* or {@link BOFRecord#TYPE_WORKSPACE_FILE}
|
||||
*
|
||||
*
|
||||
* @return the file type
|
||||
*/
|
||||
public int getFileType() {
|
||||
@ -219,12 +219,12 @@ public class OldExcelExtractor implements Closeable {
|
||||
/**
|
||||
* Retrieves the text contents of the file, as best we can
|
||||
* for these old file formats
|
||||
*
|
||||
*
|
||||
* @return the text contents of the file
|
||||
*/
|
||||
public String getText() {
|
||||
StringBuilder text = new StringBuilder();
|
||||
|
||||
|
||||
// To track formats and encodings
|
||||
CodepageRecord codepage = null;
|
||||
// TODO track the XFs and Format Strings
|
||||
@ -245,7 +245,7 @@ public class OldExcelExtractor implements Closeable {
|
||||
text.append(shr.getSheetname());
|
||||
text.append('\n');
|
||||
break;
|
||||
|
||||
|
||||
case OldLabelRecord.biff2_sid:
|
||||
case OldLabelRecord.biff345_sid:
|
||||
OldLabelRecord lr = new OldLabelRecord(ris);
|
||||
@ -260,7 +260,7 @@ public class OldExcelExtractor implements Closeable {
|
||||
text.append(sr.getString());
|
||||
text.append('\n');
|
||||
break;
|
||||
|
||||
|
||||
case NumberRecord.sid:
|
||||
NumberRecord nr = new NumberRecord(ris);
|
||||
handleNumericCell(text, nr.getValue());
|
||||
@ -271,12 +271,12 @@ public class OldExcelExtractor implements Closeable {
|
||||
// Biff 2 and 5+ share the same SID, due to a bug...
|
||||
if (biffVersion == 5) {
|
||||
FormulaRecord fr = new FormulaRecord(ris);
|
||||
if (fr.getCachedResultType() == CellType.NUMERIC.getCode()) {
|
||||
if (fr.getCachedResultTypeEnum() == CellType.NUMERIC) {
|
||||
handleNumericCell(text, fr.getValue());
|
||||
}
|
||||
} else {
|
||||
OldFormulaRecord fr = new OldFormulaRecord(ris);
|
||||
if (fr.getCachedResultType() == CellType.NUMERIC.getCode()) {
|
||||
if (fr.getCachedResultTypeEnum() == CellType.NUMERIC) {
|
||||
handleNumericCell(text, fr.getValue());
|
||||
}
|
||||
}
|
||||
@ -285,11 +285,11 @@ public class OldExcelExtractor implements Closeable {
|
||||
RKRecord rr = new RKRecord(ris);
|
||||
handleNumericCell(text, rr.getRKNumber());
|
||||
break;
|
||||
|
||||
|
||||
case CodepageRecord.sid:
|
||||
codepage = new CodepageRecord(ris);
|
||||
break;
|
||||
|
||||
|
||||
default:
|
||||
ris.readFully(IOUtils.safelyAllocate(ris.remaining(), MAX_RECORD_LENGTH));
|
||||
}
|
||||
|
@ -120,6 +120,10 @@ public final class FormulaRecord extends CellRecord {
|
||||
specialCachedValue.getTypeCode() == FormulaSpecialCachedValue.STRING;
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated POI 4.1.3, will be removed in 5.0, use getCachedResultTypeEnum until switch to enum is fully done
|
||||
*/
|
||||
@Deprecated
|
||||
public int getCachedResultType() {
|
||||
if (specialCachedValue == null) {
|
||||
return CellType.NUMERIC.getCode();
|
||||
@ -127,6 +131,18 @@ public final class FormulaRecord extends CellRecord {
|
||||
return specialCachedValue.getValueType();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the type of the cached result
|
||||
* @return A CellType
|
||||
* @since POI 4.1.3
|
||||
*/
|
||||
public CellType getCachedResultTypeEnum() {
|
||||
if (specialCachedValue == null) {
|
||||
return CellType.NUMERIC;
|
||||
}
|
||||
return specialCachedValue.getValueTypeEnum();
|
||||
}
|
||||
|
||||
public boolean getCachedBooleanValue() {
|
||||
return specialCachedValue.getBooleanValue();
|
||||
}
|
||||
|
@ -144,6 +144,10 @@ public final class FormulaSpecialCachedValue implements GenericRecord {
|
||||
return getClass().getName() + '[' + formatValue() + ']';
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated POI 4.1.3, will be removed in 5.0, use getValueTypeEnum until switch to enum is fully done
|
||||
*/
|
||||
@Deprecated
|
||||
public int getValueType() {
|
||||
int typeCode = getTypeCode();
|
||||
switch (typeCode) {
|
||||
@ -158,6 +162,25 @@ public final class FormulaSpecialCachedValue implements GenericRecord {
|
||||
throw new IllegalStateException("Unexpected type id (" + typeCode + ")");
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the type of the cached value
|
||||
* @return A CellType
|
||||
* @since POI 4.1.3
|
||||
*/
|
||||
public CellType getValueTypeEnum() {
|
||||
int typeCode = getTypeCode();
|
||||
switch (typeCode) {
|
||||
case EMPTY: // is this correct?
|
||||
case STRING:
|
||||
return CellType.STRING;
|
||||
case BOOLEAN:
|
||||
return CellType.BOOLEAN;
|
||||
case ERROR_CODE:
|
||||
return CellType.ERROR;
|
||||
}
|
||||
throw new IllegalStateException("Unexpected type id (" + typeCode + ")");
|
||||
}
|
||||
|
||||
public boolean getBooleanValue() {
|
||||
if (getTypeCode() != BOOLEAN) {
|
||||
throw new IllegalStateException("Not a boolean cached value - " + formatValue());
|
||||
|
@ -64,6 +64,10 @@ public final class OldFormulaRecord extends OldCellRecord {
|
||||
field_6_parsed_expr = Formula.read(expression_len, ris, nBytesAvailable);
|
||||
}
|
||||
|
||||
/**
|
||||
* @deprecated POI 4.1.3, will be removed in 5.0, use getCachedResultTypeEnum until switch to enum is fully done
|
||||
*/
|
||||
@Deprecated
|
||||
public int getCachedResultType() {
|
||||
if (specialCachedValue == null) {
|
||||
return CellType.NUMERIC.getCode();
|
||||
@ -71,6 +75,18 @@ public final class OldFormulaRecord extends OldCellRecord {
|
||||
return specialCachedValue.getValueType();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the type of the cached result
|
||||
* @return A CellType
|
||||
* @since POI 4.1.3
|
||||
*/
|
||||
public CellType getCachedResultTypeEnum() {
|
||||
if (specialCachedValue == null) {
|
||||
return CellType.NUMERIC;
|
||||
}
|
||||
return specialCachedValue.getValueTypeEnum();
|
||||
}
|
||||
|
||||
public boolean getCachedBooleanValue() {
|
||||
return specialCachedValue.getBooleanValue();
|
||||
}
|
||||
|
@ -653,7 +653,7 @@ public class HSSFCell extends CellBase {
|
||||
return new IllegalStateException(msg);
|
||||
}
|
||||
private static void checkFormulaCachedValueType(CellType expectedTypeCode, FormulaRecord fr) {
|
||||
CellType cachedValueType = CellType.forInt(fr.getCachedResultType());
|
||||
CellType cachedValueType = fr.getCachedResultTypeEnum();
|
||||
if (cachedValueType != expectedTypeCode) {
|
||||
throw typeMismatch(expectedTypeCode, cachedValueType, true);
|
||||
}
|
||||
@ -879,7 +879,7 @@ public class HSSFCell extends CellBase {
|
||||
}
|
||||
FormulaRecordAggregate fra = ((FormulaRecordAggregate)_record);
|
||||
FormulaRecord fr = fra.getFormulaRecord();
|
||||
switch (CellType.forInt(fr.getCachedResultType())) {
|
||||
switch (fr.getCachedResultTypeEnum()) {
|
||||
case BOOLEAN:
|
||||
return fr.getCachedBooleanValue() ? "TRUE" : "FALSE";
|
||||
case STRING:
|
||||
@ -1174,8 +1174,8 @@ public class HSSFCell extends CellBase {
|
||||
if (_cellType != CellType.FORMULA) {
|
||||
throw new IllegalStateException("Only formula cells have cached results");
|
||||
}
|
||||
int code = ((FormulaRecordAggregate)_record).getFormulaRecord().getCachedResultType();
|
||||
return CellType.forInt(code);
|
||||
|
||||
return ((FormulaRecordAggregate)_record).getFormulaRecord().getCachedResultTypeEnum();
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -85,7 +85,9 @@ public final class TestFormulaRecord {
|
||||
FormulaRecord record = new FormulaRecord(TestcaseRecordInputStream.create(FormulaRecord.sid, formulaByte));
|
||||
assertEquals("Row", 0, record.getRow());
|
||||
assertEquals("Column", 0, record.getColumn());
|
||||
//noinspection deprecation
|
||||
assertEquals(CellType.ERROR.getCode(), record.getCachedResultType());
|
||||
assertEquals(CellType.ERROR, record.getCachedResultTypeEnum());
|
||||
|
||||
byte[] output = record.serialize();
|
||||
assertEquals("Output size", 33, output.length); //includes sid+recordlength
|
||||
|
Loading…
x
Reference in New Issue
Block a user