Ejemplo n.º 1
0
        private static string GenerateDataTableProperties(DataTableProcessor dataTableProcessor)
        {
            StringBuilder sb = new StringBuilder();

            for (int i = 0; i < dataTableProcessor.RawColumnCount; i++)
            {
                if (dataTableProcessor.IsCommentColumn(i))
                {
                    // 注释列
                    continue;
                }

                if (dataTableProcessor.IsIdColumn(i))
                {
                    // 编号列
                    continue;
                }

                GenerateTool.AddTab(sb);
                GenerateTool.AppendLine(sb, "/// <summary>");
                GenerateTool.AppendFormat(sb, "/// {0}。", dataTableProcessor.GetComment(i));
                GenerateTool.AppendLine(sb, "/// </summary>");
                GenerateTool.AppendFormat(sb, "public {0} {1};", dataTableProcessor.GetLanguageKeyword(i), dataTableProcessor.GetName(i));
                GenerateTool.ReduceTab(sb);
                GenerateTool.Enter(sb);
            }

            return(sb.ToString());
        }
Ejemplo n.º 2
0
        private static string GenerateDataTableStringParser(DataTableProcessor dataTableProcessor)
        {
            StringBuilder sb = new StringBuilder();

            GenerateTool.AddTab(sb);
            GenerateTool.AppendLine(sb, "public bool ParseDataRow(string[] content)");
            GenerateTool.AppendLine(sb, "{");

            GenerateTool.AddTab(sb);
            GenerateTool.AppendLine(sb, "int index = 0;");

            for (int i = 0; i < dataTableProcessor.RawColumnCount; i++)
            {
                if (dataTableProcessor.IsCommentColumn(i))
                {
                    // 注释列
                    GenerateTool.AppendLine(sb, "index++;");
                    continue;
                }

                if (dataTableProcessor.IsIdColumn(i))
                {
                    // 编号列
                    GenerateTool.AppendLine(sb, "this.id = int.Parse(content[index++]);");
                    continue;
                }

                if (dataTableProcessor.IsSystem(i))
                {
                    string languageKeyword = dataTableProcessor.GetLanguageKeyword(i);
                    if (languageKeyword == "string")
                    {
                        GenerateTool.AppendFormat(sb, "{0} = content[index++];", dataTableProcessor.GetName(i));
                    }
                    else
                    {
                        GenerateTool.AppendFormat(sb, "{0} = {1}.Parse(content[index++]);", dataTableProcessor.GetName(i), languageKeyword);
                    }
                }
                else
                {
                    // todo
                    throw new LogicException("Non-system type is not supported at this time");
                }
            }

            GenerateTool.AppendLine(sb, "return true;");
            GenerateTool.ReduceTab(sb);
            GenerateTool.AppendLine(sb, "}");
            GenerateTool.ReduceTab(sb);
            GenerateTool.Enter(sb);

            return(sb.ToString());
        }
Ejemplo n.º 3
0
        private static string GenerateDataTableRowIds(DataTableProcessor dataTableProcessor)
        {
            StringBuilder sb = new StringBuilder();

            for (int i = dataTableProcessor.ContentStartRow; i < dataTableProcessor.RawRowCount; i++)
            {
                if (string.IsNullOrEmpty(dataTableProcessor.GetId(i)))
                {
                    break;
                }

                GenerateTool.AddTab(sb);
                GenerateTool.AppendLine(sb, "/// <summary>");
                GenerateTool.AppendFormat(sb, "/// {0}。", dataTableProcessor.GetIdComment(i));
                GenerateTool.AppendLine(sb, "/// </summary>");
                GenerateTool.AppendFormat(sb, "public const {0} {1} = {2}", dataTableProcessor.GetLanguageKeyword(dataTableProcessor.IdColumn), "id_" + dataTableProcessor.GetId(i), dataTableProcessor.GetId(i) + ";");
                GenerateTool.Enter(sb);
                GenerateTool.ReduceTab(sb);
            }
            return(sb.ToString());
        }
