예제 #1
0
        private static void Execute(FieldParserCollection parsers, List <string> lines, TabularData tabularData)
        {
            int     index = 0;
            DataRow row   = tabularData.Data.NewRow();

            bool   isArray  = false;
            var    sb       = new StringBuilder();
            string arrayKey = string.Empty;

            foreach (var line in lines)
            {
                if (!string.IsNullOrWhiteSpace(line))
                {
                    string s = line.Trim();
                    if (isArray)
                    {
                        sb.Append(s);
                        if (s[0].Equals(']'))
                        {
                            var data = sb.ToString();
                            if (parsers.Contains(arrayKey))
                            {
                                parsers[arrayKey].Parse(data.TrimEnd(','), row);
                            }
                            sb       = new StringBuilder();
                            arrayKey = string.Empty;
                            isArray  = false;
                        }
                    }
                    else
                    {
                        if (s.EndsWith("["))
                        {
                            isArray = true;
                            sb.Append("[");
                            int pos = line.IndexOf(':');
                            arrayKey = line.Substring(0, pos).Trim().Trim('"');
                        }
                        else
                        {
                            if (line.StartsWith("/*"))
                            {
                                var parts = line.Trim().Split(new char[] { ' ' });
                                if (parts.Length == 3 && Int32.TryParse(parts[1], out int i))
                                {
                                    index = i;
                                }
                            }
                            else if (line.Equals("{"))
                            {
                                row    = tabularData.Data.NewRow();
                                row[0] = index;
                            }
                            else if (line.Equals("}"))
                            {
                                tabularData.Data.Rows.Add(row);
                            }
                            else
                            {
                                int pos = line.IndexOf(':');
                                if (pos > 0)
                                {
                                    string key = line.Substring(0, pos).Trim().Trim('"');
                                    if (s.EndsWith("["))
                                    {
                                        isArray  = true;
                                        arrayKey = key;
                                        sb.Append(s);
                                    }
                                    else
                                    {
                                        string data = line.Substring(pos + 1).Trim().TrimEnd('"');
                                        if (parsers.Contains(key))
                                        {
                                            parsers[key].Parse(data.TrimEnd(',').Trim('"'), row);
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }