zhao
2021-07-02 081df17b8cc4a6e7e4f4e1e1887f24810e3ec2f9
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
/* ====================================================================
   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.Util;
    using HH.WMS.Utils.NPOI.SS.Formula;
 
    using HH.WMS.Utils.NPOI.SS.Formula.PTG;
    using HH.WMS.Utils.NPOI.SS.Formula.Constant;
 
 
    /**
     * EXTERNALNAME<p/>
     * 
     * @author Josh Micich
     */
    public class ExternalNameRecord : StandardRecord
    {
 
        public static short sid = 0x23; // as per BIFF8. (some old versions used 0x223)
 
        private static int OPT_BUILTIN_NAME = 0x0001;
        private static int OPT_AUTOMATIC_LINK = 0x0002; // m$ doc calls this fWantAdvise 
        private static int OPT_PICTURE_LINK = 0x0004;
        private static int OPT_STD_DOCUMENT_NAME = 0x0008;
        private static int OPT_OLE_LINK = 0x0010;
        //    private static int OPT_CLIP_FORMAT_MASK      = 0x7FE0;
        private static int OPT_ICONIFIED_PICTURE_LINK = 0x8000;
 
 
        private short field_1_option_flag;
        private short field_2_ixals;
        private short field_3_not_used;
        private String field_4_name;
        private HH.WMS.Utils.NPOI.SS.Formula.Formula field_5_name_definition; // TODO - junits for name definition field
 
        /**
 * 'rgoper' / 'Last received results of the DDE link'
 * (seems to be only applicable to DDE links)<br/>
 * Logically this is a 2-D array, which has been flattened into 1-D array here.
 */
        private Object[] _ddeValues;
        /**
         * (logical) number of columns in the {@link #_ddeValues} array
         */
        private int _nColumns;
        /**
         * (logical) number of rows in the {@link #_ddeValues} array
         */
        private int _nRows;
        public ExternalNameRecord()
        {
            field_2_ixals = 0;
        }
        public ExternalNameRecord(RecordInputStream in1)
        {
            field_1_option_flag = in1.ReadShort();
            field_2_ixals = in1.ReadShort();
            field_3_not_used = in1.ReadShort();
            int numChars = in1.ReadUByte();
            field_4_name = StringUtil.ReadUnicodeString(in1, numChars);
 
            // the record body can take different forms.
            // The form is dictated by the values of 3-th and 4-th bits in field_1_option_flag
            if (!IsOLELink && !IsStdDocumentNameIdentifier)
            {
                // another switch: the fWantAdvise bit specifies whether the body describes
                // an external defined name or a DDE data item
                if (IsAutomaticLink)
                {
                    if (in1.Available() > 0)
                    {
                        //body specifies DDE data item
                        int nColumns = in1.ReadUByte() + 1;
                        int nRows = in1.ReadShort() + 1;
 
                        int totalCount = nRows * nColumns;
                        _ddeValues = ConstantValueParser.Parse(in1, totalCount);
                        _nColumns = nColumns;
                        _nRows = nRows;
                    }
                }
                else
                {
                    //body specifies an external defined name
                    int formulaLen = in1.ReadUShort();
                    field_5_name_definition = Formula.Read(formulaLen, in1);
                }
            }
            //int nameLength = in1.ReadUByte();
            //int multibyteFlag = in1.ReadUByte();
            //if (multibyteFlag == 0)
            //{
            //    field_4_name = in1.ReadCompressedUnicode(nameLength);
            //}
            //else
            //{
            //    field_4_name = in1.ReadUnicodeLEString(nameLength);
            //}
            //if (!HasFormula)
            //{
            //    if (!IsStdDocumentNameIdentifier && !IsOLELink && IsAutomaticLink)
            //    {
            //        // both need to be incremented
            //        int nColumns = in1.ReadUByte() + 1;
            //        int nRows = in1.ReadShort() + 1;
 
            //        int totalCount = nRows * nColumns;
            //        _ddeValues = ConstantValueParser.Parse(in1, totalCount);
            //        _nColumns = nColumns;
            //        _nRows = nRows;
            //    }
 
            //    if (in1.Remaining > 0)
            //    {
            //        throw ReadFail("Some Unread data (is formula present?)");
            //    }
            //    field_5_name_definition = null;
            //    return;
            //}
            //int nBytesRemaining = in1.Available();
            //if (in1.Remaining <= 0)
            //{
            //    throw ReadFail("Ran out of record data trying to read formula.");
            //}
            //short formulaLen = in1.ReadShort();
            //nBytesRemaining -= 2;
            //field_5_name_definition = HH.WMS.Utils.NPOI.SS.Formula.Formula.Read(formulaLen, in1, nBytesRemaining);
        }
 
        /**
         * Convenience Function to determine if the name Is a built-in name
         */
        public bool IsBuiltInName
        {
            get
            {
                return (field_1_option_flag & OPT_BUILTIN_NAME) != 0;
            }
        }
        /**
         * For OLE and DDE, links can be either 'automatic' or 'manual'
         */
        public bool IsAutomaticLink
        {
            get { return (field_1_option_flag & OPT_AUTOMATIC_LINK) != 0; }
        }
        /**
         * only for OLE and DDE
         */
        public bool IsPicureLink
        {
            get { return (field_1_option_flag & OPT_PICTURE_LINK) != 0; }
        }
        /**
         * DDE links only. If <c>true</c>, this denotes the 'StdDocumentName'
         */
        public bool IsStdDocumentNameIdentifier
        {
            get { return (field_1_option_flag & OPT_STD_DOCUMENT_NAME) != 0; }
        }
        public bool IsOLELink
        {
            get { return (field_1_option_flag & OPT_OLE_LINK) != 0; }
        }
        public bool IsIconifiedPictureLink
        {
            get { return (field_1_option_flag & OPT_ICONIFIED_PICTURE_LINK) != 0; }
        }
        public short Ix
        {
            get
            {
                return field_2_ixals;
            }
            set
            {
                field_2_ixals = value;
            }
        }
 
        /**
         * @return the standard String representation of this name
         */
        public String Text
        {
            get { return field_4_name; }
            set { field_4_name = value; }
        }
 
        public Ptg[] GetParsedExpression()
        {
            return Formula.GetTokens(field_5_name_definition);
        }
        public void SetParsedExpression(Ptg[] ptgs)
        {
            field_5_name_definition = Formula.Create(ptgs);
        }
        protected override int DataSize
        {
            get
            {
                //int result = 3 * 2  // 3 short fields
                //    + 2 + field_4_name.Length; // nameLen and name
                //if (HasFormula)
                //{
                //    result += field_5_name_definition.EncodedSize;
                //}
                //else
                //{
                //    if (_ddeValues != null)
                //    {
                //        result += 3; // byte, short
                //        result += ConstantValueParser.GetEncodedSize(_ddeValues);
                //    }
                //}
                //return result;
                int result = 2 + 4;  // short and int
                result += StringUtil.GetEncodedSize(field_4_name) - 1; //size is byte, not short 
 
                if (!IsOLELink && !IsStdDocumentNameIdentifier)
                {
                    if (IsAutomaticLink)
                    {
                        result += 3; // byte, short
                        result += ConstantValueParser.GetEncodedSize(_ddeValues);
                    }
                    else
                    {
                        result += field_5_name_definition.EncodedSize;
                    }
                }
                return result;
            }
        }
 
        public override void Serialize(ILittleEndianOutput out1)
        {
            //out1.WriteShort(field_1_option_flag);
            //out1.WriteShort(field_2_ixals);
            //out1.WriteShort(field_3_not_used);
            //int nameLen = field_4_name.Length;
            //out1.WriteShort(nameLen);
            //StringUtil.PutCompressedUnicode(field_4_name, out1);
            //if (HasFormula)
            //{
            //    field_5_name_definition.Serialize(out1);
            //}
            //else
            //{
            //    if (_ddeValues != null)
            //    {
            //        out1.WriteByte(_nColumns - 1);
            //        out1.WriteShort(_nRows - 1);
            //        ConstantValueParser.Encode(out1, _ddeValues);
            //    }
            //}
            out1.WriteShort(field_1_option_flag);
            out1.WriteShort(field_2_ixals);
            out1.WriteShort(field_3_not_used);
 
            out1.WriteByte(field_4_name.Length);
            StringUtil.WriteUnicodeStringFlagAndData(out1, field_4_name);
 
            if (!IsOLELink && !IsStdDocumentNameIdentifier)
            {
                if (IsAutomaticLink)
                {
                    out1.WriteByte(_nColumns - 1);
                    out1.WriteShort(_nRows - 1);
                    ConstantValueParser.Encode(out1, _ddeValues);
                }
                else
                {
                    field_5_name_definition.Serialize(out1);
                }
            }
        }
 
 
        //public override int RecordSize
        //{
        //    get { return 4 + DataSize; }
        //}
 
        /*
         * Makes better error messages (while HasFormula() Is not reliable) 
         * Remove this when HasFormula() Is stable.
         */
        private Exception ReadFail(String msg)
        {
            String fullMsg = msg + " fields: (option=" + field_1_option_flag + " index=" + field_2_ixals
            + " not_used=" + field_3_not_used + " name='" + field_4_name + "')";
            return new Exception(fullMsg);
        }
 
        private bool HasFormula
        {
            get
            {
                // TODO - determine exact conditions when formula Is present
                //if (false)
                //{
                //    // "Microsoft Office Excel 97-2007 Binary File Format (.xls) Specification"
                //    // m$'s document suggests logic like this, but bugzilla 44774 att 21790 seems to disagree
                //    if (IsStdDocumentNameIdentifier)
                //    {
                //        if (IsOLELink)
                //        {
                //            // seems to be not possible according to m$ document
                //            throw new InvalidOperationException(
                //                    "flags (std-doc-name and ole-link) cannot be true at the same time");
                //        }
                //        return false;
                //    }
                //    if (IsOLELink)
                //    {
                //        return false;
                //    }
                //    return true;
                //}
 
                // This was derived by trial and error, but doesn't seem quite right
                if (IsAutomaticLink)
                {
                    return false;
                }
                return true;
            }
        }
 
        public override short Sid
        {
            get { return sid; }
        }
 
        public override String ToString()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append("[EXTERNALNAME]\n");
            sb.Append("    .options      = ").Append(field_1_option_flag).Append("\n");
            sb.Append("    .ix      = ").Append(field_2_ixals).Append("\n");
            sb.Append("    .name    = ").Append(field_4_name).Append("\n");
            if (field_5_name_definition != null)
            {
                Ptg[] ptgs = field_5_name_definition.Tokens;
                for (int i = 0; i < ptgs.Length; i++)
                {
                    Ptg ptg = ptgs[i];
                    sb.Append(ptg.ToString()).Append(ptg.RVAType).Append("\n");
                }
            }
            sb.Append("[/EXTERNALNAME]\n");
            return sb.ToString();
        }
    }
}