You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

InternalWorkbook.java 86KB

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