1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.eclipse.aether.repository;
20
21 import java.io.File;
22 import java.net.URI;
23
24
25
26
27
28
29 public final class RepositoryUriUtils {
30
31 private RepositoryUriUtils() {}
32
33 public static URI toUri(String repositoryUrl) {
34 final String protocol = protocol(repositoryUrl);
35 if ("file".equals(protocol)
36 || protocol.isEmpty()
37 || protocol.length() == 1
38 && (Character.isLetter(protocol.charAt(0)) && Character.isUpperCase(protocol.charAt(0)))) {
39 return new File(basedir(repositoryUrl)).toURI();
40 } else {
41 return URI.create(repositoryUrl);
42 }
43 }
44
45
46
47
48
49
50
51 private static String protocol(final String url) {
52 final int pos = url.indexOf(":");
53
54 if (pos == -1) {
55 return "";
56 }
57 return url.substring(0, pos).trim();
58 }
59
60
61
62
63
64
65
66 private static String basedir(String url) {
67 String protocol = protocol(url);
68
69 String retValue;
70
71 if (!protocol.isEmpty()) {
72 retValue = url.substring(protocol.length() + 1);
73 } else {
74 retValue = url;
75 }
76 retValue = decode(retValue);
77
78 if (retValue.startsWith("//")) {
79 retValue = retValue.substring(2);
80
81 if (retValue.length() >= 2 && (retValue.charAt(1) == '|' || retValue.charAt(1) == ':')) {
82
83 retValue = retValue.charAt(0) + ":" + retValue.substring(2);
84 } else {
85
86 int index = retValue.indexOf("/");
87 if (index >= 0) {
88 retValue = retValue.substring(index + 1);
89 }
90
91
92 if (retValue.length() >= 2 && (retValue.charAt(1) == '|' || retValue.charAt(1) == ':')) {
93 retValue = retValue.charAt(0) + ":" + retValue.substring(2);
94 } else if (index >= 0) {
95
96 retValue = "/" + retValue;
97 }
98 }
99 }
100
101
102 if (retValue.length() >= 2 && retValue.charAt(1) == '|') {
103 retValue = retValue.charAt(0) + ":" + retValue.substring(2);
104 }
105
106 return retValue.trim();
107 }
108
109
110
111
112
113
114
115
116 private static String decode(String url) {
117 String decoded = url;
118 if (url != null) {
119 int pos = -1;
120 while ((pos = decoded.indexOf('%', pos + 1)) >= 0) {
121 if (pos + 2 < decoded.length()) {
122 String hexStr = decoded.substring(pos + 1, pos + 3);
123 char ch = (char) Integer.parseInt(hexStr, 16);
124 decoded = decoded.substring(0, pos) + ch + decoded.substring(pos + 3);
125 }
126 }
127 }
128 return decoded;
129 }
130 }