2 * Copyright (C) 2006 The Android Open Source Project
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
19 import static android.util.Patterns.GOOD_IRI_CHAR;
21 import android.annotation.SystemApi;
23 import java.util.Locale;
24 import java.util.regex.Matcher;
25 import java.util.regex.Pattern;
32 * This is called WebAddress, rather than URL or URI, because it
33 * attempts to parse the stuff that a user will actually type into a
34 * browser address widget.
36 * Unlike java.net.uri, this parser will not choke on URIs missing
37 * schemes. It will only throw a ParseException if the input is
40 * If given an https scheme but no port, fills in port
43 // TODO(igsolla): remove WebAddress from the system SDK once the WebView apk does not
44 // longer need to be binary compatible with the API 21 version of the framework.
46 public class WebAddress {
48 private String mScheme;
52 private String mAuthInfo;
54 static final int MATCH_GROUP_SCHEME = 1;
55 static final int MATCH_GROUP_AUTHORITY = 2;
56 static final int MATCH_GROUP_HOST = 3;
57 static final int MATCH_GROUP_PORT = 4;
58 static final int MATCH_GROUP_PATH = 5;
60 static Pattern sAddressPattern = Pattern.compile(
61 /* scheme */ "(?:(http|https|file)\\:\\/\\/)?" +
62 /* authority */ "(?:([-A-Za-z0-9$_.+!*'(),;?&=]+(?:\\:[-A-Za-z0-9$_.+!*'(),;?&=]+)?)@)?" +
63 /* host */ "([" + GOOD_IRI_CHAR + "%_-][" + GOOD_IRI_CHAR + "%_\\.-]*|\\[[0-9a-fA-F:\\.]+\\])?" +
64 /* port */ "(?:\\:([0-9]*))?" +
65 /* path */ "(\\/?[^#]*)?" +
66 /* anchor */ ".*", Pattern.CASE_INSENSITIVE);
68 /** parses given uriString. */
69 public WebAddress(String address) throws ParseException {
70 if (address == null) {
71 throw new NullPointerException();
74 // android.util.Log.d(LOGTAG, "WebAddress: " + address);
82 Matcher m = sAddressPattern.matcher(address);
85 t = m.group(MATCH_GROUP_SCHEME);
86 if (t != null) mScheme = t.toLowerCase(Locale.ROOT);
87 t = m.group(MATCH_GROUP_AUTHORITY);
88 if (t != null) mAuthInfo = t;
89 t = m.group(MATCH_GROUP_HOST);
90 if (t != null) mHost = t;
91 t = m.group(MATCH_GROUP_PORT);
92 if (t != null && t.length() > 0) {
93 // The ':' character is not returned by the regex.
95 mPort = Integer.parseInt(t);
96 } catch (NumberFormatException ex) {
97 throw new ParseException("Bad port");
100 t = m.group(MATCH_GROUP_PATH);
101 if (t != null && t.length() > 0) {
102 /* handle busted myspace frontpage redirect with
103 missing initial "/" */
104 if (t.charAt(0) == '/') {
112 // nothing found... outa here
113 throw new ParseException("Bad address");
116 /* Get port from scheme or scheme from port, if necessary and
118 if (mPort == 443 && mScheme.equals("")) {
120 } else if (mPort == -1) {
121 if (mScheme.equals("https"))
124 mPort = 80; // default
126 if (mScheme.equals("")) mScheme = "http";
130 public String toString() {
132 if ((mPort != 443 && mScheme.equals("https")) ||
133 (mPort != 80 && mScheme.equals("http"))) {
134 port = ":" + Integer.toString(mPort);
136 String authInfo = "";
137 if (mAuthInfo.length() > 0) {
138 authInfo = mAuthInfo + "@";
141 return mScheme + "://" + authInfo + mHost + port + mPath;
145 public void setScheme(String scheme) {
150 public String getScheme() {
155 public void setHost(String host) {
160 public String getHost() {
165 public void setPort(int port) {
170 public int getPort() {
175 public void setPath(String path) {
180 public String getPath() {
185 public void setAuthInfo(String authInfo) {
186 mAuthInfo = authInfo;
190 public String getAuthInfo() {