public void TestProcessRecords()
        {
            bool[] wascalled = { false, }; // hack to pass boolean by ref into inner class

            IERFListener listener = new ERFListener1(ref wascalled);
            ArrayList param = new ArrayList();
            param.Add(BOFRecord.sid);
            EventRecordFactory factory = new EventRecordFactory(listener, param);

            BOFRecord bof = new BOFRecord();
            bof.Build = ((short)0);
            bof.BuildYear = ((short)1999);
            bof.RequiredVersion = (123);
            bof.Type = (BOFRecordType.Workbook);
            bof.Version = ((short)0x06);
            bof.HistoryBitMask = (BOFRecord.HISTORY_MASK);

            EOFRecord eof = EOFRecord.instance;
            byte[] bytes = new byte[bof.RecordSize + eof.RecordSize];
            int offset = 0;
            offset = bof.Serialize(offset, bytes);
            offset = eof.Serialize(offset, bytes);

            factory.ProcessRecords(new MemoryStream(bytes));
            Assert.IsTrue(wascalled[0], "The record listener must be called");
        }
        /**
         * Start Processing the Workbook stream into Model events.
         */
        public void Run(Stream stream)
        {
            EventRecordFactory factory = new EventRecordFactory(this, null);

            lastEOF = true;
            factory.ProcessRecords(stream);
        }
Beispiel #3
0
 /**
  * Start Processing the Workbook stream into Model events.
  */
 public void Run(Stream stream)
 {
     EventRecordFactory factory = new EventRecordFactory(this,null);
     lastEOF = true;
     factory.ProcessRecords(stream);
 }
        public void TestAddToExistingSheet()
        {

            // dvEmpty.xls is a simple one sheet workbook.  With a DataValidations header record but no 
            // DataValidations.  It's important that the example has one SHEETPROTECTION record.
            // Such a workbook can be Created in Excel (2007) by Adding datavalidation for one cell
            // and then deleting the row that Contains the cell.
            IWorkbook wb = HSSFTestDataSamples.OpenSampleWorkbook("dvEmpty.xls");
            int dvRow = 0;
            ISheet sheet = wb.GetSheetAt(0);
            IDataValidationHelper dataValidationHelper = sheet.GetDataValidationHelper();
            IDataValidationConstraint dc = dataValidationHelper.CreateintConstraint(OperatorType.EQUAL, "42", null);
            IDataValidation dv = dataValidationHelper.CreateValidation(dc, new CellRangeAddressList(dvRow, dvRow, 0, 0));

            dv.EmptyCellAllowed = (/*setter*/false);
            dv.ErrorStyle = (/*setter*/ERRORSTYLE.STOP);
            dv.ShowPromptBox = (/*setter*/true);
            dv.CreateErrorBox("Xxx", "Yyy");
            dv.SuppressDropDownArrow = (/*setter*/true);

            sheet.AddValidationData(dv);

            MemoryStream baos = new MemoryStream();
            try
            {
                wb.Write(baos);
            }
            catch (IOException e)
            {
                throw new RuntimeException(e);
            }

            byte[] wbData = baos.ToArray();

#if !HIDE_UNREACHABLE_CODE
            if (false)
            { // TODO (Jul 2008) fix EventRecordFactory to process unknown records, (and DV records for that matter)

                ERFListener erfListener = null; // new MyERFListener();
                EventRecordFactory erf = new EventRecordFactory(erfListener, null);
                try
                {
                    POIFSFileSystem fs = new POIFSFileSystem(new MemoryStream(baos.ToArray()));
                    throw new NotImplementedException("The method CreateDocumentInputStream of POIFSFileSystem is not implemented.");
                    //erf.ProcessRecords(fs.CreateDocumentInputStream("Workbook"));
                }
                catch (RecordFormatException e)
                {
                    throw new RuntimeException(e);
                }
                catch (IOException e)
                {
                    throw new RuntimeException(e);
                }
            }
            // else verify record ordering by navigating the raw bytes
#endif

            byte[] dvHeaderRecStart = { (byte)0xB2, 0x01, 0x12, 0x00, };
            int dvHeaderOffset = FindIndex(wbData, dvHeaderRecStart);
            Assert.IsTrue(dvHeaderOffset > 0);
            int nextRecIndex = dvHeaderOffset + 22;
            int nextSid
                = ((wbData[nextRecIndex + 0] << 0) & 0x00FF)
                + ((wbData[nextRecIndex + 1] << 8) & 0xFF00)
                ;
            // nextSid should be for a DVRecord.  If anything comes between the DV header record 
            // and the DV records, Excel will not be able to open the workbook without error.

            if (nextSid == 0x0867)
            {
                throw new AssertionException("Identified bug 45519");
            }
            Assert.AreEqual(DVRecord.sid, nextSid);
        }
        public void TestContinuedUnknownRecord()
        {
            byte[] data = {
            0, unchecked((byte)-1), 0, 0, // an unknown record with 0 Length
            0x3C , 0, 3, 0, 1, 2, 3, // a continuation record with 3 bytes of data
            0x3C , 0, 1, 0, 4 // one more continuation record with 1 byte of data
        };

            int[] recCnt =  {0} ;
            int[] offset =  {0} ;
            IERFListener listener = new ERFListener2(ref data, ref recCnt,ref offset);
            ArrayList sids = new ArrayList(2);
            sids.Add((short)-256);
            sids.Add((short)0x3C);

            EventRecordFactory factory = new EventRecordFactory(listener, sids);

            factory.ProcessRecords(new MemoryStream(data));
            Assert.AreEqual(3, recCnt[0], "nr. of Processed records");
            Assert.AreEqual(data.Length, offset[0], "nr. of Processed bytes");
        }