< prev index next >

src/java.base/share/classes/java/net/URI.java

Print this page
rev 52979 : 8215281: Use String.isEmpty() when applicable in java.base
Reviewed-by: TBD


1942                     if (c != 0)
1943                         return c;
1944                 }
1945                 return sn - tn;
1946             }
1947             return +1;
1948         } else {
1949             return -1;
1950         }
1951     }
1952 
1953 
1954     // -- String construction --
1955 
1956     // If a scheme is given then the path, if given, must be absolute
1957     //
1958     private static void checkPath(String s, String scheme, String path)
1959         throws URISyntaxException
1960     {
1961         if (scheme != null) {
1962             if ((path != null)
1963                 && ((path.length() > 0) && (path.charAt(0) != '/')))
1964                 throw new URISyntaxException(s,
1965                                              "Relative path in absolute URI");
1966         }
1967     }
1968 
1969     private void appendAuthority(StringBuilder sb,
1970                                  String authority,
1971                                  String userInfo,
1972                                  String host,
1973                                  int port)
1974     {
1975         if (host != null) {
1976             sb.append("//");
1977             if (userInfo != null) {
1978                 sb.append(quote(userInfo, L_USERINFO, H_USERINFO));
1979                 sb.append('@');
1980             }
1981             boolean needBrackets = ((host.indexOf(':') >= 0)
1982                                     && !host.startsWith("[")
1983                                     && !host.endsWith("]"));
1984             if (needBrackets) sb.append('[');
1985             sb.append(host);


2146             return ru;
2147         }
2148 
2149         // 5.2 (3): Child is absolute
2150         if (child.scheme != null)
2151             return child;
2152 
2153         URI ru = new URI();             // Resolved URI
2154         ru.scheme = base.scheme;
2155         ru.query = child.query;
2156         ru.fragment = child.fragment;
2157 
2158         // 5.2 (4): Authority
2159         if (child.authority == null) {
2160             ru.authority = base.authority;
2161             ru.host = base.host;
2162             ru.userInfo = base.userInfo;
2163             ru.port = base.port;
2164 
2165             String cp = (child.path == null) ? "" : child.path;
2166             if ((cp.length() > 0) && (cp.charAt(0) == '/')) {
2167                 // 5.2 (5): Child path is absolute
2168                 ru.path = child.path;
2169             } else {
2170                 // 5.2 (6): Resolve relative path
2171                 ru.path = resolvePath(base.path, cp, base.isAbsolute());
2172             }
2173         } else {
2174             ru.authority = child.authority;
2175             ru.host = child.host;
2176             ru.userInfo = child.userInfo;
2177             ru.host = child.host;
2178             ru.port = child.port;
2179             ru.path = child.path;
2180         }
2181 
2182         // 5.2 (7): Recombine (nothing to do here)
2183         return ru;
2184     }
2185 
2186     // If the given URI's path is normal then return the URI;
2187     // o.w., return a new URI containing the normalized path.
2188     //
2189     private static URI normalize(URI u) {
2190         if (u.isOpaque() || (u.path == null) || (u.path.length() == 0))
2191             return u;
2192 
2193         String np = normalize(u.path);
2194         if (np == u.path)
2195             return u;
2196 
2197         URI v = new URI();
2198         v.scheme = u.scheme;
2199         v.fragment = u.fragment;
2200         v.authority = u.authority;
2201         v.userInfo = u.userInfo;
2202         v.host = u.host;
2203         v.port = u.port;
2204         v.path = np;
2205         v.query = u.query;
2206         return v;
2207     }
2208 
2209     // If both URIs are hierarchical, their scheme and authority components are
2210     // identical, and the base path is a prefix of the child's path, then




1942                     if (c != 0)
1943                         return c;
1944                 }
1945                 return sn - tn;
1946             }
1947             return +1;
1948         } else {
1949             return -1;
1950         }
1951     }
1952 
1953 
1954     // -- String construction --
1955 
1956     // If a scheme is given then the path, if given, must be absolute
1957     //
1958     private static void checkPath(String s, String scheme, String path)
1959         throws URISyntaxException
1960     {
1961         if (scheme != null) {
1962             if (path != null && !path.isEmpty() && path.charAt(0) != '/')
1963                 throw new URISyntaxException(s, "Relative path in absolute URI");


1964         }
1965     }
1966 
1967     private void appendAuthority(StringBuilder sb,
1968                                  String authority,
1969                                  String userInfo,
1970                                  String host,
1971                                  int port)
1972     {
1973         if (host != null) {
1974             sb.append("//");
1975             if (userInfo != null) {
1976                 sb.append(quote(userInfo, L_USERINFO, H_USERINFO));
1977                 sb.append('@');
1978             }
1979             boolean needBrackets = ((host.indexOf(':') >= 0)
1980                                     && !host.startsWith("[")
1981                                     && !host.endsWith("]"));
1982             if (needBrackets) sb.append('[');
1983             sb.append(host);


2144             return ru;
2145         }
2146 
2147         // 5.2 (3): Child is absolute
2148         if (child.scheme != null)
2149             return child;
2150 
2151         URI ru = new URI();             // Resolved URI
2152         ru.scheme = base.scheme;
2153         ru.query = child.query;
2154         ru.fragment = child.fragment;
2155 
2156         // 5.2 (4): Authority
2157         if (child.authority == null) {
2158             ru.authority = base.authority;
2159             ru.host = base.host;
2160             ru.userInfo = base.userInfo;
2161             ru.port = base.port;
2162 
2163             String cp = (child.path == null) ? "" : child.path;
2164             if (!cp.isEmpty() && cp.charAt(0) == '/') {
2165                 // 5.2 (5): Child path is absolute
2166                 ru.path = child.path;
2167             } else {
2168                 // 5.2 (6): Resolve relative path
2169                 ru.path = resolvePath(base.path, cp, base.isAbsolute());
2170             }
2171         } else {
2172             ru.authority = child.authority;
2173             ru.host = child.host;
2174             ru.userInfo = child.userInfo;
2175             ru.host = child.host;
2176             ru.port = child.port;
2177             ru.path = child.path;
2178         }
2179 
2180         // 5.2 (7): Recombine (nothing to do here)
2181         return ru;
2182     }
2183 
2184     // If the given URI's path is normal then return the URI;
2185     // o.w., return a new URI containing the normalized path.
2186     //
2187     private static URI normalize(URI u) {
2188         if (u.isOpaque() || u.path == null || u.path.isEmpty())
2189             return u;
2190 
2191         String np = normalize(u.path);
2192         if (np == u.path)
2193             return u;
2194 
2195         URI v = new URI();
2196         v.scheme = u.scheme;
2197         v.fragment = u.fragment;
2198         v.authority = u.authority;
2199         v.userInfo = u.userInfo;
2200         v.host = u.host;
2201         v.port = u.port;
2202         v.path = np;
2203         v.query = u.query;
2204         return v;
2205     }
2206 
2207     // If both URIs are hierarchical, their scheme and authority components are
2208     // identical, and the base path is a prefix of the child's path, then


< prev index next >