View | Details | Raw Unified | Return to bug 17373
Collapse All | Expand All

(-)src/java/org/apache/poi/hssf/eventmodel/EventRecordFactory.java (-4 / +4 lines)
Lines 89-95 Link Here
89
import org.apache.poi.hssf.record.FontRecord;
89
import org.apache.poi.hssf.record.FontRecord;
90
import org.apache.poi.hssf.record.FooterRecord;
90
import org.apache.poi.hssf.record.FooterRecord;
91
import org.apache.poi.hssf.record.FormatRecord;
91
import org.apache.poi.hssf.record.FormatRecord;
92
import org.apache.poi.hssf.record.FormulaRecord;
93
import org.apache.poi.hssf.record.GridsetRecord;
92
import org.apache.poi.hssf.record.GridsetRecord;
94
import org.apache.poi.hssf.record.GutsRecord;
93
import org.apache.poi.hssf.record.GutsRecord;
95
import org.apache.poi.hssf.record.HCenterRecord;
94
import org.apache.poi.hssf.record.HCenterRecord;
Lines 151-156 Link Here
151
 * @author Andrew C. Oliver (acoliver@apache.org) - probably to blame for the bugs (so yank his chain on the list)
150
 * @author Andrew C. Oliver (acoliver@apache.org) - probably to blame for the bugs (so yank his chain on the list)
152
 * @author Marc Johnson (mjohnson at apache dot org) - methods taken from RecordFactory
151
 * @author Marc Johnson (mjohnson at apache dot org) - methods taken from RecordFactory
153
 * @author Glen Stampoultzis (glens at apache.org) - methods taken from RecordFactory
152
 * @author Glen Stampoultzis (glens at apache.org) - methods taken from RecordFactory
153
 * @author Csaba Nagy (ncsaba at yahoo dot com)
154
 */
154
 */
