public string GetNameOfChildObject(IDocumentLeafNode o)
 {
     if (null != o)
     {
         return("Infrastructure object of type " + o.GetType().FullName);
     }
     else
     {
         return("<<null>>");
     }
 }
Beispiel #2
0
        /// <summary>
        /// Get the absolute path of the node <code>node</code> starting either from the root, or from the object in the depth
        /// <code>maxDepth</code>, whatever is reached first.
        /// </summary>
        /// <param name="node">The node for which the path is to be retrieved.</param>
        /// <param name="maxDepth">The maximal hierarchie depth (the maximal number of path elements returned).</param>
        /// <returns>The path from the root or from the node in the depth <code>maxDepth</code>, whatever is reached first. The path is <b>not</b> prepended
        /// by a "/".
        /// </returns>
        public static AbsoluteDocumentPath GetPath(IDocumentLeafNode node, int maxDepth)
        {
            if (null == node)
            {
                throw new ArgumentNullException("node");
            }
            if (maxDepth <= 0)
            {
                throw new ArgumentOutOfRangeException("maxDepth should be > 0");
            }

            var list = new List <string>();

            int depth  = 0;
            var parent = node.ParentObject;

            while (parent != null)
            {
                if (depth >= maxDepth)
                {
                    break;
                }

                string name = parent.GetNameOfChildObject(node);

                if (null == name) // an empty string is a valid name, e.g. for a folder text document located in the root folder
                {
                    throw new InvalidOperationException(string.Format("Parent node (type:{0}) of node (type: {1}) did not return a valid name for the child node!", parent.GetType(), node.GetType()));
                }

                list.Add(name);
                node   = parent;
                parent = node.ParentObject;
                ++depth;
            }

            if (maxDepth == int.MaxValue && node != null && !(node is IProject))
            {
                string msg = string.Format("Document {0} is not rooted. The path so far retrieved is {1}", node, list);
                throw new InvalidOperationException(msg);
            }

            return(new AbsoluteDocumentPath(list.TakeFromUpperIndexExclusiveDownToLowerIndexInclusive(list.Count, 0)));
        }