PageRenderTime 427ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/components/forks/poi/src/loci/poi/hssf/model/Workbook.java

http://github.com/openmicroscopy/bioformats
Java | 2350 lines | 1394 code | 332 blank | 624 comment | 160 complexity | 2f47d8e4aa73ffbba0d056d2a6f68eee MD5 | raw file
Possible License(s): GPL-2.0, LGPL-2.1, Apache-2.0, BSD-2-Clause, MPL-2.0-no-copyleft-exception
  1. /*
  2. * #%L
  3. * Fork of Apache Jakarta POI.
  4. * %%
  5. * Copyright (C) 2008 - 2013 Open Microscopy Environment:
  6. * - Board of Regents of the University of Wisconsin-Madison
  7. * - Glencoe Software, Inc.
  8. * - University of Dundee
  9. * %%
  10. * Licensed under the Apache License, Version 2.0 (the "License");
  11. * you may not use this file except in compliance with the License.
  12. * You may obtain a copy of the License at
  13. *
  14. * http://www.apache.org/licenses/LICENSE-2.0
  15. *
  16. * Unless required by applicable law or agreed to in writing, software
  17. * distributed under the License is distributed on an "AS IS" BASIS,
  18. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  19. * See the License for the specific language governing permissions and
  20. * limitations under the License.
  21. * #L%
  22. */
  23. /* ====================================================================
  24. Licensed to the Apache Software Foundation (ASF) under one or more
  25. contributor license agreements. See the NOTICE file distributed with
  26. this work for additional information regarding copyright ownership.
  27. The ASF licenses this file to You under the Apache License, Version 2.0
  28. (the "License"); you may not use this file except in compliance with
  29. the License. You may obtain a copy of the License at
  30. http://www.apache.org/licenses/LICENSE-2.0
  31. Unless required by applicable law or agreed to in writing, software
  32. distributed under the License is distributed on an "AS IS" BASIS,
  33. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  34. See the License for the specific language governing permissions and
  35. limitations under the License.
  36. ==================================================================== */
  37. package loci.poi.hssf.model;
  38. import loci.poi.ddf.*;
  39. import loci.poi.hssf.record.*;
  40. import loci.poi.hssf.util.HSSFColor;
  41. import loci.poi.hssf.util.SheetReferences;
  42. import loci.poi.util.POILogFactory;
  43. import loci.poi.util.POILogger;
  44. import java.util.ArrayList;
  45. import java.util.Iterator;
  46. import java.util.List;
  47. import java.util.Locale;
  48. /**
  49. * Low level model implementation of a Workbook. Provides creational methods
  50. * for settings and objects contained in the workbook object.
  51. * <P>
  52. * This file contains the low level binary records starting at the workbook's BOF and
  53. * ending with the workbook's EOF. Use HSSFWorkbook for a high level representation.
  54. * <P>
  55. * The structures of the highlevel API use references to this to perform most of their
  56. * operations. Its probably unwise to use these low level structures directly unless you
  57. * really know what you're doing. I recommend you read the Microsoft Excel 97 Developer's
  58. * Kit (Microsoft Press) and the documentation at http://sc.openoffice.org/excelfileformat.pdf
  59. * before even attempting to use this.
  60. *
  61. *
  62. * @author Luc Girardin (luc dot girardin at macrofocus dot com)
  63. * @author Sergei Kozello (sergeikozello at mail.ru)
  64. * @author Shawn Laubach (slaubach at apache dot org) (Data Formats)
  65. * @author Andrew C. Oliver (acoliver at apache dot org)
  66. * @author Brian Sanders (bsanders at risklabs dot com) - custom palette
  67. * @author Dan Sherman (dsherman at isisph.com)
  68. * @author Glen Stampoultzis (glens at apache.org)
  69. * @see loci.poi.hssf.usermodel.HSSFWorkbook
  70. * @version 1.0-pre
  71. */
  72. public class Workbook implements Model
  73. {
  74. private static final int DEBUG = POILogger.DEBUG;
  75. // public static Workbook currentBook = null;
  76. /**
  77. * constant used to set the "codepage" wherever "codepage" is set in records
  78. * (which is duplciated in more than one record)
  79. */
  80. private final static short CODEPAGE = ( short ) 0x4b0;
  81. /**
  82. * this contains the Worksheet record objects
  83. */
  84. protected WorkbookRecordList records = new WorkbookRecordList();
  85. /**
  86. * this contains a reference to the SSTRecord so that new stings can be added
  87. * to it.
  88. */
  89. protected SSTRecord sst = null;
  90. /**
  91. * Holds the Extern Sheet with references to bound sheets
  92. */
  93. protected ExternSheetRecord externSheet= null;
  94. /**
  95. * holds the "boundsheet" records (aka bundlesheet) so that they can have their
  96. * reference to their "BOF" marker
  97. */
  98. protected ArrayList boundsheets = new ArrayList();
  99. protected ArrayList formats = new ArrayList();
  100. protected ArrayList names = new ArrayList();
  101. protected int numxfs = 0; // hold the number of extended format records
  102. protected int numfonts = 0; // hold the number of font records
  103. private short maxformatid = -1; // holds the max format id
  104. private boolean uses1904datewindowing = false; // whether 1904 date windowing is being used
  105. private DrawingManager2 drawingManager;
  106. private List escherBSERecords = new ArrayList(); // EscherBSERecord
  107. private WindowOneRecord windowOne;
  108. private FileSharingRecord fileShare;
  109. private WriteAccessRecord writeAccess;
  110. private WriteProtectRecord writeProtect;
  111. private static POILogger log = POILogFactory.getLogger(Workbook.class);
  112. /**
  113. * Creates new Workbook with no intitialization --useless right now
  114. * @see #createWorkbook(List)
  115. */
  116. public Workbook() {
  117. }
  118. /**
  119. * read support for low level
  120. * API. Pass in an array of Record objects, A Workbook
  121. * object is constructed and passed back with all of its initialization set
  122. * to the passed in records and references to those records held. Unlike Sheet
  123. * workbook does not use an offset (its assumed to be 0) since its first in a file.
  124. * If you need an offset then construct a new array with a 0 offset or write your
  125. * own ;-p.
  126. *
  127. * @param recs an array of Record objects
  128. * @return Workbook object
  129. */
  130. public static Workbook createWorkbook(List recs) {
  131. if (log.check( POILogger.DEBUG ))
  132. log.log(DEBUG, "Workbook (readfile) created with reclen=",
  133. new Integer(recs.size()));
  134. Workbook retval = new Workbook();
  135. ArrayList records = new ArrayList(recs.size() / 3);
  136. for (int k = 0; k < recs.size(); k++) {
  137. Record rec = ( Record ) recs.get(k);
  138. if (rec.getSid() == EOFRecord.sid) {
  139. records.add(rec);
  140. if (log.check( POILogger.DEBUG ))
  141. log.log(DEBUG, "found workbook eof record at " + k);
  142. break;
  143. }
  144. switch (rec.getSid()) {
  145. case BoundSheetRecord.sid :
  146. if (log.check( POILogger.DEBUG ))
  147. log.log(DEBUG, "found boundsheet record at " + k);
  148. retval.boundsheets.add(rec);
  149. retval.records.setBspos( k );
  150. break;
  151. case SSTRecord.sid :
  152. if (log.check( POILogger.DEBUG ))
  153. log.log(DEBUG, "found sst record at " + k);
  154. retval.sst = ( SSTRecord ) rec;
  155. break;
  156. case FontRecord.sid :
  157. if (log.check( POILogger.DEBUG ))
  158. log.log(DEBUG, "found font record at " + k);
  159. retval.records.setFontpos( k );
  160. retval.numfonts++;
  161. break;
  162. case ExtendedFormatRecord.sid :
  163. if (log.check( POILogger.DEBUG ))
  164. log.log(DEBUG, "found XF record at " + k);
  165. retval.records.setXfpos( k );
  166. retval.numxfs++;
  167. break;
  168. case TabIdRecord.sid :
  169. if (log.check( POILogger.DEBUG ))
  170. log.log(DEBUG, "found tabid record at " + k);
  171. retval.records.setTabpos( k );
  172. break;
  173. case ProtectRecord.sid :
  174. if (log.check( POILogger.DEBUG ))
  175. log.log(DEBUG, "found protect record at " + k);
  176. retval.records.setProtpos( k );
  177. break;
  178. case BackupRecord.sid :
  179. if (log.check( POILogger.DEBUG ))
  180. log.log(DEBUG, "found backup record at " + k);
  181. retval.records.setBackuppos( k );
  182. break;
  183. case ExternSheetRecord.sid :
  184. if (log.check( POILogger.DEBUG ))
  185. log.log(DEBUG, "found extern sheet record at " + k);
  186. retval.externSheet = ( ExternSheetRecord ) rec;
  187. break;
  188. case NameRecord.sid :
  189. if (log.check( POILogger.DEBUG ))
  190. log.log(DEBUG, "found name record at " + k);
  191. retval.names.add(rec);
  192. // retval.records.namepos = k;
  193. break;
  194. case SupBookRecord.sid :
  195. if (log.check( POILogger.DEBUG ))
  196. log.log(DEBUG, "found SupBook record at " + k);
  197. // retval.records.supbookpos = k;
  198. break;
  199. case FormatRecord.sid :
  200. if (log.check( POILogger.DEBUG ))
  201. log.log(DEBUG, "found format record at " + k);
  202. retval.formats.add(rec);
  203. retval.maxformatid = retval.maxformatid >= ((FormatRecord)rec).getIndexCode() ? retval.maxformatid : ((FormatRecord)rec).getIndexCode();
  204. break;
  205. case DateWindow1904Record.sid :
  206. if (log.check( POILogger.DEBUG ))
  207. log.log(DEBUG, "found datewindow1904 record at " + k);
  208. retval.uses1904datewindowing = ((DateWindow1904Record)rec).getWindowing() == 1;
  209. break;
  210. case PaletteRecord.sid:
  211. if (log.check( POILogger.DEBUG ))
  212. log.log(DEBUG, "found palette record at " + k);
  213. retval.records.setPalettepos( k );
  214. break;
  215. case WindowOneRecord.sid:
  216. if (log.check( POILogger.DEBUG ))
  217. log.log(DEBUG, "found WindowOneRecord at " + k);
  218. retval.windowOne = (WindowOneRecord) rec;
  219. break;
  220. case WriteAccessRecord.sid:
  221. if (log.check( POILogger.DEBUG ))
  222. log.log(DEBUG, "found WriteAccess at " + k);
  223. retval.writeAccess = (WriteAccessRecord) rec;
  224. break;
  225. case WriteProtectRecord.sid:
  226. if (log.check( POILogger.DEBUG ))
  227. log.log(DEBUG, "found WriteProtect at " + k);
  228. retval.writeProtect = (WriteProtectRecord) rec;
  229. break;
  230. case FileSharingRecord.sid:
  231. if (log.check( POILogger.DEBUG ))
  232. log.log(DEBUG, "found FileSharing at " + k);
  233. retval.fileShare = (FileSharingRecord) rec;
  234. default :
  235. }
  236. records.add(rec);
  237. }
  238. //What if we dont have any ranges and supbooks
  239. // if (retval.records.supbookpos == 0) {
  240. // retval.records.supbookpos = retval.records.bspos + 1;
  241. // retval.records.namepos = retval.records.supbookpos + 1;
  242. // }
  243. retval.records.setRecords(records);
  244. if (retval.windowOne == null) {
  245. retval.windowOne = (WindowOneRecord) retval.createWindowOne();
  246. }
  247. if (log.check( POILogger.DEBUG ))
  248. log.log(DEBUG, "exit create workbook from existing file function");
  249. return retval;
  250. }
  251. /**
  252. * Creates an empty workbook object with three blank sheets and all the empty
  253. * fields. Use this to create a workbook from scratch.
  254. */
  255. public static Workbook createWorkbook()
  256. {
  257. if (log.check( POILogger.DEBUG ))
  258. log.log( DEBUG, "creating new workbook from scratch" );
  259. Workbook retval = new Workbook();
  260. ArrayList records = new ArrayList( 30 );
  261. ArrayList formats = new ArrayList( 8 );
  262. records.add( retval.createBOF() );
  263. records.add( retval.createInterfaceHdr() );
  264. records.add( retval.createMMS() );
  265. records.add( retval.createInterfaceEnd() );
  266. records.add( retval.createWriteAccess() );
  267. records.add( retval.createCodepage() );
  268. records.add( retval.createDSF() );
  269. records.add( retval.createTabId() );
  270. retval.records.setTabpos( records.size() - 1 );
  271. records.add( retval.createFnGroupCount() );
  272. records.add( retval.createWindowProtect() );
  273. records.add( retval.createProtect() );
  274. retval.records.setProtpos( records.size() - 1 );
  275. records.add( retval.createPassword() );
  276. records.add( retval.createProtectionRev4() );
  277. records.add( retval.createPasswordRev4() );
  278. retval.windowOne = (WindowOneRecord) retval.createWindowOne();
  279. records.add( retval.windowOne );
  280. records.add( retval.createBackup() );
  281. retval.records.setBackuppos( records.size() - 1 );
  282. records.add( retval.createHideObj() );
  283. records.add( retval.createDateWindow1904() );
  284. records.add( retval.createPrecision() );
  285. records.add( retval.createRefreshAll() );
  286. records.add( retval.createBookBool() );
  287. records.add( retval.createFont() );
  288. records.add( retval.createFont() );
  289. records.add( retval.createFont() );
  290. records.add( retval.createFont() );
  291. retval.records.setFontpos( records.size() - 1 ); // last font record postion
  292. retval.numfonts = 4;
  293. // set up format records
  294. for ( int i = 0; i <= 7; i++ )
  295. {
  296. Record rec;
  297. rec = retval.createFormat( i );
  298. retval.maxformatid = retval.maxformatid >= ( (FormatRecord) rec ).getIndexCode() ? retval.maxformatid : ( (FormatRecord) rec ).getIndexCode();
  299. formats.add( rec );
  300. records.add( rec );
  301. }
  302. retval.formats = formats;
  303. for ( int k = 0; k < 21; k++ )
  304. {
  305. records.add( retval.createExtendedFormat( k ) );
  306. retval.numxfs++;
  307. }
  308. retval.records.setXfpos( records.size() - 1 );
  309. for ( int k = 0; k < 6; k++ )
  310. {
  311. records.add( retval.createStyle( k ) );
  312. }
  313. records.add( retval.createUseSelFS() );
  314. for ( int k = 0; k < 1; k++ )
  315. { // now just do 1
  316. BoundSheetRecord bsr =
  317. (BoundSheetRecord) retval.createBoundSheet( k );
  318. records.add( bsr );
  319. retval.boundsheets.add( bsr );
  320. retval.records.setBspos( records.size() - 1 );
  321. }
  322. // retval.records.supbookpos = retval.records.bspos + 1;
  323. // retval.records.namepos = retval.records.supbookpos + 2;
  324. records.add( retval.createCountry() );
  325. retval.sst = (SSTRecord) retval.createSST();
  326. records.add( retval.sst );
  327. records.add( retval.createExtendedSST() );
  328. records.add( retval.createEOF() );
  329. retval.records.setRecords(records);
  330. if (log.check( POILogger.DEBUG ))
  331. log.log( DEBUG, "exit create new workbook from scratch" );
  332. return retval;
  333. }
  334. /**Retrieves the Builtin NameRecord that matches the name and index
  335. * There shouldn't be too many names to make the sequential search too slow
  336. * @param name byte representation of the builtin name to match
  337. * @param sheetIndex Index to match
  338. * @return null if no builtin NameRecord matches
  339. */
  340. public NameRecord getSpecificBuiltinRecord(byte name, int sheetIndex)
  341. {
  342. Iterator iterator = names.iterator();
  343. while (iterator.hasNext()) {
  344. NameRecord record = ( NameRecord ) iterator.next();
  345. //print areas are one based
  346. if (record.getBuiltInName() == name && record.getIndexToSheet() == sheetIndex) {
  347. return record;
  348. }
  349. }
  350. return null;
  351. }
  352. /**
  353. * Removes the specified Builtin NameRecord that matches the name and index
  354. * @param name byte representation of the builtin to match
  355. * @param sheetIndex zero-based sheet reference
  356. */
  357. public void removeBuiltinRecord(byte name, int sheetIndex) {
  358. //the name array is smaller so searching through it should be faster than
  359. //using the findFirstXXXX methods
  360. NameRecord record = getSpecificBuiltinRecord(name, sheetIndex);
  361. if (record != null) {
  362. names.remove(record);
  363. }
  364. }
  365. public int getNumRecords() {
  366. return records.size();
  367. }
  368. /**
  369. * gets the font record at the given index in the font table. Remember
  370. * "There is No Four" (someone at M$ must have gone to Rocky Horror one too
  371. * many times)
  372. *
  373. * @param idx the index to look at (0 or greater but NOT 4)
  374. * @return FontRecord located at the given index
  375. */
  376. public FontRecord getFontRecordAt(int idx) {
  377. int index = idx;
  378. if (index > 4) {
  379. index -= 1; // adjust for "There is no 4"
  380. }
  381. if (index > (numfonts - 1)) {
  382. throw new ArrayIndexOutOfBoundsException(
  383. "There are only " + numfonts
  384. + " font records, you asked for " + idx);
  385. }
  386. FontRecord retval =
  387. ( FontRecord ) records.get((records.getFontpos() - (numfonts - 1)) + index);
  388. return retval;
  389. }
  390. /**
  391. * creates a new font record and adds it to the "font table". This causes the
  392. * boundsheets to move down one, extended formats to move down (so this function moves
  393. * those pointers as well)
  394. *
  395. * @return FontRecord that was just created
  396. */
  397. public FontRecord createNewFont() {
  398. FontRecord rec = ( FontRecord ) createFont();
  399. records.add(records.getFontpos()+1, rec);
  400. records.setFontpos( records.getFontpos() + 1 );
  401. numfonts++;
  402. return rec;
  403. }
  404. /**
  405. * gets the number of font records
  406. *
  407. * @return number of font records in the "font table"
  408. */
  409. public int getNumberOfFontRecords() {
  410. return numfonts;
  411. }
  412. /**
  413. * Sets the BOF for a given sheet
  414. *
  415. * @param sheetnum the number of the sheet to set the positing of the bof for
  416. * @param pos the actual bof position
  417. */
  418. public void setSheetBof(int sheetnum, int pos) {
  419. if (log.check( POILogger.DEBUG ))
  420. log.log(DEBUG, "setting bof for sheetnum =", new Integer(sheetnum),
  421. " at pos=", new Integer(pos));
  422. checkSheets(sheetnum);
  423. (( BoundSheetRecord ) boundsheets.get(sheetnum))
  424. .setPositionOfBof(pos);
  425. }
  426. /**
  427. * Returns the position of the backup record.
  428. */
  429. public BackupRecord getBackupRecord() {
  430. return ( BackupRecord ) records.get(records.getBackuppos());
  431. }
  432. /**
  433. * sets the name for a given sheet. If the boundsheet record doesn't exist and
  434. * its only one more than we have, go ahead and create it. If its > 1 more than
  435. * we have, except
  436. *
  437. * @param sheetnum the sheet number (0 based)
  438. * @param sheetname the name for the sheet
  439. */
  440. public void setSheetName(int sheetnum, String sheetname ) {
  441. checkSheets(sheetnum);
  442. BoundSheetRecord sheet = (BoundSheetRecord)boundsheets.get( sheetnum );
  443. sheet.setSheetname(sheetname);
  444. sheet.setSheetnameLength( (byte)sheetname.length() );
  445. }
  446. /**
  447. * Determines whether a workbook contains the privided sheet name.
  448. *
  449. * @param name the name to test
  450. * @param excludeSheetIdx the sheet to exclude from the check or -1 to include all sheets in the check.
  451. * @return true if the sheet contains the name, false otherwise.
  452. */
  453. public boolean doesContainsSheetName( String name, int excludeSheetIdx )
  454. {
  455. for ( int i = 0; i < boundsheets.size(); i++ )
  456. {
  457. BoundSheetRecord boundSheetRecord = (BoundSheetRecord) boundsheets.get( i );
  458. if (excludeSheetIdx != i && name.equals(boundSheetRecord.getSheetname()))
  459. return true;
  460. }
  461. return false;
  462. }
  463. /**
  464. * sets the name for a given sheet forcing the encoding. This is STILL A BAD IDEA.
  465. * Poi now automatically detects unicode
  466. *
  467. *@deprecated 3-Jan-06 Simply use setSheetNam e(int sheetnum, String sheetname)
  468. * @param sheetnum the sheet number (0 based)
  469. * @param sheetname the name for the sheet
  470. */
  471. public void setSheetName(int sheetnum, String sheetname, short encoding ) {
  472. checkSheets(sheetnum);
  473. BoundSheetRecord sheet = (BoundSheetRecord)boundsheets.get( sheetnum );
  474. sheet.setSheetname(sheetname);
  475. sheet.setSheetnameLength( (byte)sheetname.length() );
  476. sheet.setCompressedUnicodeFlag( (byte)encoding );
  477. }
  478. /**
  479. * sets the order of appearance for a given sheet.
  480. *
  481. * @param sheetname the name of the sheet to reorder
  482. * @param pos the position that we want to insert the sheet into (0 based)
  483. */
  484. public void setSheetOrder(String sheetname, int pos ) {
  485. int sheetNumber = getSheetIndex(sheetname);
  486. //remove the sheet that needs to be reordered and place it in the spot we want
  487. boundsheets.add(pos, boundsheets.remove(sheetNumber));
  488. }
  489. /**
  490. * gets the name for a given sheet.
  491. *
  492. * @param sheetnum the sheet number (0 based)
  493. * @return sheetname the name for the sheet
  494. */
  495. public String getSheetName(int sheetnum) {
  496. return (( BoundSheetRecord ) boundsheets.get(sheetnum))
  497. .getSheetname();
  498. }
  499. /**
  500. * get the sheet's index
  501. * @param name sheet name
  502. * @return sheet index or -1 if it was not found.
  503. */
  504. public int getSheetIndex(String name) {
  505. int retval = -1;
  506. for (int k = 0; k < boundsheets.size(); k++) {
  507. String sheet = getSheetName(k);
  508. if (sheet.equalsIgnoreCase(name)) {
  509. retval = k;
  510. break;
  511. }
  512. }
  513. return retval;
  514. }
  515. /**
  516. * if we're trying to address one more sheet than we have, go ahead and add it! if we're
  517. * trying to address >1 more than we have throw an exception!
  518. */
  519. private void checkSheets(int sheetnum) {
  520. if ((boundsheets.size()) <= sheetnum) { // if we're short one add another..
  521. if ((boundsheets.size() + 1) <= sheetnum) {
  522. throw new RuntimeException("Sheet number out of bounds!");
  523. }
  524. BoundSheetRecord bsr = (BoundSheetRecord ) createBoundSheet(sheetnum);
  525. records.add(records.getBspos()+1, bsr);
  526. records.setBspos( records.getBspos() + 1 );
  527. boundsheets.add(bsr);
  528. fixTabIdRecord();
  529. }
  530. }
  531. public void removeSheet(int sheetnum) {
  532. if (boundsheets.size() > sheetnum) {
  533. records.remove(records.getBspos() - (boundsheets.size() - 1) + sheetnum);
  534. // records.bspos--;
  535. boundsheets.remove(sheetnum);
  536. fixTabIdRecord();
  537. }
  538. }
  539. /**
  540. * make the tabid record look like the current situation.
  541. *
  542. */
  543. private void fixTabIdRecord() {
  544. TabIdRecord tir = ( TabIdRecord ) records.get(records.getTabpos());
  545. short[] tia = new short[ boundsheets.size() ];
  546. for (short k = 0; k < tia.length; k++) {
  547. tia[ k ] = k;
  548. }
  549. tir.setTabIdArray(tia);
  550. }
  551. /**
  552. * returns the number of boundsheet objects contained in this workbook.
  553. *
  554. * @return number of BoundSheet records
  555. */
  556. public int getNumSheets() {
  557. if (log.check( POILogger.DEBUG ))
  558. log.log(DEBUG, "getNumSheets=", new Integer(boundsheets.size()));
  559. return boundsheets.size();
  560. }
  561. /**
  562. * get the number of ExtendedFormat records contained in this workbook.
  563. *
  564. * @return int count of ExtendedFormat records
  565. */
  566. public int getNumExFormats() {
  567. if (log.check( POILogger.DEBUG ))
  568. log.log(DEBUG, "getXF=", new Integer(numxfs));
  569. return numxfs;
  570. }
  571. /**
  572. * gets the ExtendedFormatRecord at the given 0-based index
  573. *
  574. * @param index of the Extended format record (0-based)
  575. * @return ExtendedFormatRecord at the given index
  576. */
  577. public ExtendedFormatRecord getExFormatAt(int index) {
  578. int xfptr = records.getXfpos() - (numxfs - 1);
  579. xfptr += index;
  580. ExtendedFormatRecord retval =
  581. ( ExtendedFormatRecord ) records.get(xfptr);
  582. return retval;
  583. }
  584. /**
  585. * creates a new Cell-type Extneded Format Record and adds it to the end of
  586. * ExtendedFormatRecords collection
  587. *
  588. * @return ExtendedFormatRecord that was created
  589. */
  590. public ExtendedFormatRecord createCellXF() {
  591. ExtendedFormatRecord xf = createExtendedFormat();
  592. records.add(records.getXfpos()+1, xf);
  593. records.setXfpos( records.getXfpos() + 1 );
  594. numxfs++;
  595. return xf;
  596. }
  597. /**
  598. * Adds a string to the SST table and returns its index (if its a duplicate
  599. * just returns its index and update the counts) ASSUMES compressed unicode
  600. * (meaning 8bit)
  601. *
  602. * @param string the string to be added to the SSTRecord
  603. *
  604. * @return index of the string within the SSTRecord
  605. */
  606. public int addSSTString(UnicodeString string) {
  607. if (log.check( POILogger.DEBUG ))
  608. log.log(DEBUG, "insert to sst string='", string);
  609. if (sst == null) {
  610. insertSST();
  611. }
  612. return sst.addString(string);
  613. }
  614. /**
  615. * given an index into the SST table, this function returns the corresponding String value
  616. * @return String containing the SST String
  617. */
  618. public UnicodeString getSSTString(int str) {
  619. if (sst == null) {
  620. insertSST();
  621. }
  622. UnicodeString retval = sst.getString(str);
  623. if (log.check( POILogger.DEBUG ))
  624. log.log(DEBUG, "Returning SST for index=", new Integer(str),
  625. " String= ", retval);
  626. return retval;
  627. }
  628. /**
  629. * use this function to add a Shared String Table to an existing sheet (say
  630. * generated by a different java api) without an sst....
  631. * @see #createSST()
  632. * @see loci.poi.hssf.record.SSTRecord
  633. */
  634. public void insertSST() {
  635. if (log.check( POILogger.DEBUG ))
  636. log.log(DEBUG, "creating new SST via insertSST!");
  637. sst = ( SSTRecord ) createSST();
  638. records.add(records.size() - 1, createExtendedSST());
  639. records.add(records.size() - 2, sst);
  640. }
  641. /**
  642. * Serializes all records int the worksheet section into a big byte array. Use
  643. * this to write the Workbook out.
  644. *
  645. * @return byte array containing the HSSF-only portions of the POIFS file.
  646. */
  647. // GJS: Not used so why keep it.
  648. // public byte [] serialize() {
  649. // log.log(DEBUG, "Serializing Workbook!");
  650. // byte[] retval = null;
  651. //
  652. //// ArrayList bytes = new ArrayList(records.size());
  653. // int arraysize = getSize();
  654. // int pos = 0;
  655. //
  656. // retval = new byte[ arraysize ];
  657. // for (int k = 0; k < records.size(); k++) {
  658. //
  659. // Record record = records.get(k);
  660. //// Let's skip RECALCID records, as they are only use for optimization
  661. // if(record.getSid() != RecalcIdRecord.sid || ((RecalcIdRecord)record).isNeeded()) {
  662. // pos += record.serialize(pos, retval); // rec.length;
  663. // }
  664. // }
  665. // log.log(DEBUG, "Exiting serialize workbook");
  666. // return retval;
  667. // }
  668. /**
  669. * Serializes all records int the worksheet section into a big byte array. Use
  670. * this to write the Workbook out.
  671. * @param offset of the data to be written
  672. * @param data array of bytes to write this to
  673. */
  674. public int serialize( int offset, byte[] data )
  675. {
  676. if (log.check( POILogger.DEBUG ))
  677. log.log( DEBUG, "Serializing Workbook with offsets" );
  678. int pos = 0;
  679. SSTRecord sst = null;
  680. int sstPos = 0;
  681. boolean wroteBoundSheets = false;
  682. for ( int k = 0; k < records.size(); k++ )
  683. {
  684. Record record = records.get( k );
  685. // Let's skip RECALCID records, as they are only use for optimization
  686. if ( record.getSid() != RecalcIdRecord.sid || ( (RecalcIdRecord) record ).isNeeded() )
  687. {
  688. int len = 0;
  689. if (record instanceof SSTRecord)
  690. {
  691. sst = (SSTRecord)record;
  692. sstPos = pos;
  693. }
  694. if (record.getSid() == ExtSSTRecord.sid && sst != null)
  695. {
  696. record = sst.createExtSSTRecord(sstPos + offset);
  697. }
  698. if (record instanceof BoundSheetRecord) {
  699. if(!wroteBoundSheets) {
  700. for (int i = 0; i < boundsheets.size(); i++) {
  701. len+= ((BoundSheetRecord)boundsheets.get(i))
  702. .serialize(pos+offset+len, data);
  703. }
  704. wroteBoundSheets = true;
  705. }
  706. } else {
  707. len = record.serialize( pos + offset, data );
  708. }
  709. ///// DEBUG BEGIN /////
  710. // if (len != record.getRecordSize())
  711. // throw new IllegalStateException("Record size does not match serialized bytes. Serialized size = " + len + " but getRecordSize() returns " + record.getRecordSize());
  712. ///// DEBUG END /////
  713. pos += len; // rec.length;
  714. }
  715. }
  716. if (log.check( POILogger.DEBUG ))
  717. log.log( DEBUG, "Exiting serialize workbook" );
  718. return pos;
  719. }
  720. public int getSize()
  721. {
  722. int retval = 0;
  723. SSTRecord sst = null;
  724. for ( int k = 0; k < records.size(); k++ )
  725. {
  726. Record record = records.get( k );
  727. // Let's skip RECALCID records, as they are only use for optimization
  728. if ( record.getSid() != RecalcIdRecord.sid || ( (RecalcIdRecord) record ).isNeeded() )
  729. {
  730. if (record instanceof SSTRecord)
  731. sst = (SSTRecord)record;
  732. if (record.getSid() == ExtSSTRecord.sid && sst != null)
  733. retval += sst.calcExtSSTRecordSize();
  734. else
  735. retval += record.getRecordSize();
  736. }
  737. }
  738. return retval;
  739. }
  740. /**
  741. * creates the BOF record
  742. * @see loci.poi.hssf.record.BOFRecord
  743. * @see loci.poi.hssf.record.Record
  744. * @return record containing a BOFRecord
  745. */
  746. protected Record createBOF() {
  747. BOFRecord retval = new BOFRecord();
  748. retval.setVersion(( short ) 0x600);
  749. retval.setType(( short ) 5);
  750. retval.setBuild(( short ) 0x10d3);
  751. // retval.setBuild((short)0x0dbb);
  752. retval.setBuildYear(( short ) 1996);
  753. retval.setHistoryBitMask(0x41); // was c1 before verify
  754. retval.setRequiredVersion(0x6);
  755. return retval;
  756. }
  757. /**
  758. * creates the InterfaceHdr record
  759. * @see loci.poi.hssf.record.InterfaceHdrRecord
  760. * @see loci.poi.hssf.record.Record
  761. * @return record containing a InterfaceHdrRecord
  762. */
  763. protected Record createInterfaceHdr() {
  764. InterfaceHdrRecord retval = new InterfaceHdrRecord();
  765. retval.setCodepage(CODEPAGE);
  766. return retval;
  767. }
  768. /**
  769. * creates an MMS record
  770. * @see loci.poi.hssf.record.MMSRecord
  771. * @see loci.poi.hssf.record.Record
  772. * @return record containing a MMSRecord
  773. */
  774. protected Record createMMS() {
  775. MMSRecord retval = new MMSRecord();
  776. retval.setAddMenuCount(( byte ) 0);
  777. retval.setDelMenuCount(( byte ) 0);
  778. return retval;
  779. }
  780. /**
  781. * creates the InterfaceEnd record
  782. * @see loci.poi.hssf.record.InterfaceEndRecord
  783. * @see loci.poi.hssf.record.Record
  784. * @return record containing a InterfaceEndRecord
  785. */
  786. protected Record createInterfaceEnd() {
  787. return new InterfaceEndRecord();
  788. }
  789. /**
  790. * creates the WriteAccess record containing the logged in user's name
  791. * @see loci.poi.hssf.record.WriteAccessRecord
  792. * @see loci.poi.hssf.record.Record
  793. * @return record containing a WriteAccessRecord
  794. */
  795. protected Record createWriteAccess() {
  796. WriteAccessRecord retval = new WriteAccessRecord();
  797. try
  798. {
  799. retval.setUsername(System.getProperty("user.name"));
  800. }
  801. catch (java.security.AccessControlException e)
  802. {
  803. // AccessControlException can occur in a restricted context
  804. // (client applet/jws application or restricted security server)
  805. retval.setUsername("POI");
  806. }
  807. return retval;
  808. }
  809. /**
  810. * creates the Codepage record containing the constant stored in CODEPAGE
  811. * @see loci.poi.hssf.record.CodepageRecord
  812. * @see loci.poi.hssf.record.Record
  813. * @return record containing a CodepageRecord
  814. */
  815. protected Record createCodepage() {
  816. CodepageRecord retval = new CodepageRecord();
  817. retval.setCodepage(CODEPAGE);
  818. return retval;
  819. }
  820. /**
  821. * creates the DSF record containing a 0 since HSSF can't even create Dual Stream Files
  822. * @see loci.poi.hssf.record.DSFRecord
  823. * @see loci.poi.hssf.record.Record
  824. * @return record containing a DSFRecord
  825. */
  826. protected Record createDSF() {
  827. DSFRecord retval = new DSFRecord();
  828. retval.setDsf(
  829. ( short ) 0); // we don't even support double stream files
  830. return retval;
  831. }
  832. /**
  833. * creates the TabId record containing an array of 0,1,2. This release of HSSF
  834. * always has the default three sheets, no less, no more.
  835. * @see loci.poi.hssf.record.TabIdRecord
  836. * @see loci.poi.hssf.record.Record
  837. * @return record containing a TabIdRecord
  838. */
  839. protected Record createTabId() {
  840. TabIdRecord retval = new TabIdRecord();
  841. short[] tabidarray = {
  842. 0
  843. };
  844. retval.setTabIdArray(tabidarray);
  845. return retval;
  846. }
  847. /**
  848. * creates the FnGroupCount record containing the Magic number constant of 14.
  849. * @see loci.poi.hssf.record.FnGroupCountRecord
  850. * @see loci.poi.hssf.record.Record
  851. * @return record containing a FnGroupCountRecord
  852. */
  853. protected Record createFnGroupCount() {
  854. FnGroupCountRecord retval = new FnGroupCountRecord();
  855. retval.setCount(( short ) 14);
  856. return retval;
  857. }
  858. /**
  859. * creates the WindowProtect record with protect set to false.
  860. * @see loci.poi.hssf.record.WindowProtectRecord
  861. * @see loci.poi.hssf.record.Record
  862. * @return record containing a WindowProtectRecord
  863. */
  864. protected Record createWindowProtect() {
  865. WindowProtectRecord retval = new WindowProtectRecord();
  866. retval.setProtect(
  867. false); // by default even when we support it we won't
  868. return retval; // want it to be protected
  869. }
  870. /**
  871. * creates the Protect record with protect set to false.
  872. * @see loci.poi.hssf.record.ProtectRecord
  873. * @see loci.poi.hssf.record.Record
  874. * @return record containing a ProtectRecord
  875. */
  876. protected Record createProtect() {
  877. ProtectRecord retval = new ProtectRecord();
  878. retval.setProtect(
  879. false); // by default even when we support it we won't
  880. return retval; // want it to be protected
  881. }
  882. /**
  883. * creates the Password record with password set to 0.
  884. * @see loci.poi.hssf.record.PasswordRecord
  885. * @see loci.poi.hssf.record.Record
  886. * @return record containing a PasswordRecord
  887. */
  888. protected Record createPassword() {
  889. PasswordRecord retval = new PasswordRecord();
  890. retval.setPassword(( short ) 0); // no password by default!
  891. return retval;
  892. }
  893. /**
  894. * creates the ProtectionRev4 record with protect set to false.
  895. * @see loci.poi.hssf.record.ProtectionRev4Record
  896. * @see loci.poi.hssf.record.Record
  897. * @return record containing a ProtectionRev4Record
  898. */
  899. protected Record createProtectionRev4() {
  900. ProtectionRev4Record retval = new ProtectionRev4Record();
  901. retval.setProtect(false);
  902. return retval;
  903. }
  904. /**
  905. * creates the PasswordRev4 record with password set to 0.
  906. * @see loci.poi.hssf.record.PasswordRev4Record
  907. * @see loci.poi.hssf.record.Record
  908. * @return record containing a PasswordRev4Record
  909. */
  910. protected Record createPasswordRev4() {
  911. PasswordRev4Record retval = new PasswordRev4Record();
  912. retval.setPassword(( short ) 0); // no password by default!
  913. return retval;
  914. }
  915. /**
  916. * creates the WindowOne record with the following magic values: <P>
  917. * horizontal hold - 0x168 <P>
  918. * vertical hold - 0x10e <P>
  919. * width - 0x3a5c <P>
  920. * height - 0x23be <P>
  921. * options - 0x38 <P>
  922. * selected tab - 0 <P>
  923. * displayed tab - 0 <P>
  924. * num selected tab- 0 <P>
  925. * tab width ratio - 0x258 <P>
  926. * @see loci.poi.hssf.record.WindowOneRecord
  927. * @see loci.poi.hssf.record.Record
  928. * @return record containing a WindowOneRecord
  929. */
  930. protected Record createWindowOne() {
  931. WindowOneRecord retval = new WindowOneRecord();
  932. retval.setHorizontalHold(( short ) 0x168);
  933. retval.setVerticalHold(( short ) 0x10e);
  934. retval.setWidth(( short ) 0x3a5c);
  935. retval.setHeight(( short ) 0x23be);
  936. retval.setOptions(( short ) 0x38);
  937. retval.setSelectedTab(( short ) 0x0);
  938. retval.setDisplayedTab(( short ) 0x0);
  939. retval.setNumSelectedTabs(( short ) 1);
  940. retval.setTabWidthRatio(( short ) 0x258);
  941. return retval;
  942. }
  943. /**
  944. * creates the Backup record with backup set to 0. (loose the data, who cares)
  945. * @see loci.poi.hssf.record.BackupRecord
  946. * @see loci.poi.hssf.record.Record
  947. * @return record containing a BackupRecord
  948. */
  949. protected Record createBackup() {
  950. BackupRecord retval = new BackupRecord();
  951. retval.setBackup(
  952. ( short ) 0); // by default DONT save backups of files...just loose data
  953. return retval;
  954. }
  955. /**
  956. * creates the HideObj record with hide object set to 0. (don't hide)
  957. * @see loci.poi.hssf.record.HideObjRecord
  958. * @see loci.poi.hssf.record.Record
  959. * @return record containing a HideObjRecord
  960. */
  961. protected Record createHideObj() {
  962. HideObjRecord retval = new HideObjRecord();
  963. retval.setHideObj(( short ) 0); // by default set hide object off
  964. return retval;
  965. }
  966. /**
  967. * creates the DateWindow1904 record with windowing set to 0. (don't window)
  968. * @see loci.poi.hssf.record.DateWindow1904Record
  969. * @see loci.poi.hssf.record.Record
  970. * @return record containing a DateWindow1904Record
  971. */
  972. protected Record createDateWindow1904() {
  973. DateWindow1904Record retval = new DateWindow1904Record();
  974. retval.setWindowing(
  975. ( short ) 0); // don't EVER use 1904 date windowing...tick tock..
  976. return retval;
  977. }
  978. /**
  979. * creates the Precision record with precision set to true. (full precision)
  980. * @see loci.poi.hssf.record.PrecisionRecord
  981. * @see loci.poi.hssf.record.Record
  982. * @return record containing a PrecisionRecord
  983. */
  984. protected Record createPrecision() {
  985. PrecisionRecord retval = new PrecisionRecord();
  986. retval.setFullPrecision(
  987. true); // always use real numbers in calculations!
  988. return retval;
  989. }
  990. /**
  991. * creates the RefreshAll record with refreshAll set to true. (refresh all calcs)
  992. * @see loci.poi.hssf.record.RefreshAllRecord
  993. * @see loci.poi.hssf.record.Record
  994. * @return record containing a RefreshAllRecord
  995. */
  996. protected Record createRefreshAll() {
  997. RefreshAllRecord retval = new RefreshAllRecord();
  998. retval.setRefreshAll(false);
  999. return retval;
  1000. }
  1001. /**
  1002. * creates the BookBool record with saveLinkValues set to 0. (don't save link values)
  1003. * @see loci.poi.hssf.record.BookBoolRecord
  1004. * @see loci.poi.hssf.record.Record
  1005. * @return record containing a BookBoolRecord
  1006. */
  1007. protected Record createBookBool() {
  1008. BookBoolRecord retval = new BookBoolRecord();
  1009. retval.setSaveLinkValues(( short ) 0);
  1010. return retval;
  1011. }
  1012. /**
  1013. * creates a Font record with the following magic values: <P>
  1014. * fontheight = 0xc8<P>
  1015. * attributes = 0x0<P>
  1016. * color palette index = 0x7fff<P>
  1017. * bold weight = 0x190<P>
  1018. * Font Name Length = 5 <P>
  1019. * Font Name = Arial <P>
  1020. *
  1021. * @see loci.poi.hssf.record.FontRecord
  1022. * @see loci.poi.hssf.record.Record
  1023. * @return record containing a FontRecord
  1024. */
  1025. protected Record createFont() {
  1026. FontRecord retval = new FontRecord();
  1027. retval.setFontHeight(( short ) 0xc8);
  1028. retval.setAttributes(( short ) 0x0);
  1029. retval.setColorPaletteIndex(( short ) 0x7fff);
  1030. retval.setBoldWeight(( short ) 0x190);
  1031. retval.setFontNameLength(( byte ) 5);
  1032. retval.setFontName("Arial");
  1033. return retval;
  1034. }
  1035. /**
  1036. * Creates a FormatRecord object
  1037. * @param id the number of the format record to create (meaning its position in
  1038. * a file as M$ Excel would create it.)
  1039. * @return record containing a FormatRecord
  1040. * @see loci.poi.hssf.record.FormatRecord
  1041. * @see loci.poi.hssf.record.Record
  1042. */
  1043. protected Record createFormat(int id) { // we'll need multiple editions for
  1044. FormatRecord retval = new FormatRecord(); // the differnt formats
  1045. switch (id) {
  1046. case 0 :
  1047. retval.setIndexCode(( short ) 5);
  1048. retval.setFormatStringLength(( byte ) 0x17);
  1049. retval.setFormatString("\"$\"#,##0_);\\(\"$\"#,##0\\)");
  1050. break;
  1051. case 1 :
  1052. retval.setIndexCode(( short ) 6);
  1053. retval.setFormatStringLength(( byte ) 0x1c);
  1054. retval.setFormatString("\"$\"#,##0_);[Red]\\(\"$\"#,##0\\)");
  1055. break;
  1056. case 2 :
  1057. retval.setIndexCode(( short ) 7);
  1058. retval.setFormatStringLength(( byte ) 0x1d);
  1059. retval.setFormatString("\"$\"#,##0.00_);\\(\"$\"#,##0.00\\)");
  1060. break;
  1061. case 3 :
  1062. retval.setIndexCode(( short ) 8);
  1063. retval.setFormatStringLength(( byte ) 0x22);
  1064. retval.setFormatString(
  1065. "\"$\"#,##0.00_);[Red]\\(\"$\"#,##0.00\\)");
  1066. break;
  1067. case 4 :
  1068. retval.setIndexCode(( short ) 0x2a);
  1069. retval.setFormatStringLength(( byte ) 0x32);
  1070. retval.setFormatString(
  1071. "_(\"$\"* #,##0_);_(\"$\"* \\(#,##0\\);_(\"$\"* \"-\"_);_(@_)");
  1072. break;
  1073. case 5 :
  1074. retval.setIndexCode(( short ) 0x29);
  1075. retval.setFormatStringLength(( byte ) 0x29);
  1076. retval.setFormatString(
  1077. "_(* #,##0_);_(* \\(#,##0\\);_(* \"-\"_);_(@_)");
  1078. break;
  1079. case 6 :
  1080. retval.setIndexCode(( short ) 0x2c);
  1081. retval.setFormatStringLength(( byte ) 0x3a);
  1082. retval.setFormatString(
  1083. "_(\"$\"* #,##0.00_);_(\"$\"* \\(#,##0.00\\);_(\"$\"* \"-\"??_);_(@_)");
  1084. break;
  1085. case 7 :
  1086. retval.setIndexCode(( short ) 0x2b);
  1087. retval.setFormatStringLength(( byte ) 0x31);
  1088. retval.setFormatString(
  1089. "_(* #,##0.00_);_(* \\(#,##0.00\\);_(* \"-\"??_);_(@_)");
  1090. break;
  1091. }
  1092. return retval;
  1093. }
  1094. /**
  1095. * Creates an ExtendedFormatRecord object
  1096. * @param id the number of the extended format record to create (meaning its position in
  1097. * a file as MS Excel would create it.)
  1098. *
  1099. * @return record containing an ExtendedFormatRecord
  1100. * @see loci.poi.hssf.record.ExtendedFormatRecord
  1101. * @see loci.poi.hssf.record.Record
  1102. */
  1103. protected Record createExtendedFormat(int id) { // we'll need multiple editions
  1104. ExtendedFormatRecord retval = new ExtendedFormatRecord();
  1105. switch (id) {
  1106. case 0 :
  1107. retval.setFontIndex(( short ) 0);
  1108. retval.setFormatIndex(( short ) 0);
  1109. retval.setCellOptions(( short ) 0xfffffff5);
  1110. retval.setAlignmentOptions(( short ) 0x20);
  1111. retval.setIndentionOptions(( short ) 0);
  1112. retval.setBorderOptions(( short ) 0);
  1113. retval.setPaletteOptions(( short ) 0);
  1114. retval.setAdtlPaletteOptions(( short ) 0);
  1115. retval.setFillPaletteOptions(( short ) 0x20c0);
  1116. break;
  1117. case 1 :
  1118. retval.setFontIndex(( short ) 1);
  1119. retval.setFormatIndex(( short ) 0);
  1120. retval.setCellOptions(( short ) 0xfffffff5);
  1121. retval.setAlignmentOptions(( short ) 0x20);
  1122. retval.setIndentionOptions(( short ) 0xfffff400);
  1123. retval.setBorderOptions(( short ) 0);
  1124. retval.setPaletteOptions(( short ) 0);
  1125. retval.setAdtlPaletteOptions(( short ) 0);
  1126. retval.setFillPaletteOptions(( short ) 0x20c0);
  1127. break;
  1128. case 2 :
  1129. retval.setFontIndex(( short ) 1);
  1130. retval.setFormatIndex(( short ) 0);
  1131. retval.setCellOptions(( short ) 0xfffffff5);
  1132. retval.setAlignmentOptions(( short ) 0x20);
  1133. retval.setIndentionOptions(( short ) 0xfffff400);
  1134. retval.setBorderOptions(( short ) 0);
  1135. retval.setPaletteOptions(( short ) 0);
  1136. retval.setAdtlPaletteOptions(( short ) 0);
  1137. retval.setFillPaletteOptions(( short ) 0x20c0);
  1138. break;
  1139. case 3 :
  1140. retval.setFontIndex(( short ) 2);
  1141. retval.setFormatIndex(( short ) 0);
  1142. retval.setCellOptions(( short ) 0xfffffff5);
  1143. retval.setAlignmentOptions(( short ) 0x20);
  1144. retval.setIndentionOptions(( short ) 0xfffff400);
  1145. retval.setBorderOptions(( short ) 0);
  1146. retval.setPaletteOptions(( short ) 0);
  1147. retval.setAdtlPaletteOptions(( short ) 0);
  1148. retval.setFillPaletteOptions(( short ) 0x20c0);
  1149. break;
  1150. case 4 :
  1151. retval.setFontIndex(( short ) 2);
  1152. retval.setFormatIndex(( short ) 0);
  1153. retval.setCellOptions(( short ) 0xfffffff5);
  1154. retval.setAlignmentOptions(( short ) 0x20);
  1155. retval.setIndentionOptions(( short ) 0xfffff400);
  1156. retval.setBorderOptions(( short ) 0);
  1157. retval.setPaletteOptions(( short ) 0);
  1158. retval.setAdtlPaletteOptions(( short ) 0);
  1159. retval.setFillPaletteOptions(( short ) 0x20c0);
  1160. break;
  1161. case 5 :
  1162. retval.setFontIndex(( short ) 0);
  1163. retval.setFormatIndex(( short ) 0);
  1164. retval.setCellOptions(( short ) 0xfffffff5);
  1165. retval.setAlignmentOptions(( short ) 0x20);
  1166. retval.setIndentionOptions(( short ) 0xfffff400);
  1167. retval.setBorderOptions(( short ) 0);
  1168. retval.setPaletteOptions(( short ) 0);
  1169. retval.setAdtlPaletteOptions(( short ) 0);
  1170. retval.setFillPaletteOptions(( short ) 0x20c0);
  1171. break;
  1172. case 6 :
  1173. retval.setFontIndex(( short ) 0);
  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. case 7 :
  1184. retval.setFontIndex(( short ) 0);
  1185. retval.setFormatIndex(( short ) 0);
  1186. retval.setCellOptions(( short ) 0xfffffff5);
  1187. retval.setAlignmentOptions(( short ) 0x20);
  1188. retval.setIndentionOptions(( short ) 0xfffff400);
  1189. retval.setBorderOptions(( short ) 0);
  1190. retval.setPaletteOptions(( short ) 0);
  1191. retval.setAdtlPaletteOptions(( short ) 0);
  1192. retval.setFillPaletteOptions(( short ) 0x20c0);
  1193. break;
  1194. case 8 :
  1195. retval.setFontIndex(( short ) 0);
  1196. retval.setFormatIndex(( short ) 0);
  1197. retval.setCellOptions(( short ) 0xfffffff5);
  1198. retval.setAlignmentOptions(( short ) 0x20);
  1199. retval.setIndentionOptions(( short ) 0xfffff400);
  1200. retval.setBorderOptions(( short ) 0);
  1201. retval.setPaletteOptions(( short ) 0);
  1202. retval.setAdtlPaletteOptions(( short ) 0);
  1203. retval.setFillPaletteOptions(( short ) 0x20c0);
  1204. break;
  1205. case 9 :
  1206. retval.setFontIndex(( short ) 0);
  1207. retval.setFormatIndex(( short ) 0);
  1208. retval.setCellOptions(( short ) 0xfffffff5);
  1209. retval.setAlignmentOptions(( short ) 0x20);
  1210. retval.setIndentionOptions(( short ) 0xfffff400);
  1211. retval.setBorderOptions(( short ) 0);
  1212. retval.setPaletteOptions(( short ) 0);
  1213. retval.setAdtlPaletteOptions(( short ) 0);
  1214. retval.setFillPaletteOptions(( short ) 0x20c0);
  1215. break;
  1216. case 10 :
  1217. retval.setFontIndex(( short ) 0);
  1218. retval.setFormatIndex(( short ) 0);
  1219. retval.setCellOptions(( short ) 0xfffffff5);
  1220. retval.setAlignmentOptions(( short ) 0x20);
  1221. retval.setIndentionOptions(( short ) 0xfffff400);
  1222. retval.setBorderOptions(( short ) 0);
  1223. retval.setPaletteOptions(( short ) 0);
  1224. retval.setAdtlPaletteOptions(( short ) 0);
  1225. retval.setFillPaletteOptions(( short ) 0x20c0);
  1226. break;
  1227. case 11 :
  1228. retval.setFontIndex(( short ) 0);
  1229. retval.setFormatIndex(( short ) 0);
  1230. retval.setCellOptions(( short ) 0xfffffff5);
  1231. retval.setAlignmentOptions(( short ) 0x20);
  1232. retval.setIndentionOptions(( short ) 0xfffff400);
  1233. retval.setBorderOptions(( short ) 0);
  1234. retval.setPaletteOptions(( short ) 0);
  1235. retval.setAdtlPaletteOptions(( short ) 0);
  1236. retval.setFillPaletteOptions(( short ) 0x20c0);
  1237. break;
  1238. case 12 :
  1239. retval.setFontIndex(( short ) 0);
  1240. retval.setFormatIndex(( short ) 0);
  1241. retval.setCellOptions(( short ) 0xfffffff5);
  1242. retval.setAlignmentOptions(( short ) 0x20);
  1243. retval.setIndentionOptions(( short ) 0xfffff400);
  1244. retval.setBorderOptions(( short ) 0);
  1245. retval.setPaletteOptions(( short ) 0);
  1246. retval.setAdtlPaletteOptions(( short ) 0);
  1247. retval.setFillPaletteOptions(( short ) 0x20c0);
  1248. break;
  1249. case 13 :
  1250. retval.setFontIndex(( short ) 0);
  1251. retval.setFormatIndex(( short ) 0);
  1252. retval.setCellOptions(( short ) 0xfffffff5);
  1253. retval.setAlignmentOptions(( short ) 0x20);
  1254. retval.setIndentionOptions(( short ) 0xfffff400);
  1255. retval.setBorderOptions(( short ) 0);
  1256. retval.setPaletteOptions(( short ) 0);
  1257. retval.setAdtlPaletteOptions(( short ) 0);
  1258. retval.setFillPaletteOptions(( short ) 0x20c0);
  1259. break;
  1260. case 14 :
  1261. retval.setFontIndex(( short ) 0);
  1262. retval.setFormatIndex(( short ) 0);
  1263. retval.setCellOptions(( short ) 0xfffffff5);
  1264. retval.setAlignmentOptions(( short ) 0x20);
  1265. retval.setIndentionOptions(( short ) 0xfffff400);
  1266. retval.setBorderOptions(( short ) 0);
  1267. retval.setPaletteOptions(( short ) 0);
  1268. retval.setAdtlPaletteOptions(( short ) 0);
  1269. retval.setFillPaletteOptions(( short ) 0x20c0);
  1270. break;
  1271. // cell records
  1272. case 15 :
  1273. retval.setFontIndex(( short ) 0);
  1274. retval.setFormatIndex(( short ) 0);
  1275. retval.setCellOptions(( short ) 0x1);
  1276. retval.setAlignmentOptions(( short ) 0x20);
  1277. retval.setIndentionOptions(( short ) 0x0);
  1278. retval.setBorderOptions(( short ) 0);
  1279. retval.setPaletteOptions(( short ) 0);
  1280. retval.setAdtlPaletteOptions(( short ) 0);
  1281. retval.setFillPaletteOptions(( short ) 0x20c0);
  1282. break;
  1283. // style
  1284. case 16 :
  1285. retval.setFontIndex(( short ) 1);
  1286. retval.setFormatIndex(( short ) 0x2b);
  1287. retval.setCellOptions(( short ) 0xfffffff5);
  1288. retval.setAlignmentOptions(( short ) 0x20);
  1289. retval.setIndentionOptions(( short ) 0xfffff800);
  1290. retval.setBorderOptions(( short ) 0);
  1291. retval.setPaletteOptions(( short ) 0);
  1292. retval.setAdtlPaletteOptions(( short ) 0);
  1293. retval.setFillPaletteOptions(( short ) 0x20c0);
  1294. break;
  1295. case 17 :
  1296. retval.setFontIndex(( short ) 1);
  1297. retval.setFormatIndex(( short ) 0x29);
  1298. retval.setCellOptions(( short ) 0xfffffff5);
  1299. retval.setAlignmentOptions(( short ) 0x20);
  1300. retval.setIndentionOptions(( short ) 0xfffff800);
  1301. retval.setBorderOptions(( short ) 0);
  1302. retval.setPaletteOptions(( short ) 0);
  1303. retval.setAdtlPaletteOptions(( short ) 0);
  1304. retval.setFillPaletteOptions(( short ) 0x20c0);
  1305. break;
  1306. case 18 :
  1307. retval.setFontIndex(( short ) 1);
  1308. retval.setFormatIndex(( short ) 0x2c);
  1309. retval.setCellOptions(( short ) 0xfffffff5);
  1310. retval.setAlignmentOptions(( short ) 0x20);
  1311. retval.setIndentionOptions(( short ) 0xfffff800);
  1312. retval.setBorderOptions(( short ) 0);
  1313. retval.setPaletteOptions(( short ) 0);
  1314. retval.setAdtlPaletteOptions(( short ) 0);
  1315. retval.setFillPaletteOptions(( short ) 0x20c0);
  1316. break;
  1317. case 19 :
  1318. retval.setFontIndex(( short ) 1);
  1319. retval.setFormatIndex(( short ) 0x2a);
  1320. retval.setCellOptions(( short ) 0xfffffff5);
  1321. retval.setAlignmentOptions(( short ) 0x20);
  1322. retval.setIndentionOptions(( short ) 0xfffff800);
  1323. retval.setBorderOptions(( short ) 0);
  1324. retval.setPaletteOptions(( short ) 0);
  1325. retval.setAdtlPaletteOptions(( short ) 0);
  1326. retval.setFillPaletteOptions(( short ) 0x20c0);
  1327. break;
  1328. case 20 :
  1329. retval.setFontIndex(( short ) 1);
  1330. retval.setFormatIndex(( short ) 0x9);
  1331. retval.setCellOptions(( short ) 0xfffffff5);
  1332. retval.setAlignmentOptions(( short ) 0x20);
  1333. retval.setIndentionOptions(( short ) 0xfffff800);
  1334. retval.setBorderOptions(( short ) 0);
  1335. retval.setPaletteOptions(( short ) 0);
  1336. retval.setAdtlPaletteOptions(( short ) 0);
  1337. retval.setFillPaletteOptions(( short ) 0x20c0);
  1338. break;
  1339. // unused from this point down
  1340. case 21 :
  1341. retval.setFontIndex(( short ) 5);
  1342. retval.setFormatIndex(( short ) 0x0);
  1343. retval.setCellOptions(( short ) 0x1);
  1344. retval.setAlignmentOptions(( short ) 0x20);
  1345. retval.setIndentionOptions(( short ) 0x800);
  1346. retval.setBorderOptions(( short ) 0);
  1347. retval.setPaletteOptions(( short ) 0);
  1348. retval.setAdtlPaletteOptions(( short ) 0);
  1349. retval.setFillPaletteOptions(( short ) 0x20c0);
  1350. break;
  1351. case 22 :
  1352. retval.setFontIndex(( short ) 6);
  1353. retval.setFormatIndex(( short ) 0x0);
  1354. retval.setCellOptions(( short ) 0x1);
  1355. retval.setAlignmentOptions(( short ) 0x20);
  1356. retval.setIndentionOptions(( short ) 0x5c00);
  1357. retval.setBorderOptions(( short ) 0);
  1358. retval.setPaletteOptions(( short ) 0);
  1359. retval.setAdtlPaletteOptions(( short ) 0);
  1360. retval.setFillPaletteOptions(( short ) 0x20c0);
  1361. break;
  1362. case 23 :
  1363. retval.setFontIndex(( short ) 0);
  1364. retval.setFormatIndex(( short ) 0x31);
  1365. retval.setCellOptions(( short ) 0x1);
  1366. retval.setAlignmentOptions(( short ) 0x20);
  1367. retval.setIndentionOptions(( short ) 0x5c00);
  1368. retval.setBorderOptions(( short ) 0);
  1369. retval.setPaletteOptions(( short ) 0);
  1370. retval.setAdtlPaletteOptions(( short ) 0);
  1371. retval.setFillPaletteOptions(( short ) 0x20c0);
  1372. break;
  1373. case 24 :
  1374. retval.setFontIndex(( short ) 0);
  1375. retval.setFormatIndex(( short ) 0x8);
  1376. retval.setCellOptions(( short ) 0x1);
  1377. retval.setAlignmentOptions(( short ) 0x20);
  1378. retval.setIndentionOptions(( short ) 0x5c00);
  1379. retval.setBorderOptions(( short ) 0);
  1380. retval.setPaletteOptions(( short ) 0);
  1381. retval.setAdtlPaletteOptions(( short ) 0);
  1382. retval.setFillPaletteOptions(( short ) 0x20c0);
  1383. break;
  1384. case 25 :
  1385. retval.setFontIndex(( short ) 6);
  1386. retval.setFormatIndex(( short ) 0x8);
  1387. retval.setCellOptions(( short ) 0x1);
  1388. retval.setAlignmentOptions(( short ) 0x20);
  1389. retval.setIndentionOptions(( short ) 0x5c00);
  1390. retval.setBorderOptions(( short ) 0);
  1391. retval.setPaletteOptions(( short ) 0);
  1392. retval.setAdtlPaletteOptions(( short ) 0);
  1393. retval.setFillPaletteOptions(( short ) 0x20c0);
  1394. break;
  1395. }
  1396. return retval;
  1397. }
  1398. /**
  1399. * creates an default cell type ExtendedFormatRecord object.
  1400. * @return ExtendedFormatRecord with intial defaults (cell-type)
  1401. */
  1402. protected ExtendedFormatRecord createExtendedFormat() {
  1403. ExtendedFormatRecord retval = new ExtendedFormatRecord();
  1404. retval.setFontIndex(( short ) 0);
  1405. retval.setFormatIndex(( short ) 0x0);
  1406. retval.setCellOptions(( short ) 0x1);
  1407. retval.setAlignmentOptions(( short ) 0x20);
  1408. retval.setIndentionOptions(( short ) 0);
  1409. retval.setBorderOptions(( short ) 0);
  1410. retval.setPaletteOptions(( short ) 0);
  1411. retval.setAdtlPaletteOptions(( short ) 0);
  1412. retval.setFillPaletteOptions(( short ) 0x20c0);
  1413. retval.setTopBorderPaletteIdx(HSSFColor.BLACK.index);
  1414. retval.setBottomBorderPaletteIdx(HSSFColor.BLACK.index);
  1415. retval.setLeftBorderPaletteIdx(HSSFColor.BLACK.index);
  1416. retval.setRightBorderPaletteIdx(HSSFColor.BLACK.index);
  1417. return retval;
  1418. }
  1419. /**
  1420. * Creates a StyleRecord object
  1421. * @param id the number of the style record to create (meaning its position in
  1422. * a file as MS Excel would create it.
  1423. * @return record containing a StyleRecord
  1424. * @see loci.poi.hssf.record.StyleRecord
  1425. * @see loci.poi.hssf.record.Record
  1426. */
  1427. protected Record createStyle(int id) { // we'll need multiple editions
  1428. StyleRecord retval = new StyleRecord();
  1429. switch (id) {
  1430. case 0 :
  1431. retval.setIndex(( short ) 0xffff8010);
  1432. retval.setBuiltin(( byte ) 3);
  1433. retval.setOutlineStyleLevel(( byte ) 0xffffffff);
  1434. break;
  1435. case 1 :
  1436. retval.setIndex(( short ) 0xffff8011);
  1437. retval.setBuiltin(( byte ) 6);
  1438. retval.setOutlineStyleLevel(( byte ) 0xffffffff);
  1439. break;
  1440. case 2 :
  1441. retval.setIndex(( short ) 0xffff8012);
  1442. retval.setBuiltin(( byte ) 4);
  1443. retval.setOutlineStyleLevel(( byte ) 0xffffffff);
  1444. break;
  1445. case 3 :
  1446. retval.setIndex(( short ) 0xffff8013);
  1447. retval.setBuiltin(( byte ) 7);
  1448. retval.setOutlineStyleLevel(( byte ) 0xffffffff);
  1449. break;
  1450. case 4 :
  1451. retval.setIndex(( short ) 0xffff8000);
  1452. retval.setBuiltin(( byte ) 0);
  1453. retval.setOutlineStyleLevel(( byte ) 0xffffffff);
  1454. break;
  1455. case 5 :
  1456. retval.setIndex(( short ) 0xffff8014);
  1457. retval.setBuiltin(( byte ) 5);
  1458. retval.setOutlineStyleLevel(( byte ) 0xffffffff);
  1459. break;
  1460. }
  1461. return retval;
  1462. }
  1463. /**
  1464. * Creates a palette record initialized to the default palette
  1465. * @return a PaletteRecord instance populated with the default colors
  1466. * @see loci.poi.hssf.record.PaletteRecord
  1467. */
  1468. protected PaletteRecord createPalette()
  1469. {
  1470. return new PaletteRecord();
  1471. }
  1472. /**
  1473. * Creates the UseSelFS object with the use natural language flag set to 0 (false)
  1474. * @return record containing a UseSelFSRecord
  1475. * @see loci.poi.hssf.record.UseSelFSRecord
  1476. * @see loci.poi.hssf.record.Record
  1477. */
  1478. protected Record createUseSelFS() {
  1479. UseSelFSRecord retval = new UseSelFSRecord();
  1480. retval.setFlag(( short ) 0);
  1481. return retval;
  1482. }
  1483. /**
  1484. * create a "bound sheet" or "bundlesheet" (depending who you ask) record
  1485. * Always sets the sheet's bof to 0. You'll need to set that yourself.
  1486. * @param id either sheet 0,1 or 2.
  1487. * @return record containing a BoundSheetRecord
  1488. * @see loci.poi.hssf.record.BoundSheetRecord
  1489. * @see loci.poi.hssf.record.Record
  1490. */
  1491. protected Record createBoundSheet(int id) { // 1,2,3 sheets
  1492. BoundSheetRecord retval = new BoundSheetRecord();
  1493. switch (id) {
  1494. case 0 :
  1495. retval.setPositionOfBof(0x0); // should be set later
  1496. retval.setOptionFlags(( short ) 0);
  1497. retval.setSheetnameLength(( byte ) 0x6);
  1498. retval.setCompressedUnicodeFlag(( byte ) 0);
  1499. retval.setSheetname("Sheet1");
  1500. break;
  1501. case 1 :
  1502. retval.setPositionOfBof(0x0); // should be set later
  1503. retval.setOptionFlags(( short ) 0);
  1504. retval.setSheetnameLength(( byte ) 0x6);
  1505. retval.setCompressedUnicodeFlag(( byte ) 0);
  1506. retval.setSheetname("Sheet2");
  1507. break;
  1508. case 2 :
  1509. retval.setPositionOfBof(0x0); // should be set later
  1510. retval.setOptionFlags(( short ) 0);
  1511. retval.setSheetnameLength(( byte ) 0x6);
  1512. retval.setCompressedUnicodeFlag(( byte ) 0);
  1513. retval.setSheetname("Sheet3");
  1514. break;
  1515. }
  1516. return retval;
  1517. }
  1518. /**
  1519. * Creates the Country record with the default country set to 1
  1520. * and current country set to 7 in case of russian locale ("ru_RU") and 1 otherwise
  1521. * @return record containing a CountryRecord
  1522. * @see loci.poi.hssf.record.CountryRecord
  1523. * @see loci.poi.hssf.record.Record
  1524. */
  1525. protected Record createCountry() { // what a novel idea, create your own!
  1526. CountryRecord retval = new CountryRecord();
  1527. retval.setDefaultCountry(( short ) 1);
  1528. // from Russia with love ;)
  1529. if ( Locale.getDefault().toString().equals( "ru_RU" ) ) {
  1530. retval.setCurrentCountry(( short ) 7);
  1531. }
  1532. else {
  1533. retval.setCurrentCountry(( short ) 1);
  1534. }
  1535. return retval;
  1536. }
  1537. /**
  1538. * Creates the SST record with no strings and the unique/num string set to 0
  1539. * @return record containing a SSTRecord
  1540. * @see loci.poi.hssf.record.SSTRecord
  1541. * @see loci.poi.hssf.record.Record
  1542. */
  1543. protected Record createSST() {
  1544. return new SSTRecord();
  1545. }
  1546. /**
  1547. * Creates the ExtendedSST record with numstrings per bucket set to 0x8. HSSF
  1548. * doesn't yet know what to do with this thing, but we create it with nothing in
  1549. * it hardly just to make Excel happy and our sheets look like Excel's
  1550. *
  1551. * @return record containing an ExtSSTRecord
  1552. * @see loci.poi.hssf.record.ExtSSTRecord
  1553. * @see loci.poi.hssf.record.Record
  1554. */
  1555. protected Record createExtendedSST() {
  1556. ExtSSTRecord retval = new ExtSSTRecord();
  1557. retval.setNumStringsPerBucket(( short ) 0x8);
  1558. return retval;
  1559. }
  1560. /**
  1561. * creates the EOF record
  1562. * @see loci.poi.hssf.record.EOFRecord
  1563. * @see loci.poi.hssf.record.Record
  1564. * @return record containing a EOFRecord
  1565. */
  1566. protected Record createEOF() {
  1567. return new EOFRecord();
  1568. }
  1569. public SheetReferences getSheetReferences() {
  1570. SheetReferences refs = new SheetReferences();
  1571. if (externSheet != null) {
  1572. for (int k = 0; k < externSheet.getNumOfREFStructures(); k++) {
  1573. String sheetName = findSheetNameFromExternSheet((short)k);
  1574. refs.addSheetReference(sheetName, k);
  1575. }
  1576. }
  1577. return refs;
  1578. }
  1579. /** finds the sheet name by his extern sheet index
  1580. * @param num extern sheet index
  1581. * @return sheet name
  1582. */
  1583. public String findSheetNameFromExternSheet(short num){
  1584. String result="";
  1585. short indexToSheet = externSheet.getREFRecordAt(num).getIndexToFirstSupBook();
  1586. if (indexToSheet>-1) { //error check, bail out gracefully!
  1587. result = getSheetName(indexToSheet);
  1588. }
  1589. return result;
  1590. }
  1591. /**
  1592. * Finds the sheet index for a particular external sheet number.
  1593. * @param externSheetNumber The external sheet number to convert
  1594. * @return The index to the sheet found.
  1595. */
  1596. public int getSheetIndexFromExternSheetIndex(int externSheetNumber)
  1597. {
  1598. if (externSheetNumber >= externSheet.getNumOfREFStructures())
  1599. return -1;
  1600. else
  1601. return externSheet.getREFRecordAt(externSheetNumber).getIndexToFirstSupBook();
  1602. }
  1603. /** returns the extern sheet number for specific sheet number ,
  1604. * if this sheet doesn't exist in extern sheet , add it
  1605. * @param sheetNumber sheet number
  1606. * @return index to extern sheet
  1607. */
  1608. public short checkExternSheet(int sheetNumber){
  1609. int i = 0;
  1610. boolean flag = false;
  1611. short result = 0;
  1612. if (externSheet == null) {
  1613. externSheet = createExternSheet();
  1614. }
  1615. //Trying to find reference to this sheet
  1616. while (i < externSheet.getNumOfREFStructures() && !flag){
  1617. ExternSheetSubRecord record = externSheet.getREFRecordAt(i);
  1618. if (record.getIndexToFirstSupBook() == sheetNumber &&
  1619. record.getIndexToLastSupBook() == sheetNumber){
  1620. flag = true;
  1621. result = (short) i;
  1622. }
  1623. ++i;
  1624. }
  1625. //We Havent found reference to this sheet
  1626. if (!flag) {
  1627. result = addSheetIndexToExternSheet((short) sheetNumber);
  1628. }
  1629. return result;
  1630. }
  1631. private short addSheetIndexToExternSheet(short sheetNumber){
  1632. short result;
  1633. ExternSheetSubRecord record = new ExternSheetSubRecord();
  1634. record.setIndexToFirstSupBook(sheetNumber);
  1635. record.setIndexToLastSupBook(sheetNumber);
  1636. externSheet.addREFRecord(record);
  1637. externSheet.setNumOfREFStructures((short)(externSheet.getNumOfREFStructures() + 1));
  1638. result = (short)(externSheet.getNumOfREFStructures() - 1);
  1639. return result;
  1640. }
  1641. /** gets the total number of names
  1642. * @return number of names
  1643. */
  1644. public int getNumNames(){
  1645. int result = names.size();
  1646. return result;
  1647. }
  1648. /** gets the name record
  1649. * @param index name index
  1650. * @return name record
  1651. */
  1652. public NameRecord getNameRecord(int index){
  1653. NameRecord result = (NameRecord) names.get(index);
  1654. return result;
  1655. }
  1656. /** creates new name
  1657. * @return new name record
  1658. */
  1659. public NameRecord createName(){
  1660. NameRecord name = new NameRecord();
  1661. // Not the most efficient way but the other way was causing too many bugs
  1662. int idx = findFirstRecordLocBySid(ExternSheetRecord.sid);
  1663. if (idx == -1) idx = findFirstRecordLocBySid(SupBookRecord.sid);
  1664. if (idx == -1) idx = findFirstRecordLocBySid(CountryRecord.sid);
  1665. records.add(idx+names.size()+1, name);
  1666. names.add(name);
  1667. return name;
  1668. }
  1669. /** creates new name
  1670. * @return new name record
  1671. */
  1672. public NameRecord addName(NameRecord name)
  1673. {
  1674. // Not the most efficient way but the other way was causing too many bugs
  1675. int idx = findFirstRecordLocBySid(ExternSheetRecord.sid);
  1676. if (idx == -1) idx = findFirstRecordLocBySid(SupBookRecord.sid);
  1677. if (idx == -1) idx = findFirstRecordLocBySid(CountryRecord.sid);
  1678. records.add(idx+names.size()+1, name);
  1679. names.add(name);
  1680. return name;
  1681. }
  1682. /**Generates a NameRecord to represent a built-in region
  1683. * @return a new NameRecord unless the index is invalid
  1684. */
  1685. public NameRecord createBuiltInName(byte builtInName, int index)
  1686. {
  1687. if (index == -1 || index+1 > (int)Short.MAX_VALUE)
  1688. throw new IllegalArgumentException("Index is not valid ["+index+"]");
  1689. NameRecord name = new NameRecord(builtInName, (short)(index));
  1690. addName(name);
  1691. return name;
  1692. }
  1693. /** removes the name
  1694. * @param namenum name index
  1695. */
  1696. public void removeName(int namenum){
  1697. if (names.size() > namenum) {
  1698. int idx = findFirstRecordLocBySid(NameRecord.sid);
  1699. records.remove(idx + namenum);
  1700. names.remove(namenum);
  1701. }
  1702. }
  1703. /** creates a new extern sheet record
  1704. * @return the new extern sheet record
  1705. */
  1706. protected ExternSheetRecord createExternSheet(){
  1707. ExternSheetRecord externSheet = new ExternSheetRecord();
  1708. int idx = findFirstRecordLocBySid(CountryRecord.sid);
  1709. records.add(idx+1, externSheet);
  1710. // records.add(records.supbookpos + 1 , rec);
  1711. //We also adds the supBook for internal reference
  1712. SupBookRecord supbook = new SupBookRecord();
  1713. supbook.setNumberOfSheets((short)getNumSheets());
  1714. //supbook.setFlag();
  1715. records.add(idx+1, supbook);
  1716. // records.add(records.supbookpos + 1 , supbook);
  1717. return externSheet;
  1718. }
  1719. /**
  1720. * Returns a format index that matches the passed in format. It does not tie into HSSFDataFormat.
  1721. * @param format the format string
  1722. * @param createIfNotFound creates a new format if format not found
  1723. * @return the format id of a format that matches or -1 if none found and createIfNotFound
  1724. */
  1725. public short getFormat(String format, boolean createIfNotFound) {
  1726. Iterator iterator;
  1727. for (iterator = formats.iterator(); iterator.hasNext();) {
  1728. FormatRecord r = (FormatRecord)iterator.next();
  1729. if (r.getFormatString().equals(format)) {
  1730. return r.getIndexCode();
  1731. }
  1732. }
  1733. if (createIfNotFound) {
  1734. return createFormat(format);
  1735. }
  1736. return -1;
  1737. }
  1738. /**
  1739. * Returns the list of FormatRecords in the workbook.
  1740. * @return ArrayList of FormatRecords in the notebook
  1741. */
  1742. public ArrayList getFormats() {
  1743. return formats;
  1744. }
  1745. /**
  1746. * Creates a FormatRecord, inserts it, and returns the index code.
  1747. * @param format the format string
  1748. * @return the index code of the format record.
  1749. * @see loci.poi.hssf.record.FormatRecord
  1750. * @see loci.poi.hssf.record.Record
  1751. */
  1752. public short createFormat( String format )
  1753. {
  1754. // ++xfpos; //These are to ensure that positions are updated properly
  1755. // ++palettepos;
  1756. // ++bspos;
  1757. FormatRecord rec = new FormatRecord();
  1758. maxformatid = maxformatid >= (short) 0xa4 ? (short) ( maxformatid + 1 ) : (short) 0xa4; //Starting value from M$ empiracle study.
  1759. rec.setIndexCode( maxformatid );
  1760. rec.setFormatStringLength( (byte) format.length() );
  1761. rec.setFormatString( format );
  1762. int pos = 0;
  1763. while ( pos < records.size() && records.get( pos ).getSid() != FormatRecord.sid )
  1764. pos++;
  1765. pos += formats.size();
  1766. formats.add( rec );
  1767. records.add( pos, rec );
  1768. return maxformatid;
  1769. }
  1770. /**
  1771. * Returns the first occurance of a record matching a particular sid.
  1772. */
  1773. public Record findFirstRecordBySid(short sid) {
  1774. for (Iterator iterator = records.iterator(); iterator.hasNext(); ) {
  1775. Record record = ( Record ) iterator.next();
  1776. if (record.getSid() == sid) {
  1777. return record;
  1778. }
  1779. }
  1780. return null;
  1781. }
  1782. /**
  1783. * Returns the index of a record matching a particular sid.
  1784. * @param sid The sid of the record to match
  1785. * @return The index of -1 if no match made.
  1786. */
  1787. public int findFirstRecordLocBySid(short sid) {
  1788. int index = 0;
  1789. for (Iterator iterator = records.iterator(); iterator.hasNext(); ) {
  1790. Record record = ( Record ) iterator.next();
  1791. if (record.getSid() == sid) {
  1792. return index;
  1793. }
  1794. index ++;
  1795. }
  1796. return -1;
  1797. }
  1798. /**
  1799. * Returns the next occurance of a record matching a particular sid.
  1800. */
  1801. public Record findNextRecordBySid(short sid, int pos) {
  1802. int matches = 0;
  1803. for (Iterator iterator = records.iterator(); iterator.hasNext(); ) {
  1804. Record record = ( Record ) iterator.next();
  1805. if (record.getSid() == sid) {
  1806. if (matches++ == pos)
  1807. return record;
  1808. }
  1809. }
  1810. return null;
  1811. }
  1812. public List getRecords()
  1813. {
  1814. return records.getRecords();
  1815. }
  1816. // public void insertChartRecords( List chartRecords )
  1817. // {
  1818. // backuppos += chartRecords.size();
  1819. // fontpos += chartRecords.size();
  1820. // palettepos += chartRecords.size();
  1821. // bspos += chartRecords.size();
  1822. // xfpos += chartRecords.size();
  1823. //
  1824. // records.addAll(protpos, chartRecords);
  1825. // }
  1826. /**
  1827. * Whether date windowing is based on 1/2/1904 or 1/1/1900.
  1828. * Some versions of Excel (Mac) can save workbooks using 1904 date windowing.
  1829. *
  1830. * @return true if using 1904 date windowing
  1831. */
  1832. public boolean isUsing1904DateWindowing() {
  1833. return uses1904datewindowing;
  1834. }
  1835. /**
  1836. * Returns the custom palette in use for this workbook; if a custom palette record
  1837. * does not exist, then it is created.
  1838. */
  1839. public PaletteRecord getCustomPalette()
  1840. {
  1841. PaletteRecord palette;
  1842. int palettePos = records.getPalettepos();
  1843. if (palettePos != -1) {
  1844. Record rec = records.get(palettePos);
  1845. if (rec instanceof PaletteRecord) {
  1846. palette = (PaletteRecord) rec;
  1847. } else throw new RuntimeException("InternalError: Expected PaletteRecord but got a '"+rec+"'");
  1848. }
  1849. else
  1850. {
  1851. palette = createPalette();
  1852. //Add the palette record after the bof which is always the first record
  1853. records.add(1, palette);
  1854. records.setPalettepos(1);
  1855. }
  1856. return palette;
  1857. }
  1858. /**
  1859. * Creates a drawing group record. If it already exists then it's modified.
  1860. */
  1861. public void createDrawingGroup()
  1862. {
  1863. if (drawingManager == null)
  1864. {
  1865. EscherContainerRecord dggContainer = new EscherContainerRecord();
  1866. EscherDggRecord dgg = new EscherDggRecord();
  1867. EscherOptRecord opt = new EscherOptRecord();
  1868. EscherSplitMenuColorsRecord splitMenuColors = new EscherSplitMenuColorsRecord();
  1869. dggContainer.setRecordId((short) 0xF000);
  1870. dggContainer.setOptions((short) 0x000F);
  1871. dgg.setRecordId(EscherDggRecord.RECORD_ID);
  1872. dgg.setOptions((short)0x0000);
  1873. dgg.setShapeIdMax(1024);
  1874. dgg.setNumShapesSaved(0);
  1875. dgg.setDrawingsSaved(0);
  1876. dgg.setFileIdClusters(new EscherDggRecord.FileIdCluster[] {} );
  1877. drawingManager = new DrawingManager2(dgg);
  1878. EscherContainerRecord bstoreContainer = null;
  1879. if (escherBSERecords.size() > 0)
  1880. {
  1881. bstoreContainer = new EscherContainerRecord();
  1882. bstoreContainer.setRecordId( EscherContainerRecord.BSTORE_CONTAINER );
  1883. bstoreContainer.setOptions( (short) ( (escherBSERecords.size() << 4) | 0xF ) );
  1884. for ( Iterator iterator = escherBSERecords.iterator(); iterator.hasNext(); )
  1885. {
  1886. EscherRecord escherRecord = (EscherRecord) iterator.next();
  1887. bstoreContainer.addChildRecord( escherRecord );
  1888. }
  1889. }
  1890. opt.setRecordId((short) 0xF00B);
  1891. opt.setOptions((short) 0x0033);
  1892. opt.addEscherProperty( new EscherBoolProperty(EscherProperties.TEXT__SIZE_TEXT_TO_FIT_SHAPE, 524296) );
  1893. opt.addEscherProperty( new EscherRGBProperty(EscherProperties.FILL__FILLCOLOR, 0x08000041) );
  1894. opt.addEscherProperty( new EscherRGBProperty(EscherProperties.LINESTYLE__COLOR, 134217792) );
  1895. splitMenuColors.setRecordId((short) 0xF11E);
  1896. splitMenuColors.setOptions((short) 0x0040);
  1897. splitMenuColors.setColor1(0x0800000D);
  1898. splitMenuColors.setColor2(0x0800000C);
  1899. splitMenuColors.setColor3(0x08000017);
  1900. splitMenuColors.setColor4(0x100000F7);
  1901. dggContainer.addChildRecord(dgg);
  1902. if (bstoreContainer != null)
  1903. dggContainer.addChildRecord( bstoreContainer );
  1904. dggContainer.addChildRecord(opt);
  1905. dggContainer.addChildRecord(splitMenuColors);
  1906. int dgLoc = findFirstRecordLocBySid(DrawingGroupRecord.sid);
  1907. if (dgLoc == -1)
  1908. {
  1909. DrawingGroupRecord drawingGroup = new DrawingGroupRecord();
  1910. drawingGroup.addEscherRecord(dggContainer);
  1911. int loc = findFirstRecordLocBySid(CountryRecord.sid);
  1912. getRecords().add(loc+1, drawingGroup);
  1913. }
  1914. else
  1915. {
  1916. DrawingGroupRecord drawingGroup = new DrawingGroupRecord();
  1917. drawingGroup.addEscherRecord(dggContainer);
  1918. getRecords().set(dgLoc, drawingGroup);
  1919. }
  1920. }
  1921. }
  1922. public WindowOneRecord getWindowOne() {
  1923. return windowOne;
  1924. }
  1925. public EscherBSERecord getBSERecord(int pictureIndex)
  1926. {
  1927. return (EscherBSERecord)escherBSERecords.get(pictureIndex-1);
  1928. }
  1929. public int addBSERecord(EscherBSERecord e)
  1930. {
  1931. createDrawingGroup();
  1932. // maybe we don't need that as an instance variable anymore
  1933. escherBSERecords.add( e );
  1934. int dgLoc = findFirstRecordLocBySid(DrawingGroupRecord.sid);
  1935. DrawingGroupRecord drawingGroup = (DrawingGroupRecord) getRecords().get( dgLoc );
  1936. EscherContainerRecord dggContainer = (EscherContainerRecord) drawingGroup.getEscherRecord( 0 );
  1937. EscherContainerRecord bstoreContainer;
  1938. if (dggContainer.getChild( 1 ).getRecordId() == EscherContainerRecord.BSTORE_CONTAINER )
  1939. {
  1940. bstoreContainer = (EscherContainerRecord) dggContainer.getChild( 1 );
  1941. }
  1942. else
  1943. {
  1944. bstoreContainer = new EscherContainerRecord();
  1945. bstoreContainer.setRecordId( EscherContainerRecord.BSTORE_CONTAINER );
  1946. dggContainer.getChildRecords().add( 1, bstoreContainer );
  1947. }
  1948. bstoreContainer.setOptions( (short) ( (escherBSERecords.size() << 4) | 0xF ) );
  1949. bstoreContainer.addChildRecord( e );
  1950. return escherBSERecords.size();
  1951. }
  1952. public DrawingManager2 getDrawingManager()
  1953. {
  1954. return drawingManager;
  1955. }
  1956. public WriteProtectRecord getWriteProtect() {
  1957. if (this.writeProtect == null) {
  1958. this.writeProtect = new WriteProtectRecord();
  1959. int i = 0;
  1960. for (i = 0;
  1961. i < records.size() && !(records.get(i) instanceof BOFRecord);
  1962. i++) {
  1963. }
  1964. records.add(i+1,this.writeProtect);
  1965. }
  1966. return this.writeProtect;
  1967. }
  1968. public WriteAccessRecord getWriteAccess() {
  1969. if (this.writeAccess == null) {
  1970. this.writeAccess = (WriteAccessRecord)createWriteAccess();
  1971. int i = 0;
  1972. for (i = 0;
  1973. i < records.size() && !(records.get(i) instanceof InterfaceEndRecord);
  1974. i++) {
  1975. }
  1976. records.add(i+1,this.writeAccess);
  1977. }
  1978. return this.writeAccess;
  1979. }
  1980. public FileSharingRecord getFileSharing() {
  1981. if (this.fileShare == null) {
  1982. this.fileShare = new FileSharingRecord();
  1983. int i = 0;
  1984. for (i = 0;
  1985. i < records.size() && !(records.get(i) instanceof WriteAccessRecord);
  1986. i++) {
  1987. }
  1988. records.add(i+1,this.fileShare);
  1989. }
  1990. return this.fileShare;
  1991. }
  1992. /**
  1993. * protect a workbook with a password (not encypted, just sets writeprotect
  1994. * flags and the password.
  1995. * @param password to set
  1996. */
  1997. public void writeProtectWorkbook( String password, String username ) {
  1998. int protIdx = -1;
  1999. FileSharingRecord frec = getFileSharing();
  2000. WriteAccessRecord waccess = getWriteAccess();
  2001. WriteProtectRecord wprotect = getWriteProtect();
  2002. frec.setReadOnly((short)1);
  2003. frec.setPassword(FileSharingRecord.hashPassword(password));
  2004. frec.setUsername(username);
  2005. waccess.setUsername(username);
  2006. }
  2007. /**
  2008. * removes the write protect flag
  2009. */
  2010. public void unwriteProtectWorkbook() {
  2011. records.remove(fileShare);
  2012. records.remove(writeProtect);
  2013. fileShare = null;
  2014. writeProtect = null;
  2015. }
  2016. }