zhao
2021-06-04 c7ec496f9e41c2227103b3ef776e4a3f91bce6b2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
/* ====================================================================
   Licensed to the Apache Software Foundation (ASF) under one or more
   contributor license agreements.  See the NOTICE file distributed with
   this work for additional information regarding copyright ownership.
   The ASF licenses this file to You under the Apache License, Version 2.0
   (the "License"); you may not use this file except in compliance with
   the License.  You may obtain a copy of the License at
 
       http://www.apache.org/licenses/LICENSE-2.0
 
   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
==================================================================== */
 
namespace HH.WMS.Utils.NPOI.HSSF.Record.Aggregates
{
 
    using System;
    using System.Text;
    using System.Collections;
    using HH.WMS.Utils.NPOI.HSSF.Model;
    using HH.WMS.Utils.NPOI.HSSF.Record;
    using HH.WMS.Utils.NPOI.HSSF.Util;
    using HH.WMS.Utils.NPOI.SS.UserModel;
    using HH.WMS.Utils.NPOI.Util;
    using System.Collections.Generic;
    /**
     * Groups the page settings records for a worksheet.<p/>
     * 
     * See OOO excelfileformat.pdf sec 4.4 'Page Settings Block'
     * 
     * @author Josh Micich
     */
    public class PageSettingsBlock : RecordAggregate
    {
        // Every one of these component records is optional 
        // (The whole PageSettingsBlock may not be present) 
        private PageBreakRecord _rowBreaksRecord;
        private PageBreakRecord _columnBreaksRecord;
        private HeaderRecord header;
        private FooterRecord footer;
        private HCenterRecord _hCenter;
        private VCenterRecord _vCenter;
        private LeftMarginRecord _leftMargin;
        private RightMarginRecord _rightMargin;
        private TopMarginRecord _topMargin;
        private BottomMarginRecord _bottomMargin;
        // fix warning CS0169 "never used": private Record _pls;
        private PrintSetupRecord printSetup;
        private Record _bitmap;
        private HeaderFooterRecord _headerFooter;
 
        private List<HeaderFooterRecord> _sviewHeaderFooters = new List<HeaderFooterRecord>();
        private List<PLSAggregate> _plsRecords;
 
        private Record _printSize;
 
        public PageSettingsBlock(RecordStream rs)
        {
            _plsRecords = new List<PLSAggregate>();
            while (ReadARecord(rs)) ;
        }
 
        /**
         * Creates a PageSettingsBlock with default settings
         */
        public PageSettingsBlock()
        {
            _plsRecords = new List<PLSAggregate>();
            _rowBreaksRecord = new HorizontalPageBreakRecord();
            _columnBreaksRecord = new VerticalPageBreakRecord();
            header = new HeaderRecord(string.Empty);
            footer = new FooterRecord(string.Empty);
            _hCenter = CreateHCenter();
            _vCenter = CreateVCenter();
            printSetup = CreatePrintSetup();
        }
        /**
         * @return <c>true</c> if the specified Record sid is one belonging to the 
         * 'Page Settings Block'.
         */
        public static bool IsComponentRecord(int sid)
        {
            switch (sid)
            {
                case HorizontalPageBreakRecord.sid:
                case VerticalPageBreakRecord.sid:
                case HeaderRecord.sid:
                case FooterRecord.sid:
                case HCenterRecord.sid:
                case VCenterRecord.sid:
                case LeftMarginRecord.sid:
                case RightMarginRecord.sid:
                case TopMarginRecord.sid:
                case BottomMarginRecord.sid:
                case UnknownRecord.PLS_004D:
                case PrintSetupRecord.sid:
                case UnknownRecord.BITMAP_00E9:
                case UnknownRecord.PRINTSIZE_0033:
                case HeaderFooterRecord.sid: // extra header/footer settings supported by Excel 2007
                    return true;
            }
            return false;
        }
 
