static Dictionary GetTagAttributes()

in Tools/UIRecorder/UIRecorder/GenerateXPath.cs [257:350]


        static Dictionary<string, string> GetTagAttributes(string xpath)
        {
            Dictionary<string, string> foundAttrs = new Dictionary<string, string>();
            List<string> listAttrs = new List<string>();

            // Get tag and groups of []
            var sb = new System.Text.StringBuilder();
            string Tag = null;
            int ncount = 0;
            int nBracketCount = 0;
            foreach (char c in xpath)
            {
                if (c == '[')
                {
                    nBracketCount++;

                    if (sb.Length > 0 && Tag == null && nBracketCount == 1)
                    {
                        Tag = sb.ToString();
                        sb.Clear();
                    }

                    sb.Append(c);
                }
                else if (c == ']')
                {
                    nBracketCount--;

                    if (nBracketCount == 0)
                    {
                        sb.Append(c);
                        listAttrs.Add(sb.ToString());
                        sb.Clear();
                    }
                    else
                    {
                        sb.Append(c);
                    }
                }
                else if (c == '/' && nBracketCount == 0)
                {
                    if (Tag == null && sb.Length > 0)
                    {
                        Tag = sb.ToString();
                        break;
                    }

                    if (Tag != null)
                    {
                        break;
                    }
                }
                else
                {
                    sb.Append(c);
                }

                ncount++;
            }

            if (Tag != null)
            {
                foundAttrs.Add("Tag", Tag);
            }
            else if (Tag == null && sb.Length > 0)
            {
                foundAttrs.Add("Tag", sb.ToString());
            }
            else
            {
                //unexpected
                return null;
            }

            foreach (string attrNameValue in listAttrs)
            {
                const string patternNameValue = @"\[([^=]+)=([^\]]+)\]";
                var regNameValue = new System.Text.RegularExpressions.Regex(patternNameValue, System.Text.RegularExpressions.RegexOptions.IgnoreCase);
                System.Text.RegularExpressions.Match matchNameValue = regNameValue.Match(attrNameValue);
                if (matchNameValue.Success)
                {
                    if (matchNameValue.Groups.Count == 3)
                    {
                        var valStr = matchNameValue.Groups[2].Value;
                        if (valStr.StartsWith("\"") && valStr.EndsWith("\""))
                            valStr = valStr.Substring(1, valStr.Length - 2);

                        foundAttrs.Add(matchNameValue.Groups[1].Value, valStr);
                    }
                }
            }

            return foundAttrs;
        }