Beispiel #1
0
        /// <summary>
        /// Creates a stub Workbook from the supplied records,
        /// suitable for use with the {@link HSSFFormulaParser}
        /// </summary>
        /// <param name="externs">The ExternSheetRecords in your file</param>
        /// <param name="bounds">The BoundSheetRecords in your file</param>
        /// <param name="sst">TThe SSTRecord in your file.</param>
        /// <returns>A stub Workbook suitable for use with HSSFFormulaParser</returns>
        public static InternalWorkbook CreateStubWorkbook(ExternSheetRecord[] externs,
                BoundSheetRecord[] bounds, SSTRecord sst)
        {
            List<Record> wbRecords = new List<Record>();

            // Core Workbook records go first
            if (bounds != null)
            {
                for (int i = 0; i < bounds.Length; i++)
                {
                    wbRecords.Add(bounds[i]);
                }
            }
            if (sst != null)
            {
                wbRecords.Add(sst);
            }

            // Now we can have the ExternSheetRecords,
            //  preceded by a SupBookRecord
            if (externs != null)
            {
                wbRecords.Add(SupBookRecord.CreateInternalReferences(
                        (short)externs.Length));
                for (int i = 0; i < externs.Length; i++)
                {
                    wbRecords.Add(externs[i]);
                }
            }

            // Finally we need an EoF record
            wbRecords.Add(EOFRecord.instance);

            return InternalWorkbook.CreateWorkbook(wbRecords);
        }
Beispiel #2
0
 /**
  * @param rawData serialization of one {@link SSTRecord} and zero or more {@link ContinueRecord}s
  */
 private static SSTRecord CreateSSTFromRawData(byte[] rawData)
 {
     RecordInputStream in1 = new RecordInputStream(new MemoryStream(rawData));
     in1.NextRecord();
     SSTRecord result = new SSTRecord(in1);
     Assert.AreEqual(0, in1.Remaining);
     Assert.IsTrue(!in1.HasNextRecord);
     return result;
 }
Beispiel #3
0
        public override bool Equals(Object o)
        {
            if ((o == null) || (o.GetType() != this.GetType()))
            {
                return(false);
            }
            SSTRecord other = (SSTRecord)o;

            return((field_1_num_strings == other
                    .field_1_num_strings) && (field_2_num_unique_strings == other
                                              .field_2_num_unique_strings) && field_3_strings
                   .Equals(other.field_3_strings));
        }
Beispiel #4
0
        public void TestSimpleConstructor()
        {
            SSTRecord record = new SSTRecord();

            Assert.AreEqual(0, record.NumStrings);
            Assert.AreEqual(0, record.NumUniqueStrings);
            Assert.AreEqual(0, record.CountStrings);
            byte[] output = record.Serialize();
            byte[] expected =
                {
                    (byte) record.Sid, (byte) ( record.Sid >> 8 ),
                    (byte) 8, (byte) 0, (byte) 0, (byte) 0, (byte) 0,
                    (byte) 0, (byte) 0, (byte) 0, (byte) 0, (byte) 0
                };

            Assert.AreEqual(expected.Length, output.Length);
            for (int k = 0; k < expected.Length; k++)
            {
                Assert.AreEqual(expected[k], output[k], k.ToString());
            }
        }
Beispiel #5
0
        public void TestSimpleAddString()
        {
            SSTRecord record = new SSTRecord();
            UnicodeString s1 = new UnicodeString("Hello world");

            // \u2122 is the encoding of the trademark symbol ...
            UnicodeString s2 = new UnicodeString("Hello world\u2122");

            Assert.AreEqual(0, record.AddString(s1));
            Assert.AreEqual(s1, record.GetString(0));
            Assert.AreEqual(1, record.CountStrings);
            Assert.AreEqual(1, record.NumStrings);
            Assert.AreEqual(1, record.NumUniqueStrings);
            Assert.AreEqual(0, record.AddString(s1));
            Assert.AreEqual(s1, record.GetString(0));
            Assert.AreEqual(1, record.CountStrings);
            Assert.AreEqual(2, record.NumStrings);
            Assert.AreEqual(1, record.NumUniqueStrings);
            Assert.AreEqual(1, record.AddString(s2));
            Assert.AreEqual(s2, record.GetString(1));
            Assert.AreEqual(2, record.CountStrings);
            Assert.AreEqual(3, record.NumStrings);
            Assert.AreEqual(2, record.NumUniqueStrings);
            IEnumerator iter = record.GetStrings();

            while (iter.MoveNext())
            {
                UnicodeString ucs = (UnicodeString)iter.Current;

                if (ucs.Equals(s1))
                {
                    Assert.AreEqual((byte)0, ucs.OptionFlags);
                }
                else if (ucs.Equals(s2))
                {
                    Assert.AreEqual((byte)1, ucs.OptionFlags);
                }
                else
                {
                    Assert.Fail("cannot match string: " + ucs.String);
                }
            }
        }