        private bool ReadARecord(RecordStream rs)
        {
            switch (rs.PeekNextSid())
            {
                case HorizontalPageBreakRecord.sid:
                    CheckNotPresent(_rowBreaksRecord);
                    _rowBreaksRecord = (PageBreakRecord)rs.GetNext();
                    break;
                case VerticalPageBreakRecord.sid:
                    CheckNotPresent(_columnBreaksRecord);
                    _columnBreaksRecord = (PageBreakRecord)rs.GetNext();
                    break;
                case HeaderRecord.sid:
                    CheckNotPresent(header);
                    header = (HeaderRecord)rs.GetNext();
                    break;
                case FooterRecord.sid:
                    CheckNotPresent(footer);
                    footer = (FooterRecord)rs.GetNext();
                    break;
                case HCenterRecord.sid:
                    CheckNotPresent(_hCenter);
                    _hCenter = (HCenterRecord)rs.GetNext();
                    break;
                case VCenterRecord.sid:
                    CheckNotPresent(_vCenter);
                    _vCenter = (VCenterRecord)rs.GetNext();
                    break;
                case LeftMarginRecord.sid:
                    CheckNotPresent(_leftMargin);
                    _leftMargin = (LeftMarginRecord)rs.GetNext();
                    break;
                case RightMarginRecord.sid:
                    CheckNotPresent(_rightMargin);
                    _rightMargin = (RightMarginRecord)rs.GetNext();
                    break;
                case TopMarginRecord.sid:
                    CheckNotPresent(_topMargin);
                    _topMargin = (TopMarginRecord)rs.GetNext();
                    break;
                case BottomMarginRecord.sid:
                    CheckNotPresent(_bottomMargin);
                    _bottomMargin = (BottomMarginRecord)rs.GetNext();
                    break;
                case UnknownRecord.PLS_004D: // PLS
                    _plsRecords.Add(new PLSAggregate(rs));
                    break;
                case PrintSetupRecord.sid:
                    CheckNotPresent(printSetup);
                    printSetup = (PrintSetupRecord)rs.GetNext();
                    break;
                case UnknownRecord.BITMAP_00E9: // BITMAP
                    CheckNotPresent(_bitmap);
                    _bitmap = rs.GetNext();
                    break;
                case UnknownRecord.PRINTSIZE_0033:
                    CheckNotPresent(_printSize);
                    _printSize = rs.GetNext();
                    break;
                case HeaderFooterRecord.sid:
                    HeaderFooterRecord hf = (HeaderFooterRecord)rs.GetNext();
                    if (hf.IsCurrentSheet)
                        _headerFooter = hf;
                    else
                        _sviewHeaderFooters.Add(hf);
                    break;
                default:
                    // all other record types are not part of the PageSettingsBlock
                    return false;
            }
            return true;
        }
        private void CheckNotPresent(Record rec)
        {
            if (rec != null)
            {
                throw new RecordFormatException("Duplicate PageSettingsBlock record (sid=0x"
                        + StringUtil.ToHexString(rec.Sid) + ")");
            }
        }
        private PageBreakRecord RowBreaksRecord
        {
            get
            {
                if (_rowBreaksRecord == null)
                {
                    _rowBreaksRecord = new HorizontalPageBreakRecord();
                }
                return _rowBreaksRecord;
            }
        }
 
        private PageBreakRecord ColumnBreaksRecord
        {
            get
            {
                if (_columnBreaksRecord == null)
                {
                    _columnBreaksRecord = new VerticalPageBreakRecord();
                }
                return _columnBreaksRecord;
            }
        }
 
        public IEnumerator GetEnumerator()
        {
            return _plsRecords.GetEnumerator();
        }
 
        /**
         * Sets a page break at the indicated column
         *
         */
        public void SetColumnBreak(int column, int fromRow, int toRow)
        {
            this.ColumnBreaksRecord.AddBreak(column, fromRow, toRow);
        }
 
        /**
         * Removes a page break at the indicated column
         *
         */
        public void RemoveColumnBreak(int column)
        {
            this.ColumnBreaksRecord.RemoveBreak(column);
        }
 
