KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hssf > usermodel > SanityChecker


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18
19 package org.apache.poi.hssf.usermodel;
20
21 import junit.framework.Assert;
22 import org.apache.poi.hssf.model.Sheet;
23 import org.apache.poi.hssf.model.Workbook;
24 import org.apache.poi.hssf.record.*;
25
26 import java.util.List JavaDoc;
27
28 /**
29  * Designed to check wither the records written actually make sense.
30  */

31 public class SanityChecker
32         extends Assert
33 {
34     static class CheckRecord
35     {
36         Class JavaDoc record;
37         char occurance; // 1 = one time, M = 1..many times, * = 0..many, 0 = optional
38
private boolean together;
39
40         public CheckRecord( Class JavaDoc record, char occurance )
41         {
42             this(record, occurance, true);
43         }
44
45         /**
46          * @param record The record type to check
47          * @param occurance The occurance 1 = occurs once, M = occurs many times
48          * @param together
49          */

50         public CheckRecord(Class JavaDoc record, char occurance, boolean together)
51         {
52             this.record = record;
53             this.occurance = occurance;
54             this.together = together;
55         }
56
57         public Class JavaDoc getRecord()
58         {
59             return record;
60         }
61
62         public char getOccurance()
63         {
64             return occurance;
65         }
66
67         public boolean isRequired()
68         {
69             return occurance == '1' || occurance == 'M';
70         }
71
72         public boolean isOptional()
73         {
74             return occurance == '0' || occurance == '*';
75         }
76
77         public boolean isTogether()
78         {
79             return together;
80         }
81
82         public boolean isMany()
83         {
84             return occurance == '*' || occurance == 'M';
85         }
86
87         public int match( List JavaDoc records, int recordIdx )
88         {
89             int firstRecord = findFirstRecord(records, getRecord(), recordIdx);
90             if (isRequired())
91             {
92                 return matchRequired( firstRecord, records, recordIdx );
93             }
94             else
95             {
96                 return matchOptional( firstRecord, records, recordIdx );
97             }
98         }
99
100         private int matchOptional( int firstRecord, List JavaDoc records, int recordIdx )
101         {
102             if (firstRecord == -1)
103             {
104                 return recordIdx;
105             }
106
107             return matchOneOrMany( records, firstRecord );
108 // return matchOneOrMany( records, recordIdx );
109
}
110
111         private int matchRequired( int firstRecord, List JavaDoc records, int recordIdx )
112         {
113             if (firstRecord == -1)
114             {
115                 fail("Manditory record missing or out of order: " + record);
116             }
117
118             return matchOneOrMany( records, firstRecord );
119 // return matchOneOrMany( records, recordIdx );
120
}
121
122         private int matchOneOrMany( List JavaDoc records, int recordIdx )
123         {
124             if (isZeroOrOne())
125             {
126                 // check no other records
127
if (findFirstRecord(records, getRecord(), recordIdx+1) != -1)
128                     fail("More than one record matched for " + getRecord().getName());
129             }
130             else if (isZeroToMany())
131             {
132                 if (together)
133                 {
134                     int nextIdx = findFirstRecord(records, record, recordIdx+1);
135                     while (nextIdx != -1)
136                     {
137                         if (nextIdx - 1 != recordIdx)
138                             fail("Records are not together " + record.getName());
139                         recordIdx = nextIdx;
140                         nextIdx = findFirstRecord(records, record, recordIdx+1);
141                     }
142                 }
143             }
144             return recordIdx+1;
145         }
146
147         private boolean isZeroToMany()
148         {
149             return occurance == '*' || occurance == 'M';
150         }
151
152         private boolean isZeroOrOne()
153         {
154             return occurance == '0' || occurance == '1';
155         }
156     }
157
158     CheckRecord[] workbookRecords = new CheckRecord[] {
159         new CheckRecord(BOFRecord.class, '1'),
160         new CheckRecord(InterfaceHdrRecord.class, '1'),
161         new CheckRecord(MMSRecord.class, '1'),
162         new CheckRecord(InterfaceEndRecord.class, '1'),
163         new CheckRecord(WriteAccessRecord.class, '1'),
164         new CheckRecord(CodepageRecord.class, '1'),
165         new CheckRecord(DSFRecord.class, '1'),
166         new CheckRecord(TabIdRecord.class, '1'),
167         new CheckRecord(FnGroupCountRecord.class, '1'),
168         new CheckRecord(WindowProtectRecord.class, '1'),
169         new CheckRecord(ProtectRecord.class, '1'),
170         new CheckRecord(PasswordRev4Record.class, '1'),
171         new CheckRecord(WindowOneRecord.class, '1'),
172         new CheckRecord(BackupRecord.class, '1'),
173         new CheckRecord(HideObjRecord.class, '1'),
174         new CheckRecord(DateWindow1904Record.class, '1'),
175         new CheckRecord(PrecisionRecord.class, '1'),
176         new CheckRecord(RefreshAllRecord.class, '1'),
177         new CheckRecord(BookBoolRecord.class, '1'),
178         new CheckRecord(FontRecord.class, 'M'),
179         new CheckRecord(FormatRecord.class, 'M'),
180         new CheckRecord(ExtendedFormatRecord.class, 'M'),
181         new CheckRecord(StyleRecord.class, 'M'),
182         new CheckRecord(UseSelFSRecord.class, '1'),
183         new CheckRecord(BoundSheetRecord.class, 'M'),
184         new CheckRecord(CountryRecord.class, '1'),
185         new CheckRecord(SupBookRecord.class, '0'),
186         new CheckRecord(ExternSheetRecord.class, '0'),
187         new CheckRecord(NameRecord.class, '*'),
188         new CheckRecord(SSTRecord.class, '1'),
189         new CheckRecord(ExtSSTRecord.class, '1'),
190         new CheckRecord(EOFRecord.class, '1'),
191     };
192
193     CheckRecord[] sheetRecords = new CheckRecord[] {
194         new CheckRecord(BOFRecord.class, '1'),
195         new CheckRecord(CalcModeRecord.class, '1'),
196         new CheckRecord(RefModeRecord.class, '1'),
197         new CheckRecord(IterationRecord.class, '1'),
198         new CheckRecord(DeltaRecord.class, '1'),
199         new CheckRecord(SaveRecalcRecord.class, '1'),
200         new CheckRecord(PrintHeadersRecord.class, '1'),
201         new CheckRecord(PrintGridlinesRecord.class, '1'),
202         new CheckRecord(GridsetRecord.class, '1'),
203         new CheckRecord(GutsRecord.class, '1'),
204         new CheckRecord(DefaultRowHeightRecord.class, '1'),
205         new CheckRecord(WSBoolRecord.class, '1'),
206         new CheckRecord(HeaderRecord.class, '1'),
207         new CheckRecord(FooterRecord.class, '1'),
208         new CheckRecord(HCenterRecord.class, '1'),
209         new CheckRecord(VCenterRecord.class, '1'),
210         new CheckRecord(PrintSetupRecord.class, '1'),
211         new CheckRecord(DefaultColWidthRecord.class, '1'),
212         new CheckRecord(DimensionsRecord.class, '1'),
213         new CheckRecord(WindowTwoRecord.class, '1'),
214         new CheckRecord(SelectionRecord.class, '1'),
215         new CheckRecord(EOFRecord.class, '1')
216     };
217
218     private void checkWorkbookRecords(Workbook workbook)
219     {
220         List JavaDoc records = workbook.getRecords();
221         assertTrue(records.get(0) instanceof BOFRecord);
222         assertTrue(records.get(records.size() - 1) instanceof EOFRecord);
223
224         checkRecordOrder(records, workbookRecords);
225 // checkRecordsTogether(records, workbookRecords);
226
}
227
228     private void checkSheetRecords(Sheet sheet)
229     {
230         List JavaDoc records = sheet.getRecords();
231         assertTrue(records.get(0) instanceof BOFRecord);
232         assertTrue(records.get(records.size() - 1) instanceof EOFRecord);
233
234         checkRecordOrder(records, sheetRecords);
235 // checkRecordsTogether(records, sheetRecords);
236
}
237
238     public void checkHSSFWorkbook(HSSFWorkbook wb)
239     {
240         checkWorkbookRecords(wb.getWorkbook());
241         for (int i = 0; i < wb.getNumberOfSheets(); i++)
242             checkSheetRecords(wb.getSheetAt(i).getSheet());
243
244     }
245
246     /*
247     private void checkRecordsTogether(List records, CheckRecord[] check)
248     {
249         for ( int checkIdx = 0; checkIdx < check.length; checkIdx++ )
250         {
251             int recordIdx = findFirstRecord(records, check[checkIdx].getRecord());
252             boolean notFoundAndRecordRequired = (recordIdx == -1 && check[checkIdx].isRequired());
253             if (notFoundAndRecordRequired)
254             {
255                 fail("Expected to find record of class " + check.getClass() + " but did not");
256             }
257             else if (recordIdx >= 0)
258             {
259                 if (check[checkIdx].isMany())
260                 {
261                     // Skip records that are together
262                     while (recordIdx < records.size() && check[checkIdx].getRecord().isInstance(records.get(recordIdx)))
263                         recordIdx++;
264                 }
265
266                 // Make sure record does not occur in remaining records (after the next)
267                 recordIdx++;
268                 for (int recordIdx2 = recordIdx; recordIdx2 < records.size(); recordIdx2++)
269                 {
270                     if (check[checkIdx].getRecord().isInstance(records.get(recordIdx2)))
271                         fail("Record occurs scattered throughout record chain:\n" + records.get(recordIdx2));
272                 }
273             }
274         }
275     } */

276
277     private static int findFirstRecord( List JavaDoc records, Class JavaDoc record, int startIndex )
278     {
279         for (int i = startIndex; i < records.size(); i++)
280         {
281             if (record.getName().equals(records.get(i).getClass().getName()))
282                 return i;
283         }
284         return -1;
285     }
286
287 // private static int findFirstRecord( List records, Class record )
288
// {
289
// return findFirstRecord ( records, record, 0 );
290
// }
291

292     void checkRecordOrder(List JavaDoc records, CheckRecord[] check)
293     {
294         int recordIdx = 0;
295         for ( int checkIdx = 0; checkIdx < check.length; checkIdx++ )
296         {
297             recordIdx = check[checkIdx].match(records, recordIdx);
298         }
299     }
300
301     /*
302     void checkRecordOrder(List records, CheckRecord[] check)
303     {
304         int checkIndex = 0;
305         for (int recordIndex = 0; recordIndex < records.size(); recordIndex++)
306         {
307             Record record = (Record) records.get(recordIndex);
308             if (check[checkIndex].getRecord().isInstance(record))
309             {
310                 if (check[checkIndex].getOccurance() == 'M')
311                 {
312                     // skip over duplicate records if multiples are allowed
313                     while (recordIndex+1 < records.size() && check[checkIndex].getRecord().isInstance(records.get(recordIndex+1)))
314                         recordIndex++;
315 // lastGoodMatch = recordIndex;
316                 }
317                 else if (check[checkIndex].getOccurance() == '1')
318                 {
319                     // Check next record to make sure there's not more than one
320                     if (recordIndex != records.size() - 1)
321                     {
322                         if (check[checkIndex].getRecord().isInstance(records.get(recordIndex+1)))
323                         {
324                             fail("More than one occurance of record found:\n" + records.get(recordIndex).toString());
325                         }
326                     }
327 // lastGoodMatch = recordIndex;
328                 }
329 // else if (check[checkIndex].getOccurance() == '0')
330 // {
331 //
332 // }
333                 checkIndex++;
334             }
335             if (checkIndex >= check.length)
336                 return;
337         }
338         fail("Could not find required record: " + check[checkIndex]);
339     } */

340
341 }
342
Popular Tags