import org.apache.poi.hssf.record.pivottable.ViewDefinitionRecord;
import org.apache.poi.hssf.record.pivottable.ViewFieldsRecord;
import org.apache.poi.hssf.record.pivottable.ViewSourceRecord;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.util.HexDump;
import org.apache.poi.util.LittleEndian;
ps = System.out;
}
- POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(cmdArgs.getFile()));
- InputStream is = fs.createDocumentInputStream("Workbook");
+ InputStream is = getPOIFSInputStream(cmdArgs.getFile());
if (cmdArgs.shouldOutputRawHexOnly()) {
int size = is.available();
}
}
+ protected static InputStream getPOIFSInputStream(File file)
+ throws IOException, FileNotFoundException {
+ POIFSFileSystem fs = new POIFSFileSystem(new FileInputStream(file));
+ String workbookName = HSSFWorkbook.getWorkbookDirEntryName(fs.getRoot());
+ return fs.createDocumentInputStream(workbookName);
+ }
+
protected static void runBiffViewer(PrintStream ps, InputStream is,
boolean dumpInterpretedRecords, boolean dumpHex, boolean zeroAlignHexDump,
boolean suppressHeader) {
package org.apache.poi.hssf.dev;
-import java.io.FileInputStream;
-import java.io.InputStream;
+import java.io.File;
import java.io.IOException;
+import java.io.InputStream;
-import org.apache.poi.poifs.filesystem.POIFSFileSystem;
-import org.apache.poi.hssf.record.Record;
-
-import org.apache.poi.hssf.eventusermodel.HSSFRequest;
-import org.apache.poi.hssf.eventusermodel.HSSFListener;
import org.apache.poi.hssf.eventusermodel.HSSFEventFactory;
+import org.apache.poi.hssf.eventusermodel.HSSFListener;
+import org.apache.poi.hssf.eventusermodel.HSSFRequest;
+import org.apache.poi.hssf.record.Record;
/**
*
{
}
- public void run()
- throws IOException
- {
- FileInputStream fin = new FileInputStream(file);
- POIFSFileSystem poifs = new POIFSFileSystem(fin);
- InputStream din = poifs.createDocumentInputStream("Workbook");
+ public void run() throws IOException {
+ InputStream din = BiffViewer.getPOIFSInputStream(new File(file));
HSSFRequest req = new HSSFRequest();
req.addListenerForAllRecords(new HSSFListener()
package org.apache.poi.hssf.dev;
-import java.io.FileInputStream;
+import java.io.File;
import java.util.List;
import org.apache.poi.hssf.model.HSSFFormulaParser;
import org.apache.poi.hssf.record.Record;
import org.apache.poi.hssf.record.RecordFactory;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
-import org.apache.poi.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.ss.formula.ptg.ExpPtg;
import org.apache.poi.ss.formula.ptg.FuncPtg;
import org.apache.poi.ss.formula.ptg.OperationPtg;
public void run()
throws Exception
{
- POIFSFileSystem fs =
- new POIFSFileSystem(new FileInputStream(file));
List<Record> records =
RecordFactory
- .createRecords(fs.createDocumentInputStream("Workbook"));
+ .createRecords(BiffViewer.getPOIFSInputStream(new File(file)));
for (int k = 0; k < records.size(); k++)
{
package org.apache.poi.hssf.dev;
-import java.io.FileInputStream;
+import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import org.apache.poi.hssf.record.Record;
import org.apache.poi.hssf.record.RecordFactory;
import org.apache.poi.hssf.record.RecordInputStream;
-import org.apache.poi.poifs.filesystem.POIFSFileSystem;
/**
* This is a low-level debugging class, which simply prints
public void run()
throws IOException
{
- FileInputStream fin = new FileInputStream(file);
- POIFSFileSystem poifs = new POIFSFileSystem(fin);
- InputStream din = poifs.createDocumentInputStream("Workbook");
+ InputStream din = BiffViewer.getPOIFSInputStream(new File(file));
RecordInputStream rinp = new RecordInputStream(din);
while(rinp.hasNextRecord()) {
package org.apache.poi.hssf.record;
-import org.apache.poi.ss.formula.constant.ConstantValueParser;
import org.apache.poi.ss.formula.Formula;
+import org.apache.poi.ss.formula.constant.ConstantValueParser;
import org.apache.poi.ss.formula.ptg.Ptg;
import org.apache.poi.util.LittleEndianOutput;
import org.apache.poi.util.StringUtil;
if(!isOLELink() && !isStdDocumentNameIdentifier()){
if(isAutomaticLink()){
- result += 3; // byte, short
- result += ConstantValueParser.getEncodedSize(_ddeValues);
+ if(_ddeValues != null) {
+ result += 3; // byte, short
+ result += ConstantValueParser.getEncodedSize(_ddeValues);
+ }
} else {
result += field_5_name_definition.getEncodedSize();
}
if(!isOLELink() && !isStdDocumentNameIdentifier()){
if(isAutomaticLink()){
- out.writeByte(_nColumns-1);
- out.writeShort(_nRows-1);
- ConstantValueParser.encode(out, _ddeValues);
+ if(_ddeValues != null) {
+ out.writeByte(_nColumns-1);
+ out.writeShort(_nRows-1);
+ ConstantValueParser.encode(out, _ddeValues);
+ }
} else {
field_5_name_definition.serialize(out);
}
import org.apache.poi.hssf.model.InternalSheet;
import org.apache.poi.hssf.model.InternalWorkbook;
import org.apache.poi.hssf.model.RecordStream;
-import org.apache.poi.hssf.record.AbstractEscherHolderRecord;
-import org.apache.poi.hssf.record.BackupRecord;
-import org.apache.poi.hssf.record.DrawingGroupRecord;
-import org.apache.poi.hssf.record.ExtendedFormatRecord;
-import org.apache.poi.hssf.record.FontRecord;
-import org.apache.poi.hssf.record.LabelRecord;
-import org.apache.poi.hssf.record.LabelSSTRecord;
-import org.apache.poi.hssf.record.NameRecord;
-import org.apache.poi.hssf.record.RecalcIdRecord;
-import org.apache.poi.hssf.record.Record;
-import org.apache.poi.hssf.record.RecordFactory;
-import org.apache.poi.hssf.record.SSTRecord;
-import org.apache.poi.hssf.record.UnknownRecord;
+import org.apache.poi.hssf.record.*;
import org.apache.poi.hssf.record.aggregates.RecordAggregate.RecordVisitor;
import org.apache.poi.hssf.record.common.UnicodeString;
import org.apache.poi.hssf.util.CellReference;
};
- private static String getWorkbookDirEntryName(DirectoryNode directory) {
+ public static String getWorkbookDirEntryName(DirectoryNode directory) {
- String[] potentialNames = WORKBOOK_DIR_ENTRY_NAMES;
- for (int i = 0; i < potentialNames.length; i++) {
- String wbName = potentialNames[i];
+ for (int i = 0; i < WORKBOOK_DIR_ENTRY_NAMES.length; i++) {
+ String wbName = WORKBOOK_DIR_ENTRY_NAMES[i];
try {
directory.getEntry(wbName);
return wbName;
if (rval)
{
_entries.remove(entry);
- _byname.remove(entry.getName());
+ _byname.remove(entry.getName());
- if(_ofilesystem != null) {
+ if(_ofilesystem != null) {
_ofilesystem.remove(entry);
- } else {
- _nfilesystem.remove(entry);
- }
+ } else {
+ _nfilesystem.remove(entry);
+ }
}
return rval;
}
* name exists in this DirectoryEntry
*/
- public Entry getEntry(final String name)
- throws FileNotFoundException
- {
+ public Entry getEntry(final String name) throws FileNotFoundException {
Entry rval = null;
- if (name != null)
- {
+ if (name != null) {
rval = _byname.get(name);
}
- if (rval == null)
- {
-
+ if (rval == null) {
// either a null name was given, or there is no such name
throw new FileNotFoundException("no such entry: \"" + name
- + "\"");
+ + "\", had: " + _byname.keySet());
}
return rval;
}
* @return true if the Entry is a DirectoryEntry, else false
*/
+ @Override
public boolean isDirectoryEntry()
{
return true;
* false
*/
+ @Override
protected boolean isDeleteOK()
{
* @return an Iterator; may not be null, but may have an empty
* back end store
*/
- @SuppressWarnings("unchecked")
+ @SuppressWarnings({ "unchecked", "rawtypes" })
public Iterator getViewableIterator()
{
List components = new ArrayList();
package org.apache.poi.hssf.dev;
+import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.io.File;
+import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
+import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.junit.Test;
/**
System.out.println("Had " + count + " files");
}
- private int runWithDir(String dir) {
+ private int runWithDir(String dir) throws IOException {
List<String> failed = new ArrayList<String>();
String[] files = new File(dir).list(new FilenameFilter() {
return files.length;
}
- private void runWithArrayOfFiles(String[] files, String dir, List<String> failed) {
+ private void runWithArrayOfFiles(String[] files, String dir, List<String> failed) throws IOException {
for(String file : files) {
try {
runOneFile(dir, file, failed);
}
e.printStackTrace();
+
+ // try to read it in HSSFWorkbook to quickly fail if we cannot read the file there at all and thus probably can use SILENT_EXCLUDED instead
+ FileInputStream stream = new FileInputStream(new File(dir, file));
+ try {
+ assertNotNull(new HSSFWorkbook(stream));
+ } finally {
+ stream.close();
+ }
+
if(!EXCLUDED.contains(file)) {
failed.add(file);
}
package org.apache.poi.hssf.dev;
import java.io.File;
-import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintStream;
import java.util.List;
-import org.apache.poi.poifs.filesystem.POIFSFileSystem;
-
public class TestBiffViewer extends BaseXLSIteratingTest {
static {
- // TODO: is it ok to fail these?
// Look at the output of the test for the detailed stacktrace of the failures...
- EXCLUDED.add("WORKBOOK_in_capitals.xls");
- EXCLUDED.add("password.xls");
- EXCLUDED.add("NoGutsRecords.xls");
- EXCLUDED.add("BOOK_in_capitals.xls");
- EXCLUDED.add("XRefCalc.xls");
- EXCLUDED.add("50833.xls"); // probably a problem in BiffViewer
- EXCLUDED.add("43493.xls");
- EXCLUDED.add("51832.xls");
- EXCLUDED.add("OddStyleRecord.xls");
-
+ //EXCLUDED.add("");
+
+ // these are likely ok to fail
+ SILENT_EXCLUDED.add("XRefCalc.xls"); // "Buffer overrun"
+ SILENT_EXCLUDED.add("50833.xls"); // "Name is too long" when setting username
+ SILENT_EXCLUDED.add("OddStyleRecord.xls");
+ SILENT_EXCLUDED.add("NoGutsRecords.xls");
+ SILENT_EXCLUDED.add("51832.xls"); // password
+ SILENT_EXCLUDED.add("43493.xls"); // HSSFWorkbook cannot open it as well
+ SILENT_EXCLUDED.add("password.xls");
SILENT_EXCLUDED.add("46904.xls");
};
@Override
void runOneFile(String dir, String file, List<String> failed) throws IOException {
- FileInputStream inStream = new FileInputStream(new File(dir, file));
+ InputStream is = BiffViewer.getPOIFSInputStream(new File(dir, file));
try {
- POIFSFileSystem fs = new POIFSFileSystem(inStream);
- InputStream is = fs.createDocumentInputStream("Workbook");
- try {
- // use a NullOutputStream to not write the bytes anywhere for best runtime
- BiffViewer.runBiffViewer(new PrintStream(NULL_OUTPUT_STREAM), is, true, true, true, false);
- } finally {
- is.close();
- }
+ // use a NullOutputStream to not write the bytes anywhere for best runtime
+ BiffViewer.runBiffViewer(new PrintStream(NULL_OUTPUT_STREAM), is, true, true, true, false);
} finally {
- inStream.close();
+ is.close();
}
}
+
+// @Test
+// public void testOneFile() throws Exception {
+// List<String> failed = new ArrayList<String>();
+// runOneFile("test-data/spreadsheet", "WORKBOOK_in_capitals.xls", failed);
+//
+// assertTrue("Expected to have no failed except the ones excluded, but had: " + failed,
+// failed.isEmpty());
+// }
}
public class TestEFBiffViewer extends BaseXLSIteratingTest {
static {
- // TODO: is it ok to fail these?
// Look at the output of the test for the detailed stacktrace of the failures...
- EXCLUDED.add("password.xls");
- EXCLUDED.add("XRefCalc.xls");
- EXCLUDED.add("43493.xls");
- EXCLUDED.add("51832.xls");
+ //EXCLUDED.add("");
+
+ // these are likely ok to fail
+ SILENT_EXCLUDED.add("XRefCalc.xls");
+ SILENT_EXCLUDED.add("password.xls");
+ SILENT_EXCLUDED.add("51832.xls"); // password
+ SILENT_EXCLUDED.add("43493.xls"); // HSSFWorkbook cannot open it as well
};
@Override
package org.apache.poi.hssf.dev;
+import static org.junit.Assert.assertTrue;
+
import java.io.File;
import java.io.PrintStream;
+import java.util.ArrayList;
import java.util.List;
+import org.junit.Test;
+
public class TestReSave extends BaseXLSIteratingTest {
static {
// TODO: is it ok to fail these?
// Look at the output of the test for the detailed stacktrace of the failures...
- EXCLUDED.add("password.xls");
- EXCLUDED.add("43493.xls");
- EXCLUDED.add("51832.xls");
- EXCLUDED.add("49219.xls");
EXCLUDED.add("49931.xls");
+ // these are likely ok to fail
+ SILENT_EXCLUDED.add("password.xls");
+ SILENT_EXCLUDED.add("43493.xls"); // HSSFWorkbook cannot open it as well
SILENT_EXCLUDED.add("46904.xls");
+ SILENT_EXCLUDED.add("51832.xls"); // password
};
@Override
try {
ReSave.main(new String[] { new File(dir, file).getAbsolutePath() });
+
+ // also try BiffViewer on the saved file
+ new TestBiffViewer().runOneFile(dir, file.replace(".xls", "-saved.xls"), failed);
+
try {
// had one case where the re-saved could not be re-saved!
ReSave.main(new String[] { new File(dir, file.replace(".xls", "-saved.xls")).getAbsolutePath() });
System.setOut(save);
}
}
+
+ @Test
+ public void testOneFile() throws Exception {
+ List<String> failed = new ArrayList<String>();
+ runOneFile("test-data/spreadsheet", "49219.xls", failed);
+
+ assertTrue("Expected to have no failed except the ones excluded, but had: " + failed,
+ failed.isEmpty());
+ }
}
static {
// TODO: is it ok to fail these?
// Look at the output of the test for the detailed stacktrace of the failures...
- EXCLUDED.add("WORKBOOK_in_capitals.xls");
- EXCLUDED.add("NoGutsRecords.xls");
- EXCLUDED.add("BOOK_in_capitals.xls");
- EXCLUDED.add("OddStyleRecord.xls");
+ //EXCLUDED.add("");
+ // these are likely ok to fail
SILENT_EXCLUDED.add("46904.xls");
};