Beispiel #6
0
        public void TestSSTRecordBug()
        {
            // create an SSTRecord and write a certain pattern of strings
            // to it ... then serialize it and verify the content
            SSTRecord record = new SSTRecord();

            // the record will start with two integers, then this string
            // ... that will eat up 16 of the 8224 bytes that the record
            // can hold
            record.AddString(new UnicodeString("Hello"));

            // now we have an Additional 8208 bytes, which is an exact
            // multiple of 16 bytes
            long Testvalue = 1000000000000L;

            for (int k = 0; k < 2000; k++)
            {
                record.AddString(new UnicodeString((Testvalue + k).ToString()));
            }
            byte[] content = new byte[record.RecordSize];

            record.Serialize(0, content);
            Assert.AreEqual(8224, LittleEndian.GetShort(content, 2));
            Assert.AreEqual(ContinueRecord.sid, LittleEndian.GetShort(content, 8228));
            Assert.AreEqual(8224, LittleEndian.GetShort(content, 8228 + 2));
            Assert.AreEqual((byte)13, content[4 + 8228]);
            Assert.AreEqual(ContinueRecord.sid, LittleEndian.GetShort(content, 2 * 8228));
            Assert.AreEqual(8224, LittleEndian.GetShort(content, 8228 * 2 + 2));
            Assert.AreEqual((byte)13, content[4 + 8228 * 2]);
            Assert.AreEqual(ContinueRecord.sid, LittleEndian.GetShort(content, 3 * 8228));
            Assert.AreEqual((byte)13, content[4 + 8228 * 3]);
        }
Beispiel #7
0
        public void TestHugeStrings()
        {
            SSTRecord record = new SSTRecord();
            byte[][] bstrings =
                {
                    new byte[9000], new byte[7433], new byte[9002],
                    new byte[16998]
                };
            UnicodeString[] strings = new UnicodeString[bstrings.Length];
            int total_length = 0;

            for (int k = 0; k < bstrings.Length; k++)
            {
                Arrays.Fill(bstrings[k], (byte)('a' + k));
                strings[k] = new UnicodeString(new String(ConvertByteToChar(bstrings[k])));
                record.AddString(strings[k]);
                total_length += 3 + bstrings[k].Length;
            }

            // add overhead of SST record
            total_length += 8;

            // add overhead of broken strings
            total_length += 4;

            // add overhead of six records
            total_length += (6 * 4);
            byte[] content = new byte[record.RecordSize];

            record.Serialize(0, content);
            Assert.AreEqual(total_length, content.Length);

            //Deserialize the record.
            RecordInputStream recStream = new RecordInputStream(new MemoryStream(content));
            recStream.NextRecord();
            record = new SSTRecord(recStream);

            Assert.AreEqual(strings.Length, record.NumStrings);
            Assert.AreEqual(strings.Length, record.NumUniqueStrings);
            Assert.AreEqual(strings.Length, record.CountStrings);
            for (int k = 0; k < strings.Length; k++)
            {
                Assert.AreEqual(strings[k], record.GetString(k));
            }
            record = new SSTRecord();
            bstrings[1] = new byte[bstrings[1].Length - 1];
            for (int k = 0; k < bstrings.Length; k++)
            {
                if ((bstrings[k].Length % 2) == 1)
                {
                    Arrays.Fill(bstrings[k], (byte)('a' + k));
                    strings[k] = new UnicodeString(new String(ConvertByteToChar(bstrings[k])));
                }
                else
                {
                    char[] data = new char[bstrings[k].Length / 2];

                    Arrays.Fill(data, (char)('\u2122' + k));
                    strings[k] = new UnicodeString(new String(data));
                }
                record.AddString(strings[k]);
            }
            content = new byte[record.RecordSize];
            record.Serialize(0, content);
            total_length--;
            Assert.AreEqual(total_length, content.Length);

            recStream = new RecordInputStream(new MemoryStream(content));
            recStream.NextRecord();
            record = new SSTRecord(recStream);

            Assert.AreEqual(strings.Length, record.NumStrings);
            Assert.AreEqual(strings.Length, record.NumUniqueStrings);
            Assert.AreEqual(strings.Length, record.CountStrings);
            for (int k = 0; k < strings.Length; k++)
            {
                Assert.AreEqual(strings[k], record.GetString(k));
            }
        }
