1 /
55
56 package org.apache.poi.hssf.model;
57
58 import java.io.*;
59
60 import java.util.ArrayList;
61 import java.util.List;
62 import java.util.Iterator;
63 import java.util.Locale;
64
65
66 import org.apache.poi.util.POILogger;
67 import org.apache.poi.util.POILogFactory;
68
69 import org.apache.poi.hssf.record.*;
70 import org.apache.poi.hssf.util.SheetReferences;
71 import org.apache.poi.hssf.util.HSSFColor;
72
73
94
95 public class Workbook {
96 private static final int DEBUG = POILogger.DEBUG;
97
98
99
100
104
105 private final static short CODEPAGE = ( short ) 0x4b0;
106
107
110
111 protected ArrayList records = null;
112
113
117
118 protected SSTRecord sst = null;
119
120
123
124 protected ExternSheetRecord externSheet= null;
125
126
130
131
132 protected ArrayList boundsheets = new ArrayList();
133
134 protected ArrayList formats = new ArrayList();
135
136 protected ArrayList names = new ArrayList();
137
138 protected int bspos =
139 0;
140 protected int tabpos =
141 0;
142 protected int fontpos =
143 0;
144 protected int numfonts =
145 0;
146 protected int xfpos =
147 0;
148 protected int numxfs =
149 0;
150 private int backuppos =
151 0;
152 private int namepos =
153 0;
154 private int supbookpos =
155 0;
156 private short maxformatid =
157 -1;
158
159 private static POILogger log =
160 POILogFactory.getLogger(Workbook.class);
161
162
166
167 public Workbook() {
168 }
169
170
182
183 public static Workbook createWorkbook(List recs) {
184 log.log(DEBUG, "Workbook (readfile) created with reclen=",
185 new Integer(recs.size()));
186 Workbook retval = new Workbook();
187 ArrayList records = new ArrayList(recs.size() / 3);
188
189 for (int k = 0; k < recs.size(); k++) {
190 Record rec = ( Record ) recs.get(k);
191
192 if (rec.getSid() == EOFRecord.sid) {
193 records.add(rec);
194 log.log(DEBUG, "found workbook eof record at " + k);
195 break;
196 }
197 switch (rec.getSid()) {
198
199 case BoundSheetRecord.sid :
200 log.log(DEBUG, "found boundsheet record at " + k);
201 retval.boundsheets.add(rec);
202 retval.bspos = k;
203 break;
204
205 case SSTRecord.sid :
206 log.log(DEBUG, "found sst record at " + k);
207 retval.sst = ( SSTRecord ) rec;
208 break;
209
210 case FontRecord.sid :
211 log.log(DEBUG, "found font record at " + k);
212 retval.fontpos = k;
213 retval.numfonts++;
214 break;
215
216 case ExtendedFormatRecord.sid :
217 log.log(DEBUG, "found XF record at " + k);
218 retval.xfpos = k;
219 retval.numxfs++;
220 break;
221
222 case TabIdRecord.sid :
223 log.log(DEBUG, "found tabid record at " + k);
224 retval.tabpos = k;
225 break;
226
227 case BackupRecord.sid :
228 log.log(DEBUG, "found backup record at " + k);
229 retval.backuppos = k;
230 break;
231 case ExternSheetRecord.sid :
232 log.log(DEBUG, "found extern sheet record at " + k);
233 retval.externSheet = ( ExternSheetRecord ) rec;
234 break;
235 case NameRecord.sid :
236 log.log(DEBUG, "found name record at " + k);
237 retval.names.add(rec);
238 retval.namepos = k;
239 break;
240 case 0x1AE :
241
242
243 log.log(DEBUG, "found SupBook record at " + k);
244 retval.supbookpos = k;
245 break;
246 case FormatRecord.sid :
247 log.log(DEBUG, "found format record at " + k);
248 retval.formats.add(rec);
249 retval.maxformatid = retval.maxformatid >= ((FormatRecord)rec).getIndexCode() ? retval.maxformatid : ((FormatRecord)rec).getIndexCode();
250 break;
251
252 default :
253 }
254 records.add(rec);
255 }
256
257 if (retval.supbookpos == 0) {
258 retval.supbookpos = retval.bspos + 1;
259 retval.namepos = retval.supbookpos + 1;
260 }
261
262 retval.records = records;
263 log.log(DEBUG, "exit create workbook from existing file function");
264 return retval;
265 }
266
267
271
272 public static Workbook createWorkbook() {
273 log.log(DEBUG, "creating new workbook from scratch");
274 Workbook retval = new Workbook();
275 ArrayList records = new ArrayList(30);
276 ArrayList formats = new ArrayList(8);
277
278 records.add(retval.createBOF());
279 records.add(retval.createInterfaceHdr());
280 records.add(retval.createMMS());
281 records.add(retval.createInterfaceEnd());
282 records.add(retval.createWriteAccess());
283 records.add(retval.createCodepage());
284 records.add(retval.createDSF());
285 records.add(retval.createTabId());
286 retval.tabpos = records.size() - 1;
287 records.add(retval.createFnGroupCount());
288 records.add(retval.createWindowProtect());
289 records.add(retval.createProtect());
290 records.add(retval.createPassword());
291 records.add(retval.createProtectionRev4());
292 records.add(retval.createPasswordRev4());
293 records.add(retval.createWindowOne());
294 records.add(retval.createBackup());
295 retval.backuppos = records.size() - 1;
296 records.add(retval.createHideObj());
297 records.add(retval.createDateWindow1904());
298 records.add(retval.createPrecision());
299 records.add(retval.createRefreshAll());
300 records.add(retval.createBookBool());
301 records.add(retval.createFont());
302 records.add(retval.createFont());
303 records.add(retval.createFont());
304 records.add(retval.createFont());
305 retval.fontpos = records.size() - 1;
306 retval.numfonts = 4;
307
308
309 for (int i = 0; i <= 7; i++) {
310 Record rec;
311 rec = retval.createFormat(i);
312 retval.maxformatid = retval.maxformatid >= ((FormatRecord)rec).getIndexCode() ? retval.maxformatid : ((FormatRecord)rec).getIndexCode();
313 formats.add(rec);
314 records.add(rec);
315 }
316 retval.formats = formats;
317
318 for (int k = 0; k < 21; k++) {
319 records.add(retval.createExtendedFormat(k));
320 retval.numxfs++;
321 }
322 retval.xfpos = records.size() - 1;
323 for (int k = 0; k < 6; k++) {
324 records.add(retval.createStyle(k));
325 }
326 records.add(retval.createUseSelFS());
327 for (int k = 0; k < 1; k++) {
328 BoundSheetRecord bsr =
329 ( BoundSheetRecord ) retval.createBoundSheet(k);
330
331 records.add(bsr);
332 retval.boundsheets.add(bsr);
333 retval.bspos = records.size() - 1;
334 }
335 records.add(retval.createCountry());
336 retval.sst = ( SSTRecord ) retval.createSST();
337 records.add(retval.sst);
338 records.add(retval.createExtendedSST());
339
340
341 records.add(retval.createEOF());
342 retval.records = records;
343 log.log(DEBUG, "exit create new workbook from scratch");
344 return retval;
345 }
346
347 public int getNumRecords() {
348 return records.size();
349 }
350
351
359
360 public FontRecord getFontRecordAt(int idx) {
361 int index = idx;
362
363 if (index > 4) {
364 index -= 1;
365 }
366 if (index > (numfonts - 1)) {
367 throw new ArrayIndexOutOfBoundsException(
368 "There are only " + numfonts
369 + " font records, you asked for " + idx);
370 }
371 FontRecord retval =
372 ( FontRecord ) records.get((fontpos - (numfonts - 1)) + index);
373
374 return retval;
375 }
376
377
384
385 public FontRecord createNewFont() {
386 FontRecord rec = ( FontRecord ) createFont();
387
388 ++fontpos;
389 ++bspos;
390 ++xfpos;
391 records.add(fontpos, rec);
392 numfonts++;
393 return rec;
394 }
395
396
401
402 public int getNumberOfFontRecords() {
403 return numfonts;
404 }
405
406
412
413 public void setSheetBof(int sheetnum, int pos) {
414 log.log(DEBUG, "setting bof for sheetnum =", new Integer(sheetnum),
415 " at pos=", new Integer(pos));
416 checkSheets(sheetnum);
417 (( BoundSheetRecord ) boundsheets.get(sheetnum))
418 .setPositionOfBof(pos);
419 }
420
421
424
425 public BackupRecord getBackupRecord() {
426 return ( BackupRecord ) records.get(backuppos);
427 }
428
429
430
438
439
440 public void setSheetName(int sheetnum, String sheetname ) {
441 setSheetName( sheetnum, sheetname, (byte)0 );
442 }
443
444 public void setSheetName(int sheetnum, String sheetname, short encoding ) {
445 checkSheets(sheetnum);
446 BoundSheetRecord sheet = (BoundSheetRecord)boundsheets.get( sheetnum );
447 sheet.setSheetname(sheetname);
448 sheet.setSheetnameLength( (byte)sheetname.length() );
449 sheet.setCompressedUnicodeFlag( (byte)encoding );
450 }
451
452
458
459 public String getSheetName(int sheetnum) {
460 return (( BoundSheetRecord ) boundsheets.get(sheetnum))
461 .getSheetname();
462 }
463
464
469
470 public int getSheetIndex(String name) {
471 int retval = -1;
472
473 for (int k = 0; k < boundsheets.size(); k++) {
474 String sheet = getSheetName(k);
475
476 if (sheet.equals(name)) {
477 retval = k;
478 break;
479 }
480 }
481 return retval;
482 }
483
484
488
489 private void checkSheets(int sheetnum) {
490 if ((boundsheets.size()) <= sheetnum) {
491 if ((boundsheets.size() + 1) <= sheetnum) {
492 throw new RuntimeException("Sheet number out of bounds!");
493 }
494 BoundSheetRecord bsr =
495 ( BoundSheetRecord ) createBoundSheet(sheetnum);
496
497 records.add(++bspos, bsr);
498 boundsheets.add(bsr);
499 fixTabIdRecord();
500 }
501 }
502
503 public void removeSheet(int sheetnum) {
504 if (boundsheets.size() > sheetnum) {
505 records.remove(bspos - (boundsheets.size() - 1) + sheetnum);
506 bspos--;
507 boundsheets.remove(sheetnum);
508 fixTabIdRecord();
509 }
510 }
511
512
516
517 private void fixTabIdRecord() {
518 TabIdRecord tir = ( TabIdRecord ) records.get(tabpos);
519 short[] tia = new short[ boundsheets.size() ];
520
521 for (short k = 0; k < tia.length; k++) {
522 tia[ k ] = k;
523 }
524 tir.setTabIdArray(tia);
525 }
526
527
532
533 public int getNumSheets() {
534 log.log(DEBUG, "getNumSheets=", new Integer(boundsheets.size()));
535 return boundsheets.size();
536 }
537
538
543
544 public int getNumExFormats() {
545 log.log(DEBUG, "getXF=", new Integer(boundsheets.size()));
546 return numxfs;
547 }
548
549
555
556 public ExtendedFormatRecord getExFormatAt(int index) {
557 int xfptr = xfpos - (numxfs - 1);
558
559 xfptr += index;
560 ExtendedFormatRecord retval =
561 ( ExtendedFormatRecord ) records.get(xfptr);
562
563 return retval;
564 }
565
566
572
573 public ExtendedFormatRecord createCellXF() {
574 ExtendedFormatRecord xf = createExtendedFormat();
575
576 ++xfpos;
577 ++bspos;
578 records.add(xfpos, xf);
579 numxfs++;
580 return xf;
581 }
582
583
591
592 public int addSSTString(String string, boolean use16bits) {
593 log.log(DEBUG, "insert to sst string='", string, "' and use16bits= ",
594 new Boolean(use16bits));
595 if (sst == null) {
596 insertSST();
597 }
598 return sst.addString(string, use16bits);
599 }
600
601
610
611 public int addSSTString(String string) {
612 return addSSTString(string, false);
613 }
614
615
619
620 public String getSSTString(int str) {
621 if (sst == null) {
622 insertSST();
623 }
624 String retval = sst.getString(str);
625
626 log.log(DEBUG, "Returning SST for index=", new Integer(str),
627 " String= ", retval);
628 return retval;
629 }
630
631
637
638 public void insertSST() {
639 log.log(DEBUG, "creating new SST via insertSST!");
640 sst = ( SSTRecord ) createSST();
641 records.add(records.size() - 1, createExtendedSST());
642 records.add(records.size() - 2, sst);
643 }
644
645
651
652 public byte [] serialize() {
653 log.log(DEBUG, "Serializing Workbook!");
654 byte[] retval = null;
655
656
657 int arraysize = getSize();
658 int pos = 0;
659
660
661
662
663
664
665
666
667
668 retval = new byte[ arraysize ];
669 for (int k = 0; k < records.size(); k++) {
670
671
672
673 pos += (( Record ) records.get(k)).serialize(pos,
674 retval);
675 }
676 log.log(DEBUG, "Exiting serialize workbook");
677 return retval;
678 }
679
680
686
687 public int serialize(int offset, byte [] data) {
688 log.log(DEBUG, "Serializing Workbook with offsets");
689
690
691
692 int pos = 0;
693
694
695
696
697
698
699
700
701
702
703 for (int k = 0; k < records.size(); k++) {
704
705
706
707 pos += (( Record ) records.get(k)).serialize(pos + offset,
708 data);
709 }
710 log.log(DEBUG, "Exiting serialize workbook");
711 return pos;
712 }
713
714 public int getSize() {
715 int retval = 0;
716
717 for (int k = 0; k < records.size(); k++) {
718 retval += (( Record ) records.get(k)).getRecordSize();
719 }
720 return retval;
721 }
722
723
729
730 protected Record createBOF() {
731 BOFRecord retval = new BOFRecord();
732
733 retval.setVersion(( short ) 0x600);
734 retval.setType(( short ) 5);
735 retval.setBuild(( short ) 0x10d3);
736
737
738 retval.setBuildYear(( short ) 1996);
739 retval.setHistoryBitMask(0x41);
740 retval.setRequiredVersion(0x6);
741 return retval;
742 }
743
744
750
751 protected Record createInterfaceHdr() {
752 InterfaceHdrRecord retval = new InterfaceHdrRecord();
753
754 retval.setCodepage(CODEPAGE);
755 return retval;
756 }
757
758
764
765 protected Record createMMS() {
766 MMSRecord retval = new MMSRecord();
767
768 retval.setAddMenuCount(( byte ) 0);
769 retval.setDelMenuCount(( byte ) 0);
770 return retval;
771 }
772
773
779
780 protected Record createInterfaceEnd() {
781 return new InterfaceEndRecord();
782 }
783
784
790
791 protected Record createWriteAccess() {
792 WriteAccessRecord retval = new WriteAccessRecord();
793
794 retval.setUsername(System.getProperty("user.name"));
795 return retval;
796 }
797
798
804
805 protected Record createCodepage() {
806 CodepageRecord retval = new CodepageRecord();
807
808 retval.setCodepage(CODEPAGE);
809 return retval;
810 }
811
812
818
819 protected Record createDSF() {
820 DSFRecord retval = new DSFRecord();
821
822 retval.setDsf(
823 ( short ) 0);
824 return retval;
825 }
826
827
834
835 protected Record createTabId() {
836 TabIdRecord retval = new TabIdRecord();
837 short[] tabidarray = {
838 0
839 };
840
841 retval.setTabIdArray(tabidarray);
842 return retval;
843 }
844
845
851
852 protected Record createFnGroupCount() {
853 FnGroupCountRecord retval = new FnGroupCountRecord();
854
855 retval.setCount(( short ) 14);
856 return retval;
857 }
858
859
865
866 protected Record createWindowProtect() {
867 WindowProtectRecord retval = new WindowProtectRecord();
868
869 retval.setProtect(
870 false);
871 return retval;
872 }
873
874
880
881 protected Record createProtect() {
882 ProtectRecord retval = new ProtectRecord();
883
884 retval.setProtect(
885 false);
886 return retval;
887 }
888
889
895
896 protected Record createPassword() {
897 PasswordRecord retval = new PasswordRecord();
898
899 retval.setPassword(( short ) 0);
900 return retval;
901 }
902
903
909
910 protected Record createProtectionRev4() {
911 ProtectionRev4Record retval = new ProtectionRev4Record();
912
913 retval.setProtect(false);
914 return retval;
915 }
916
917
923
924 protected Record createPasswordRev4() {
925 PasswordRev4Record retval = new PasswordRev4Record();
926
927 retval.setPassword(( short ) 0);
928 return retval;
929 }
930
931
946
947 protected Record createWindowOne() {
948 WindowOneRecord retval = new WindowOneRecord();
949
950 retval.setHorizontalHold(( short ) 0x168);
951 retval.setVerticalHold(( short ) 0x10e);
952 retval.setWidth(( short ) 0x3a5c);
953 retval.setHeight(( short ) 0x23be);
954 retval.setOptions(( short ) 0x38);
955 retval.setSelectedTab(( short ) 0x0);
956 retval.setDisplayedTab(( short ) 0x0);
957 retval.setNumSelectedTabs(( short ) 1);
958 retval.setTabWidthRatio(( short ) 0x258);
959 return retval;
960 }
961
962
968
969 protected Record createBackup() {
970 BackupRecord retval = new BackupRecord();
971
972 retval.setBackup(
973 ( short ) 0);
974 return retval;
975 }
976
977
983
984 protected Record createHideObj() {
985 HideObjRecord retval = new HideObjRecord();
986
987 retval.setHideObj(( short ) 0);
988 return retval;
989 }
990
991
997
998 protected Record createDateWindow1904() {
999 DateWindow1904Record retval = new DateWindow1904Record();
1000
1001 retval.setWindowing(
1002 ( short ) 0);
1003 return retval;
1004 }
1005
1006
1012
1013 protected Record createPrecision() {
1014 PrecisionRecord retval = new PrecisionRecord();
1015
1016 retval.setFullPrecision(
1017 true);
1018 return retval;
1019 }
1020
1021
1027
1028 protected Record createRefreshAll() {
1029 RefreshAllRecord retval = new RefreshAllRecord();
1030
1031 retval.setRefreshAll(false);
1032 return retval;
1033 }
1034
1035
1041
1042 protected Record createBookBool() {
1043 BookBoolRecord retval = new BookBoolRecord();
1044
1045 retval.setSaveLinkValues(( short ) 0);
1046 return retval;
1047 }
1048
1049
1062
1063 protected Record createFont() {
1064 FontRecord retval = new FontRecord();
1065
1066 retval.setFontHeight(( short ) 0xc8);
1067 retval.setAttributes(( short ) 0x0);
1068 retval.setColorPaletteIndex(( short ) 0x7fff);
1069 retval.setBoldWeight(( short ) 0x190);
1070 retval.setFontNameLength(( byte ) 5);
1071 retval.setFontName("Arial");
1072 return retval;
1073 }
1074
1075
1083
1084 protected Record createFormat(int id) {
1085 FormatRecord retval = new FormatRecord();
1086
1087 switch (id) {
1088
1089 case 0 :
1090 retval.setIndexCode(( short ) 5);
1091 retval.setFormatStringLength(( byte ) 0x17);
1092 retval.setFormatString("\"$\"#,##0_);\\(\"$\"#,##0\\)");
1093 break;
1094
1095 case 1 :
1096 retval.setIndexCode(( short ) 6);
1097 retval.setFormatStringLength(( byte ) 0x1c);
1098 retval.setFormatString("\"$\"#,##0_);[Red]\\(\"$\"#,##0\\)");
1099 break;
1100
1101 case 2 :
1102 retval.setIndexCode(( short ) 7);
1103 retval.setFormatStringLength(( byte ) 0x1d);
1104 retval.setFormatString("\"$\"#,##0.00_);\\(\"$\"#,##0.00\\)");
1105 break;
1106
1107 case 3 :
1108 retval.setIndexCode(( short ) 8);
1109 retval.setFormatStringLength(( byte ) 0x22);
1110 retval.setFormatString(
1111 "\"$\"#,##0.00_);[Red]\\(\"$\"#,##0.00\\)");
1112 break;
1113
1114 case 4 :
1115 retval.setIndexCode(( short ) 0x2a);
1116 retval.setFormatStringLength(( byte ) 0x32);
1117 retval.setFormatString(
1118 "_(\"$\"* #,##0_);_(\"$\"* \\(#,##0\\);_(\"$\"* \"-\"_);_(@_)");
1119 break;
1120
1121 case 5 :
1122 retval.setIndexCode(( short ) 0x29);
1123 retval.setFormatStringLength(( byte ) 0x29);
1124 retval.setFormatString(
1125 "_(* #,##0_);_(* \\(#,##0\\);_(* \"-\"_);_(@_)");
1126 break;
1127
1128 case 6 :
1129 retval.setIndexCode(( short ) 0x2c);
1130 retval.setFormatStringLength(( byte ) 0x3a);
1131 retval.setFormatString(
1132 "_(\"$\"* #,##0.00_);_(\"$\"* \\(#,##0.00\\);_(\"$\"* \"-\"??_);_(@_)");
1133 break;
1134
1135 case 7 :
1136 retval.setIndexCode(( short ) 0x2b);
1137 retval.setFormatStringLength(( byte ) 0x31);
1138 retval.setFormatString(
1139 "_(* #,##0.00_);_(* \\(#,##0.00\\);_(* \"-\"??_);_(@_)");
1140 break;
1141 }
1142 return retval;
1143 }
1144
1145
1154
1155 protected Record createExtendedFormat(int id) {
1156 ExtendedFormatRecord retval = new ExtendedFormatRecord();
1157
1158 switch (id) {
1159
1160 case 0 :
1161 retval.setFontIndex(( short ) 0);
1162 retval.setFormatIndex(( short ) 0);
1163 retval.setCellOptions(( short ) 0xfffffff5);
1164 retval.setAlignmentOptions(( short ) 0x20);
1165 retval.setIndentionOptions(( short ) 0);
1166 retval.setBorderOptions(( short ) 0);
1167 retval.setPaletteOptions(( short ) 0);
1168 retval.setAdtlPaletteOptions(( short ) 0);
1169 retval.setFillPaletteOptions(( short ) 0x20c0);
1170 break;
1171
1172 case 1 :
1173 retval.setFontIndex(( short ) 1);
1174 retval.setFormatIndex(( short ) 0);
1175 retval.setCellOptions(( short ) 0xfffffff5);
1176 retval.setAlignmentOptions(( short ) 0x20);
1177 retval.setIndentionOptions(( short ) 0xfffff400);
1178 retval.setBorderOptions(( short ) 0);
1179 retval.setPaletteOptions(( short ) 0);
1180 retval.setAdtlPaletteOptions(( short ) 0);
1181 retval.setFillPaletteOptions(( short ) 0x20c0);
1182 break;
1183
1184 case 2 :
1185 retval.setFontIndex(( short ) 1);
1186 retval.setFormatIndex(( short ) 0);
1187 retval.setCellOptions(( short ) 0xfffffff5);
1188 retval.setAlignmentOptions(( short ) 0x20);
1189 retval.setIndentionOptions(( short ) 0xfffff400);
1190 retval.setBorderOptions(( short ) 0);
1191 retval.setPaletteOptions(( short ) 0);
1192 retval.setAdtlPaletteOptions(( short ) 0);
1193 retval.setFillPaletteOptions(( short ) 0x20c0);
1194 break;
1195
1196 case 3 :
1197 retval.setFontIndex(( short ) 2);
1198 retval.setFormatIndex(( short ) 0);
1199 retval.setCellOptions(( short ) 0xfffffff5);
1200 retval.setAlignmentOptions(( short ) 0x20);
1201 retval.setIndentionOptions(( short ) 0xfffff400);
1202 retval.setBorderOptions(( short ) 0);
1203 retval.setPaletteOptions(( short ) 0);
1204 retval.setAdtlPaletteOptions(( short ) 0);
1205 retval.setFillPaletteOptions(( short ) 0x20c0);
1206 break;
1207
1208 case 4 :
1209 retval.setFontIndex(( short ) 2);
1210 retval.setFormatIndex(( short ) 0);
1211 retval.setCellOptions(( short ) 0xfffffff5);
1212 retval.setAlignmentOptions(( short ) 0x20);
1213 retval.setIndentionOptions(( short ) 0xfffff400);
1214 retval.setBorderOptions(( short ) 0);
1215 retval.setPaletteOptions(( short ) 0);
1216 retval.setAdtlPaletteOptions(( short ) 0);
1217 retval.setFillPaletteOptions(( short ) 0x20c0);
1218 break;
1219
1220 case 5 :
1221 retval.setFontIndex(( short ) 0);
1222 retval.setFormatIndex(( short ) 0);
1223 retval.setCellOptions(( short ) 0xfffffff5);
1224 retval.setAlignmentOptions(( short ) 0x20);
1225 retval.setIndentionOptions(( short ) 0xfffff400);
1226 retval.setBorderOptions(( short ) 0);
1227 retval.setPaletteOptions(( short ) 0);
1228 retval.setAdtlPaletteOptions(( short ) 0);
1229 retval.setFillPaletteOptions(( short ) 0x20c0);
1230 break;
1231
1232 case 6 :
1233 retval.setFontIndex(( short ) 0);
1234 retval.setFormatIndex(( short ) 0);
1235 retval.setCellOptions(( short ) 0xfffffff5);
1236 retval.setAlignmentOptions(( short ) 0x20);
1237 retval.setIndentionOptions(( short ) 0xfffff400);
1238 retval.setBorderOptions(( short ) 0);
1239 retval.setPaletteOptions(( short ) 0);
1240 retval.setAdtlPaletteOptions(( short ) 0);
1241 retval.setFillPaletteOptions(( short ) 0x20c0);
1242 break;
1243
1244 case 7 :
1245 retval.setFontIndex(( short ) 0);
1246 retval.setFormatIndex(( short ) 0);
1247 retval.setCellOptions(( short ) 0xfffffff5);
1248 retval.setAlignmentOptions(( short ) 0x20);
1249 retval.setIndentionOptions(( short ) 0xfffff400);
1250 retval.setBorderOptions(( short ) 0);
1251 retval.setPaletteOptions(( short ) 0);
1252 retval.setAdtlPaletteOptions(( short ) 0);
1253 retval.setFillPaletteOptions(( short ) 0x20c0);
1254 break;
1255
1256 case 8 :
1257 retval.setFontIndex(( short ) 0);
1258 retval.setFormatIndex(( short ) 0);
1259 retval.setCellOptions(( short ) 0xfffffff5);
1260 retval.setAlignmentOptions(( short ) 0x20);
1261 retval.setIndentionOptions(( short ) 0xfffff400);
1262 retval.setBorderOptions(( short ) 0);
1263 retval.setPaletteOptions(( short ) 0);
1264 retval.setAdtlPaletteOptions(( short ) 0);
1265 retval.setFillPaletteOptions(( short ) 0x20c0);
1266 break;
1267
1268 case 9 :
1269 retval.setFontIndex(( short ) 0);
1270 retval.setFormatIndex(( short ) 0);
1271 retval.setCellOptions(( short ) 0xfffffff5);
1272 retval.setAlignmentOptions(( short ) 0x20);
1273 retval.setIndentionOptions(( short ) 0xfffff400);
1274 retval.setBorderOptions(( short ) 0);
1275 retval.setPaletteOptions(( short ) 0);
1276 retval.setAdtlPaletteOptions(( short ) 0);
1277 retval.setFillPaletteOptions(( short ) 0x20c0);
1278 break;
1279
1280 case 10 :
1281 retval.setFontIndex(( short ) 0);
1282 retval.setFormatIndex(( short ) 0);
1283 retval.setCellOptions(( short ) 0xfffffff5);
1284 retval.setAlignmentOptions(( short ) 0x20);
1285 retval.setIndentionOptions(( short ) 0xfffff400);
1286 retval.setBorderOptions(( short ) 0);
1287 retval.setPaletteOptions(( short ) 0);
1288 retval.setAdtlPaletteOptions(( short ) 0);
1289 retval.setFillPaletteOptions(( short ) 0x20c0);
1290 break;
1291
1292 case 11 :
1293 retval.setFontIndex(( short ) 0);
1294 retval.setFormatIndex(( short ) 0);
1295 retval.setCellOptions(( short ) 0xfffffff5);
1296 retval.setAlignmentOptions(( short ) 0x20);
1297 retval.setIndentionOptions(( short ) 0xfffff400);
1298 retval.setBorderOptions(( short ) 0);
1299 retval.setPaletteOptions(( short ) 0);
1300 retval.setAdtlPaletteOptions(( short ) 0);
1301 retval.setFillPaletteOptions(( short ) 0x20c0);
1302 break;
1303
1304 case 12 :
1305 retval.setFontIndex(( short ) 0);
1306 retval.setFormatIndex(( short ) 0);
1307 retval.setCellOptions(( short ) 0xfffffff5);
1308 retval.setAlignmentOptions(( short ) 0x20);
1309 retval.setIndentionOptions(( short ) 0xfffff400);
1310 retval.setBorderOptions(( short ) 0);
1311 retval.setPaletteOptions(( short ) 0);
1312 retval.setAdtlPaletteOptions(( short ) 0);
1313 retval.setFillPaletteOptions(( short ) 0x20c0);
1314 break;
1315
1316 case 13 :
1317 retval.setFontIndex(( short ) 0);
1318 retval.setFormatIndex(( short ) 0);
1319 retval.setCellOptions(( short ) 0xfffffff5);
1320 retval.setAlignmentOptions(( short ) 0x20);
1321 retval.setIndentionOptions(( short ) 0xfffff400);
1322 retval.setBorderOptions(( short ) 0);
1323 retval.setPaletteOptions(( short ) 0);
1324 retval.setAdtlPaletteOptions(( short ) 0);
1325 retval.setFillPaletteOptions(( short ) 0x20c0);
1326 break;
1327
1328 case 14 :
1329 retval.setFontIndex(( short ) 0);
1330 retval.setFormatIndex(( short ) 0);
1331 retval.setCellOptions(( short ) 0xfffffff5);
1332 retval.setAlignmentOptions(( short ) 0x20);
1333 retval.setIndentionOptions(( short ) 0xfffff400);
1334 retval.setBorderOptions(( short ) 0);
1335 retval.setPaletteOptions(( short ) 0);
1336 retval.setAdtlPaletteOptions(( short ) 0);
1337 retval.setFillPaletteOptions(( short ) 0x20c0);
1338 break;
1339
1340
1341 case 15 :
1342 retval.setFontIndex(( short ) 0);
1343 retval.setFormatIndex(( short ) 0);
1344 retval.setCellOptions(( short ) 0x1);
1345 retval.setAlignmentOptions(( short ) 0x20);
1346 retval.setIndentionOptions(( short ) 0x0);
1347 retval.setBorderOptions(( short ) 0);
1348 retval.setPaletteOptions(( short ) 0);
1349 retval.setAdtlPaletteOptions(( short ) 0);
1350 retval.setFillPaletteOptions(( short ) 0x20c0);
1351 break;
1352
1353
1354 case 16 :
1355 retval.setFontIndex(( short ) 1);
1356 retval.setFormatIndex(( short ) 0x2b);
1357 retval.setCellOptions(( short ) 0xfffffff5);
1358 retval.setAlignmentOptions(( short ) 0x20);
1359 retval.setIndentionOptions(( short ) 0xfffff800);
1360 retval.setBorderOptions(( short ) 0);
1361 retval.setPaletteOptions(( short ) 0);
1362 retval.setAdtlPaletteOptions(( short ) 0);
1363 retval.setFillPaletteOptions(( short ) 0x20c0);
1364 break;
1365
1366 case 17 :
1367 retval.setFontIndex(( short ) 1);
1368 retval.setFormatIndex(( short ) 0x29);
1369 retval.setCellOptions(( short ) 0xfffffff5);
1370 retval.setAlignmentOptions(( short ) 0x20);
1371 retval.setIndentionOptions(( short ) 0xfffff800);
1372 retval.setBorderOptions(( short ) 0);
1373 retval.setPaletteOptions(( short ) 0);
1374 retval.setAdtlPaletteOptions(( short ) 0);
1375 retval.setFillPaletteOptions(( short ) 0x20c0);
1376 break;
1377
1378 case 18 :
1379 retval.setFontIndex(( short ) 1);
1380 retval.setFormatIndex(( short ) 0x2c);
1381 retval.setCellOptions(( short ) 0xfffffff5);
1382 retval.setAlignmentOptions(( short ) 0x20);
1383 retval.setIndentionOptions(( short ) 0xfffff800);
1384 retval.setBorderOptions(( short ) 0);
1385 retval.setPaletteOptions(( short ) 0);
1386 retval.setAdtlPaletteOptions(( short ) 0);
1387 retval.setFillPaletteOptions(( short ) 0x20c0);
1388 break;
1389
1390 case 19 :
1391 retval.setFontIndex(( short ) 1);
1392 retval.setFormatIndex(( short ) 0x2a);
1393 retval.setCellOptions(( short ) 0xfffffff5);
1394 retval.setAlignmentOptions(( short ) 0x20);
1395 retval.setIndentionOptions(( short ) 0xfffff800);
1396 retval.setBorderOptions(( short ) 0);
1397 retval.setPaletteOptions(( short ) 0);
1398 retval.setAdtlPaletteOptions(( short ) 0);
1399 retval.setFillPaletteOptions(( short ) 0x20c0);
1400 break;
1401
1402 case 20 :
1403 retval.setFontIndex(( short ) 1);
1404 retval.setFormatIndex(( short ) 0x9);
1405 retval.setCellOptions(( short ) 0xfffffff5);
1406 retval.setAlignmentOptions(( short ) 0x20);
1407 retval.setIndentionOptions(( short ) 0xfffff800);
1408 retval.setBorderOptions(( short ) 0);
1409 retval.setPaletteOptions(( short ) 0);
1410 retval.setAdtlPaletteOptions(( short ) 0);
1411 retval.setFillPaletteOptions(( short ) 0x20c0);
1412 break;
1413
1414
1415 case 21 :
1416 retval.setFontIndex(( short ) 5);
1417 retval.setFormatIndex(( short ) 0x0);
1418 retval.setCellOptions(( short ) 0x1);
1419 retval.setAlignmentOptions(( short ) 0x20);
1420 retval.setIndentionOptions(( short ) 0x800);
1421 retval.setBorderOptions(( short ) 0);
1422 retval.setPaletteOptions(( short ) 0);
1423 retval.setAdtlPaletteOptions(( short ) 0);
1424 retval.setFillPaletteOptions(( short ) 0x20c0);
1425 break;
1426
1427 case 22 :
1428 retval.setFontIndex(( short ) 6);
1429 retval.setFormatIndex(( short ) 0x0);
1430 retval.setCellOptions(( short ) 0x1);
1431 retval.setAlignmentOptions(( short ) 0x20);
1432 retval.setIndentionOptions(( short ) 0x5c00);
1433 retval.setBorderOptions(( short ) 0);
1434 retval.setPaletteOptions(( short ) 0);
1435 retval.setAdtlPaletteOptions(( short ) 0);
1436 retval.setFillPaletteOptions(( short ) 0x20c0);
1437 break;
1438
1439 case 23 :
1440 retval.setFontIndex(( short ) 0);
1441 retval.setFormatIndex(( short ) 0x31);
1442 retval.setCellOptions(( short ) 0x1);
1443 retval.setAlignmentOptions(( short ) 0x20);
1444 retval.setIndentionOptions(( short ) 0x5c00);
1445 retval.setBorderOptions(( short ) 0);
1446 retval.setPaletteOptions(( short ) 0);
1447 retval.setAdtlPaletteOptions(( short ) 0);
1448 retval.setFillPaletteOptions(( short ) 0x20c0);
1449 break;
1450
1451 case 24 :
1452 retval.setFontIndex(( short ) 0);
1453 retval.setFormatIndex(( short ) 0x8);
1454 retval.setCellOptions(( short ) 0x1);
1455 retval.setAlignmentOptions(( short ) 0x20);
1456 retval.setIndentionOptions(( short ) 0x5c00);
1457 retval.setBorderOptions(( short ) 0);
1458 retval.setPaletteOptions(( short ) 0);
1459 retval.setAdtlPaletteOptions(( short ) 0);
1460 retval.setFillPaletteOptions(( short ) 0x20c0);
1461 break;
1462
1463 case 25 :
1464 retval.setFontIndex(( short ) 6);
1465 retval.setFormatIndex(( short ) 0x8);
1466 retval.setCellOptions(( short ) 0x1);
1467 retval.setAlignmentOptions(( short ) 0x20);
1468 retval.setIndentionOptions(( short ) 0x5c00);
1469 retval.setBorderOptions(( short ) 0);
1470 retval.setPaletteOptions(( short ) 0);
1471 retval.setAdtlPaletteOptions(( short ) 0);
1472 retval.setFillPaletteOptions(( short ) 0x20c0);
1473 break;
1474 }
1475 return retval;
1476 }
1477
1478
1482
1483 protected ExtendedFormatRecord createExtendedFormat() {
1484 ExtendedFormatRecord retval = new ExtendedFormatRecord();
1485
1486 retval.setFontIndex(( short ) 0);
1487 retval.setFormatIndex(( short ) 0x0);
1488 retval.setCellOptions(( short ) 0x1);
1489 retval.setAlignmentOptions(( short ) 0x20);
1490 retval.setIndentionOptions(( short ) 0);
1491 retval.setBorderOptions(( short ) 0);
1492 retval.setPaletteOptions(( short ) 0);
1493 retval.setAdtlPaletteOptions(( short ) 0);
1494 retval.setFillPaletteOptions(( short ) 0x20c0);
1495 retval.setTopBorderPaletteIdx(HSSFColor.BLACK.index);
1496 retval.setBottomBorderPaletteIdx(HSSFColor.BLACK.index);
1497 retval.setLeftBorderPaletteIdx(HSSFColor.BLACK.index);
1498 retval.setRightBorderPaletteIdx(HSSFColor.BLACK.index);
1499 return retval;
1500 }
1501
1502
1510
1511 protected Record createStyle(int id) {
1512 StyleRecord retval = new StyleRecord();
1513
1514 switch (id) {
1515
1516 case 0 :
1517 retval.setIndex(( short ) 0xffff8010);
1518 retval.setBuiltin(( byte ) 3);
1519 retval.setOutlineStyleLevel(( byte ) 0xffffffff);
1520 break;
1521
1522 case 1 :
1523 retval.setIndex(( short ) 0xffff8011);
1524 retval.setBuiltin(( byte ) 6);
1525 retval.setOutlineStyleLevel(( byte ) 0xffffffff);
1526 break;
1527
1528 case 2 :
1529 retval.setIndex(( short ) 0xffff8012);
1530 retval.setBuiltin(( byte ) 4);
1531 retval.setOutlineStyleLevel(( byte ) 0xffffffff);
1532 break;
1533
1534 case 3 :
1535 retval.setIndex(( short ) 0xffff8013);
1536 retval.setBuiltin(( byte ) 7);
1537 retval.setOutlineStyleLevel(( byte ) 0xffffffff);
1538 break;
1539
1540 case 4 :
1541 retval.setIndex(( short ) 0xffff8000);
1542 retval.setBuiltin(( byte ) 0);
1543 retval.setOutlineStyleLevel(( byte ) 0xffffffff);
1544 break;
1545
1546 case 5 :
1547 retval.setIndex(( short ) 0xffff8014);
1548 retval.setBuiltin(( byte ) 5);
1549 retval.setOutlineStyleLevel(( byte ) 0xffffffff);
1550 break;
1551 }
1552 return retval;
1553 }
1554
1555
1561
1562 protected Record createUseSelFS() {
1563 UseSelFSRecord retval = new UseSelFSRecord();
1564
1565 retval.setFlag(( short ) 0);
1566 return retval;
1567 }
1568
1569
1577
1578 protected Record createBoundSheet(int id) {
1579 BoundSheetRecord retval = new BoundSheetRecord();
1580
1581 switch (id) {
1582
1583 case 0 :
1584 retval.setPositionOfBof(0x0);
1585 retval.setOptionFlags(( short ) 0);
1586 retval.setSheetnameLength(( byte ) 0x6);
1587 retval.setCompressedUnicodeFlag(( byte ) 0);
1588 retval.setSheetname("Sheet1");
1589 break;
1590
1591 case 1 :
1592 retval.setPositionOfBof(0x0);
1593 retval.setOptionFlags(( short ) 0);
1594 retval.setSheetnameLength(( byte ) 0x6);
1595 retval.setCompressedUnicodeFlag(( byte ) 0);
1596 retval.setSheetname("Sheet2");
1597 break;
1598
1599 case 2 :
1600 retval.setPositionOfBof(0x0);
1601 retval.setOptionFlags(( short ) 0);
1602 retval.setSheetnameLength(( byte ) 0x6);
1603 retval.setCompressedUnicodeFlag(( byte ) 0);
1604 retval.setSheetname("Sheet3");
1605 break;
1606 }
1607 return retval;
1608 }
1609
1610
1617
1618 protected Record createCountry() {
1619 CountryRecord retval = new CountryRecord();
1620
1621 retval.setDefaultCountry(( short ) 1);
1622
1623
1624 if ( Locale.getDefault().toString().equals( "ru_RU" ) ) {
1625 retval.setCurrentCountry(( short ) 7);
1626 }
1627 else {
1628 retval.setCurrentCountry(( short ) 1);
1629 }
1630
1631 return retval;
1632 }
1633
1634
1640
1641 protected Record createSST() {
1642 return new SSTRecord();
1643 }
1644
1645
1654
1655 protected Record createExtendedSST() {
1656 ExtSSTRecord retval = new ExtSSTRecord();
1657
1658 retval.setNumStringsPerBucket(( short ) 0x8);
1659 return retval;
1660 }
1661
1662
1668
1669 protected Record createEOF() {
1670 return new EOFRecord();
1671 }
1672
1673 public SheetReferences getSheetReferences() {
1674 SheetReferences refs = new SheetReferences();
1675
1676 if (externSheet != null) {
1677 for (int k = 0; k < externSheet.getNumOfREFStructures(); k++) {
1678 String sheetName = findSheetNameFromExternSheet((short)k);
1679 refs.addSheetReference(sheetName, k);
1680 }
1681 }
1682 return refs;
1683 }
1684
1685
1689 public String findSheetNameFromExternSheet(short num){
1690 String result;
1691
1692 short indexToSheet = externSheet.getREFRecordAt(num).getIndexToFirstSupBook();
1693 result = getSheetName(indexToSheet);
1694
1695 return result;
1696 }
1697
1698
1703 public short checkExternSheet(int sheetNumber){
1704
1705 int i = 0;
1706 boolean flag = false;
1707 short result = 0;
1708
1709 if (externSheet == null) {
1710 externSheet = createExternSheet();
1711 }
1712
1713
1714 while (i < externSheet.getNumOfREFStructures() && !flag){
1715 ExternSheetSubRecord record = externSheet.getREFRecordAt(i);
1716
1717 if (record.getIndexToFirstSupBook() == sheetNumber &&
1718 record.getIndexToLastSupBook() == sheetNumber){
1719 flag = true;
1720 result = (short) i;
1721 }
1722
1723 ++i;
1724 }
1725
1726
1727 if (!flag) {
1728 result = addSheetIndexToExternSheet((short) sheetNumber);
1729 }
1730
1731 return result;
1732 }
1733
1734 private short addSheetIndexToExternSheet(short sheetNumber){
1735 short result;
1736
1737 ExternSheetSubRecord record = new ExternSheetSubRecord();
1738 record.setIndexToFirstSupBook(sheetNumber);
1739 record.setIndexToLastSupBook(sheetNumber);
1740 externSheet.addREFRecord(record);
1741 externSheet.setNumOfREFStructures((short)(externSheet.getNumOfREFStructures() + 1));
1742 result = (short)(externSheet.getNumOfREFStructures() - 1);
1743
1744 return result;
1745 }
1746
1747
1748
1749
1752 public int getNumNames(){
1753 int result = names.size();
1754
1755 return result;
1756 }
1757
1758
1762 public NameRecord getNameRecord(int index){
1763 NameRecord result = (NameRecord) names.get(index);
1764
1765 return result;
1766
1767 }
1768
1769
1772 public NameRecord createName(){
1773
1774 NameRecord name = new NameRecord();
1775
1776 records.add(++namepos, name);
1777 names.add(name);
1778
1779 return name;
1780 }
1781
1782
1785 public void removeName(int namenum){
1786 if (names.size() > namenum) {
1787 records.remove(namepos - (names.size() - 1) + namenum);
1788 namepos--;
1789 names.remove(namenum);
1790 }
1791
1792 }
1793
1794
1797 protected ExternSheetRecord createExternSheet(){
1798 ExternSheetRecord rec = new ExternSheetRecord();
1799
1800 records.add(supbookpos + 1 , rec);
1801
1802
1803 SupBookRecord supbook = new SupBookRecord();
1804
1805 supbook.setNumberOfSheets((short)getNumSheets());
1806
1807
1808 records.add(supbookpos + 1 , supbook);
1809
1810 return rec;
1811 }
1812
1813
1819 public short getFormat(String format, boolean createIfNotFound) {
1820 Iterator iterator;
1821 for (iterator = formats.iterator(); iterator.hasNext();) {
1822 FormatRecord r = (FormatRecord)iterator.next();
1823 if (r.getFormatString().equals(format)) {
1824 return r.getIndexCode();
1825 }
1826 }
1827
1828 if (createIfNotFound) {
1829 return createFormat(format);
1830 }
1831
1832 return -1;
1833 }
1834
1835
1839 public ArrayList getFormats() {
1840 return formats;
1841 }
1842
1843
1850 public short createFormat(String format) {
1851 FormatRecord rec = new FormatRecord();
1852 maxformatid = maxformatid >= (short)0xa4 ? (short)(maxformatid + 1) : (short)0xa4;
1853 rec.setIndexCode(maxformatid);
1854 rec.setFormatStringLength((byte)format.length());
1855 rec.setFormatString(format);
1856
1857 int pos = 0;
1858 while (pos < records.size() && ((Record)records.get(pos)).getSid() != FormatRecord.sid)
1859 pos++;
1860 pos += formats.size();
1861 formats.add(rec);
1862 records.add(pos, rec);
1863 return maxformatid;
1864 }
1865
1866
1867
1870
1871 public Record findFirstRecordBySid(short sid) {
1872 for (Iterator iterator = records.iterator(); iterator.hasNext(); ) {
1873 Record record = ( Record ) iterator.next();
1874
1875 if (record.getSid() == sid) {
1876 return record;
1877 }
1878 }
1879 return null;
1880 }
1881
1882
1885 public Record findNextRecordBySid(short sid, int pos) {
1886 Iterator iterator = records.iterator();
1887 for (;pos > 0 && iterator.hasNext(); iterator.next(),pos--);
1888 while (iterator.hasNext()) {
1889 Record record = ( Record ) iterator.next();
1890
1891 if (record.getSid() == sid) {
1892 return record;
1893 }
1894 }
1895 return null;
1896 }
1897
1898 public List getRecords()
1899 {
1900 return records;
1901 }
1902 }
1903