        public override void VisitContainedRecords(RecordVisitor rv)
        {
            VisitIfPresent(_rowBreaksRecord, rv);
            VisitIfPresent(_columnBreaksRecord, rv);
            // Write out empty header / footer records if these are missing
            if (header == null)
            {
                rv.VisitRecord(new HeaderRecord(""));
            }
            else
            {
                rv.VisitRecord(header);
            }
            if (footer == null)
            {
                rv.VisitRecord(new FooterRecord(""));
            }
            else
            {
                rv.VisitRecord(footer);
            }
            VisitIfPresent(_hCenter, rv);
            VisitIfPresent(_vCenter, rv);
            VisitIfPresent(_leftMargin, rv);
            VisitIfPresent(_rightMargin, rv);
            VisitIfPresent(_topMargin, rv);
            VisitIfPresent(_bottomMargin, rv);
            foreach (RecordAggregate pls in _plsRecords) {
                pls.VisitContainedRecords(rv);
            }
            VisitIfPresent(printSetup, rv);
            VisitIfPresent(_bitmap, rv);
            VisitIfPresent(_printSize, rv);
            VisitIfPresent(_headerFooter, rv);
        }
        private static void VisitIfPresent(Record r, RecordVisitor rv)
        {
            if (r != null)
            {
                rv.VisitRecord(r);
            }
        }
        private static void VisitIfPresent(PageBreakRecord r, RecordVisitor rv)
        {
            if (r != null)
            {
                if (r.IsEmpty)
                {
                    // its OK to not serialize empty page break records
                    return;
                }
                rv.VisitRecord(r);
            }
        }
 
        /**
         * Creates the HCenter Record and sets it to false (don't horizontally center)
         */
        private static HCenterRecord CreateHCenter()
        {
            HCenterRecord retval = new HCenterRecord();
 
            retval.HCenter = (false);
            return retval;
        }
 
        /**
         * Creates the VCenter Record and sets it to false (don't horizontally center)
        */
        private static VCenterRecord CreateVCenter()
        {
            VCenterRecord retval = new VCenterRecord();
 
            retval.VCenter = (false);
            return retval;
        }
 
        /**
         * Creates the PrintSetup Record and sets it to defaults and marks it invalid
         * @see org.apache.poi.hssf.record.PrintSetupRecord
         * @see org.apache.poi.hssf.record.Record
         * @return record containing a PrintSetupRecord
         */
        private static PrintSetupRecord CreatePrintSetup()
        {
            PrintSetupRecord retval = new PrintSetupRecord();
 
            retval.PaperSize = ((short)1);
            retval.Scale = ((short)100);
            retval.PageStart = ((short)1);
            retval.FitWidth = ((short)1);
            retval.FitHeight = ((short)1);
            retval.Options = ((short)2);
            retval.HResolution = ((short)300);
            retval.VResolution = ((short)300);
            retval.HeaderMargin = (0.5);
            retval.FooterMargin = (0.5);
            retval.Copies = ((short)1);
            return retval;
        }
 
 
        /**
         * Returns the HeaderRecord.
         * @return HeaderRecord for the sheet.
         */
        public HeaderRecord Header
        {
            get
            {
                return header;
            }
            set 
            {
                header = value;
            }
        }
 
        /**
         * Returns the FooterRecord.
         * @return FooterRecord for the sheet.
         */
        public FooterRecord Footer
        {
            get
            {
                return footer;
            }
            set { footer = value; }
        }
 
        /**
         * Returns the PrintSetupRecord.
         * @return PrintSetupRecord for the sheet.
         */
        public PrintSetupRecord PrintSetup
        {
            get
            {
                return printSetup;
            }
            set 
            {
                printSetup = value;
            }
        }
 
 
        private Margin GetMarginRec(MarginType margin)
        {
            switch (margin)
            {
                case MarginType.LeftMargin: return _leftMargin;
                case MarginType.RightMargin: return _rightMargin;
                case MarginType.TopMargin: return _topMargin;
                case MarginType.BottomMargin: return _bottomMargin;
                default:
                    throw new InvalidOperationException("Unknown margin constant:  " + (short)margin);
            }
        }
 
 
        /**
         * Gets the size of the margin in inches.
         * @param margin which margin to Get
         * @return the size of the margin
         */
        public double GetMargin(MarginType margin)
        {
            Margin m = GetMarginRec(margin);
            if (m != null)
            {
                return m.Margin;
            }
            else
            {
                switch (margin)
                {
                    case MarginType.LeftMargin:
                        return .75;
                    case MarginType.RightMargin:
                        return .75;
                    case MarginType.TopMargin:
                        return 1.0;
                    case MarginType.BottomMargin:
                        return 1.0;
                }
                throw new InvalidOperationException("Unknown margin constant:  " + margin);
            }
        }
 
