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
/* ====================================================================
   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 HH.WMS.Utils.NPOI.SS.Formula;
    using HH.WMS.Utils.NPOI.SS.Formula.PTG;
 
    /**
     * This class performs 'operand class' transformation. Non-base Tokens are classified into three 
     * operand classes:
     * <ul>
     * <li>reference</li> 
     * <li>value</li> 
     * <li>array</li> 
     * </ul>
     * <p/>
     * 
     * The operand class chosen for each Token depends on the formula type and the Token's place
     * in the formula. If POI Gets the operand class wrong, Excel <em>may</em> interpret the formula
     * incorrectly.  This condition is typically manifested as a formula cell that displays as '#VALUE!',
     * but resolves correctly when the user presses F2, enter.<p/>
     * 
     * The logic implemented here was partially inspired by the description in
     * "OpenOffice.org's Documentation of the Microsoft Excel File Format".  The model presented there
     * seems To be inconsistent with observed Excel behaviour (These differences have not been fully
     * investigated). The implementation in this class Has been heavily modified in order To satisfy
     * concrete examples of how Excel performs the same logic (see TestRVA).<p/>
     * 
     * Hopefully, as Additional important test cases are identified and Added To the test suite, 
     * patterns might become more obvious in this code and allow for simplification.
     * 
     * @author Josh Micich
     */
    class OperandClassTransformer
    {
 
        private FormulaType _formulaType;
 
        public OperandClassTransformer(FormulaType formulaType)
        {
            _formulaType = formulaType;
        }
 
        /**
         * Traverses the supplied formula parse tree, calling <c>Ptg.SetClass()</c> for each non-base
         * Token To Set its operand class.
         */
        public void TransformFormula(ParseNode rootNode)
        {
            byte rootNodeOperandClass;
            switch (_formulaType)
            {
                case FormulaType.CELL:
                    rootNodeOperandClass = Ptg.CLASS_VALUE;
                    break;
                case FormulaType.ARRAY:
                    rootNodeOperandClass = Ptg.CLASS_ARRAY;
                    break;
                case FormulaType.NAMEDRANGE:
                case FormulaType.DATAVALIDATION_LIST:
                    rootNodeOperandClass = Ptg.CLASS_REF;
                    break;
                default:
                    throw new Exception("Incomplete code - formula type ("
                            + _formulaType + ") not supported yet");
 
            }
            TransformNode(rootNode, rootNodeOperandClass, false);
        }
 
        /**
         * @param callerForceArrayFlag <c>true</c> if one of the current node's parents is a 
         * function Ptg which Has been Changed from default 'V' To 'A' type (due To requirements on
         * the function return value).
         */
        private void TransformNode(ParseNode node, byte desiredOperandClass,
                bool callerForceArrayFlag)
        {
            Ptg token = node.GetToken();
            ParseNode[] children = node.GetChildren();
            bool IsSimpleValueFunc = IsSimpleValueFunction(token);
 
            if (IsSimpleValueFunc)
            {
                bool localForceArray = desiredOperandClass == Ptg.CLASS_ARRAY;
                for (int i = 0; i < children.Length; i++)
                {
                    TransformNode(children[i], desiredOperandClass, localForceArray);
                }
                SetSimpleValueFuncClass((AbstractFunctionPtg)token, desiredOperandClass, callerForceArrayFlag);
                return;
            }
        if (IsSingleArgSum(token)) {
            // Need to process the argument of SUM with transformFunctionNode below
            // so make a dummy FuncVarPtg for that call.
            token = FuncVarPtg.SUM;
            // Note - the tAttrSum token (node.getToken()) is a base
            // token so does not need to have its operand class set
        }
            if (token is ValueOperatorPtg || token is ControlPtg
                || token is MemFuncPtg
                || token is MemAreaPtg
                || token is UnionPtg)
            {
                // Value Operator Ptgs and Control are base Tokens, so Token will be unchanged
                // but any child nodes are processed according To desiredOperandClass and callerForceArrayFlag
 
                // As per OOO documentation Sec 3.2.4 "Token Class Transformation", "Step 1"
                // All direct operands of value operators that are initially 'R' type will 
                // be converted To 'V' type.
                byte localDesiredOperandClass = desiredOperandClass == Ptg.CLASS_REF ? Ptg.CLASS_VALUE : desiredOperandClass;
                for (int i = 0; i < children.Length; i++)
                {
                    TransformNode(children[i], localDesiredOperandClass, callerForceArrayFlag);
                }
                return;
            }
            if (token is AbstractFunctionPtg)
            {
                TransformFunctionNode((AbstractFunctionPtg)token, children, desiredOperandClass, callerForceArrayFlag);
                return;
            }
            if (children.Length > 0)
            {
                //if (token == RangePtg.instance)
                if(token is OperationPtg)
                {
                    // TODO is any Token transformation required under the various ref operators?
                    return;
                }
                throw new InvalidOperationException("Node should not have any children");
            }
 
            if (token.IsBaseToken)
            {
                // nothing To do
                return;
            }
            token.PtgClass = (TransformClass(token.PtgClass, desiredOperandClass, callerForceArrayFlag));
        }
        private static bool IsSingleArgSum(Ptg token)
        {
            if (token is AttrPtg)
            {
                AttrPtg attrPtg = (AttrPtg)token;
                return attrPtg.IsSum;
            }
            return false;
        }
        private static bool IsSimpleValueFunction(Ptg token)
        {
            if (token is AbstractFunctionPtg)
            {
                AbstractFunctionPtg aptg = (AbstractFunctionPtg)token;
                if (aptg.DefaultOperandClass != Ptg.CLASS_VALUE)
                {
                    return false;
                }
                int numberOfOperands = aptg.NumberOfOperands;
                for (int i = numberOfOperands - 1; i >= 0; i--)
                {
                    if (aptg.GetParameterClass(i) != Ptg.CLASS_VALUE)
                    {
                        return false;
                    }
                }
                return true;
            }
            return false;
        }
 
        private byte TransformClass(byte currentOperandClass, byte desiredOperandClass,
                bool callerForceArrayFlag)
        {
            switch (desiredOperandClass)
            {
                case Ptg.CLASS_VALUE:
                    if (!callerForceArrayFlag)
                    {
                        return Ptg.CLASS_VALUE;
                    }
                    return Ptg.CLASS_ARRAY;
                    //break;
                // else fall through
                case Ptg.CLASS_ARRAY:
                    return Ptg.CLASS_ARRAY;
                case Ptg.CLASS_REF:
                    if (!callerForceArrayFlag)
                    {
                        return currentOperandClass;
                    }
                    return Ptg.CLASS_REF;
            }
            throw new InvalidOperationException("Unexpected operand class (" + desiredOperandClass + ")");
        }
 
        private void TransformFunctionNode(AbstractFunctionPtg afp, ParseNode[] children,
                byte desiredOperandClass, bool callerForceArrayFlag)
        {
 
            bool localForceArrayFlag;
            byte defaultReturnOperandClass = afp.DefaultOperandClass;
 
            if (callerForceArrayFlag)
            {
                switch (defaultReturnOperandClass)
                {
                    case Ptg.CLASS_REF:
                        if (desiredOperandClass == Ptg.CLASS_REF)
                        {
                            afp.PtgClass = (Ptg.CLASS_REF);
                        }
                        else
                        {
                            afp.PtgClass = (Ptg.CLASS_ARRAY);
                        }
                        localForceArrayFlag = false;
                        break;
                    case Ptg.CLASS_ARRAY:
                        afp.PtgClass = (Ptg.CLASS_ARRAY);
                        localForceArrayFlag = false;
                        break;
                    case Ptg.CLASS_VALUE:
                        afp.PtgClass = (Ptg.CLASS_ARRAY);
                        localForceArrayFlag = true;
                        break;
                    default:
                        throw new InvalidOperationException("Unexpected operand class ("
                                + defaultReturnOperandClass + ")");
                }
            }
            else
            {
                if (defaultReturnOperandClass == desiredOperandClass)
                {
                    localForceArrayFlag = false;
                    // an alternative would have been To for non-base Ptgs To Set their operand class 
                    // from their default, but this would require the call in many subclasses because
                    // the default OC is not known until the end of the constructor
                    afp.PtgClass = (defaultReturnOperandClass);
                }
                else
                {
                    switch (desiredOperandClass)
                    {
                        case Ptg.CLASS_VALUE:
                            // always OK To Set functions To return 'value'
                            afp.PtgClass = (Ptg.CLASS_VALUE);
                            localForceArrayFlag = false;
                            break;
                        case Ptg.CLASS_ARRAY:
                            switch (defaultReturnOperandClass)
                            {
                                case Ptg.CLASS_REF:
                                    afp.PtgClass = (Ptg.CLASS_REF);
                                    //                                afp.SetClass(Ptg.CLASS_ARRAY);
                                    break;
                                case Ptg.CLASS_VALUE:
                                    afp.PtgClass = (Ptg.CLASS_ARRAY);
                                    break;
                                default:
                                    throw new InvalidOperationException("Unexpected operand class ("
                                            + defaultReturnOperandClass + ")");
                            }
                            localForceArrayFlag = (defaultReturnOperandClass == Ptg.CLASS_VALUE);
                            break;
                        case Ptg.CLASS_REF:
                            switch (defaultReturnOperandClass)
                            {
                                case Ptg.CLASS_ARRAY:
                                    afp.PtgClass=(Ptg.CLASS_ARRAY);
                                    break;
                                case Ptg.CLASS_VALUE:
                                    afp.PtgClass=(Ptg.CLASS_VALUE);
                                    break;
                                default:
                                    throw new InvalidOperationException("Unexpected operand class ("
                                            + defaultReturnOperandClass + ")");
                            }
                            localForceArrayFlag = false;
                            break;
                        default:
                            throw new InvalidOperationException("Unexpected operand class ("
                                    + desiredOperandClass + ")");
                    }
 
                }
            }
 
            for (int i = 0; i < children.Length; i++)
            {
                ParseNode child = children[i];
                byte paramOperandClass = afp.GetParameterClass(i);
                TransformNode(child, paramOperandClass, localForceArrayFlag);
            }
        }
 
        private void SetSimpleValueFuncClass(AbstractFunctionPtg afp,
                byte desiredOperandClass, bool callerForceArrayFlag)
        {
 
            if (callerForceArrayFlag || desiredOperandClass == Ptg.CLASS_ARRAY)
            {
                afp.PtgClass = (Ptg.CLASS_ARRAY);
            }
            else
            {
                afp.PtgClass = (Ptg.CLASS_VALUE);
            }
        }
    }
}