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.

PageSettingsBlock.java 21KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679
  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.record.aggregates;
  16. import java.util.ArrayList;
  17. import java.util.Collections;
  18. import java.util.Iterator;
  19. import java.util.List;
  20. import java.util.Arrays;
  21. import org.apache.poi.hssf.model.RecordStream;
  22. import org.apache.poi.hssf.model.InternalSheet;
  23. import org.apache.poi.hssf.record.*;
  24. /**
  25. * Groups the page settings records for a worksheet.<p/>
  26. *
  27. * See OOO excelfileformat.pdf sec 4.4 'Page Settings Block'
  28. *
  29. * @author Josh Micich
  30. */
  31. public final class PageSettingsBlock extends RecordAggregate {
  32. /**
  33. * PLS is potentially a <i>continued</i> record, but is currently uninterpreted by POI
  34. */
  35. private static final class PLSAggregate extends RecordAggregate {
  36. private static final ContinueRecord[] EMPTY_CONTINUE_RECORD_ARRAY = { };
  37. private final Record _pls;
  38. /**
  39. * holds any continue records found after the PLS record.<br/>
  40. * This would not be required if PLS was properly interpreted.
  41. * Currently, PLS is an {@link UnknownRecord} and does not automatically
  42. * include any trailing {@link ContinueRecord}s.
  43. */
  44. private ContinueRecord[] _plsContinues;
  45. public PLSAggregate(RecordStream rs) {
  46. _pls = rs.getNext();
  47. if (rs.peekNextSid()==ContinueRecord.sid) {
  48. List<ContinueRecord> temp = new ArrayList<ContinueRecord>();
  49. while (rs.peekNextSid()==ContinueRecord.sid) {
  50. temp.add((ContinueRecord)rs.getNext());
  51. }
  52. _plsContinues = new ContinueRecord[temp.size()];
  53. temp.toArray(_plsContinues);
  54. } else {
  55. _plsContinues = EMPTY_CONTINUE_RECORD_ARRAY;
  56. }
  57. }
  58. @Override
  59. public void visitContainedRecords(RecordVisitor rv) {
  60. rv.visitRecord(_pls);
  61. for (int i = 0; i < _plsContinues.length; i++) {
  62. rv.visitRecord(_plsContinues[i]);
  63. }
  64. }
  65. }
  66. // Every one of these component records is optional
  67. // (The whole PageSettingsBlock may not be present)
  68. private PageBreakRecord _rowBreaksRecord;
  69. private PageBreakRecord _columnBreaksRecord;
  70. private HeaderRecord _header;
  71. private FooterRecord _footer;
  72. private HCenterRecord _hCenter;
  73. private VCenterRecord _vCenter;
  74. private LeftMarginRecord _leftMargin;
  75. private RightMarginRecord _rightMargin;
  76. private TopMarginRecord _topMargin;
  77. private BottomMarginRecord _bottomMargin;
  78. private final List<PLSAggregate> _plsRecords;
  79. private PrintSetupRecord _printSetup;
  80. private Record _bitmap;
  81. private HeaderFooterRecord _headerFooter;
  82. /**
  83. * HeaderFooterRecord records belonging to preceding CustomViewSettingsRecordAggregates.
  84. * The indicator of such records is a non-zero GUID,
  85. * see {@link org.apache.poi.hssf.record.HeaderFooterRecord#getGuid()}
  86. */
  87. private List<HeaderFooterRecord> _sviewHeaderFooters = new ArrayList<HeaderFooterRecord>();
  88. private Record _printSize;
  89. public PageSettingsBlock(RecordStream rs) {
  90. _plsRecords = new ArrayList<PLSAggregate>();
  91. while(true) {
  92. if (!readARecord(rs)) {
  93. break;
  94. }
  95. }
  96. }
  97. /**
  98. * Creates a PageSettingsBlock with default settings
  99. */
  100. public PageSettingsBlock() {
  101. _plsRecords = new ArrayList<PLSAggregate>();
  102. _rowBreaksRecord = new HorizontalPageBreakRecord();
  103. _columnBreaksRecord = new VerticalPageBreakRecord();
  104. _header = new HeaderRecord("");
  105. _footer = new FooterRecord("");
  106. _hCenter = createHCenter();
  107. _vCenter = createVCenter();
  108. _printSetup = createPrintSetup();
  109. }
  110. /**
  111. * @return <code>true</code> if the specified Record sid is one belonging to the
  112. * 'Page Settings Block'.
  113. */
  114. public static boolean isComponentRecord(int sid) {
  115. switch (sid) {
  116. case HorizontalPageBreakRecord.sid:
  117. case VerticalPageBreakRecord.sid:
  118. case HeaderRecord.sid:
  119. case FooterRecord.sid:
  120. case HCenterRecord.sid:
  121. case VCenterRecord.sid:
  122. case LeftMarginRecord.sid:
  123. case RightMarginRecord.sid:
  124. case TopMarginRecord.sid:
  125. case BottomMarginRecord.sid:
  126. case UnknownRecord.PLS_004D:
  127. case PrintSetupRecord.sid:
  128. case UnknownRecord.BITMAP_00E9:
  129. case UnknownRecord.PRINTSIZE_0033:
  130. case HeaderFooterRecord.sid: // extra header/footer settings supported by Excel 2007
  131. return true;
  132. }
  133. return false;
  134. }
  135. private boolean readARecord(RecordStream rs) {
  136. switch (rs.peekNextSid()) {
  137. case HorizontalPageBreakRecord.sid:
  138. checkNotPresent(_rowBreaksRecord);
  139. _rowBreaksRecord = (PageBreakRecord) rs.getNext();
  140. break;
  141. case VerticalPageBreakRecord.sid:
  142. checkNotPresent(_columnBreaksRecord);
  143. _columnBreaksRecord = (PageBreakRecord) rs.getNext();
  144. break;
  145. case HeaderRecord.sid:
  146. checkNotPresent(_header);
  147. _header = (HeaderRecord) rs.getNext();
  148. break;
  149. case FooterRecord.sid:
  150. checkNotPresent(_footer);
  151. _footer = (FooterRecord) rs.getNext();
  152. break;
  153. case HCenterRecord.sid:
  154. checkNotPresent(_hCenter);
  155. _hCenter = (HCenterRecord) rs.getNext();
  156. break;
  157. case VCenterRecord.sid:
  158. checkNotPresent(_vCenter);
  159. _vCenter = (VCenterRecord) rs.getNext();
  160. break;
  161. case LeftMarginRecord.sid:
  162. checkNotPresent(_leftMargin);
  163. _leftMargin = (LeftMarginRecord) rs.getNext();
  164. break;
  165. case RightMarginRecord.sid:
  166. checkNotPresent(_rightMargin);
  167. _rightMargin = (RightMarginRecord) rs.getNext();
  168. break;
  169. case TopMarginRecord.sid:
  170. checkNotPresent(_topMargin);
  171. _topMargin = (TopMarginRecord) rs.getNext();
  172. break;
  173. case BottomMarginRecord.sid:
  174. checkNotPresent(_bottomMargin);
  175. _bottomMargin = (BottomMarginRecord) rs.getNext();
  176. break;
  177. case UnknownRecord.PLS_004D:
  178. _plsRecords.add(new PLSAggregate(rs));
  179. break;
  180. case PrintSetupRecord.sid:
  181. checkNotPresent(_printSetup);
  182. _printSetup = (PrintSetupRecord)rs.getNext();
  183. break;
  184. case UnknownRecord.BITMAP_00E9:
  185. checkNotPresent(_bitmap);
  186. _bitmap = rs.getNext();
  187. break;
  188. case UnknownRecord.PRINTSIZE_0033:
  189. checkNotPresent(_printSize);
  190. _printSize = rs.getNext();
  191. break;
  192. case HeaderFooterRecord.sid:
  193. //there can be multiple HeaderFooterRecord records belonging to different sheet views
  194. HeaderFooterRecord hf = (HeaderFooterRecord)rs.getNext();
  195. if(hf.isCurrentSheet()) _headerFooter = hf;
  196. else {
  197. _sviewHeaderFooters.add(hf);
  198. }
  199. break;
  200. default:
  201. // all other record types are not part of the PageSettingsBlock
  202. return false;
  203. }
  204. return true;
  205. }
  206. private void checkNotPresent(Record rec) {
  207. if (rec != null) {
  208. throw new RecordFormatException("Duplicate PageSettingsBlock record (sid=0x"
  209. + Integer.toHexString(rec.getSid()) + ")");
  210. }
  211. }
  212. private PageBreakRecord getRowBreaksRecord() {
  213. if (_rowBreaksRecord == null) {
  214. _rowBreaksRecord = new HorizontalPageBreakRecord();
  215. }
  216. return _rowBreaksRecord;
  217. }
  218. private PageBreakRecord getColumnBreaksRecord() {
  219. if (_columnBreaksRecord == null) {
  220. _columnBreaksRecord = new VerticalPageBreakRecord();
  221. }
  222. return _columnBreaksRecord;
  223. }
  224. /**
  225. * Sets a page break at the indicated column
  226. *
  227. */
  228. public void setColumnBreak(short column, short fromRow, short toRow) {
  229. getColumnBreaksRecord().addBreak(column, fromRow, toRow);
  230. }
  231. /**
  232. * Removes a page break at the indicated column
  233. *
  234. */
  235. public void removeColumnBreak(int column) {
  236. getColumnBreaksRecord().removeBreak(column);
  237. }
  238. public void visitContainedRecords(RecordVisitor rv) {
  239. // Replicates record order from Excel 2007, though this is not critical
  240. visitIfPresent(_rowBreaksRecord, rv);
  241. visitIfPresent(_columnBreaksRecord, rv);
  242. // Write out empty header / footer records if these are missing
  243. if (_header == null) {
  244. rv.visitRecord(new HeaderRecord(""));
  245. } else {
  246. rv.visitRecord(_header);
  247. }
  248. if (_footer == null) {
  249. rv.visitRecord(new FooterRecord(""));
  250. } else {
  251. rv.visitRecord(_footer);
  252. }
  253. visitIfPresent(_hCenter, rv);
  254. visitIfPresent(_vCenter, rv);
  255. visitIfPresent(_leftMargin, rv);
  256. visitIfPresent(_rightMargin, rv);
  257. visitIfPresent(_topMargin, rv);
  258. visitIfPresent(_bottomMargin, rv);
  259. for (RecordAggregate pls : _plsRecords) {
  260. pls.visitContainedRecords(rv);
  261. }
  262. visitIfPresent(_printSetup, rv);
  263. visitIfPresent(_bitmap, rv);
  264. visitIfPresent(_printSize, rv);
  265. visitIfPresent(_headerFooter, rv);
  266. }
  267. private static void visitIfPresent(Record r, RecordVisitor rv) {
  268. if (r != null) {
  269. rv.visitRecord(r);
  270. }
  271. }
  272. private static void visitIfPresent(PageBreakRecord r, RecordVisitor rv) {
  273. if (r != null) {
  274. if (r.isEmpty()) {
  275. // its OK to not serialize empty page break records
  276. return;
  277. }
  278. rv.visitRecord(r);
  279. }
  280. }
  281. /**
  282. * creates the HCenter Record and sets it to false (don't horizontally center)
  283. */
  284. private static HCenterRecord createHCenter() {
  285. HCenterRecord retval = new HCenterRecord();
  286. retval.setHCenter(false);
  287. return retval;
  288. }
  289. /**
  290. * creates the VCenter Record and sets it to false (don't horizontally center)
  291. */
  292. private static VCenterRecord createVCenter() {
  293. VCenterRecord retval = new VCenterRecord();
  294. retval.setVCenter(false);
  295. return retval;
  296. }
  297. /**
  298. * creates the PrintSetup Record and sets it to defaults and marks it invalid
  299. * @see org.apache.poi.hssf.record.PrintSetupRecord
  300. * @see org.apache.poi.hssf.record.Record
  301. * @return record containing a PrintSetupRecord
  302. */
  303. private static PrintSetupRecord createPrintSetup() {
  304. PrintSetupRecord retval = new PrintSetupRecord();
  305. retval.setPaperSize(( short ) 1);
  306. retval.setScale(( short ) 100);
  307. retval.setPageStart(( short ) 1);
  308. retval.setFitWidth(( short ) 1);
  309. retval.setFitHeight(( short ) 1);
  310. retval.setOptions(( short ) 2);
  311. retval.setHResolution(( short ) 300);
  312. retval.setVResolution(( short ) 300);
  313. retval.setHeaderMargin( 0.5);
  314. retval.setFooterMargin( 0.5);
  315. retval.setCopies(( short ) 1);
  316. return retval;
  317. }
  318. /**
  319. * Returns the HeaderRecord.
  320. * @return HeaderRecord for the sheet.
  321. */
  322. public HeaderRecord getHeader ()
  323. {
  324. return _header;
  325. }
  326. /**
  327. * Sets the HeaderRecord.
  328. * @param newHeader The new HeaderRecord for the sheet.
  329. */
  330. public void setHeader (HeaderRecord newHeader)
  331. {
  332. _header = newHeader;
  333. }
  334. /**
  335. * Returns the FooterRecord.
  336. * @return FooterRecord for the sheet.
  337. */
  338. public FooterRecord getFooter ()
  339. {
  340. return _footer;
  341. }
  342. /**
  343. * Sets the FooterRecord.
  344. * @param newFooter The new FooterRecord for the sheet.
  345. */
  346. public void setFooter (FooterRecord newFooter)
  347. {
  348. _footer = newFooter;
  349. }
  350. /**
  351. * Returns the PrintSetupRecord.
  352. * @return PrintSetupRecord for the sheet.
  353. */
  354. public PrintSetupRecord getPrintSetup ()
  355. {
  356. return _printSetup;
  357. }
  358. /**
  359. * Sets the PrintSetupRecord.
  360. * @param newPrintSetup The new PrintSetupRecord for the sheet.
  361. */
  362. public void setPrintSetup (PrintSetupRecord newPrintSetup)
  363. {
  364. _printSetup = newPrintSetup;
  365. }
  366. private Margin getMarginRec(int marginIndex) {
  367. switch (marginIndex) {
  368. case InternalSheet.LeftMargin: return _leftMargin;
  369. case InternalSheet.RightMargin: return _rightMargin;
  370. case InternalSheet.TopMargin: return _topMargin;
  371. case InternalSheet.BottomMargin: return _bottomMargin;
  372. }
  373. throw new IllegalArgumentException( "Unknown margin constant: " + marginIndex );
  374. }
  375. /**
  376. * Gets the size of the margin in inches.
  377. * @param margin which margin to get
  378. * @return the size of the margin
  379. */
  380. public double getMargin(short margin) {
  381. Margin m = getMarginRec(margin);
  382. if (m != null) {
  383. return m.getMargin();
  384. }
  385. switch (margin) {
  386. case InternalSheet.LeftMargin: return .75;
  387. case InternalSheet.RightMargin: return .75;
  388. case InternalSheet.TopMargin: return 1.0;
  389. case InternalSheet.BottomMargin: return 1.0;
  390. }
  391. throw new IllegalArgumentException( "Unknown margin constant: " + margin );
  392. }
  393. /**
  394. * Sets the size of the margin in inches.
  395. * @param margin which margin to get
  396. * @param size the size of the margin
  397. */
  398. public void setMargin(short margin, double size) {
  399. Margin m = getMarginRec(margin);
  400. if (m == null) {
  401. switch (margin) {
  402. case InternalSheet.LeftMargin:
  403. _leftMargin = new LeftMarginRecord();
  404. m = _leftMargin;
  405. break;
  406. case InternalSheet.RightMargin:
  407. _rightMargin = new RightMarginRecord();
  408. m = _rightMargin;
  409. break;
  410. case InternalSheet.TopMargin:
  411. _topMargin = new TopMarginRecord();
  412. m = _topMargin;
  413. break;
  414. case InternalSheet.BottomMargin:
  415. _bottomMargin = new BottomMarginRecord();
  416. m = _bottomMargin;
  417. break;
  418. default :
  419. throw new IllegalArgumentException( "Unknown margin constant: " + margin );
  420. }
  421. }
  422. m.setMargin( size );
  423. }
  424. /**
  425. * Shifts all the page breaks in the range "count" number of rows/columns
  426. * @param breaks The page record to be shifted
  427. * @param start Starting "main" value to shift breaks
  428. * @param stop Ending "main" value to shift breaks
  429. * @param count number of units (rows/columns) to shift by
  430. */
  431. private static void shiftBreaks(PageBreakRecord breaks, int start, int stop, int count) {
  432. Iterator<PageBreakRecord.Break> iterator = breaks.getBreaksIterator();
  433. List<PageBreakRecord.Break> shiftedBreak = new ArrayList<PageBreakRecord.Break>();
  434. while(iterator.hasNext())
  435. {
  436. PageBreakRecord.Break breakItem = iterator.next();
  437. int breakLocation = breakItem.main;
  438. boolean inStart = (breakLocation >= start);
  439. boolean inEnd = (breakLocation <= stop);
  440. if(inStart && inEnd)
  441. shiftedBreak.add(breakItem);
  442. }
  443. iterator = shiftedBreak.iterator();
  444. while (iterator.hasNext()) {
  445. PageBreakRecord.Break breakItem = iterator.next();
  446. breaks.removeBreak(breakItem.main);
  447. breaks.addBreak((short)(breakItem.main+count), breakItem.subFrom, breakItem.subTo);
  448. }
  449. }
  450. /**
  451. * Sets a page break at the indicated row
  452. * @param row
  453. */
  454. public void setRowBreak(int row, short fromCol, short toCol) {
  455. getRowBreaksRecord().addBreak((short)row, fromCol, toCol);
  456. }
  457. /**
  458. * Removes a page break at the indicated row
  459. * @param row
  460. */
  461. public void removeRowBreak(int row) {
  462. if (getRowBreaksRecord().getBreaks().length < 1)
  463. throw new IllegalArgumentException("Sheet does not define any row breaks");
  464. getRowBreaksRecord().removeBreak((short)row);
  465. }
  466. /**
  467. * Queries if the specified row has a page break
  468. * @param row
  469. * @return true if the specified row has a page break
  470. */
  471. public boolean isRowBroken(int row) {
  472. return getRowBreaksRecord().getBreak(row) != null;
  473. }
  474. /**
  475. * Queries if the specified column has a page break
  476. *
  477. * @return <code>true</code> if the specified column has a page break
  478. */
  479. public boolean isColumnBroken(int column) {
  480. return getColumnBreaksRecord().getBreak(column) != null;
  481. }
  482. /**
  483. * Shifts the horizontal page breaks for the indicated count
  484. * @param startingRow
  485. * @param endingRow
  486. * @param count
  487. */
  488. public void shiftRowBreaks(int startingRow, int endingRow, int count) {
  489. shiftBreaks(getRowBreaksRecord(), startingRow, endingRow, count);
  490. }
  491. /**
  492. * Shifts the vertical page breaks for the indicated count
  493. * @param startingCol
  494. * @param endingCol
  495. * @param count
  496. */
  497. public void shiftColumnBreaks(short startingCol, short endingCol, short count) {
  498. shiftBreaks(getColumnBreaksRecord(), startingCol, endingCol, count);
  499. }
  500. /**
  501. * @return all the horizontal page breaks, never <code>null</code>
  502. */
  503. public int[] getRowBreaks() {
  504. return getRowBreaksRecord().getBreaks();
  505. }
  506. /**
  507. * @return the number of row page breaks
  508. */
  509. public int getNumRowBreaks(){
  510. return getRowBreaksRecord().getNumBreaks();
  511. }
  512. /**
  513. * @return all the column page breaks, never <code>null</code>
  514. */
  515. public int[] getColumnBreaks(){
  516. return getColumnBreaksRecord().getBreaks();
  517. }
  518. /**
  519. * @return the number of column page breaks
  520. */
  521. public int getNumColumnBreaks(){
  522. return getColumnBreaksRecord().getNumBreaks();
  523. }
  524. public VCenterRecord getVCenter() {
  525. return _vCenter;
  526. }
  527. public HCenterRecord getHCenter() {
  528. return _hCenter;
  529. }
  530. /**
  531. * HEADERFOOTER is new in 2007. Some apps seem to have scattered this record long after
  532. * the {@link PageSettingsBlock} where it belongs.
  533. */
  534. public void addLateHeaderFooter(HeaderFooterRecord rec) {
  535. if (_headerFooter != null) {
  536. throw new IllegalStateException("This page settings block already has a header/footer record");
  537. }
  538. if (rec.getSid() != HeaderFooterRecord.sid) {
  539. throw new RecordFormatException("Unexpected header-footer record sid: 0x" + Integer.toHexString(rec.getSid()));
  540. }
  541. _headerFooter = rec;
  542. }
  543. /**
  544. * This method reads PageSettingsBlock records from the supplied RecordStream until the first
  545. * non-PageSettingsBlock record is encountered. As each record is read, it is incorporated
  546. * into this PageSettingsBlock.
  547. * <p/>
  548. * The latest Excel version seems to write the PageSettingsBlock uninterrupted. However there
  549. * are several examples (that Excel reads OK) where these records are not written together:
  550. * <ul>
  551. * <li><b>HEADER_FOOTER(0x089C) after WINDOW2</b> - This record is new in 2007. Some apps
  552. * seem to have scattered this record long after the PageSettingsBlock where it belongs
  553. * test samples: SharedFormulaTest.xls, ex44921-21902.xls, ex42570-20305.xls</li>
  554. * <li><b>PLS, WSBOOL, PageSettingsBlock</b> - WSBOOL is not a PSB record.
  555. * This happens in the test sample file "NoGutsRecords.xls" and "WORKBOOK_in_capitals.xls"</li>
  556. * <li><b>Margins after DIMENSION</b> - All of PSB should be before DIMENSION. (Bug-47199)</li>
  557. * </ul>
  558. * These were probably written by other applications (or earlier versions of Excel). It was
  559. * decided to not write specific code for detecting each of these cases. POI now tolerates
  560. * PageSettingsBlock records scattered all over the sheet record stream, and in any order, but
  561. * does not allow duplicates of any of those records.
  562. *
  563. * <p/>
  564. * <b>Note</b> - when POI writes out this PageSettingsBlock, the records will always be written
  565. * in one consolidated block (in the standard ordering) regardless of how scattered the records
  566. * were when they were originally read.
  567. *
  568. * @throws RecordFormatException if any PSB record encountered has the same type (sid) as
  569. * a record that is already part of this PageSettingsBlock
  570. */
  571. public void addLateRecords(RecordStream rs) {
  572. while(true) {
  573. if (!readARecord(rs)) {
  574. break;
  575. }
  576. }
  577. }
  578. /**
  579. * Some apps can define multiple HeaderFooterRecord records for a sheet.
  580. * When saving such a file Excel 2007 re-positions them according to the following rules:
  581. * - take a HeaderFooterRecord and read 16-byte GUID at offset 12. If it is zero,
  582. * it means the current sheet and the given HeaderFooterRecord belongs to this PageSettingsBlock
  583. * - If GUID is not zero then search in preceding CustomViewSettingsRecordAggregates.
  584. * Compare first 16 bytes of UserSViewBegin with the HeaderFooterRecord's GUID. If match,
  585. * then append the HeaderFooterRecord to this CustomViewSettingsRecordAggregates
  586. *
  587. * @param sheetRecords the list of sheet records read so far
  588. */
  589. public void positionRecords(List<RecordBase> sheetRecords) {
  590. // Take a copy to loop over, so we can update the real one
  591. // without concurrency issues
  592. List<HeaderFooterRecord> hfRecordsToIterate = new ArrayList<HeaderFooterRecord>(_sviewHeaderFooters);
  593. // loop through HeaderFooterRecord records having not-empty GUID and match them with
  594. // CustomViewSettingsRecordAggregate blocks having UserSViewBegin with the same GUID
  595. for(final HeaderFooterRecord hf : hfRecordsToIterate) {
  596. for (RecordBase rb : sheetRecords) {
  597. if (rb instanceof CustomViewSettingsRecordAggregate) {
  598. final CustomViewSettingsRecordAggregate cv = (CustomViewSettingsRecordAggregate) rb;
  599. cv.visitContainedRecords(new RecordVisitor() {
  600. public void visitRecord(Record r) {
  601. if (r.getSid() == UserSViewBegin.sid) {
  602. byte[] guid1 = ((UserSViewBegin) r).getGuid();
  603. byte[] guid2 = hf.getGuid();
  604. if (Arrays.equals(guid1, guid2)) {
  605. cv.append(hf);
  606. _sviewHeaderFooters.remove(hf);
  607. }
  608. }
  609. }
  610. });
  611. }
  612. }
  613. }
  614. }
  615. }