155
public class EventRecordFactory
155
public class EventRecordFactory
156
{
156
{
Lines 289-296 Link Here
289
     * @param in the InputStream from which the records will be
289
     * @param in the InputStream from which the records will be
290
     *           obtained
290
     *           obtained
291
     *
291
     *
292
     * @return an array of Records created from the InputStream
293
     *
294
     * @exception RecordFormatException on error processing the
292
     * @exception RecordFormatException on error processing the
295
     *            InputStream
293
     *            InputStream
296
     */
294
     */
Lines 337-343 Link Here
337
335
338
                        if (record != null)
336
                        if (record != null)
339
                        {
337
                        {
340
                            if (rectype == ContinueRecord.sid)
338
                            if (rectype == ContinueRecord.sid &&
339
                                ! (last_record instanceof ContinueRecord) && // include continuation records after
340
                                ! (last_record instanceof UnknownRecord) )   // unknown records or previous continuation records
341
                            {
341
                            {
342
                                if (last_record == null)
342
                                if (last_record == null)
343
                                {
343
                                {
(-)src/java/org/apache/poi/hssf/record/ContinueRecord.java (-7 / +10 lines)
Lines 65-70 Link Here
65
 *               stream; content is tailored to that prior record<P>
65
 *               stream; content is tailored to that prior record<P>
66
 * @author Marc Johnson (mjohnson at apache dot org)
66
 * @author Marc Johnson (mjohnson at apache dot org)
67
 * @author Andrew C. Oliver (acoliver at apache dot org)
67
 * @author Andrew C. Oliver (acoliver at apache dot org)
68
 * @author Csaba Nagy (ncsaba at yahoo dot com)
68
 * @version 2.0-pre
69
 * @version 2.0-pre
69
 */
70
 */
70
71
Lines 116-132 Link Here
116
    public byte [] serialize()
117
    public byte [] serialize()
117
    {
118
    {
118
        byte[] retval = new byte[ field_1_data.length + 4 ];
119
        byte[] retval = new byte[ field_1_data.length + 4 ];
119
120
        serialize(0, retval);
120
        LittleEndian.putShort(retval, 0, sid);
121
        LittleEndian.putShort(retval, 2, ( short ) field_1_data.length);
122
        System.arraycopy(field_1_data, 0, retval, 4, field_1_data.length);
123
        return retval;
121
        return retval;
124
    }
122
    }
125
123
126
    public int serialize(int offset, byte [] data)
124
    public int serialize(int offset, byte [] data)
127
    {
125
    {
128
        throw new RecordFormatException(
126
129
            "You're not supposed to serialize Continue records like this directly");
127
        LittleEndian.putShort(data, offset, sid);
128
        LittleEndian.putShort(data, offset + 2, ( short ) field_1_data.length);
129
        System.arraycopy(field_1_data, 0, data, offset + 4, field_1_data.length);
130
        return field_1_data.length + 4;
131
        // throw new RecordFormatException(
132
        //    "You're not supposed to serialize Continue records like this directly");
130
    }
133
    }
131
134
132
    /**
135
    /**
Lines 212-218 Link Here
212
215
213
    protected void fillFields(byte [] ignored_parm1, short ignored_parm2)
216
    protected void fillFields(byte [] ignored_parm1, short ignored_parm2)
214
    {
217
    {
215
218
        this.field_1_data = ignored_parm1;
216
        // throw new RecordFormatException("Are you crazy?  Don't fill a continue record");
219
        // throw new RecordFormatException("Are you crazy?  Don't fill a continue record");
217
        // do nothing
220
        // do nothing
218
    }
221
    }
(-)src/java/org/apache/poi/hssf/record/RecordFactory.java (-2 / +5 lines)
Lines 69-78 Link Here
69
 * Description:  Takes a stream and outputs an array of Record objects.<P>
69
 * Description:  Takes a stream and outputs an array of Record objects.<P>
70
 *
70
 *
71
 * @deprecated use EventRecordFactory instead
71
 * @deprecated use EventRecordFactory instead
72
 * @see org.apache.poi.hssf.record.EventRecordFactory
72
 * @see org.apache.poi.hssf.eventmodel.EventRecordFactory
73
 * @author Andrew C. Oliver (acoliver at apache dot org)
73
 * @author Andrew C. Oliver (acoliver at apache dot org)
74
 * @author Marc Johnson (mjohnson at apache dot org)
74
 * @author Marc Johnson (mjohnson at apache dot org)
75
 * @author Glen Stampoultzis (glens at apache.org)
75
 * @author Glen Stampoultzis (glens at apache.org)
76
 * @author Csaba Nagy (ncsaba at yahoo dot com)
76
 * @version 1.0-pre
77
 * @version 1.0-pre
77
 */
78
 */
78
79
Lines 208-214 Link Here
208
209
209
                        if (record != null)
210
                        if (record != null)
210
                        {
211
                        {
211
                            if (rectype == ContinueRecord.sid)
212
                            if (rectype == ContinueRecord.sid &&
213
                                ! (last_record instanceof ContinueRecord) && // include continuation records after
214
                                ! (last_record instanceof UnknownRecord) )   // unknown records or previous continuation records
212
                            {
215
                            {
213
                                if (last_record == null)
216
                                if (last_record == null)
214
                                {
217
                                {
(-)src/testcases/org/apache/poi/hssf/eventmodel/TestEventRecordFactory.java (+59 lines)
Lines 3-12 Link Here
3
import java.io.ByteArrayInputStream;
3
import java.io.ByteArrayInputStream;
4
import java.io.EOFException;
4
import java.io.EOFException;
5
import java.util.Iterator;
5
import java.util.Iterator;
6
import java.util.Arrays;
6
7
7
import org.apache.poi.hssf.record.BOFRecord;
8
import org.apache.poi.hssf.record.BOFRecord;
8
import org.apache.poi.hssf.record.EOFRecord;
9
import org.apache.poi.hssf.record.EOFRecord;
9
import org.apache.poi.hssf.record.Record;
10
import org.apache.poi.hssf.record.Record;
11
import org.apache.poi.hssf.record.UnknownRecord;
12
import org.apache.poi.hssf.record.ContinueRecord;
10
13
11
import junit.framework.TestCase;
14
import junit.framework.TestCase;
12
15
Lines 14-19 Link Here
14
 * enclosing_type describe the purpose here
17
 * enclosing_type describe the purpose here
15
 * 
18
 * 
16
 * @author Andrew C. Oliver acoliver@apache.org
19
 * @author Andrew C. Oliver acoliver@apache.org
20
 * @author Csaba Nagy (ncsaba at yahoo dot com)
17
 */
21
 */
18
public class TestEventRecordFactory extends TestCase
22
public class TestEventRecordFactory extends TestCase
19
{
23
{
Lines 173-176 Link Here
173
      //  fail("not implemented");
177
      //  fail("not implemented");
174
    }
178
    }
175
    
179
    
180
181
    /**
182
     * TEST NAME:  Test Creating ContinueRecords After Unknown Records From An InputStream <P>
183
     * OBJECTIVE:  Test that the RecordFactory given an InputStream
184
     *             constructs the expected records.<P>
185
     * SUCCESS:    Record factory creates the expected records.<P>
186
     * FAILURE:    The wrong records are created or contain the wrong values <P>
187
     *
188
     */
189
     public void testContinuedUnknownRecord()
190
     {
191
        final byte[]   data    = new byte[]
192
        {
193
            0, -1, 0, 0, // an unknown record with 0 length
194
            0x3C , 0, 3, 0, 1, 2, 3, // a continuation record with 3 bytes of data
195
            0x3C , 0, 1, 0, 4 // one more continuation record with 1 byte of data
196
        };
197
198
        final int[] recCnt = { 0 };
199
        final int[] offset = { 0 };
200
        factory.registerListener(
201
          new ERFListener() {
202
              private String[] expectedRecordTypes = {
203
                  UnknownRecord.class.getName(),
204
                  ContinueRecord.class.getName(),
205
                  ContinueRecord.class.getName()
206
              };
207
              public boolean processRecord(Record rec)
208
              {
209
                  // System.out.println(rec.toString());
210
                  assertEquals(
211
                    "Record type",
212
                    expectedRecordTypes[recCnt[0]],
213
                    rec.getClass().getName()
214
                  );
215
                  compareData(rec, "Record " + recCnt[0] + ": ");
216
                  recCnt[0]++;
217
                  return true;
218
              }
219
              private void compareData(Record record, String message) {
220
                  byte[] recData = record.serialize();
221
                  for (int i = 0; i < recData.length; i++) {
222
                      assertEquals(message + " data byte " + i, data[offset[0]++], recData[i]);
223
                  }
224
              }
225
          },
226
          new short[] {-256, 0x3C}
227
        );
228
229
        factory.processRecords(new ByteArrayInputStream(data));
230
        assertEquals("nr. of processed records", 3, recCnt[0]);
231
        assertEquals("nr. of processed bytes", data.length, offset[0]);
232
    }
233
234
176
}
235
}
(-)src/testcases/org/apache/poi/hssf/record/TestRecordFactory.java (+44 lines)
Lines 55-66 Link Here
55
55
56
package org.apache.poi.hssf.record;
56
package org.apache.poi.hssf.record;
57
57
58
import java.io.ByteArrayInputStream;
59
58
import junit.framework.TestCase;
60
import junit.framework.TestCase;
59
61
60
/**
62
/**
61
 * Tests the record factory
63
 * Tests the record factory
62
 * @author Glen Stampoultzis (glens at apache.org)
64
 * @author Glen Stampoultzis (glens at apache.org)
63
 * @author Andrew C. Oliver (acoliver at apache dot org)
65
 * @author Andrew C. Oliver (acoliver at apache dot org)
66
 * @author Csaba Nagy (ncsaba at yahoo dot com)
64
 */
67
 */
65
68
66
public class TestRecordFactory
69
public class TestRecordFactory
Lines 156-161 Link Here
156
        assertEquals(515, numberRecord.getSid());
159
        assertEquals(515, numberRecord.getSid());
157
        assertEquals(0.0, numberRecord.getValue(), 0.001);
160
        assertEquals(0.0, numberRecord.getValue(), 0.001);
158
        assertEquals(21, numberRecord.getXFIndex());
161
        assertEquals(21, numberRecord.getXFIndex());
162
    }
163
164
    /**
165
     * TEST NAME:  Test Creating ContinueRecords After Unknown Records From An InputStream <P>
166
     * OBJECTIVE:  Test that the RecordFactory given an InputStream
167
     *             constructs the expected array of records.<P>
168
     * SUCCESS:    Record factory creates the expected records.<P>
169
     * FAILURE:    The wrong records are created or contain the wrong values <P>
170
     *
171
     */
172
    public void testContinuedUnknownRecord()
173
    {
174
        byte[]   data    = new byte[]
175
        {
176
            0, -1, 0, 0, // an unknown record with 0 length
177
            0x3C , 0, 3, 0, 1, 2, 3, // a continuation record with 3 bytes of data
178
            0x3C , 0, 1, 0, 4 // one more continuation record with 1 byte of data
179
        };
180
181
        ByteArrayInputStream bois = new ByteArrayInputStream(data);
182
        Record[] records = (Record[])
183
          RecordFactory.createRecords(bois).toArray(new Record[0]);
184
        assertEquals("Created record count", 3, records.length);
185
        assertEquals("1st record's type",
186
                     UnknownRecord.class.getName(),
187
                     records[ 0 ].getClass().getName());
188
        assertEquals("1st record's sid", (short)-256, records[0].getSid());
189
        assertEquals("2nd record's type",
190
                     ContinueRecord.class.getName(),
191
                     records[ 1 ].getClass().getName());
192
        ContinueRecord record = (ContinueRecord) records[1];
193
        assertEquals("2nd record's sid", 0x3C, record.getSid());
194
        assertEquals("1st data byte", 1, record.getData()[ 0 ]);
195
        assertEquals("2nd data byte", 2, record.getData()[ 1 ]);
196
        assertEquals("3rd data byte", 3, record.getData()[ 2 ]);
197
        assertEquals("3rd record's type",
198
                     ContinueRecord.class.getName(),
199
                     records[ 2 ].getClass().getName());
200
        record = (ContinueRecord) records[2];
201
        assertEquals("3nd record's sid", 0x3C, record.getSid());
202
        assertEquals("4th data byte", 4, record.getData()[ 0 ]);
159
    }
203
    }
160
204
161
    public static void main(String [] ignored_args)
205
    public static void main(String [] ignored_args)

Return to bug 17373