zhao
2021-07-19 8347f2fbddbd25369359dcb2da1233ac48a19fdc
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
/* ====================================================================
   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.SS.Formula
{
    using System;
    using System.Collections;
    using HH.WMS.Utils.NPOI.HSSF.Record;
    using HH.WMS.Utils.NPOI.SS.Formula;
    using HH.WMS.Utils.NPOI.Util;
 
    using HH.WMS.Utils.NPOI.SS.Util;
    using HH.WMS.Utils.NPOI.SS.Formula.PTG;
 
    /**
     * Encapsulates an encoded formula token array. 
     * 
     * @author Josh Micich
     */
    public class Formula
    {
 
        private static Formula EMPTY = new Formula(new byte[0], 0);
 
        /** immutable */
        private byte[] _byteEncoding;
        private int _encodedTokenLen;
 
        private Formula(byte[] byteEncoding, int encodedTokenLen)
        {
            _byteEncoding = byteEncoding;
            _encodedTokenLen = encodedTokenLen;
            //if (false) { // set to true to eagerly check Ptg decoding 
            //    LittleEndianByteArrayInputStream in1 = new LittleEndianByteArrayInputStream(byteEncoding);
            //    Ptg.ReadTokens(encodedTokenLen, in1);
            //    int nUnusedBytes = _byteEncoding.Length - in1.GetReadIndex();
            //    if (nUnusedBytes > 0) {
            //        // TODO - this seems to occur when IntersectionPtg is present
            //        // This example file "IntersectionPtg.xls"
            //        // used by test: TestIntersectionPtg.testReading()
            //        // has 10 bytes unused at the end of the formula
            //        // 10 extra bytes are just 0x01 and 0x00
            //        Console.WriteLine(nUnusedBytes + " unused bytes at end of formula");
            //    }
            //}
        }
        /**
         * Convenience method for {@link #read(int, LittleEndianInput, int)}
         */
        public static Formula Read(int encodedTokenLen, ILittleEndianInput in1)
        {
            return Read(encodedTokenLen, in1, encodedTokenLen);
        }
        /**
         * When there are no array constants present, <c>encodedTokenLen</c>==<c>totalEncodedLen</c>
         * @param encodedTokenLen number of bytes in the stream taken by the plain formula tokens
         * @param totalEncodedLen the total number of bytes in the formula (includes trailing encoding
         * for array constants, but does not include 2 bytes for initial <c>ushort encodedTokenLen</c> field.
         * @return A new formula object as read from the stream.  Possibly empty, never <code>null</code>.
         */
        public static Formula Read(int encodedTokenLen, ILittleEndianInput in1, int totalEncodedLen)
        {
            byte[] byteEncoding = new byte[totalEncodedLen];
            in1.ReadFully(byteEncoding);
            return new Formula(byteEncoding, encodedTokenLen);
        }
 
        public Ptg[] Tokens
        {
            get
            {
                ILittleEndianInput in1 = new LittleEndianByteArrayInputStream(_byteEncoding);
                return Ptg.ReadTokens(_encodedTokenLen, in1);
            }
        }
        /**
         * Writes  The formula encoding is includes:
         * <ul>
         * <li>ushort tokenDataLen</li>
         * <li>tokenData</li>
         * <li>arrayConstantData (if present)</li>
         * </ul>
         */
        public void Serialize(ILittleEndianOutput out1)
        {
            out1.WriteShort(_encodedTokenLen);
            out1.Write(_byteEncoding);
        }
 
        public void SerializeTokens(ILittleEndianOutput out1)
        {
            out1.Write(_byteEncoding, 0, _encodedTokenLen);
        }
        public void SerializeArrayConstantData(ILittleEndianOutput out1)
        {
            int len = _byteEncoding.Length - _encodedTokenLen;
            out1.Write(_byteEncoding, _encodedTokenLen, len);
        }
 
 
        /**
         * @return total formula encoding length.  The formula encoding includes:
         * <ul>
         * <li>ushort tokenDataLen</li>
         * <li>tokenData</li>
         * <li>arrayConstantData (optional)</li>
         * </ul>
         * Note - this value is different to <c>tokenDataLength</c>
         */
        public int EncodedSize
        {
            get
            {
                return 2 + _byteEncoding.Length;
            }
        }
        /**
         * This method is often used when the formula length does not appear immediately before
         * the encoded token data.
         * 
         * @return the encoded length of the plain formula tokens.  This does <em>not</em> include
         * the leading ushort field, nor any trailing array constant data.
         */
        public int EncodedTokenSize
        {
            get
            {
                return _encodedTokenLen;
            }
        }
 
        /**
         * Creates a {@link Formula} object from a supplied {@link Ptg} array. 
         * Handles <code>null</code>s OK.
         * @param ptgs may be <code>null</code>
         * @return Never <code>null</code> (Possibly empty if the supplied <c>ptgs</c> is <code>null</code>)
         */
        public static Formula Create(Ptg[] ptgs)
        {
            if (ptgs == null || ptgs.Length < 1)
            {
                return EMPTY;
            }
            int totalSize = Ptg.GetEncodedSize(ptgs);
            byte[] encodedData = new byte[totalSize];
            Ptg.SerializePtgs(ptgs, encodedData, 0);
            int encodedTokenLen = Ptg.GetEncodedSizeWithoutArrayData(ptgs);
            return new Formula(encodedData, encodedTokenLen);
        }
        /**
         * Gets the {@link Ptg} array from the supplied {@link Formula}. 
         * Handles <code>null</code>s OK.
         * 
         * @param formula may be <code>null</code>
         * @return possibly <code>null</code> (if the supplied <c>formula</c> is <code>null</code>)
         */
        public static Ptg[] GetTokens(Formula formula)
        {
            if (formula == null)
            {
                return null;
            }
            return formula.Tokens;
        }
 
        public Formula Copy()
        {
            // OK to return this because immutable
            return this;
        }
 
        /**
         * Gets the locator for the corresponding {@link SharedFormulaRecord}, {@link ArrayRecord} or
         * {@link TableRecord} if this formula belongs to such a grouping.  The {@link CellReference}
         * returned by this method will  match the top left corner of the range of that grouping. 
         * The return value is usually not the same as the location of the cell containing this formula.
         * 
         * @return the firstRow &amp; firstColumn of an array formula or shared formula that this formula
         * belongs to.  <code>null</code> if this formula is not part of an array or shared formula.
         */
        public CellReference ExpReference
        {
            get
            {
                byte[] data = _byteEncoding;
                if (data.Length != 5)
                {
                    // tExp and tTbl are always 5 bytes long, and the only ptg in the formula
                    return null;
                }
                switch (data[0])
                {
                    case ExpPtg.sid:
                        break;
                    case TblPtg.sid:
                        break;
                    default:
                        return null;
                }
                int firstRow = LittleEndian.GetUShort(data, 1);
                int firstColumn = LittleEndian.GetUShort(data, 3);
                return new CellReference(firstRow, firstColumn);
            }
        }
        public bool IsSame(Formula other)
        {
            return Arrays.Equals(_byteEncoding, other._byteEncoding);
        }
    }
}