zhao
2021-07-02 23ee356c6f260ecc1a48bbb8bd60932b979e4698
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
/* ====================================================================
   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.EventUserModel
{
    using System;
 
    using System.Collections;
 
    using HH.WMS.Utils.NPOI.HSSF.Model;
    using HH.WMS.Utils.NPOI.HSSF.Record;
    using HH.WMS.Utils.NPOI.HSSF.UserModel;
    using System.Collections.Generic;
 
    /// <summary>
    /// When working with the EventUserModel, if you want to
    /// Process formulas, you need an instance of
    /// Workbook to pass to a HSSFWorkbook,
    /// to finally give to HSSFFormulaParser,
    /// and this will build you stub ones.
    /// Since you're working with the EventUserModel, you
    /// wouldn't want to Get a full Workbook and
    ///  HSSFWorkbook, as they would eat too much memory.
    /// Instead, you should collect a few key records as they
    /// go past, then call this once you have them to build a
    /// stub Workbook, and from that a stub
    /// HSSFWorkbook, to use with the HSSFFormulaParser.
    /// The records you should collect are:
    /// ExternSheetRecord
    /// BoundSheetRecord
    /// You should probably also collect SSTRecord,
    /// but it's not required to pass this in.
    /// To help, this class includes a HSSFListener wrapper
    /// that will do the collecting for you.
    /// </summary>
    public class EventWorkbookBuilder
    {
        /// <summary>
        /// Wraps up your stub Workbook as a stub
        /// HSSFWorkbook, ready for passing to
        /// HSSFFormulaParser
        /// </summary>
        /// <param name="workbook">The stub workbook.</param>
        /// <returns></returns>
        public static HSSFWorkbook CreateStubHSSFWorkbook(InternalWorkbook workbook)
        {
            return new StubHSSFWorkbook(workbook);
        }
 
        /// <summary>
        /// Creates a stub Workbook from the supplied records,
        /// suitable for use with the {@link HSSFFormulaParser}
        /// </summary>
        /// <param name="externs">The ExternSheetRecords in your file</param>
        /// <param name="bounds">The BoundSheetRecords in your file</param>
        /// <param name="sst">TThe SSTRecord in your file.</param>
        /// <returns>A stub Workbook suitable for use with HSSFFormulaParser</returns>
        public static InternalWorkbook CreateStubWorkbook(ExternSheetRecord[] externs,
                BoundSheetRecord[] bounds, SSTRecord sst)
        {
            List<Record> wbRecords = new List<Record>();
 
            // Core Workbook records go first
            if (bounds != null)
            {
                for (int i = 0; i < bounds.Length; i++)
                {
                    wbRecords.Add(bounds[i]);
                }
            }
            if (sst != null)
            {
                wbRecords.Add(sst);
            }
 
            // Now we can have the ExternSheetRecords,
            //  preceded by a SupBookRecord
            if (externs != null)
            {
                wbRecords.Add(SupBookRecord.CreateInternalReferences(
                        (short)externs.Length));
                for (int i = 0; i < externs.Length; i++)
                {
                    wbRecords.Add(externs[i]);
                }
            }
 
            // Finally we need an EoF record
            wbRecords.Add(EOFRecord.instance);
 
            return InternalWorkbook.CreateWorkbook(wbRecords);
        }
 
        /// <summary>
        /// Creates a stub workbook from the supplied records,
        /// suitable for use with the HSSFFormulaParser
        /// </summary>
        /// <param name="externs">The ExternSheetRecords in your file</param>
        /// <param name="bounds">A stub Workbook suitable for use with HSSFFormulaParser</param>
        /// <returns>A stub Workbook suitable for use with {@link HSSFFormulaParser}</returns>
        public static InternalWorkbook CreateStubWorkbook(ExternSheetRecord[] externs,
                BoundSheetRecord[] bounds)
        {
            return CreateStubWorkbook(externs, bounds, null);
        }
 
 
        /// <summary>
        /// A wrapping HSSFListener which will collect
        /// BoundSheetRecords and {@link ExternSheetRecord}s as
        /// they go past, so you can Create a Stub {@link Workbook} from
        /// them once required.
        /// </summary>
        public class SheetRecordCollectingListener : HSSFListener
        {
            private HSSFListener childListener;
            private ArrayList boundSheetRecords = new ArrayList();
            private ArrayList externSheetRecords = new ArrayList();
            private SSTRecord sstRecord = null;
 
            /// <summary>
            /// Initializes a new instance of the <see cref="SheetRecordCollectingListener"/> class.
            /// </summary>
            /// <param name="childListener">The child listener.</param>
            public SheetRecordCollectingListener(HSSFListener childListener)
            {
                this.childListener = childListener;
            }
 
 
            /// <summary>
            /// Gets the bound sheet records.
            /// </summary>
            /// <returns></returns>
            public BoundSheetRecord[] GetBoundSheetRecords()
            {
                return (BoundSheetRecord[])boundSheetRecords.ToArray(
                        typeof(BoundSheetRecord)
                );
            }
            /// <summary>
            /// Gets the extern sheet records.
            /// </summary>
            /// <returns></returns>
            public ExternSheetRecord[] GetExternSheetRecords()
            {
                return (ExternSheetRecord[])externSheetRecords.ToArray(
                        typeof(ExternSheetRecord)
                );
            }
            /// <summary>
            /// Gets the SST record.
            /// </summary>
            /// <returns></returns>
            public SSTRecord GetSSTRecord()
            {
                return sstRecord;
            }
 
            /// <summary>
            /// Gets the stub HSSF workbook.
            /// </summary>
            /// <returns></returns>
            public HSSFWorkbook GetStubHSSFWorkbook()
            {
                return CreateStubHSSFWorkbook(
                        GetStubWorkbook()
                );
            }
            /// <summary>
            /// Gets the stub workbook.
            /// </summary>
            /// <returns></returns>
            public InternalWorkbook GetStubWorkbook()
            {
                return CreateStubWorkbook(
                        GetExternSheetRecords(), GetBoundSheetRecords(),
                        GetSSTRecord()
                );
            }
 
 
            /// <summary>
            /// Process this record ourselves, and then
            /// pass it on to our child listener
            /// </summary>
            /// <param name="record">The record.</param>
            public void ProcessRecord(Record record)
            {
                // Handle it ourselves
                ProcessRecordInternally(record);
 
                // Now pass on to our child
                childListener.ProcessRecord(record);
            }
 
            /// <summary>
            /// Process the record ourselves, but do not
            /// pass it on to the child Listener.
            /// </summary>
            /// <param name="record">The record.</param>
            public void ProcessRecordInternally(Record record)
            {
                if (record is BoundSheetRecord)
                {
                    boundSheetRecords.Add(record);
                }
                else if (record is ExternSheetRecord)
                {
                    externSheetRecords.Add(record);
                }
                else if (record is SSTRecord)
                {
                    sstRecord = (SSTRecord)record;
                }
            }
        }
 
        /**
         * Let us at the {@link Workbook} constructor on
         *  {@link HSSFWorkbook}
         */
        private class StubHSSFWorkbook : HSSFWorkbook
        {
            public StubHSSFWorkbook(InternalWorkbook wb)
                : base(wb)
            {
 
            }
        }
    }
}