Skip to content

Commit

Permalink
Add two new functions to Utils that remove the traling slash in a str…
Browse files Browse the repository at this point in the history
…ing, and remove the scheme from URL string.
  • Loading branch information
daniloercoli committed Sep 15, 2015
1 parent 8447eea commit 43eac0e
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,8 @@ public static String getBlogNameFromAccountMap(Map<String, Object> account) {
* Return the blog home URL setting or the host name if home URL is an empty string.
*/
public static String getHomeURLOrHostNameFromAccountMap(Map<String, Object> account) {
String homeURL = MapUtils.getMapStr(account, "homeURL").replace("http://", "").replace("https://", "").trim();
if (homeURL.endsWith("/")) {
homeURL = homeURL.substring(0, homeURL.length() -1);
}
String homeURL = UrlUtils.removeScheme(MapUtils.getMapStr(account, "homeURL"));
homeURL = StringUtils.removeTrailingSlash(homeURL);

if (homeURL.length() == 0) {
return StringUtils.getHost(MapUtils.getMapStr(account, "url"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,14 @@ public static String capitalize(final String str) {
return new StringBuilder(strLen).append(Character.toTitleCase(firstChar)).append(str.substring(1)).toString();
}

public static String removeTrailingSlash(final String str) {
if (TextUtils.isEmpty(str) || !str.endsWith("/")) {
return str;
}

return str.substring(0, str.length() -1);
}

/*
* Wrap an image URL in a photon URL
* Check out http://developer.wordpress.com/docs/photon/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,25 @@ public static String normalizeUrl(final String urlString) {
}
}


/**
* returns the passed url without the scheme
*/
public static String removeScheme(final String urlString) {
if (urlString == null) {
return null;
}

int doubleslash = urlString.indexOf("//");
if (doubleslash == -1) {
doubleslash = 0;
} else {
doubleslash += 2;
}

return urlString.substring(doubleslash, urlString.length());
}

/**
* returns the passed url without the query parameters
*/
Expand Down

0 comments on commit 43eac0e

Please sign in to comment.