Ejemplo n.º 4
0
        private static string GenerateDataTablePropertyArray(DataTableProcessor dataTableProcessor)
        {
            List <PropertyCollection> propertyCollections = new List <PropertyCollection>();

            for (int i = 0; i < dataTableProcessor.RawColumnCount; i++)
            {
                if (dataTableProcessor.IsCommentColumn(i))
                {
                    // 注释列
                    continue;
                }

                if (dataTableProcessor.IsIdColumn(i))
                {
                    // 编号列
                    continue;
                }

                string name = dataTableProcessor.GetName(i);
                if (!EndWithNumberRegex.IsMatch(name))
                {
                    continue;
                }

                string propertyCollectionName = EndWithNumberRegex.Replace(name, string.Empty);
                int    id = int.Parse(EndWithNumberRegex.Match(name).Value);

                PropertyCollection propertyCollection = null;
                foreach (PropertyCollection pc in propertyCollections)
                {
                    if (pc.Name == propertyCollectionName)
                    {
                        propertyCollection = pc;
                        break;
                    }
                }

                if (propertyCollection == null)
                {
                    propertyCollection = new PropertyCollection(propertyCollectionName, dataTableProcessor.GetLanguageKeyword(i));
                    propertyCollections.Add(propertyCollection);
                }

                propertyCollection.AddItem(id, name);
            }

            StringBuilder sb = new StringBuilder();

            foreach (PropertyCollection propertyCollection in propertyCollections)
            {
                GenerateTool.AddTab(sb);
                GenerateTool.AppendFormat(sb, "private KeyValuePair<int, {1}>[] {0} = null;", propertyCollection.Name, propertyCollection.LanguageKeyword);

                // 1
                GenerateTool.Enter(sb);
                GenerateTool.AppendFormat(sb, "public int {0}Count", propertyCollection.Name);
                GenerateTool.AppendLine(sb, "{");
                GenerateTool.AddTab(sb);
                GenerateTool.AppendLine(sb, "get");
                GenerateTool.AppendLine(sb, "{");
                GenerateTool.AddTab(sb);
                GenerateTool.AppendFormat(sb, "return this.{0}.Length;", propertyCollection.Name);
                GenerateTool.ReduceTab(sb);
                GenerateTool.AppendLine(sb, "}");
                GenerateTool.ReduceTab(sb);
                GenerateTool.AppendLine(sb, "}");
                GenerateTool.ReduceTab(sb);

                // 2
                GenerateTool.Enter(sb);

                GenerateTool.AddTab(sb);
                GenerateTool.AppendFormat(sb, "public {1} Get{0}(int id)", propertyCollection.Name, propertyCollection.LanguageKeyword);
                GenerateTool.AppendLine(sb, "{");
                GenerateTool.AddTab(sb);

                GenerateTool.AppendFormat(sb, "foreach (KeyValuePair<int, {1}> i in this.{0})", propertyCollection.Name, propertyCollection.LanguageKeyword);
                GenerateTool.AppendLine(sb, "{");
                GenerateTool.AddTab(sb);

                GenerateTool.AppendLine(sb, "if (i.Key == id)");
                GenerateTool.AppendLine(sb, "{");
                GenerateTool.AddTab(sb);

                GenerateTool.AppendLine(sb, "return i.Value;");
                GenerateTool.ReduceTab(sb);
                GenerateTool.AppendLine(sb, "}");
                GenerateTool.ReduceTab(sb);
                GenerateTool.AppendLine(sb, "}");

                GenerateTool.Enter(sb);
                GenerateTool.AppendFormat(sb, "throw new ArgumentException(string.Format(\"Get{0} with invalid id '{{0}}'.\", id.ToString()));", propertyCollection.Name);
                GenerateTool.ReduceTab(sb);
                GenerateTool.AppendLine(sb, "}");
                GenerateTool.ReduceTab(sb);

                //3
                GenerateTool.Enter(sb);
                GenerateTool.AddTab(sb);
                GenerateTool.AppendFormat(sb, "public {1} Get{0}At(int index)", propertyCollection.Name, propertyCollection.LanguageKeyword);
                GenerateTool.AppendLine(sb, "{");
                GenerateTool.AddTab(sb);

                GenerateTool.AppendFormat(sb, "if (index < 0 || index >= this.{0}.Length)", propertyCollection.Name);
                GenerateTool.AppendLine(sb, "{");
                GenerateTool.AddTab(sb);
                GenerateTool.AppendFormat(sb, "throw new ArgumentException(string.Format(\"Get{0}At with invalid index '{{0}}'.\", index.ToString()));", propertyCollection.Name);
                GenerateTool.ReduceTab(sb);
                GenerateTool.AppendLine(sb, "}");
                GenerateTool.Enter(sb);
                GenerateTool.AppendFormat(sb, "return this.{0}[index].Value;", propertyCollection.Name);
                GenerateTool.ReduceTab(sb);
                GenerateTool.AppendLine(sb, "}");
                GenerateTool.ReduceTab(sb);
            }

            if (propertyCollections.Count > 0)
            {
                GenerateTool.Enter(sb);
            }

            GenerateTool.AddTab(sb);
            GenerateTool.AppendLine(sb, "private void GeneratePropertyArray()");
            GenerateTool.AppendLine(sb, "{");
            GenerateTool.AddTab(sb);

            foreach (PropertyCollection propertyCollection in propertyCollections)
            {
                GenerateTool.AppendFormat(sb, "this.{0} = new KeyValuePair<int, {1}>[]", propertyCollection.Name, propertyCollection.LanguageKeyword);
                GenerateTool.AppendLine(sb, "{");
                GenerateTool.AddTab(sb);

                int itemCount = propertyCollection.ItemCount;
                for (int i = 0; i < itemCount; i++)
                {
                    KeyValuePair <int, string> item = propertyCollection.GetItem(i);

                    GenerateTool.AppendFormat(sb, "new KeyValuePair<int, {0}>({1}, {2}),", propertyCollection.LanguageKeyword, item.Key.ToString(), item.Value);
                }

                GenerateTool.ReduceTab(sb);
                GenerateTool.AppendLine(sb, "}");
            }

            GenerateTool.ReduceTab(sb);
            GenerateTool.AppendLine(sb, "}");
            GenerateTool.ReduceTab(sb);
            return(sb.ToString());
        }