Fix that NullPointerException should NEVER be thrown by programmer, make exceptions more informative, simplify code logic, flip equals() on strings

...and other small fixes
This commit is contained in:
Miłosz Sieradzki 2014-07-25 20:18:44 +02:00
parent c7b97621c7
commit 3c8e46188a

View File

@ -10,8 +10,6 @@ import java.util.regex.Pattern;
import static android.util.Patterns.GOOD_IRI_CHAR; import static android.util.Patterns.GOOD_IRI_CHAR;
/** /**
* {@hide}
*
* Web Address Parser * Web Address Parser
* *
* This is called WebAddress, rather than URL or URI, because it attempts to * This is called WebAddress, rather than URL or URI, because it attempts to
@ -54,14 +52,13 @@ public class WebAddress {
/* anchor */".*", Pattern.CASE_INSENSITIVE); /* anchor */".*", Pattern.CASE_INSENSITIVE);
/** /**
* parses given uriString. * Parses given URI-like string.
*/ */
public WebAddress(String address) throws Exception { public WebAddress(String address) {
if (address == null) {
throw new NullPointerException();
}
// android.util.Log.d(LOGTAG, "WebAddress: " + address); if (address == null) {
throw new IllegalArgumentException("address can't be null");
}
mScheme = ""; mScheme = "";
mHost = ""; mHost = "";
@ -71,7 +68,11 @@ public class WebAddress {
Matcher m = sAddressPattern.matcher(address); Matcher m = sAddressPattern.matcher(address);
String t; String t;
if (m.matches()) { if (!m.matches()) {
throw new IllegalArgumentException("Parsing of address '" +
address + "' failed");
}
t = m.group(MATCH_GROUP_SCHEME); t = m.group(MATCH_GROUP_SCHEME);
if (t != null) { if (t != null) {
mScheme = t.toLowerCase(Locale.ROOT); mScheme = t.toLowerCase(Locale.ROOT);
@ -85,16 +86,16 @@ public class WebAddress {
mHost = t; mHost = t;
} }
t = m.group(MATCH_GROUP_PORT); t = m.group(MATCH_GROUP_PORT);
if (t != null && t.length() > 0) { if (t != null && !t.isEmpty()) {
// The ':' character is not returned by the regex. // The ':' character is not returned by the regex.
try { try {
mPort = Integer.parseInt(t); mPort = Integer.parseInt(t);
} catch (NumberFormatException ex) { } catch (NumberFormatException ex) {
throw new Exception(); throw new RuntimeException("Parsing of port number failed", ex);
} }
} }
t = m.group(MATCH_GROUP_PATH); t = m.group(MATCH_GROUP_PATH);
if (t != null && t.length() > 0) { if (t != null && !t.isEmpty()) {
/* /*
* handle busted myspace frontpage redirect with missing initial * handle busted myspace frontpage redirect with missing initial
* "/" * "/"
@ -102,42 +103,38 @@ public class WebAddress {
if (t.charAt(0) == '/') { if (t.charAt(0) == '/') {
mPath = t; mPath = t;
} else { } else {
mPath = "/" + t; mPath = '/' + t;
} }
} }
} else {
// nothing found... outa here
throw new Exception();
}
/* /*
* Get port from scheme or scheme from port, if necessary and possible * Get port from scheme or scheme from port, if necessary and possible
*/ */
if (mPort == 443 && mScheme.equals("")) { if (mPort == 443 && "".equals(mScheme)) {
mScheme = "https"; mScheme = "https";
} else if (mPort == -1) { } else if (mPort == -1) {
if (mScheme.equals("https")) { if ("https".equals(mScheme)) {
mPort = 443; mPort = 443;
} else { } else {
mPort = 80; // default mPort = 80; // default
} }
} }
if (mScheme.equals("")) { if ("".equals(mScheme)) {
mScheme = "http"; mScheme = "http";
} }
} }
@Override @Override
public String toString() { public String toString() {
String port = ""; String port = "";
if ((mPort != 443 && mScheme.equals("https")) if ((mPort != 443 && "https".equals(mScheme))
|| (mPort != 80 && mScheme.equals("http"))) { || (mPort != 80 && "http".equals(mScheme))) {
port = ":" + Integer.toString(mPort); port = ':' + Integer.toString(mPort);
} }
String authInfo = ""; String authInfo = "";
if (mAuthInfo.length() > 0) { if (!mAuthInfo.isEmpty()) {
authInfo = mAuthInfo + "@"; authInfo = mAuthInfo + '@';
} }
return mScheme + "://" + authInfo + mHost + port + mPath; return mScheme + "://" + authInfo + mHost + port + mPath;