Beispiel #8
0
        public void Test50779_2()
        {
            byte[] bytes = HexRead.ReadFromString(data_50779_2);

            RecordInputStream in1 = TestcaseRecordInputStream.Create(bytes);
            Assert.AreEqual(SSTRecord.sid, in1.Sid);
            SSTRecord src = new SSTRecord(in1);
            Assert.AreEqual(81, src.NumStrings);

            byte[] Serialized = src.Serialize();

            in1 = TestcaseRecordInputStream.Create(Serialized);
            Assert.AreEqual(SSTRecord.sid, in1.Sid);
            SSTRecord dst = new SSTRecord(in1);
            Assert.AreEqual(81, dst.NumStrings);

            AssertAreEqual(src, dst);
        }
Beispiel #9
0
        /**
         * deep comparison of two SST records
         */
        public static void AssertAreEqual(SSTRecord expected, SSTRecord actual)
        {
            Assert.AreEqual(expected.NumStrings, actual.NumStrings, "number of strings");
            Assert.AreEqual(expected.NumUniqueStrings, actual.NumUniqueStrings, "number of unique strings");
            Assert.AreEqual(expected.CountStrings, actual.CountStrings, "count of strings");
            for (int k = 0; k < expected.CountStrings; k++)
            {
                UnicodeString us1 = expected.GetString(k);
                UnicodeString us2 = actual.GetString(k);

                Assert.IsTrue(us1.Equals(us2), "String at idx=" + k);
            }
        }
