Example #1
0
        static Exception _TryParseList(string text, out IteratedSegmentSequence segments) {
            segments = null;
            string[] items = text.Split('/', '\\');
            List<Segment> results = new List<Segment>(items.Length);

            foreach (string s in items) {
                Segment sgt;
                if (s.Length == 0) {
                    // TODO Only apply at real root; enforce match segment as file or directory
                    sgt = new RootSegment();

                } else if (DEVICE.IsMatch(s)) {
                    sgt = new DeviceSegment(DEVICE.Match(s).Groups["Name"].Value);

                } else if (s == "*") {
                    sgt = new AnyDirectorySegment();

                } else if (s == "**") {
                    sgt = new RecursiveSegment();

                    // TODO Support directory navigation
                } else if (s == "..") {
                    throw new NotImplementedException();

                } else if (s == ".") {
                    sgt = new CwdSegment();

                } else {
                    string segment = ExpandSegment(s);
                    if (segment == null)
                        return Failure.NotParsable("text", typeof(Glob));

                    sgt = new MatchSegment(segment);
                }

                results.Add(sgt);
            }

            segments = new IteratedSegmentSequence(results.ToArray());
            return null;
        }
Example #2
0
        static Exception _TryParse(string text, IServiceProvider serviceProvider, out QualifiedName result)
        {
            serviceProvider = serviceProvider ?? ServiceProvider.Null;
            result          = null;

            if (string.IsNullOrEmpty(text))
            {
                throw Failure.NullOrEmptyString(nameof(text));
            }

            // Remove decorations:  [prefix:b] ==> prefix:b
            if (text[0] == '[' && text[text.Length - 1] == ']')
            {
                text = text.Substring(1, text.Length - 2);
            }
            else if (text[0] == '{')
            {
                int num = text.LastIndexOf('}');

                if ((num <= 1) || (num == (text.Length - 1)))
                {
                    return(Failure.NotParsable("text", typeof(QualifiedName)));
                }

                if (num - 1 == 0)
                {
                    // The default namespace is used (as in '{} expandedName')
                    result = NamespaceUri.Default.GetName(text.Trim());
                    return(null);
                }
                else
                {
                    // Some other namespace is used
                    string ns        = text.Substring(1, num - 1);
                    string localName = text.Substring(num + 1).Trim();

                    NamespaceUri nu = NamespaceUri._TryParse(ns, false);
                    if (nu == null)
                    {
                        return(Failure.NotParsable("text", typeof(QualifiedName)));
                    }
                    result = nu.GetName(localName);
                    return(null);
                }
            }

            if (!text.Contains(":"))
            {
                result = NamespaceUri.Default.GetName(text.Trim());
                return(null);
            }

            var resolver = (IXmlNamespaceResolver)serviceProvider.GetService(typeof(IXmlNamespaceResolver))
                           ?? XmlNamespaceResolver.Global;

            int index = text.IndexOf(':');

            string prefix = text.Substring(0, index);
            string name   = text.Substring(index + 1);
            string fullNs = resolver.LookupNamespace(prefix);

            if (fullNs != null)
            {
                result = QualifiedName.Create(fullNs, name);
                return(null);
            }
            return(Failure.NotParsable("text", typeof(QualifiedName), RuntimeFailure.CannotExpandPrefixNotFound(prefix)));
        }