Пример #1
0
        public static IEnumerable <WebLink> Parse(string linkFormat)
        {
            if (string.IsNullOrEmpty(linkFormat))
            {
                yield break;
            }

            string[] resources = SplitOn(linkFormat, ',');

            foreach (string resource in resources)
            {
                string[] attributes = SplitOn(resource, ';');
                if (attributes[0][0] != '<' || attributes[0][attributes[0].Length - 1] != '>')
                {
                    throw new ArgumentException();
                }
                WebLink link = new WebLink(attributes[0].Substring(1, attributes[0].Length - 2));

                for (int i = 1; i < attributes.Length; i++)
                {
                    int    eq   = attributes[i].IndexOf('=');
                    string name = eq == -1 ? attributes[i] : attributes[i].Substring(0, eq);

                    if (ParseStrictMode && SingleOccuranceAttributes.Contains(name))
                    {
                        throw new ArgumentException($"'{name}' occurs multiple times");
                    }


                    if (eq == -1)
                    {
                        link.Attributes.Add(name);
                    }
                    else
                    {
                        string value = attributes[i].Substring(eq + 1);
                        if (value[0] == '"')
                        {
                            if (value[value.Length - 1] != '"')
                            {
                                throw new ArgumentException();
                            }
                            value = value.Substring(1, value.Length - 2);
                        }
                        link.Attributes.Set(name, value);
                    }
                }

                yield return(link);
            }
        }
Пример #2
0
        private static IEnumerable <WebLink> ParseCommon(CBORObject links, Dictionary <string, CBORObject> dictionary)
        {
            if (links.Type != CBORType.Array)
            {
                throw new ArgumentException("Not an array");
            }

            for (int i = 0; i < links.Count; i++)
            {
                CBORObject resource = links[i];
                if (resource.Type != CBORType.Map)
                {
                    throw new ArgumentException("Element not correctly formatted");
                }

                string name;
                if (resource.ContainsKey("href"))
                {
                    name = resource["href"].AsString();
                }
                else
                {
                    name = resource[CBORObject.FromObject(1)].AsString();
                }

                WebLink link = new WebLink(name);

                foreach (CBORObject key in resource.Keys)
                {
                    string keyName = null;
                    if (dictionary != null && key.Type == CBORType.Number)
                    {
                        foreach (KeyValuePair <string, CBORObject> kvp in dictionary)
                        {
                            if (key.Equals(kvp.Value))
                            {
                                keyName = kvp.Key;
                                break;
                            }
                        }
                    }
                    if (keyName == null)
                    {
                        keyName = key.AsString();
                    }

                    if (ParseStrictMode && SingleOccuranceAttributes.Contains(keyName))
                    {
                        throw new ArgumentException($"'{keyName}' occurs multiple times");
                    }

                    CBORObject value = resource[key];
                    if (value.Type == CBORType.Boolean)
                    {
                        link.Attributes.Add(name);
                    }
                    else if (value.Type == CBORType.TextString)
                    {
                        link.Attributes.Add(name, value.AsString());
                    }
                    else if (value.Type == CBORType.Array)
                    {
                        for (int i1 = 0; i1 < value.Count; i1++)
                        {
                            if (value.Type == CBORType.Boolean)
                            {
                                link.Attributes.Add(name);
                            }
                            else if (value.Type == CBORType.TextString)
                            {
                                link.Attributes.Add(name, value.AsString());
                            }
                            else
                            {
                                throw new ArgumentException("incorrect type");
                            }
                        }
                    }
                    else
                    {
                        throw new ArgumentException("incorrect type");
                    }
                }

                yield return(link);
            }
        }