1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.view.facelets.util;
20
21
22
23
24
25 public final class Path
26 {
27
28 public static final String normalize(String path)
29 {
30 if (path.length() == 0)
31 return path;
32 String n = path;
33 boolean abs = false;
34 while (n.indexOf('\\') >= 0)
35 {
36 n = n.replace('\\', '/');
37 }
38 if (n.charAt(0) != '/')
39 {
40 n = '/' + n;
41 abs = true;
42 }
43 int idx = 0;
44 while (true)
45 {
46 idx = n.indexOf("%20");
47 if (idx == -1)
48 {
49 break;
50 }
51 n = n.substring(0, idx) + " " + n.substring(idx + 3);
52 }
53 while (true)
54 {
55 idx = n.indexOf("/./");
56 if (idx == -1)
57 {
58 break;
59 }
60 n = n.substring(0, idx) + n.substring(idx + 2);
61 }
62 if (abs)
63 {
64 n = n.substring(1);
65 }
66 return n;
67 }
68
69 public static final String relative(String ctx, String path)
70 {
71 if (path.length() == 0)
72 {
73 return context(ctx);
74 }
75 String c = context(normalize(ctx));
76 String p = normalize(path);
77 p = c + p;
78
79 int idx = 0;
80 while (true)
81 {
82 idx = p.indexOf("/../");
83 if (idx == -1)
84 {
85 break;
86 }
87 int s = p.lastIndexOf('/', idx - 3);
88 if (s == -1)
89 {
90 break;
91 }
92 p = p.substring(0, s) + p.substring(idx + 3);
93 }
94 return p;
95 }
96
97 public static final String context(String path)
98 {
99 int idx = path.lastIndexOf('/');
100 if (idx == -1)
101 {
102 return "/";
103 }
104 return path.substring(0, idx + 1);
105 }
106
107 }