Ejemplo n.º 1
0
        public static String Serialize(Resource resource, IEnumerable <Option> query, Boolean recursive)
        {
            StringBuilder linkFormat = new StringBuilder();

            // skip hidden and empty root in recursive mode, always skip non-matching resources
            if ((!resource.Hidden && (resource.Name.Length > 0) || !recursive) &&
                Matches(resource, query))
            {
                linkFormat.Append("<")
                .Append(resource.Path)
                .Append(">");

                foreach (LinkAttribute attr in resource.Attributes)
                {
                    linkFormat.Append(Separator);
                    attr.Serialize(linkFormat);
                }
            }

            if (recursive)
            {
                foreach (Resource sub in resource.GetSubResources())
                {
                    String next = Serialize(sub, query, true);

                    if (next.Length > 0)
                    {
                        if (linkFormat.Length > 3)
                        {
                            linkFormat.Append(Delimiter);
                        }
                        linkFormat.Append(next);
                    }
                }
            }

            return(linkFormat.ToString());
        }
Ejemplo n.º 2
0
        private static Boolean Matches(Resource resource, IEnumerable <Option> query)
        {
            if (resource == null)
            {
                return(false);
            }

            if (query == null)
            {
                return(true);
            }

            foreach (Option q in query)
            {
                String s     = q.StringValue;
                Int32  delim = s.IndexOf('=');
                if (delim == -1)
                {
                    // flag attribute
                    if (resource.GetAttributes(s).Count > 0)
                    {
                        return(true);
                    }
                }
                else
                {
                    String attrName = s.Substring(0, delim);
                    String expected = s.Substring(delim + 1);

                    if (attrName.Equals(LinkFormat.Link))
                    {
                        if (expected.EndsWith("*"))
                        {
                            return(resource.Path.StartsWith(expected.Substring(0, expected.Length - 1)));
                        }
                        else
                        {
                            return(resource.Path.Equals(expected));
                        }
                    }

                    foreach (LinkAttribute attr in resource.GetAttributes(attrName))
                    {
                        String actual = attr.Value.ToString();

                        // get prefix length according to "*"
                        Int32 prefixLength = expected.IndexOf('*');
                        if (prefixLength >= 0 && prefixLength < actual.Length)
                        {
                            // reduce to prefixes
                            expected = expected.Substring(0, prefixLength);
                            actual   = actual.Substring(0, prefixLength);
                        }

                        // handle case like rt=[Type1 Type2]
                        if (actual.IndexOf(' ') > -1)
                        {
                            foreach (String part in actual.Split(' '))
                            {
                                if (part.Equals(expected))
                                {
                                    return(true);
                                }
                            }
                        }

                        if (expected.Equals(actual))
                        {
                            return(true);
                        }
                    }
                }
            }

            return(false);
        }