        /**
         * Sets the size of the margin in inches.
         * @param margin which margin to Get
         * @param size the size of the margin
         */
        public void SetMargin(MarginType margin, double size)
        {
            Margin m = GetMarginRec(margin);
            if (m == null)
            {
                switch (margin)
                {
                    case MarginType.LeftMargin:
                        _leftMargin = new LeftMarginRecord();
                        m = _leftMargin;
                        break;
                    case MarginType.RightMargin:
                        _rightMargin = new RightMarginRecord();
                        m = _rightMargin;
                        break;
                    case MarginType.TopMargin:
                        _topMargin = new TopMarginRecord();
                        m = _topMargin;
                        break;
                    case MarginType.BottomMargin:
                        _bottomMargin = new BottomMarginRecord();
                        m = _bottomMargin;
                        break;
                    default:
                        throw new InvalidOperationException("Unknown margin constant:  " + margin);
                }
            }
            m.Margin= size;
        }
 
        /**
         * Shifts all the page breaks in the range "count" number of rows/columns
         * @param breaks The page record to be shifted
         * @param start Starting "main" value to shift breaks
         * @param stop Ending "main" value to shift breaks
         * @param count number of units (rows/columns) to shift by
         */
        private static void ShiftBreaks(PageBreakRecord breaks, int start, int stop, int count) {
 
        IEnumerator iterator = breaks.GetBreaksEnumerator();
        IList shiftedBreak = new ArrayList();
        while(iterator.MoveNext())
        {
            PageBreakRecord.Break breakItem = (PageBreakRecord.Break)iterator.Current;
            int breakLocation = breakItem.main;
            bool inStart = (breakLocation >= start);
            bool inEnd = (breakLocation <= stop);
            if(inStart && inEnd)
                shiftedBreak.Add(breakItem);
        }
 
        iterator = shiftedBreak.GetEnumerator();
        while (iterator.MoveNext()) {
            PageBreakRecord.Break breakItem = (PageBreakRecord.Break)iterator.Current;
            breaks.RemoveBreak(breakItem.main);
            breaks.AddBreak((short)(breakItem.main+count), breakItem.subFrom, breakItem.subTo);
        }
    }
 
 
        /**
         * Sets a page break at the indicated row
         * @param row
         */
        public void SetRowBreak(int row, short fromCol, short toCol)
        {
            this.RowBreaksRecord.AddBreak((short)row, fromCol, toCol);
        }
 
        /**
         * Removes a page break at the indicated row
         * @param row
         */
        public void RemoveRowBreak(int row)
        {
            if (this.RowBreaksRecord.GetBreaks().Length < 1)
                throw new ArgumentException("Sheet does not define any row breaks");
            this.RowBreaksRecord.RemoveBreak((short)row);
        }
 
        /**
         * Queries if the specified row has a page break
         * @param row
         * @return true if the specified row has a page break
         */
        public bool IsRowBroken(int row)
        {
            return this.RowBreaksRecord.GetBreak(row) != null;
        }
 
 
        /**
         * Queries if the specified column has a page break
         *
         * @return <c>true</c> if the specified column has a page break
         */
        public bool IsColumnBroken(int column)
        {
            return this.ColumnBreaksRecord.GetBreak(column) != null;
        }
 
        /**
         * Shifts the horizontal page breaks for the indicated count
         * @param startingRow
         * @param endingRow
         * @param count
         */
        public void ShiftRowBreaks(int startingRow, int endingRow, int count)
        {
            ShiftBreaks(this.RowBreaksRecord, startingRow, endingRow, count);
        }
 
        /**
         * Shifts the vertical page breaks for the indicated count
         * @param startingCol
         * @param endingCol
         * @param count
         */
        public void ShiftColumnBreaks(short startingCol, short endingCol, short count)
        {
            ShiftBreaks(this.ColumnBreaksRecord, startingCol, endingCol, count);
        }
 
