Пример #1
0
        /// <summary>
        /// Adds two fields to the class.
        /// </summary>
        public void AddFields(HtmlElementCollection htmls)
        {
            foreach (HtmlElement html in htmls)
            {
                string fieldName = html.Children[0].OuterText.Trim();
                string fieldType = GetType(html.Children[1].OuterText.Trim());
                string fieldMemo = html.Children[2].OuterText;

                // Declare the widthValue field.
                CodeMemberField widthValueField = new CodeMemberField();

                widthValueField.Attributes       = MemberAttributes.Private;
                widthValueField.CustomAttributes = new CodeAttributeDeclarationCollection {
                    new CodeAttributeDeclaration("JsonProperty", new CodeAttributeArgument(
                                                     new CodePrimitiveExpression(fieldName)))
                };

                widthValueField.Name = "_" + CodeGenerationHelper.MakeFirstCharLowerCase(fieldName);
                widthValueField.Type = new CodeTypeReference(fieldType);
                widthValueField.Comments.Add(new CodeCommentStatement(
                                                 fieldMemo));

                targetClass.Members.Add(widthValueField);
            }
        }
Пример #2
0
        /// <summary>
        /// Add three properties to the class.
        /// </summary>
        public void AddProperties(HtmlElementCollection htmls)
        {
            foreach (HtmlElement html in htmls)
            {
                string rePropertName = string.Empty;
                string propertName   = html.Children[0].OuterText.Trim();
                string propertType   = GetType(html.Children[1].OuterText.Trim());
                string propertMemo   = html.Children[2].OuterText;

                // Declare the read-only Width property.
                CodeMemberProperty widthProperty = new CodeMemberProperty();

                widthProperty.Attributes =
                    MemberAttributes.Public | MemberAttributes.Final;

                if (propertName.Substring(0, 1).ToUpper() == propertName.Substring(0, 1))
                {
                    rePropertName = CodeGenerationHelper.MakeFirstCharUpperCase(propertName);

                    rePropertName = string.Format("ReName_{0}", rePropertName);

                    widthProperty.Name = CodeGenerationHelper.MakeFirstCharUpperCase(rePropertName);
                }
                else
                {
                    widthProperty.Name = CodeGenerationHelper.MakeFirstCharUpperCase(propertName);
                }


                widthProperty.HasGet = true;
                widthProperty.HasSet = true;
                widthProperty.Type   = new CodeTypeReference(propertType);
                widthProperty.Comments.Add(new CodeCommentStatement(
                                               propertMemo));
                widthProperty.GetStatements.Add(new CodeMethodReturnStatement(
                                                    new CodeFieldReferenceExpression(
                                                        new CodeThisReferenceExpression(), "_" + CodeGenerationHelper.MakeFirstCharLowerCase(propertName))));

                widthProperty.SetStatements.Add(new CodeAssignStatement(
                                                    new CodeFieldReferenceExpression(
                                                        new CodeThisReferenceExpression(), "_" + CodeGenerationHelper.MakeFirstCharLowerCase(propertName)), new CodePropertySetValueReferenceExpression()));


                targetClass.Members.Add(widthProperty);
            }
        }
Пример #3
0
        /// <summary>
        /// Adds two fields to the class.
        /// </summary>
        public void AddFields(string fieldName, string fieldType, string fieldMemo)
        {
            // Declare the widthValue field.
            CodeMemberField widthValueField = new CodeMemberField();

            widthValueField.Attributes       = MemberAttributes.Private;
            widthValueField.CustomAttributes = new CodeAttributeDeclarationCollection {
                new CodeAttributeDeclaration("JsonProperty", new CodeAttributeArgument(
                                                 new CodePrimitiveExpression(fieldName)))
            };

            widthValueField.Name = "_" + CodeGenerationHelper.MakeFirstCharLowerCase(fieldName);
            widthValueField.Type = new CodeTypeReference(GetType(fieldType));
            widthValueField.Comments.Add(new CodeCommentStatement(
                                             fieldMemo));

            targetClass.Members.Add(widthValueField);
        }
