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
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
/* ====================================================================
   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
{
 
    using System;
    using System.Text;
    using System.Collections;
    using HH.WMS.Utils.NPOI.DDF;
    using HH.WMS.Utils.NPOI.Util;
    using System.Collections.Generic;
    using HH.WMS.Utils.NPOI.HSSF.Util;
 
    /**
     * The escher container record is used to hold escher records.  It is abstract and
     * must be subclassed for maximum benefit.
     *
     * @author Glen Stampoultzis (glens at apache.org)
     * @author Michael Zalewski (zalewski at optonline.net)
     */
    public abstract class AbstractEscherHolderRecord : Record
    {
        private static bool DESERIALISE;
        static AbstractEscherHolderRecord()
        {
            DESERIALISE = false;
            //try
            //{
            //    DESERIALISE = (System.Configuration.ConfigurationManager.AppSettings["poi.deserialize.escher"] != null);
            //}
            //catch (Exception)
            //{
            //    DESERIALISE = false;
            //}
        }
 
        private List<EscherRecord> escherRecords;
        private LazilyConcatenatedByteArray rawDataContainer = new LazilyConcatenatedByteArray();
        //private byte[] rawData;
 
 
        public AbstractEscherHolderRecord()
        {
            escherRecords = new List<EscherRecord>();
        }
 
        /**
         * Constructs a Bar record and Sets its fields appropriately.
         *
         * @param in the RecordInputstream to Read the record from
         */
        public AbstractEscherHolderRecord(RecordInputStream in1)
        {
            escherRecords = new List<EscherRecord>();
            if (!DESERIALISE)
            {
                rawDataContainer.Concatenate(in1.ReadRemainder());
            }
            else
            {
                byte[] data = in1.ReadAllContinuedRemainder();
                ConvertToEscherRecords(0, data.Length, data);
            }
 
        }
 
        protected void ConvertRawBytesToEscherRecords()
        {
            byte[] rawData = RawData;
            ConvertToEscherRecords(0, rawData.Length, rawData);
        }
        private void ConvertToEscherRecords(int offset, int size, byte[] data)
        {
            escherRecords.Clear();
            EscherRecordFactory recordFactory = new DefaultEscherRecordFactory();
            int pos = offset;
            while (pos < offset + size)
            {
                EscherRecord r = recordFactory.CreateRecord(data, pos);
                int bytesRead = r.FillFields(data, pos, recordFactory);
                escherRecords.Add(r);
                pos += bytesRead;
            }
        }
 
        public override String ToString()
        {
            StringBuilder buffer = new StringBuilder();
 
            String nl = Environment.NewLine;
            buffer.Append('[' + RecordName + ']' + nl);
            if (escherRecords.Count == 0)
                buffer.Append("No Escher Records Decoded" + nl);
            foreach (EscherRecord r in escherRecords)
            {
                buffer.Append(r.ToString());
            }
            buffer.Append("[/" + RecordName + ']' + nl);
 
            return buffer.ToString();
        }
 
        protected abstract String RecordName { get; }
 
        public override int Serialize(int offset, byte[] data)
        {
            LittleEndian.PutShort(data, 0 + offset, Sid);
            LittleEndian.PutShort(data, 2 + offset, (short)(RecordSize - 4));
            byte[] rawData = RawData;
            if (escherRecords.Count == 0 && rawData != null)
            {
                LittleEndian.PutShort(data, 0 + offset, Sid);
                LittleEndian.PutShort(data, 2 + offset, (short)(RecordSize - 4));
                Array.Copy(rawData, 0, data, 4 + offset, rawData.Length);
                return rawData.Length + 4;
            }
            LittleEndian.PutShort(data, 0 + offset, Sid);
            LittleEndian.PutShort(data, 2 + offset, (short)(RecordSize - 4));
 
            int pos = offset + 4;
            foreach (EscherRecord r in escherRecords)
            {
                pos += r.Serialize(pos, data, new NullEscherSerializationListener());
            }
            return RecordSize;
        }
 
 
 
        /**
         * Size of record (including 4 byte header)
         */
        public override int RecordSize
        {
            get
            {
                byte[] rawData = RawData;
                if (escherRecords.Count == 0 && rawData != null)
                {
                    return rawData.Length + 4;
                }
                else
                {
                    int size = 4;
                    foreach (EscherRecord r in escherRecords)
                    {
                        //EscherRecord r = (EscherRecord)iterator.Current;
                        size += r.RecordSize;
                    }
                    return size;
                }
            }
        }
 
        public override object Clone()
        {
            return CloneViaReserialise();
        }
 
 
        /**
 * Clone the current record, via a call to serialise
 *  it, and another to Create a new record from the
 *  bytes.
 * May only be used for classes which don't have
 *  internal counts / ids in them. For those which
 *  do, a full record-aware serialise is needed, which
 *  allocates new ids / counts as needed.
 */
        //public override Record CloneViaReserialise()
        //{
        //    // Do it via a re-serialise
        //    // It's a cheat, but it works...
        //    byte[] b = this.Serialize();
        //    using (var ms = new System.IO.MemoryStream(b))
        //    {
        //        RecordInputStream rinp = new RecordInputStream(ms);
        //        rinp.NextRecord();
 
        //        Record[] r = RecordFactory.CreateRecord(rinp);
        //        if (r.Length != 1)
        //        {
        //            throw new InvalidOperationException("Re-serialised a record to Clone it, but got " + r.Length + " records back!");
        //        }
        //        return r[0];
        //    }
        //}
 
        public void AddEscherRecord(int index, EscherRecord element)
        {
            escherRecords.Insert(index, element);
        }
 
        public bool AddEscherRecord(EscherRecord element)
        {
            escherRecords.Add(element);
            return true;
        }
 
        public List<EscherRecord> EscherRecords
        {
            get { return escherRecords; }
        }
 
        public void ClearEscherRecords()
        {
            escherRecords.Clear();
        }
 
        /**
         * If we have a EscherContainerRecord as one of our
         *  children (and most top level escher holders do),
         *  then return that.
         */
        public EscherContainerRecord GetEscherContainer()
        {
            for (IEnumerator it = escherRecords.GetEnumerator(); it.MoveNext(); )
            {
                Object er = it.Current;
                if (er is EscherContainerRecord)
                {
                    return (EscherContainerRecord)er;
                }
            }
            return null;
        }
 
        /**
         * Descends into all our children, returning the
         *  first EscherRecord with the given id, or null
         *  if none found
         */
        public EscherRecord FindFirstWithId(short id)
        {
            return FindFirstWithId(id, EscherRecords);
        }
        private EscherRecord FindFirstWithId(short id, List<EscherRecord> records)
        {
            // Check at our level
            for (IEnumerator it = records.GetEnumerator(); it.MoveNext(); )
            {
                EscherRecord r = (EscherRecord)it.Current;
                if (r.RecordId == id)
                {
                    return r;
                }
            }
 
            // Then Check our children in turn
            for (IEnumerator it = records.GetEnumerator(); it.MoveNext(); )
            {
                EscherRecord r = (EscherRecord)it.Current;
                if (r.IsContainerRecord)
                {
                    EscherRecord found =
                        FindFirstWithId(id, r.ChildRecords);
                    if (found != null)
                    {
                        return found;
                    }
                }
            }
 
            // Not found in this lot
            return null;
        }
 
 
        public EscherRecord GetEscherRecord(int index)
        {
            return (EscherRecord)escherRecords[index];
        }
 
        /**
         * Big drawing Group records are split but it's easier to deal with them
         * as a whole Group so we need to join them toGether.
         */
        public void Join(AbstractEscherHolderRecord record)
        {
            rawDataContainer.Concatenate(record.RawData);
        }
 
        public void ProcessContinueRecord(byte[] record)
        {
            rawDataContainer.Concatenate(record);
        }
 
        public byte[] RawData
        {
            get { return rawDataContainer.ToArray(); }
            set
            {
                rawDataContainer.Clear();
                rawDataContainer.Concatenate(value);
            }
        }
 
        /**
         * Convert raw data to escher records.
         */
        public void Decode()
        {
            byte[] rawData = RawData;
            ConvertToEscherRecords(0, rawData.Length, rawData);
        }
 
    }
 
}