        /**
         * @return all the horizontal page breaks, never <c>null</c>
         */
        public int[] RowBreaks
        {
            get
            {
                return this.RowBreaksRecord.GetBreaks();
            }
        }
 
        /**
         * @return the number of row page breaks
         */
        public int NumRowBreaks
        {
            get
            {
                return this.RowBreaksRecord.NumBreaks;
            }
        }
 
        /**
         * @return all the column page breaks, never <c>null</c>
         */
        public int[] ColumnBreaks
        {
            get
            {
                return this.ColumnBreaksRecord.GetBreaks();
            }
        }
 
        /**
         * @return the number of column page breaks
         */
        public int NumColumnBreaks
        {
            get
            {
                return this.ColumnBreaksRecord.NumBreaks;
            }
        }
 
        public VCenterRecord VCenter
        {
            get { return _vCenter; }
        }
 
        public HCenterRecord HCenter
        {
            get { return _hCenter; }
        }
        /// <summary>
        ///  HEADERFOOTER is new in 2007.  Some apps seem to have scattered this record long after
        /// the PageSettingsBlock where it belongs.
        /// </summary>
        /// <param name="rec"></param>
        public void AddLateHeaderFooter(HeaderFooterRecord rec)
        {
            if (_headerFooter != null)
            {
                throw new ArgumentNullException("This page settings block already has a header/footer record");
            }
            if (rec.Sid != UnknownRecord.HEADER_FOOTER_089C)
            {
                throw new RecordFormatException("Unexpected header-footer record sid: 0x" + StringUtil.ToHexString(rec.Sid));
            }
            _headerFooter = rec;
        }
 
 
        /// <summary>
        /// This method reads PageSettingsBlock records from the supplied RecordStream until the first non-PageSettingsBlock record is encountered.
        /// As each record is read, it is incorporated into this PageSettingsBlock.
        /// </summary>
        /// <param name="rs"></param> 
        public void AddLateRecords(RecordStream rs)
        {
            while (true)
            {
                if (!ReadARecord(rs))
                {
                    break;
                }
            }
        }
        public void PositionRecords(List<RecordBase> sheetRecords)
        {
            // Take a copy to loop over, so we can update the real one
            //  without concurrency issues
            List<HeaderFooterRecord> hfRecordsToIterate = new List<HeaderFooterRecord>(_sviewHeaderFooters);
 
            // loop through HeaderFooterRecord records having not-empty GUID and match them with
            // CustomViewSettingsRecordAggregate blocks having UserSViewBegin with the same GUID
            foreach (HeaderFooterRecord hf in hfRecordsToIterate)
            {
                foreach (RecordBase rb in sheetRecords)
                {
                    if (rb is CustomViewSettingsRecordAggregate)
                    {
                        CustomViewSettingsRecordAggregate cv = (CustomViewSettingsRecordAggregate)rb;
                        cv.VisitContainedRecords(new CustomRecordVisitor1(cv,hf,_sviewHeaderFooters));
                    }
                }
            }
        }
        private class CustomRecordVisitor1 : RecordVisitor
        {
            CustomViewSettingsRecordAggregate _cv;
            HeaderFooterRecord _hf;
            List<HeaderFooterRecord> _sviewHeaderFooters;
 
            public CustomRecordVisitor1(CustomViewSettingsRecordAggregate cv, HeaderFooterRecord hf, List<HeaderFooterRecord> sviewHeaderFooter)
            {
                this._cv = cv;
                this._hf = hf;
                this._sviewHeaderFooters = sviewHeaderFooter;
            }
 
            #region RecordVisitor Members
 
            public void VisitRecord(Record r)
            {
                if (r.Sid == UserSViewBegin.sid)
                {
                    byte[] guid1 = ((UserSViewBegin)r).Guid;
                    byte[] guid2 = _hf.Guid;
                    if (Arrays.Equals(guid1, guid2))
                    {
                        _cv.Append(_hf);
                        _sviewHeaderFooters.Remove(_hf);
                    }
                }
            }
 
            #endregion
        }
 
    }
}