public override void Parse(TabularData tabularData) { List <string> lines = new List <string>(); if (tabularData.Info != null) { lines = tabularData.Info.AllLines(); } else if (!string.IsNullOrWhiteSpace(tabularData.Body)) { lines = new List <string>(tabularData.Body.AllLines()); } FieldParserFactory factory = new FieldParserFactory(); if (SetupDataTable(factory, tabularData, lines, out FieldParserCollection parsers)) { Execute(parsers, lines, tabularData); tabularData.IsOkay = true; } else { tabularData.IsOkay = false; } }
private static bool SetupDataTable(FieldParserFactory factory, TabularData tabularData, List <string> lines, out FieldParserCollection parsers) { parsers = new FieldParserCollection(); tabularData.Data = new DataTable(); bool b = false; int index = 0; bool isArray = false; string arrayKey = string.Empty; tabularData.Data.Columns.Add(new DataColumn("seq", typeof(Int32))); for (int i = 0; !b && i < lines.Count; i++) { string line = lines[i]; string s = line.Trim(); if (isArray) { if (line.EndsWith("],") || line.EndsWith("]")) { var parser = factory.Create(index, arrayKey, "array"); tabularData.Data.Columns.Add(parser.ToDataColumn()); tabularData.Fields.Add(parser.ToField(index)); parsers.Add(parser); isArray = false; arrayKey = string.Empty; } } else { if (line.Equals("}")) { b = true; } else if (!string.IsNullOrWhiteSpace(line) && !line.StartsWith("/*") && !line.Equals("{")) { index++; int pos = line.IndexOf(':'); if (pos > 0) { string key = line.Substring(0, pos).Trim().Trim('"'); if (key.Equals("_id")) { var parser = new ObjectIdFieldParser() { Key = key, Position = index }; tabularData.Data.Columns.Add(parser.ToDataColumn()); tabularData.Fields.Add(parser.ToField(index)); parsers.Add(parser); } else if (!s.EndsWith("[")) { string data = line.Substring(pos + 1).Trim().TrimEnd(',').Trim('"'); var parser = factory.Create(index, key, data); tabularData.Data.Columns.Add(parser.ToDataColumn()); tabularData.Fields.Add(parser.ToField(index)); parsers.Add(parser); } else { isArray = true; arrayKey = key; } } } } } return(b); }