2
* Copyright (c) 2008, 2009, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation. Oracle designates this
8
* particular file as subject to the "Classpath" exception as provided
9
* by Oracle in the LICENSE file that accompanied this code.
11
* This code is distributed in the hope that it will be useful, but WITHOUT
12
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14
* version 2 for more details (a copy is included in the LICENSE file that
15
* accompanied this code).
17
* You should have received a copy of the GNU General Public License version
18
* 2 along with this work; if not, write to the Free Software Foundation,
19
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22
* or visit www.oracle.com if you need additional information or have any
29
import java.net.URISyntaxException;
32
* Utility methods to convert between Path and URIs.
35
class WindowsUriSupport {
36
private WindowsUriSupport() {
39
// suffix for IPv6 literal address
40
private static final String IPV6_LITERAL_SUFFIX = ".ipv6-literal.net";
43
* Returns URI to represent the given (absolute) path
45
private static URI toUri(String path, boolean isUnc, boolean addSlash) {
50
int slash = path.indexOf('\\', 2);
51
uriHost = path.substring(2, slash);
52
uriPath = path.substring(slash).replace('\\', '/');
54
// handle IPv6 literal addresses
55
// 1. drop .ivp6-literal.net
56
// 2. replace "-" with ":"
57
// 3. replace "s" with "%" (zone/scopeID delimiter)
58
if (uriHost.endsWith(IPV6_LITERAL_SUFFIX)) {
60
.substring(0, uriHost.length() - IPV6_LITERAL_SUFFIX.length())
66
uriPath = "/" + path.replace('\\', '/');
69
// append slash if known to be directory
73
// return file:///C:/My%20Documents or file://server/share/foo
75
return new URI("file", uriHost, uriPath, null);
76
} catch (URISyntaxException x) {
78
throw new AssertionError(x);
81
// if we get here it means we've got a UNC with reserved characters
82
// in the server name. The authority component cannot contain escaped
83
// octets so fallback to encoding the server name into the URI path
85
uriPath = "//" + path.replace('\\', '/');
89
return new URI("file", null, uriPath, null);
90
} catch (URISyntaxException x) {
91
throw new AssertionError(x);
96
* Converts given Path to a URI
98
static URI toUri(NetPath path) {
99
path = path.toAbsolutePath();
100
String s = path.toString();
102
// trailing slash will be added if file is a directory. Skip check if
103
// already have trailing space
104
boolean addSlash = false;
105
if (!s.endsWith("\\")) {
107
addSlash = cli.System.IO.Directory.Exists(s);
108
} catch (Throwable x) {
112
return toUri(s, path.isUnc(), addSlash);
116
* Converts given URI to a Path
118
static NetPath fromUri(NetFileSystem fs, URI uri) {
119
if (!uri.isAbsolute())
120
throw new IllegalArgumentException("URI is not absolute");
122
throw new IllegalArgumentException("URI is not hierarchical");
123
String scheme = uri.getScheme();
124
if ((scheme == null) || !scheme.equalsIgnoreCase("file"))
125
throw new IllegalArgumentException("URI scheme is not \"file\"");
126
if (uri.getFragment() != null)
127
throw new IllegalArgumentException("URI has a fragment component");
128
if (uri.getQuery() != null)
129
throw new IllegalArgumentException("URI has a query component");
130
String path = uri.getPath();
132
throw new IllegalArgumentException("URI path component is empty");
135
String auth = uri.getAuthority();
136
if (auth != null && !auth.equals("")) {
137
String host = uri.getHost();
139
throw new IllegalArgumentException("URI authority component has undefined host");
140
if (uri.getUserInfo() != null)
141
throw new IllegalArgumentException("URI authority component has user-info");
142
if (uri.getPort() != -1)
143
throw new IllegalArgumentException("URI authority component has port number");
146
// 1. drop enclosing brackets
147
// 2. replace ":" with "-"
148
// 3. replace "%" with "s" (zone/scopeID delimiter)
149
// 4. Append .ivp6-literal.net
150
if (host.startsWith("[")) {
151
host = host.substring(1, host.length()-1)
154
host += IPV6_LITERAL_SUFFIX;
157
// reconstitute the UNC
158
path = "\\\\" + host + path;
160
if ((path.length() > 2) && (path.charAt(2) == ':')) {
161
// "/c:/foo" --> "c:/foo"
162
path = path.substring(1);
165
return new NetPath(fs, path);