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
/* ====================================================================
   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.Function
{
    using System;
    using System.Reflection;
    using System.Collections;
    using System.IO;
    using System.Text.RegularExpressions;
    using HH.WMS.Utils.NPOI.SS.Formula;
    using HH.WMS.Utils.NPOI.SS;
    using HH.WMS.Utils.NPOI.SS.Formula.PTG;
    using System.Globalization;
 
    /**
     * Converts the text meta-data file into a <c>FunctionMetadataRegistry</c>
     * 
     * @author Josh Micich
     */
    class FunctionMetadataReader
    {
 
        //private const String METADATA_FILE_NAME = "functionMetadata.txt";
 
        /** plain ASCII text metadata file uses three dots for ellipsis */
        private static String ELLIPSIS = "...";
 
        private static string TAB_DELIM_PATTERN = @"\t";
        private static string SPACE_DELIM_PATTERN = @"\s";
        private static byte[] EMPTY_BYTE_ARRAY = { };
 
        private static String[] DIGIT_ENDING_FUNCTION_NAMES = {
        // Digits at the end of a function might be due to a left-over footnote marker.
        // except in these cases
        "LOG10", "ATAN2", "DAYS360", "SUMXMY2", "SUMX2MY2", "SUMX2PY2",
    };
        private static ArrayList DIGIT_ENDING_FUNCTION_NAMES_Set = new ArrayList();
 
        static FunctionMetadataReader()
        {
            for (int i = 0; i < DIGIT_ENDING_FUNCTION_NAMES.Length; i++)
            {
                DIGIT_ENDING_FUNCTION_NAMES_Set.Add(DIGIT_ENDING_FUNCTION_NAMES[i]);
            }
        }
 
        public static FunctionMetadataRegistry CreateRegistry()
        {
            using (StringReader br = new StringReader(Resource1.functionMetadata))
            {
 
                FunctionDataBuilder fdb = new FunctionDataBuilder(400);
 
                try
                {
                    while (true)
                    {
                        String line = br.ReadLine();
                        if (line == null)
                        {
                            break;
                        }
                        if (line.Length < 1 || line[0] == '#')
                        {
                            continue;
                        }
                        String TrimLine = line.Trim();
                        if (TrimLine.Length < 1)
                        {
                            continue;
                        }
                        ProcessLine(fdb, line);
                    }
                }
                catch (IOException)
                {
                    throw;
                }
 
                return fdb.Build();
            }
        }
 
        private static void ProcessLine(FunctionDataBuilder fdb, String line)
        {
 
            Regex regex = new Regex(TAB_DELIM_PATTERN);
            String[] parts = regex.Split(line);
            if (parts.Length != 8)
            {
                throw new Exception("Bad line format '" + line + "' - expected 8 data fields");
            }
            int functionIndex = ParseInt(parts[0]);
            String functionName = parts[1];
            int minParams = ParseInt(parts[2]);
            int maxParams = ParseInt(parts[3]);
            byte returnClassCode = ParseReturnTypeCode(parts[4]);
            byte[] parameterClassCodes = ParseOperandTypeCodes(parts[5]);
            // 6 IsVolatile
            bool hasNote = parts[7].Length > 0;
 
            ValidateFunctionName(functionName);
            // TODO - make POI use IsVolatile
            fdb.Add(functionIndex, functionName, minParams, maxParams,
                    returnClassCode, parameterClassCodes, hasNote);
        }
 
 
        private static byte ParseReturnTypeCode(String code)
        {
            if (code.Length == 0)
            {
                return Ptg.CLASS_REF; // happens for GetPIVOTDATA
            }
            return ParseOperandTypeCode(code);
        }
 
        private static byte[] ParseOperandTypeCodes(String codes)
        {
            if (codes.Length < 1)
            {
                return EMPTY_BYTE_ARRAY; // happens for GetPIVOTDATA
            }
            if (IsDash(codes))
            {
                // '-' means empty:
                return EMPTY_BYTE_ARRAY;
            }
            Regex regex = new Regex(SPACE_DELIM_PATTERN);
            String[] array = regex.Split(codes);
            int nItems = array.Length;
            if (ELLIPSIS.Equals(array[nItems - 1]))
            {
                // ellipsis is optional, and ignored
                // (all Unspecified params are assumed to be the same as the last)
                nItems--;
            }
            byte[] result = new byte[nItems];
            for (int i = 0; i < nItems; i++)
            {
                result[i] = ParseOperandTypeCode(array[i]);
            }
            return result;
        }
 
        private static bool IsDash(String codes)
        {
            if (codes.Length == 1)
            {
                switch (codes[0])
                {
                    case '-':
                        return true;
                }
            }
            return false;
        }
 
        private static byte ParseOperandTypeCode(String code)
        {
            if (code.Length != 1)
            {
                throw new Exception("Bad operand type code format '" + code + "' expected single char");
            }
            switch (code[0])
            {
                case 'V': return Ptg.CLASS_VALUE;
                case 'R': return Ptg.CLASS_REF;
                case 'A': return Ptg.CLASS_ARRAY;
            }
            throw new ArgumentException("Unexpected operand type code '" + code + "' (" + (int)code[0] + ")");
        }
 
        /**
         * Makes sure that footnote digits from the original OOO document have not been accidentally 
         * left behind
         */
        private static void ValidateFunctionName(String functionName)
        {
            int len = functionName.Length;
            int ix = len - 1;
            if (!Char.IsDigit(functionName[ix]))
            {
                return;
            }
            while (ix >= 0)
            {
                if (!Char.IsDigit(functionName[ix]))
                {
                    break;
                }
                ix--;
            }
            if (DIGIT_ENDING_FUNCTION_NAMES_Set.Contains(functionName))
            {
                return;
            }
            throw new Exception("Invalid function name '" + functionName
                    + "' (is footnote number incorrectly Appended)");
        }
 
        private static int ParseInt(String valStr)
        {
            return int.Parse(valStr, CultureInfo.InvariantCulture);
        }
    }
}