Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/4.0.3/EPPlus-4.0.3/Utils/UriHelper.cs @ 12178

Last change on this file since 12178 was 12074, checked in by sraggl, 9 years ago

#2341: Added EPPlus-4.0.3 to ExtLibs

File size: 2.5 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5
6namespace OfficeOpenXml.Utils
7{
8    internal class UriHelper
9    {
10        internal static Uri ResolvePartUri(Uri sourceUri, Uri targetUri)
11        {
12           if(targetUri.OriginalString.StartsWith("/"))
13            {
14                return targetUri;
15            }
16            string[] source = sourceUri.OriginalString.Split('/');
17            string[] target = targetUri.OriginalString.Split('/');
18
19            int t = target.Length - 1;
20            int s;
21            if(sourceUri.OriginalString.EndsWith("/")) //is the source a directory?
22            {
23                s = source.Length-1;
24            }
25            else
26            {
27                s=source.Length-2;
28            }
29
30            string file = target[t--];
31
32            while (t >= 0)
33            {
34                if (target[t] == ".")
35                {
36                    break;
37                }
38                else if (target[t] == "..")
39                {
40                    s--;
41                    t--;
42                }
43                else
44                {
45                    file = target[t--] + "/" + file;
46                }
47            }
48            if (s >= 0)
49            {
50                for(int i=s;i>=0;i--)
51                {
52                    file = source[i] + "/" + file;
53                }
54            }
55            return new Uri(file,UriKind.RelativeOrAbsolute);
56        }
57
58        internal static Uri GetRelativeUri(Uri WorksheetUri, Uri uri)
59        {
60            string[] source = WorksheetUri.OriginalString.Split('/');
61            string[] target = uri.OriginalString.Split('/');
62
63            int slen;
64            if (WorksheetUri.OriginalString.EndsWith("/"))
65            {
66                slen = source.Length;
67            }
68            else
69            {
70                slen = source.Length-1;
71            }
72            int i = 0;
73            while (i < slen && i < target.Length && source[i] == target[i])
74            {
75                i++;
76            }
77
78            string dirUp="";
79            for (int s = i; s < slen; s++)
80            {
81                dirUp += "../";
82            }
83            string file = "";
84            for (int t = i; t < target.Length; t++)
85            {               
86                file += (file==""?"":"/") + target[t];
87            }
88            return new Uri(dirUp+file,UriKind.Relative);
89        }
90    }
91}
Note: See TracBrowser for help on using the repository browser.