Ejemplo n.º 1
0
        /// <summary>
        /// Get relative path , relative to another object
        /// based on most common root
        /// </summary>
        /// <param name="otherObject"></param>
        /// <param name="flatStructure"></param>
        /// <returns></returns>
        public string GetRelativePath(IEPubInternalPath otherObject, bool flatStructure)
        {
            if (flatStructure && _supportFlatStructure)
            {
                if (_path.Count < 1)
                {
                    throw new Exception("Path has to contain at least one element besides root");
                }
                return(_path[_path.Count - 1].Name);
            }
            int commongPathIndex = 0;

            // locate position where common path starts
            while ((commongPathIndex < _path.Count) &&
                   (commongPathIndex < otherObject.Path.Count) &&
                   (0 == String.Compare(_path[commongPathIndex].Name, otherObject.Path[commongPathIndex].Name, StringComparison.OrdinalIgnoreCase)))
            {
                commongPathIndex++;
            }
            var sb = new StringBuilder();
            int i  = commongPathIndex;

            // for all not common folders in path depth of other object we need to go folder up
            while ((i < otherObject.Path.Count) &&
                   (otherObject.Path[i].Type != PathType.File))
            {
                sb.Append("../");
                i++;
            }
            // add the rest of the path starting from the common root
            for (i = commongPathIndex; i < _path.Count; i++)
            {
                if (_path[i].Type == PathType.Folder)
                {
                    sb.AppendFormat("{0}/", _path[i].Name);
                }
                else if (_path[i].Type == PathType.File)
                {
                    sb.Append(_path[i].Name);
                }
            }
            return(sb.ToString());
        }
Ejemplo n.º 2
0
 /// <summary>
 /// Get relative path , relative to another object 
 /// based on most common root
 /// </summary>
 /// <param name="otherObject"></param>
 /// <param name="flatStructure"></param>
 /// <returns></returns>
 public string GetRelativePath(IEPubInternalPath otherObject, bool flatStructure)
 {
     if (flatStructure && _supportFlatStructure)
     {
         if (_path.Count < 1)
         {
             throw new Exception("Path has to contain at least one element besides root");
         }
         return _path[_path.Count - 1].Name;
     }
     int commongPathIndex = 0;
     // locate position where common path starts
     while ((commongPathIndex < _path.Count)
         && (commongPathIndex < otherObject.Path.Count)
         && (0 == String.Compare(_path[commongPathIndex].Name, otherObject.Path[commongPathIndex].Name, StringComparison.OrdinalIgnoreCase)))
     {
         commongPathIndex++;
     }
     var sb = new StringBuilder();
     int i = commongPathIndex;
     // for all not common folders in path depth of other object we need to go folder up
     while ( (i < otherObject.Path.Count) 
         && (otherObject.Path[i].Type != PathType.File))
     {
         sb.Append("../");
         i++;
     }
     // add the rest of the path starting from the common root
     for (i = commongPathIndex; i < _path.Count; i++)
     {
         if (_path[i].Type == PathType.Folder)
         {
             sb.AppendFormat("{0}/",_path[i].Name);
         }
         else if (_path[i].Type == PathType.File)
         {
             sb.Append(_path[i].Name);
         }
     }
     return sb.ToString();
 }