Beispiel #10
0
        private bool areSameSSTs(SSTRecord a, SSTRecord b)
        {

            if (a.NumStrings != b.NumStrings)
            {
                return false;
            }
            int nElems = a.NumUniqueStrings;
            if (nElems != b.NumUniqueStrings)
            {
                return false;
            }
            for (int i = 0; i < nElems; i++)
            {
                if (!a.GetString(i).Equals(b.GetString(i)))
                {
                    return false;
                }
            }
            return true;
        }
            /// <summary>
            /// Process an HSSF Record. Called when a record occurs in an HSSF file.
            /// </summary>
            /// <param name="record"></param>
            public void ProcessRecord(Record record)
            {
                String thisText = null;
                int thisRow = -1;

                switch (record.Sid)
                {
                    case BoundSheetRecord.sid:
                        BoundSheetRecord sr = (BoundSheetRecord)record;
                        sheetNames.Add(sr.Sheetname);
                        break;
                    case BOFRecord.sid:
                        BOFRecord bof = (BOFRecord)record;
                        if (bof.Type == BOFRecord.TYPE_WORKSHEET)
                        {
                            sheetNum++;
                            rowNum = -1;

                            if (includeSheetNames)
                            {
                                if (text.Length > 0) text.Append("\n");
                                text.Append(sheetNames[sheetNum]);
                            }
                        }
                        break;
                    case SSTRecord.sid:
                        sstRecord = (SSTRecord)record;
                        break;

                    case FormulaRecord.sid:
                        FormulaRecord frec = (FormulaRecord)record;
                        thisRow = frec.Row;

                        if (formulasNotResults)
                        {
                            thisText = HSSFFormulaParser.ToFormulaString((HSSFWorkbook)null, frec.ParsedExpression);
                        }
                        else
                        {
                            if (frec.HasCachedResultString)
                            {
                                // Formula result is a string
                                // This is stored in the next record
                                outputNextStringValue = true;
                                nextRow = frec.Row;
                            }
                            else
                            {
                                thisText = FormatNumberDateCell(frec, frec.Value);
                            }
                        }
                        break;
                    case StringRecord.sid:
                        if (outputNextStringValue)
                        {
                            // String for formula
                            StringRecord srec = (StringRecord)record;
                            thisText = srec.String;
                            thisRow = nextRow;
                            outputNextStringValue = false;
                        }
                        break;
                    case LabelRecord.sid:
                        LabelRecord lrec = (LabelRecord)record;
                        thisRow = lrec.Row;
                        thisText = lrec.Value;
                        break;
                    case LabelSSTRecord.sid:
                        LabelSSTRecord lsrec = (LabelSSTRecord)record;
                        thisRow = lsrec.Row;
                        if (sstRecord == null)
                        {
                            throw new Exception("No SST record found");
                        }
                        thisText = sstRecord.GetString(lsrec.SSTIndex).ToString();
                        break;
                    case NoteRecord.sid:
                        NoteRecord nrec = (NoteRecord)record;
                        thisRow = nrec.Row;
                        // TODO: Find object to match nrec.GetShapeId()
                        break;
                    case NumberRecord.sid:
                        NumberRecord numrec = (NumberRecord)record;
                        thisRow = numrec.Row;
                        thisText = FormatNumberDateCell(numrec, numrec.Value);
                        break;
                    default:
                        break;
                }

                if (thisText != null)
                {
                    if (thisRow != rowNum)
                    {
                        rowNum = thisRow;
                        if (text.Length > 0)
                            text.Append("\n");
                    }
                    else
                    {
                        text.Append("\t");
                    }
                    text.Append(thisText);
                }
            }
        /*
         * Serializes all records int the worksheet section into a big byte array. Use
         * this to Write the Workbook out.
         *
         * @return byte array containing the HSSF-only portions of the POIFS file.
         */
        // GJS: Not used so why keep it.
        //    public byte [] Serialize() {
        //        log.Log(DEBUG, "Serializing Workbook!");
        //        byte[] retval    = null;
        //
        ////         ArrayList bytes     = new ArrayList(records.Count);
        //        int    arraysize = Size;
        //        int    pos       = 0;
        //
        //        retval = new byte[ arraysize ];
        //        for (int k = 0; k < records.Count; k++) {
        //
        //            Record record = records[k];
        ////             Let's skip RECALCID records, as they are only use for optimization
        //	    if(record.Sid != RecalcIdRecord.Sid || ((RecalcIdRecord)record).IsNeeded) {
        //                pos += record.Serialize(pos, retval);   // rec.Length;
        //	    }
        //        }
        //        log.Log(DEBUG, "Exiting Serialize workbook");
        //        return retval;
        //    }

        /**
         * Serializes all records int the worksheet section into a big byte array. Use
         * this to Write the Workbook out.
         * @param offset of the data to be written
         * @param data array of bytes to Write this to
         */

        public int Serialize(int offset, byte[] data)
        {
            //if (log.Check(POILogger.DEBUG))
            //    log.Log(DEBUG, "Serializing Workbook with offsets");

            int pos = 0;

            SSTRecord sst = null;
            int sstPos = 0;
            bool wroteBoundSheets = false;
            for (int k = 0; k < records.Count; k++)
            {

                Record record = records[k];
                // Let's skip RECALCID records, as they are only use for optimization
                if (record.Sid != RecalcIdRecord.sid || ((RecalcIdRecord)record).IsNeeded)
                {
                    int len = 0;
                    if (record is SSTRecord)
                    {
                        sst = (SSTRecord)record;
                        sstPos = pos;
                    }
                    if (record.Sid == ExtSSTRecord.sid && sst != null)
                    {
                        record = sst.CreateExtSSTRecord(sstPos + offset);
                    }
                    if (record is BoundSheetRecord)
                    {
                        if (!wroteBoundSheets)
                        {
                            for (int i = 0; i < boundsheets.Count; i++)
                            {
                                len += ((BoundSheetRecord)boundsheets[i])
                                                 .Serialize(pos + offset + len, data);
                            }
                            wroteBoundSheets = true;
                        }
                    }
                    else
                    {
                        len = record.Serialize(pos + offset, data);
                    }
                    /////  DEBUG BEGIN /////
                    //                if (len != record.RecordSize)
                    //                    throw new InvalidOperationException("Record size does not match Serialized bytes.  Serialized size = " + len + " but RecordSize returns " + record.RecordSize);
                    /////  DEBUG END /////
                    pos += len;   // rec.Length;
                }
            }
            //if (log.Check(POILogger.DEBUG))
            //    log.Log(DEBUG, "Exiting Serialize workbook");
            return pos;
        }
        /**
         * use this function to Add a Shared String Table to an existing sheet (say
         * generated by a different java api) without an sst....
         * @see #CreateSST()
         * @see org.apache.poi.hssf.record.SSTRecord
         */

        public void InsertSST()
        {
            //if (log.Check(POILogger.DEBUG))
            //    log.Log(DEBUG, "creating new SST via InsertSST!");
            sst = new SSTRecord();
            records.Add(records.Count- 1, CreateExtendedSST());
            records.Add(records.Count - 2, sst);
        }
Beispiel #14
0
 /// <summary>
 /// Process the record ourselves, but do not
 /// pass it on to the child Listener.
 /// </summary>
 /// <param name="record">The record.</param>
 public void ProcessRecordInternally(Record record)
 {
     if (record is BoundSheetRecord)
     {
         boundSheetRecords.Add(record);
     }
     else if (record is ExternSheetRecord)
     {
         externSheetRecords.Add(record);
     }
     else if (record is SSTRecord)
     {
         sstRecord = (SSTRecord)record;
     }
 }