\r
package org.apache.poi.hssf.dev;\r
\r
+import java.io.FileInputStream;\r
+import java.io.FileOutputStream;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.io.OutputStream;\r
+import java.lang.reflect.Field;\r
+import java.util.ArrayList;\r
+import java.util.List;\r
+\r
import org.apache.poi.ddf.EscherRecord;\r
import org.apache.poi.hssf.model.InternalWorkbook;\r
-import org.apache.poi.hssf.record.*;\r
+import org.apache.poi.hssf.record.DrawingGroupRecord;\r
+import org.apache.poi.hssf.record.EscherAggregate;\r
import org.apache.poi.hssf.usermodel.HSSFPatriarch;\r
import org.apache.poi.hssf.usermodel.HSSFWorkbook;\r
import org.apache.poi.poifs.filesystem.POIFSFileSystem;\r
\r
-import java.io.*;\r
-import java.lang.reflect.Field;\r
-import java.util.ArrayList;\r
-import java.util.List;\r
-\r
/**\r
* Utility for representing drawings contained in a binary Excel file as a XML tree\r
*\r
outputStream.close();\r
}\r
\r
- public static void writeToFile(FileOutputStream fos, InputStream xlsWorkbook, boolean excludeWorkbookRecords, String[] params) throws IOException {\r
+ public static void writeToFile(OutputStream fos, InputStream xlsWorkbook, boolean excludeWorkbookRecords, String[] params) throws IOException {\r
POIFSFileSystem fs = new POIFSFileSystem(xlsWorkbook);\r
HSSFWorkbook workbook = new HSSFWorkbook(fs);\r
InternalWorkbook internalWorkbook = getInternalWorkbook(workbook);\r
package org.apache.poi.hssf.dev;
-import java.io.DataInputStream;
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.OutputStream;
-import java.io.OutputStreamWriter;
-import java.io.PrintStream;
-import java.io.Writer;
+import java.io.*;
import java.util.ArrayList;
import java.util.List;
import org.apache.poi.hssf.record.*;
import org.apache.poi.hssf.record.RecordInputStream.LeftoverDataException;
import org.apache.poi.hssf.record.chart.*;
-import org.apache.poi.hssf.record.pivottable.*;
+import org.apache.poi.hssf.record.pivottable.DataItemRecord;
+import org.apache.poi.hssf.record.pivottable.ExtendedPivotTableViewFieldsRecord;
+import org.apache.poi.hssf.record.pivottable.PageItemRecord;
+import org.apache.poi.hssf.record.pivottable.StreamIDRecord;
+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.poifs.filesystem.POIFSFileSystem;
import org.apache.poi.util.HexDump;
import org.apache.poi.util.LittleEndian;
boolean dumpInterpretedRecords = cmdArgs.shouldDumpRecordInterpretations();
boolean dumpHex = cmdArgs.shouldDumpBiffHex();
boolean zeroAlignHexDump = dumpInterpretedRecords; // TODO - fix non-zeroAlign
- BiffRecordListener recListener = new BiffRecordListener(dumpHex ? new OutputStreamWriter(ps) : null, zeroAlignHexDump, cmdArgs.suppressHeader());
- is = new BiffDumpingStream(is, recListener);
- createRecords(is, ps, recListener, dumpInterpretedRecords);
+ runBiffViewer(ps, is, dumpInterpretedRecords, dumpHex, zeroAlignHexDump,
+ cmdArgs.suppressHeader());
}
ps.close();
} catch (Exception e) {
}
}
+ protected static void runBiffViewer(PrintStream ps, InputStream is,
+ boolean dumpInterpretedRecords, boolean dumpHex, boolean zeroAlignHexDump,
+ boolean suppressHeader) {
+ BiffRecordListener recListener = new BiffRecordListener(dumpHex ? new OutputStreamWriter(ps) : null, zeroAlignHexDump, suppressHeader);
+ is = new BiffDumpingStream(is, recListener);
+ createRecords(is, ps, recListener, dumpInterpretedRecords);
+ }
+
private static final class BiffRecordListener implements IBiffRecordListener {
private final Writer _hexDumpWriter;
private final List<String> _headers;
_currentPos = 0;
}
+ @Override
public int read() throws IOException {
if (_currentPos >= _currentSize) {
fillNextBuffer();
formatBufferIfAtEndOfRec();
return result;
}
+ @Override
public int read(byte[] b, int off, int len) throws IOException {
if (_currentPos >= _currentSize) {
fillNextBuffer();
return result;
}
+ @Override
public int available() throws IOException {
return _currentSize - _currentPos + _is.available();
}
int globalOffset = _overallStreamPos-_currentSize;
_listener.processRecord(globalOffset, _recordCounter, sid, dataSize, _data);
}
+ @Override
public void close() throws IOException {
_is.close();
}
import org.apache.poi.hssf.record.FormulaRecord;
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;
import org.apache.poi.ss.formula.ptg.Ptg;
-import org.apache.poi.hssf.usermodel.HSSFWorkbook;
-import org.apache.poi.poifs.filesystem.POIFSFileSystem;
/**
* FormulaViewer - finds formulas in a BIFF8 file and attempts to read them/display
for (int k = 0; k < records.size(); k++)
{
- Record record = ( Record ) records.get(k);
+ Record record = records.get(k);
if (record.getSid() == FormulaRecord.sid)
{
return true;
}
+ @Override
public String toString() {
StringBuffer sb = new StringBuffer();
sb.append("[OBJ]\n");
- for (int i = 0; i < subrecords.size(); i++) {
- SubRecord record = subrecords.get(i);
- sb.append("SUBRECORD: ").append(record.toString());
+ if(subrecords != null) { // there are special cases where this can be, see comments in constructor above
+ for (int i = 0; i < subrecords.size(); i++) {
+ SubRecord record = subrecords.get(i);
+ sb.append("SUBRECORD: ").append(record.toString());
+ }
}
sb.append("[/OBJ]\n");
return sb.toString();
}
+ @Override
public int getRecordSize() {
if (_uninterpretedData != null) {
return _uninterpretedData.length + 4;
return size + 4;
}
+ @Override
public int serialize(int offset, byte[] data) {
int recSize = getRecordSize();
int dataSize = recSize - 4;
return recSize;
}
+ @Override
public short getSid() {
return sid;
}
return subrecords.add(o);
}
+ @Override
public Object clone() {
ObjRecord rec = new ObjRecord();
buffer.append(" .wOffset =").append(HexDump.shortToHex(wOffset)).append('\n');
buffer.append(" .at =").append(HexDump.shortToHex(at)).append('\n');
buffer.append(" .grbit =").append(HexDump.shortToHex(grbit)).append('\n');
- buffer.append(" .unused =").append(HexDump.shortToHex(unused)).append('\n');
+ if(unused != null)
+ buffer.append(" .unused =").append(HexDump.shortToHex(unused)).append('\n');
buffer.append("[/CATLAB]\n");
return buffer.toString();
--- /dev/null
+package org.apache.poi.hssf.dev;
+
+import static org.junit.Assert.assertTrue;
+
+import java.io.File;
+import java.io.FilenameFilter;
+import java.io.IOException;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.junit.Test;
+
+/**
+ * Base class for integration-style tests which iterate over all test-files
+ * and execute the same action to find out if any change breaks these applications.
+ */
+public abstract class BaseXLSIteratingTest {
+ protected static final OutputStream NULL_OUTPUT_STREAM = new NullOutputStream();
+
+ protected static final List<String> EXCLUDED = new ArrayList<String>();
+ protected static final List<String> SILENT_EXCLUDED = new ArrayList<String>();
+
+ @Test
+ public void testMain() throws Exception {
+ int count = runWithDir("test-data/spreadsheet");
+ count += runWithDir("test-data/hpsf");
+
+ System.out.println("Had " + count + " files");
+ }
+
+ private int runWithDir(String dir) {
+ List<String> failed = new ArrayList<String>();
+
+ String[] files = new File(dir).list(new FilenameFilter() {
+ public boolean accept(File arg0, String arg1) {
+ return arg1.toLowerCase().endsWith(".xls");
+ }
+ });
+
+ runWithArrayOfFiles(files, dir, failed);
+
+ assertTrue("Expected to have no failed except the ones excluded, but had: " + failed,
+ failed.isEmpty());
+
+ return files.length;
+ }
+
+ private void runWithArrayOfFiles(String[] files, String dir, List<String> failed) {
+ for(String file : files) {
+ try {
+ runOneFile(dir, file, failed);
+ } catch (Exception e) {
+ System.out.println("Failed: " + file);
+ if(SILENT_EXCLUDED.contains(file)) {
+ continue;
+ }
+
+ e.printStackTrace();
+ if(!EXCLUDED.contains(file)) {
+ failed.add(file);
+ }
+ }
+ }
+ }
+
+ abstract void runOneFile(String dir, String file, List<String> failed) throws Exception;
+
+ /**
+ * Implementation of an OutputStream which does nothing, used
+ * to redirect stdout to avoid spamming the console with output
+ */
+ private static class NullOutputStream extends OutputStream {
+ @Override
+ public void write(byte[] b, int off, int len) {
+ }
+
+ @Override
+ public void write(int b) {
+ }
+
+ @Override
+ public void write(byte[] b) throws IOException {
+ }
+ }
+}
--- /dev/null
+package org.apache.poi.hssf.dev;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.InputStream;
+import java.io.PrintStream;
+import java.util.List;
+
+import org.junit.Ignore;
+import org.junit.Test;
+
+public class TestBiffDrawingToXml 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");
+ };
+
+ @Override
+ @Ignore("Not yet done, nearly all files fail with various errors, remove this method when done to use the one from the abstract base class!...")
+ @Test
+ public void testMain() throws Exception {
+ }
+
+ @Override
+ void runOneFile(String dir, String file, List<String> failed)
+ throws Exception {
+ PrintStream save = System.out;
+ try {
+ //System.setOut(new PrintStream(TestBiffViewer.NULL_OUTPUT_STREAM));
+ // use a NullOutputStream to not write the bytes anywhere for best runtime
+ InputStream wb = new FileInputStream(new File(dir, file));
+ try {
+ BiffDrawingToXml.writeToFile(NULL_OUTPUT_STREAM, wb, false, new String[] {});
+ } finally {
+ wb.close();
+ }
+ } finally {
+ System.setOut(save);
+ }
+ }
+}
--- /dev/null
+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");
+
+ 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));
+ 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();
+ }
+ } finally {
+ inStream.close();
+ }
+ }
+}
--- /dev/null
+package org.apache.poi.hssf.dev;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.PrintStream;
+import java.util.List;
+
+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");
+ };
+
+ @Override
+ void runOneFile(String dir, String file, List<String> failed) throws IOException {
+ PrintStream save = System.out;
+ try {
+ // redirect standard out during the test to avoid spamming the console with output
+ System.setOut(new PrintStream(NULL_OUTPUT_STREAM));
+
+ EFBiffViewer.main(new String[] { new File(dir, file).getAbsolutePath() });
+ } finally {
+ System.setOut(save);
+ }
+ }
+}
--- /dev/null
+package org.apache.poi.hssf.dev;
+
+import java.io.File;
+import java.io.PrintStream;
+import java.util.List;
+
+import org.junit.Ignore;
+import org.junit.Test;
+
+public class TestFormulaViewer 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("NoGutsRecords.xls");
+// EXCLUDED.add("BOOK_in_capitals.xls");
+// EXCLUDED.add("46904.xls");
+// EXCLUDED.add("OddStyleRecord.xls");
+ };
+
+ @Override
+ @Ignore("Not yet done, nearly all files fail with various errors, remove this method when done to use the one from the abstract base class!...")
+ @Test
+ public void testMain() throws Exception {
+ }
+
+ @Override
+ void runOneFile(String dir, String file, List<String> failed) throws Exception {
+ PrintStream save = System.out;
+ try {
+ // redirect standard out during the test to avoid spamming the console with output
+ System.setOut(new PrintStream(NULL_OUTPUT_STREAM));
+
+ FormulaViewer viewer = new FormulaViewer();
+ viewer.setFile(new File(dir, file).getAbsolutePath());
+ viewer.setList(true);
+ viewer.run();
+ } finally {
+ System.setOut(save);
+ }
+ }
+}
--- /dev/null
+package org.apache.poi.hssf.dev;
+
+import java.io.File;
+import java.io.PrintStream;
+import java.util.List;
+
+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");
+
+ SILENT_EXCLUDED.add("46904.xls");
+ };
+
+ @Override
+ void runOneFile(String dir, String file, List<String> failed) throws Exception {
+ // avoid running on files leftover from previous failed runs
+ if(file.endsWith("-saved.xls")) {
+ return;
+ }
+
+ PrintStream save = System.out;
+ try {
+ // redirect standard out during the test to avoid spamming the console with output
+ System.setOut(new PrintStream(NULL_OUTPUT_STREAM));
+
+ try {
+ ReSave.main(new String[] { new File(dir, file).getAbsolutePath() });
+ 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() });
+ } finally {
+ // clean up the re-re-saved file
+ new File(dir, file.replace(".xls", "-saved.xls").replace(".xls", "-saved.xls")).delete();
+ }
+ } finally {
+ // clean up the re-saved file
+ new File(dir, file.replace(".xls", "-saved.xls")).delete();
+ }
+
+ } finally {
+ System.setOut(save);
+ }
+ }
+}
--- /dev/null
+package org.apache.poi.hssf.dev;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.PrintStream;
+import java.util.List;
+
+public class TestRecordLister 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("NoGutsRecords.xls");
+ EXCLUDED.add("BOOK_in_capitals.xls");
+ EXCLUDED.add("OddStyleRecord.xls");
+
+ SILENT_EXCLUDED.add("46904.xls");
+ };
+
+ @Override
+ void runOneFile(String dir, String file, List<String> failed) throws IOException {
+ PrintStream save = System.out;
+ try {
+ // redirect standard out during the test to avoid spamming the console with output
+ System.setOut(new PrintStream(NULL_OUTPUT_STREAM));
+
+ RecordLister viewer = new RecordLister();
+ viewer.setFile(new File(dir, file).getAbsolutePath());
+ viewer.run();
+ } finally {
+ System.setOut(save);
+ }
+ }
+}