Пример #4
0
        private void LoLDtoAutoGenerateProcess1()
        {
            string dtoPath = txbDefaultDtoPath.Text.Trim();
            Dictionary <string, List <HtmlElement> > dics = new Dictionary <string, List <HtmlElement> >();

            if (!Directory.Exists(dtoPath))
            {
                Directory.CreateDirectory(dtoPath);
            }

            try
            {
                string html = System.IO.File.ReadAllText(@"D:\WangjqWork\MyTool\LoLMetroAT\LoLAutoGenerateTool\Riot Developer Portal.txt", Encoding.Unicode);

                CQ dom = html;

                CQ lstDivBold = dom["div"];

                Dictionary <LoLClass, List <LoLProperties> > lstClassNamesForApi = new Dictionary <LoLClass, List <LoLProperties> >();

                foreach (var divNode in lstDivBold.ToList())
                {
                    var divAttr = divNode.Attributes;

                    if (divAttr["class"] == "api_detail inner_content" && divAttr["loaded"] == "true")
                    {
                        lstClassNamesForApi.Clear();

                        string apiName = divNode.Attributes["api-name"];
                        string key     = string.Empty;

                        string[] apiNames = apiName.Split('-');
                        foreach (string an in apiNames)
                        {
                            if (string.IsNullOrEmpty(key))
                            {
                                key = CodeGenerationHelper.MakeFirstCharUpperCase(an);
                            }
                            else
                            {
                                key += "_" + CodeGenerationHelper.MakeFirstCharUpperCase(an);
                            }
                        }

                        string keyPath = Path.Combine(dtoPath, key);

                        if (!Directory.Exists(keyPath))
                        {
                            Directory.CreateDirectory(keyPath);
                        }

                        CQ v3DivDom  = divNode.OuterHTML;
                        CQ lstUlBold = v3DivDom["ul"];

                        foreach (var ulNode in lstUlBold.ToList())
                        {
                            var ulAttr = ulNode.Attributes;

                            if (ulAttr["id"] == "resources")
                            {
                                CQ v3UlDom = ulNode.OuterHTML;

                                CQ lstLiBold = v3UlDom["li"];

                                foreach (var liNode in lstLiBold.ToList())
                                {
                                    var liAttr = liNode.Attributes;

                                    if (liAttr["class"] == "get operation")
                                    {
                                        CQ v3LiDom = liNode.OuterHTML;

                                        CQ lstLiDivBold = v3LiDom["div"];

                                        foreach (var liDivNode in lstLiDivBold.ToList())
                                        {
                                            var liDivAttr = liDivNode.Attributes;

                                            if (liDivAttr["class"] == "content")
                                            {
                                                CQ v3LiDivDom       = liDivNode.OuterHTML;
                                                CQ lstDetailDivBold = v3LiDivDom["div"];

                                                var lstLDDBS = lstDetailDivBold.Where(lDDB => lDDB.Attributes["class"] == "block response_body");

                                                string className = string.Empty;

                                                foreach (var lddb in lstLDDBS)
                                                {
                                                    CQ lddbDom = lddb.InnerHTML;

                                                    if (lddbDom["h4"].Length > 0)
                                                    {
                                                        continue;
                                                    }
                                                    else
                                                    {
                                                        if (lddbDom["h5"].Length > 0)
                                                        {
                                                            className = lddbDom["h5"][0].InnerText.Trim();
                                                            if (apiName.Contains("static"))
                                                            {
                                                                className = className + "Static";
                                                            }

                                                            LoLClass lc = new LoLClass();
                                                            lc.ClassName = className;
                                                            lc.ClassMemo = lddb.InnerText.Split(new string[] { Environment.NewLine }, StringSplitOptions.None)[0].Replace(className, string.Empty).Trim();


                                                            if (lstClassNamesForApi.Keys.SingleOrDefault(lstLClass => lstLClass.ClassName == className) == null)
                                                            {
                                                                lstClassNamesForApi.Add(lc, new List <LoLProperties>());
                                                            }
                                                            else
                                                            {
                                                                lc = lstClassNamesForApi.Keys.SingleOrDefault(lstLClass => lstLClass.ClassName == className);
                                                            }

                                                            CQ trs = lddbDom["tbody"]["tr"];

                                                            foreach (var trNode in trs)
                                                            {
                                                                CQ tdDom = trNode.OuterHTML;
                                                                CQ tds   = tdDom["td"];

                                                                if (tds.Length > 0)
                                                                {
                                                                    LoLProperties lp = new LoLProperties();

                                                                    lp.ProtyName = tds[0].InnerText.Trim();
                                                                    lp.ProtyType = tds[1].InnerText.Trim();
                                                                    lp.ProtyMemo = tds[2].InnerText.Split(new string[] { Environment.NewLine }, StringSplitOptions.None)[0].Replace(className, string.Empty).Trim();

                                                                    if (lstClassNamesForApi[lc].SingleOrDefault(lProty => lProty.ProtyName == lp.ProtyName) == null)
                                                                    {
                                                                        lstClassNamesForApi[lc].Add(lp);
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }

                        string codeNamespace = string.Format("{0}.{1}", LoL_Library_Name, key);

                        //if (staticFlg)
                        //{
                        //    foreach (var lcnfKey in lstClassNamesForApi.Keys)
                        //    {
                        //        if (lp.ProtyType.Contains(lcnfKey.ClassName.Replace("Static", "")))
                        //        {
                        //            lp.ProtyType = lp.ProtyType.Replace(lcnfKey.ClassName.Replace("Static", ""), lcnfKey.ClassName);
                        //        }
                        //    }
                        //}
                        bool staticFlg = false;
                        foreach (LoLClass lstKey in lstClassNamesForApi.Keys)
                        {
                            staticFlg = false;
                            Sample sample    = new Sample(codeNamespace, lstKey.ClassName, lstKey.ClassMemo);;
                            string classPath = Path.Combine(keyPath, lstKey.ClassName + ".cs");

                            if (lstKey.ClassName.Contains("Static"))
                            {
                                staticFlg = true;
                            }
                            else
                            {
                                staticFlg = false;
                            }

                            if (lstKey.ClassName == "MasteryTreeListDtoStatic")
                            {
                            }

                            foreach (var lProperty in lstClassNamesForApi[lstKey])
                            {
                                if (staticFlg)
                                {
                                    foreach (LoLClass lstOrgKey in lstClassNamesForApi.Keys)
                                    {
                                        string orgClassName = lstOrgKey.ClassName.Replace("Static", "");

                                        if (lProperty.ProtyType.Contains(orgClassName))
                                        {
                                            string reProtyType = string.Empty;

                                            if (lProperty.ProtyType.Contains("List[") || lProperty.ProtyType.Contains("Map[") || lProperty.ProtyType.Contains("Set["))
                                            {
                                                reProtyType = lProperty.ProtyType.Replace("List[", "").Replace("Map[", "").Replace("Set[", "").Replace("]", "").Trim();

                                                if (reProtyType.Contains(','))
                                                {
                                                    int rIndex   = reProtyType.IndexOf(',');
                                                    int orgIndex = reProtyType.IndexOf(orgClassName);

                                                    if (rIndex > orgIndex)
                                                    {
                                                        reProtyType = reProtyType.Substring(0, rIndex).Trim();
                                                    }
                                                    else
                                                    {
                                                        reProtyType = reProtyType.Substring(orgIndex).Trim();
                                                    }
                                                }


                                                if (reProtyType == orgClassName)
                                                {
                                                    lProperty.ProtyType = lProperty.ProtyType.Replace(reProtyType, lstOrgKey.ClassName);
                                                }
                                            }
                                            else
                                            {
                                                if (orgClassName == lProperty.ProtyType)
                                                {
                                                    lProperty.ProtyType = lProperty.ProtyType.Replace(orgClassName, lstOrgKey.ClassName);
                                                }
                                            }

                                            // string reProtyType = lProperty.ProtyType.Replace(orgClassName, lstOrgKey.ClassName);


                                            continue;
                                        }
                                    }
                                }

                                sample.AddFields(lProperty.ProtyName, lProperty.ProtyType, lProperty.ProtyMemo);
                                sample.AddProperties(lProperty.ProtyName, lProperty.ProtyType, lProperty.ProtyMemo);
                            }

                            sample.GenerateCSharpCode(classPath);
                        }
                    }
                }

                MessageBox.Show("Convert End !");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
Пример #5
0
        private void LoLDtoAutoGenerateProcess()
        {
            string dtoPath = txbDefaultDtoPath.Text.Trim();
            Dictionary <string, List <HtmlElement> > dics = new Dictionary <string, List <HtmlElement> >();

            if (!Directory.Exists(dtoPath))
            {
                Directory.CreateDirectory(dtoPath);
            }

            try
            {
                HtmlElement hElements = webBrowser1.Document.GetElementById(Api_Detail_Id).Document.GetElementById(Resources_Id);

                foreach (HtmlElement curElement in hElements.Children)
                {
                    List <HtmlElement> list = new List <HtmlElement>();
                    string             key  = string.Empty;

                    HtmlElementCollection spanElements = curElement.GetElementsByTagName(Span_TagName);

                    foreach (HtmlElement spanElement in spanElements)
                    {
                        if (!string.IsNullOrEmpty(spanElement.OuterHtml) && spanElement.OuterHtml.Contains(Span_Rex))
                        {
                            key = spanElement.OuterText.Trim();

                            if (key.Contains("-") || key.Contains("."))
                            {
                                key = key.Replace("-", "_").Replace(".", "");
                            }

                            //key = spanElement.OuterText.Trim().Replace("-", "");
                            //key = key.Substring(0, key.Length - 4);
                        }
                    }

                    HtmlElementCollection chidElements = curElement.Children[1].GetElementsByTagName(Div_TagName);

                    foreach (HtmlElement chidCurElement in chidElements)
                    {
                        if (chidCurElement.OuterHtml.Length <= Div_Rex.Length)
                        {
                            continue;
                        }

                        if (!string.IsNullOrEmpty(chidCurElement.OuterHtml) && chidCurElement.OuterHtml.Substring(0, Div_Rex.Length).Equals(Div_Rex))
                        {
                            list.Add(chidCurElement);
                        }
                    }

                    if (dics.ContainsKey(key))
                    {
                        dics[key].AddRange(list);
                    }
                    else
                    {
                        dics.Add(key, list);
                    }
                }

                List <string> dtoNames = new List <string>();
                string        oldKey   = string.Empty;

                foreach (string key in dics.Keys)
                {
                    string keyPath = Path.Combine(dtoPath, CodeGenerationHelper.MakeFirstCharUpperCase(key));

                    if (!Directory.Exists(keyPath))
                    {
                        Directory.CreateDirectory(keyPath);
                    }

                    foreach (HtmlElement htmlVal in dics[key])
                    {
                        string className = htmlVal.GetElementsByTagName(B_TagName)[0].OuterText.Trim();

                        if (dtoNames.Contains(className))
                        {
                            continue;
                        }

                        string codeNamespace = string.Format("{0}.{1}", LoL_Library_Name, CodeGenerationHelper.MakeFirstCharUpperCase(key));
                        string classMemo     = htmlVal.OuterText.Split(new string[] { Environment.NewLine }, StringSplitOptions.None)[0].Replace(className, string.Empty).TrimStart();
                        string classPath     = Path.Combine(keyPath, className + ".cs");

                        Sample sample = new Sample(codeNamespace, className, classMemo);

                        HtmlElementCollection properts = htmlVal.GetElementsByTagName(Tbody_TagName)[0].GetElementsByTagName(Tr_TagName);

                        sample.AddFields(properts);
                        sample.AddProperties(properts);
                        //sample.AddMethod();
                        //sample.AddConstructor();
                        //sample.AddEntryPoint();

                        sample.GenerateCSharpCode(classPath);

                        dtoNames.Add(className);
                    }

                    dtoNames.Clear();
                }

                dtoNames = null;



                MessageBox.Show("Convert End !");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }