public void ExtendedTest()
        {
            String         input = "</my/Päth>;rt=\"MyName\";if=\"/someRef/path\";ct=42;obs;sz=10";
            RemoteResource root  = RemoteResource.NewRoot(input);

            RemoteResource my = new RemoteResource("my");

            my.ResourceType = "replacement";
            root.AddSubResource(my);

            RemoteResource res = root.GetResource("/my/Päth");

            Assert.IsNotNull(res);
            res = root.GetResource("my/Päth");
            Assert.IsNotNull(res);
            res = root.GetResource("my");
            res = res.GetResource("Päth");
            Assert.IsNotNull(res);
            res = res.GetResource("/my/Päth");
            Assert.IsNotNull(res);

            Assert.AreEqual(res.Name, "Päth");
            Assert.AreEqual(res.Path, "/my/Päth");
            Assert.AreEqual(res.ResourceType, "MyName");
            Assert.AreEqual(res.InterfaceDescriptions[0], "/someRef/path");
            Assert.AreEqual(1, res.Attributes.GetContentTypes().Count());
            Assert.AreEqual("42", res.Attributes.GetContentTypes().First());
            Assert.AreEqual(10, res.MaximumSizeEstimate);
            Assert.AreEqual(true, res.Observable);

            res = root.GetResource("my");
            Assert.IsNotNull(res);
            Assert.AreEqual("replacement", res.ResourceTypes[0]);
        }
Beispiel #2
0
        public static RemoteResource Deserialize(String linkFormat)
        {
            RemoteResource root    = new RemoteResource(String.Empty);
            Scanner        scanner = new Scanner(linkFormat);

            String path = null;

            while ((path = scanner.Find(ResourceNameRegex)) != null)
            {
                path = path.Substring(1, path.Length - 2);

                // Retrieve specified resource, create if necessary
                RemoteResource resource = new RemoteResource(path);

                LinkAttribute attr = null;
                while (scanner.Find(DelimiterRegex, 1) == null && (attr = ParseAttribute(scanner)) != null)
                {
                    AddAttribute(resource.Attributes, attr);
                }

                root.AddSubResource(resource);
            }

            return(root);
        }
Beispiel #3
0
        public static RemoteResource Deserialize(string linkFormat)
        {
            RemoteResource root = new RemoteResource(string.Empty);

            if (string.IsNullOrEmpty(linkFormat))
            {
                return(root);
            }

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

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

                RemoteResource resource = new RemoteResource(attributes[0].Substring(1, attributes[0].Length - 2));

                for (int i = 1; i < attributes.Length; i++)
                {
                    int eq = attributes[i].IndexOf('=');
                    if (eq == -1)
                    {
                        resource.Attributes.Add(attributes[i]);
                    }
                    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);
                        }
                        resource.Attributes.Add(attributes[i].Substring(0, eq), value);
                    }
                }

                root.AddSubResource(resource);
            }

            return(root);
        }
Beispiel #4
0
        private static RemoteResource DeserializeCbor(CBORObject cbor)
        {
            if (cbor.Type != CBORType.Array)
            {
                throw new ArgumentException();
            }

            RemoteResource root = new RemoteResource(string.Empty);

            for (int i = 0; i < cbor.Count; i++)
            {
                string href;
                if (cbor[i].ContainsKey("href"))
                {
                    href = cbor[i]["href"].AsString();
                }
                else
                {
                    href = cbor[i][CBORObject.FromObject(1)].AsString();
                }

                RemoteResource child = new RemoteResource(href);

                foreach (CBORObject key in cbor[i].Keys)
                {
                    string keyName;
                    if (key.Type == CBORType.Number)
                    {
                        keyName = null;
                        foreach (KeyValuePair <string, CBORObject> kvp  in _CborAttributeKeys)
                        {
                            if (key.Equals(kvp.Value))
                            {
                                keyName = kvp.Key;
                                break;
                            }
                        }
                        if (keyName == null)
                        {
                            throw new ArgumentException("Invalid numeric key");
                        }
                    }
                    else if (key.Type == CBORType.TextString)
                    {
                        keyName = key.AsString();
                    }
                    else
                    {
                        throw new ArgumentException("Unexpected key type found");
                    }

                    if (keyName == "href")
                    {
                        continue;
                    }

                    CBORObject value = cbor[i][key];

                    if (value.Type == CBORType.TextString)
                    {
                        child.Attributes.Add(keyName, value.AsString());
                    }
                    else if (value.Type == CBORType.Boolean)
                    {
                        child.Attributes.Add(keyName);
                    }
                    else if (value.Type == CBORType.Array)
                    {
                        for (int i1 = 0; i1 < value.Count; i1++)
                        {
                            if (value[i1].Type == CBORType.TextString)
                            {
                                child.Attributes.Add(keyName, value[i1].AsString());
                            }
                            else if (value[i1].Type == CBORType.Boolean)
                            {
                                if (value[i1].AsBoolean() != true)
                                {
                                    throw new ArgumentException("false unexpectedly found");
                                }
                                child.Attributes.Add(keyName);
                            }
                            else
                            {
                                throw new ArgumentException("Unexpected value type found");
                            }
                        }
                    }
                    else
                    {
                        throw new ArgumentException("Unexpected value type found");
                    }
                }

                root.AddSubResource(child);
            }


            return(root);
        }