Esempio n. 1
0
            internal HierarchicalNodeToken(string name, HierarchicalNode current, HierarchicalNode parent = null)
            {
                this.Name    = name;
                this.Current = current;

                this.Parent = parent ?? (current != null ? current.InnerParent : null);
            }
Esempio n. 2
0
        protected HierarchicalNode(string name, HierarchicalNode parent, char pathSeparatorChar = DefaultPathSeparatorChar)
        {
            if (string.IsNullOrWhiteSpace(name))
            {
                throw new ArgumentNullException("name");
            }

            if (name.Contains(pathSeparatorChar.ToString()))
            {
                throw new ArgumentException("The name contains path separator char.");
            }

            if (JF.Common.StringExtensions.ContainsCharacters(name, @"./\*?!@#$%^&"))
            {
                throw new ArgumentException("The name contains invalid character(s).");
            }

            _name             = name.Trim();
            _parent           = parent;
            PathSeparatorChar = pathSeparatorChar;
        }
Esempio n. 3
0
        internal protected HierarchicalNode FindNode(string[] paths, Func <HierarchicalNodeToken, HierarchicalNode> onStep = null)
        {
            if (paths == null || paths.Length == 0)
            {
                return(null);
            }

            //确保当前子节点集合已经被加载过
            this.EnsureChildren();

            //当前节点默认为本节点
            var current = this;

            //如果第一个部分是空字符则表示路径是以斜杠(/)打头或第一个部分是以斜杠(/)打头则从根节点开始查找
            if (string.IsNullOrWhiteSpace(paths[0]) || paths[0].Trim()[0] == PathSeparatorChar)
            {
                current = this.FindRoot();
            }

            int pathIndex = 0, partIndex = 0;

            string[] parts = null;

            while (current != null && pathIndex < paths.Length)
            {
                var part = string.Empty;
                HierarchicalNode parent = null;

                if (parts == null && paths[pathIndex].Contains(PathSeparatorChar.ToString()))
                {
                    parts     = paths[pathIndex].Split(PathSeparatorChar);
                    partIndex = 0;
                }

                if (parts == null)
                {
                    part = paths[pathIndex++].Trim();
                }
                else
                {
                    if (partIndex < parts.Length)
                    {
                        part = parts[partIndex++].Trim();
                    }
                    else
                    {
                        parts = null;
                        pathIndex++;
                        continue;
                    }
                }

                switch (part)
                {
                case "":
                case ".":
                    continue;

                case "..":
                    current = current._parent;
                    parent  = current != null ? current._parent : null;
                    break;

                default:
                    parent  = current;
                    current = current.GetChild(part);
                    break;
                }

                if (onStep != null)
                {
                    current = onStep(new HierarchicalNodeToken(part, current, parent));

                    if (current == null)
                    {
                        return(null);
                    }
                }
            }

            return(current);
        }