Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
Path |
|
| 6.333333333333333;6.333 |
1 | /* | |
2 | * Licensed to the Apache Software Foundation (ASF) under one | |
3 | * or more contributor license agreements. See the NOTICE file | |
4 | * distributed with this work for additional information | |
5 | * regarding copyright ownership. The ASF licenses this file | |
6 | * to you under the Apache License, Version 2.0 (the | |
7 | * "License"); you may not use this file except in compliance | |
8 | * with the License. You may obtain a copy of the License at | |
9 | * | |
10 | * http://www.apache.org/licenses/LICENSE-2.0 | |
11 | * | |
12 | * Unless required by applicable law or agreed to in writing, | |
13 | * software distributed under the License is distributed on an | |
14 | * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | |
15 | * KIND, either express or implied. See the License for the | |
16 | * specific language governing permissions and limitations | |
17 | * under the License. | |
18 | */ | |
19 | package org.apache.myfaces.view.facelets.util; | |
20 | ||
21 | /** | |
22 | * @author Jacob Hookom | |
23 | * @version $Id$ | |
24 | */ | |
25 | 0 | public final class Path |
26 | { | |
27 | ||
28 | public static String normalize(String path) | |
29 | { | |
30 | 0 | if (path.length() == 0) |
31 | { | |
32 | 0 | return path; |
33 | } | |
34 | 0 | String n = path; |
35 | 0 | boolean abs = false; |
36 | 0 | while (n.indexOf('\\') >= 0) |
37 | { | |
38 | 0 | n = n.replace('\\', '/'); |
39 | } | |
40 | 0 | if (n.charAt(0) != '/') |
41 | { | |
42 | 0 | n = '/' + n; |
43 | 0 | abs = true; |
44 | } | |
45 | 0 | int idx = 0; |
46 | while (true) | |
47 | { | |
48 | 0 | idx = n.indexOf("%20"); |
49 | 0 | if (idx == -1) |
50 | { | |
51 | 0 | break; |
52 | } | |
53 | 0 | n = n.substring(0, idx) + " " + n.substring(idx + 3); |
54 | } | |
55 | while (true) | |
56 | { | |
57 | 0 | idx = n.indexOf("/./"); |
58 | 0 | if (idx == -1) |
59 | { | |
60 | 0 | break; |
61 | } | |
62 | 0 | n = n.substring(0, idx) + n.substring(idx + 2); |
63 | } | |
64 | 0 | if (abs) |
65 | { | |
66 | 0 | n = n.substring(1); |
67 | } | |
68 | 0 | return n; |
69 | } | |
70 | ||
71 | public static String relative(String ctx, String path) | |
72 | { | |
73 | 0 | if (path.length() == 0) |
74 | { | |
75 | 0 | return context(ctx); |
76 | } | |
77 | 0 | String c = context(normalize(ctx)); |
78 | 0 | String p = normalize(path); |
79 | 0 | p = c + p; |
80 | ||
81 | 0 | int idx = 0; |
82 | while (true) | |
83 | { | |
84 | 0 | idx = p.indexOf("/../"); |
85 | 0 | if (idx == -1) |
86 | { | |
87 | 0 | break; |
88 | } | |
89 | 0 | int s = p.lastIndexOf('/', idx - 3); |
90 | 0 | if (s == -1) |
91 | { | |
92 | 0 | break; |
93 | } | |
94 | 0 | p = p.substring(0, s) + p.substring(idx + 3); |
95 | 0 | } |
96 | 0 | return p; |
97 | } | |
98 | ||
99 | public static String context(String path) | |
100 | { | |
101 | 0 | int idx = path.lastIndexOf('/'); |
102 | 0 | if (idx == -1) |
103 | { | |
104 | 0 | return "/"; |
105 | } | |
106 | 0 | return path.substring(0, idx + 1); |
107 | } | |
108 | ||
109 | } |