Skip to content

Commit 6057398

Browse files
committed
use our own StringUtils at more places
1 parent 720b7d8 commit 6057398

6 files changed

Lines changed: 80 additions & 25 deletions

File tree

src/main/java/org/htmlunit/WebClient.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1393,8 +1393,7 @@ private static WebResponse makeWebResponseForAboutUrl(final WebRequest webReques
13931393
}
13941394

13951395
final String urlWithoutQuery = StringUtils.substringBefore(urlString, "?");
1396-
if (!"blank".equalsIgnoreCase(org.apache.commons.lang3.StringUtils
1397-
.substringAfter(urlWithoutQuery, UrlUtils.ABOUT_SCHEME))) {
1396+
if (!"blank".equalsIgnoreCase(StringUtils.substringAfter(urlWithoutQuery, UrlUtils.ABOUT_SCHEME))) {
13981397
throw new MalformedURLException(url + " is not supported, only about:blank is supported at the moment.");
13991398
}
14001399
return new StringWebResponse("", url);

src/main/java/org/htmlunit/html/HtmlForm.java

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import java.util.function.Predicate;
3333
import java.util.regex.Pattern;
3434

35-
import org.apache.commons.lang3.StringUtils;
3635
import org.apache.commons.logging.Log;
3736
import org.apache.commons.logging.LogFactory;
3837
import org.htmlunit.BrowserVersion;
@@ -54,6 +53,7 @@
5453
import org.htmlunit.util.ArrayUtils;
5554
import org.htmlunit.util.EncodingSniffer;
5655
import org.htmlunit.util.NameValuePair;
56+
import org.htmlunit.util.StringUtils;
5757
import org.htmlunit.util.UrlUtils;
5858

5959
/**
@@ -158,15 +158,13 @@ && getAttributeDirect(ATTRIBUTE_NOVALIDATE) != ATTRIBUTE_NOT_DEFINED) {
158158
}
159159

160160
final String action = getActionAttribute().trim();
161-
if (org.htmlunit.util.StringUtils.startsWithIgnoreCase(action,
162-
JavaScriptURLConnection.JAVASCRIPT_PREFIX)) {
161+
if (StringUtils.startsWithIgnoreCase(action, JavaScriptURLConnection.JAVASCRIPT_PREFIX)) {
163162
htmlPage.executeJavaScript(action, "Form action", getStartLineNumber());
164163
return;
165164
}
166165
}
167166
else {
168-
if (org.htmlunit.util.StringUtils.startsWithIgnoreCase(getActionAttribute(),
169-
JavaScriptURLConnection.JAVASCRIPT_PREFIX)) {
167+
if (StringUtils.startsWithIgnoreCase(getActionAttribute(), JavaScriptURLConnection.JAVASCRIPT_PREFIX)) {
170168
// The action is JavaScript but JavaScript isn't enabled.
171169
return;
172170
}
@@ -273,8 +271,7 @@ public WebRequest getWebRequest(final SubmittableElement submitElement) {
273271
method = HttpMethod.POST;
274272
}
275273
else {
276-
if (!"get".equalsIgnoreCase(methodAttribute)
277-
&& org.htmlunit.util.StringUtils.isNotBlank(methodAttribute)) {
274+
if (!"get".equalsIgnoreCase(methodAttribute) && StringUtils.isNotBlank(methodAttribute)) {
278275
notifyIncorrectness("Incorrect submit method >" + getMethodAttribute() + "<. Using >GET<.");
279276
}
280277
method = HttpMethod.GET;
@@ -298,8 +295,8 @@ public WebRequest getWebRequest(final SubmittableElement submitElement) {
298295
queryFormFields = HttpUtils.toQueryFormFields(parameters, enc);
299296

300297
// action may already contain some query parameters: they have to be removed
301-
actionUrl = org.htmlunit.util.StringUtils.substringBefore(actionUrl, "#");
302-
actionUrl = org.htmlunit.util.StringUtils.substringBefore(actionUrl, "?");
298+
actionUrl = StringUtils.substringBefore(actionUrl, "#");
299+
actionUrl = StringUtils.substringBefore(actionUrl, "?");
303300
parameters.clear(); // parameters have been added to query
304301
}
305302

@@ -365,7 +362,7 @@ private boolean relContainsNoreferrer() {
365362
String rel = getRelAttribute();
366363
if (rel != null) {
367364
rel = rel.toLowerCase(Locale.ROOT);
368-
return ArrayUtils.contains(org.htmlunit.util.StringUtils.splitAtBlank(rel), "noreferrer");
365+
return ArrayUtils.contains(StringUtils.splitAtBlank(rel), "noreferrer");
369366
}
370367
return false;
371368
}
@@ -480,7 +477,7 @@ private static boolean isValidForSubmission(final HtmlElement element, final Sub
480477
return false;
481478
}
482479

483-
if (org.htmlunit.util.StringUtils.isEmptyString(element.getAttributeDirect(NAME_ATTRIBUTE))) {
480+
if (StringUtils.isEmptyString(element.getAttributeDirect(NAME_ATTRIBUTE))) {
484481
return false;
485482
}
486483

src/main/java/org/htmlunit/javascript/host/html/HTMLAnchorElement.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@
2626
import java.util.List;
2727
import java.util.Locale;
2828

29-
import org.apache.commons.lang3.StringUtils;
3029
import org.htmlunit.BrowserVersion;
3130
import org.htmlunit.HttpHeader;
3231
import org.htmlunit.SgmlPage;
@@ -41,6 +40,7 @@
4140
import org.htmlunit.javascript.configuration.JsxGetter;
4241
import org.htmlunit.javascript.configuration.JsxSetter;
4342
import org.htmlunit.javascript.host.dom.DOMTokenList;
43+
import org.htmlunit.util.StringUtils;
4444
import org.htmlunit.util.UrlUtils;
4545

4646
/**
@@ -212,7 +212,7 @@ public void setRev(final String rel) {
212212
@JsxGetter
213213
public String getReferrerPolicy() {
214214
String attrib = getDomNodeOrDie().getAttribute("referrerPolicy");
215-
if (org.htmlunit.util.StringUtils.isEmptyOrNull(attrib)) {
215+
if (StringUtils.isEmptyOrNull(attrib)) {
216216
return "";
217217
}
218218
attrib = attrib.toLowerCase(Locale.ROOT);
@@ -262,8 +262,8 @@ public String getSearch() {
262262
public void setSearch(final String search) throws Exception {
263263
final String query;
264264
if (search == null
265-
|| org.htmlunit.util.StringUtils.isEmptyString(search)
266-
|| org.htmlunit.util.StringUtils.equalsChar('?', search)) {
265+
|| StringUtils.isEmptyString(search)
266+
|| StringUtils.equalsChar('?', search)) {
267267
query = null;
268268
}
269269
else if (search.charAt(0) == '?') {
@@ -375,11 +375,11 @@ public String getHostname() {
375375
@JsxSetter
376376
public void setHostname(final String hostname) throws Exception {
377377
if (getBrowserVersion().hasFeature(JS_ANCHOR_HOSTNAME_IGNORE_BLANK)) {
378-
if (!org.htmlunit.util.StringUtils.isBlank(hostname)) {
378+
if (!StringUtils.isBlank(hostname)) {
379379
setUrl(UrlUtils.getUrlWithNewHost(getUrl(), hostname));
380380
}
381381
}
382-
else if (!org.htmlunit.util.StringUtils.isEmptyOrNull(hostname)) {
382+
else if (!StringUtils.isEmptyOrNull(hostname)) {
383383
setUrl(UrlUtils.getUrlWithNewHost(getUrl(), hostname));
384384
}
385385
}
@@ -398,7 +398,7 @@ public String getPathname() {
398398
String href = anchor.getHrefAttribute();
399399
if (href.length() > 1 && Character.isLetter(href.charAt(0)) && ':' == href.charAt(1)) {
400400
if (browser.hasFeature(JS_ANCHOR_PROTOCOL_COLON_UPPER_CASE_DRIVE_LETTERS)) {
401-
href = StringUtils.capitalize(href);
401+
href = org.apache.commons.lang3.StringUtils.capitalize(href);
402402
}
403403
if (browser.hasFeature(JS_ANCHOR_PATHNAME_PREFIX_WIN_DRIVES_URL)) {
404404
href = "/" + href;
@@ -482,7 +482,7 @@ public String getProtocol() {
482482
if (anchor.getHrefAttribute().startsWith("http")) {
483483
return ":";
484484
}
485-
return org.htmlunit.util.StringUtils.substringBefore(anchor.getHrefAttribute(), "/");
485+
return StringUtils.substringBefore(anchor.getHrefAttribute(), "/");
486486
}
487487
}
488488

@@ -498,7 +498,7 @@ public void setProtocol(final String protocol) throws Exception {
498498
return;
499499
}
500500

501-
final String bareProtocol = org.htmlunit.util.StringUtils.substringBefore(protocol, ":").trim();
501+
final String bareProtocol = StringUtils.substringBefore(protocol, ":").trim();
502502
if (!UrlUtils.isValidScheme(bareProtocol)) {
503503
return;
504504
}
@@ -656,7 +656,7 @@ public String getUsername() {
656656
if (userInfo == null) {
657657
return "";
658658
}
659-
return StringUtils.substringBefore(userInfo, ':');
659+
return org.apache.commons.lang3.StringUtils.substringBefore(userInfo, ':');
660660
}
661661
catch (final MalformedURLException e) {
662662
return "";

src/main/java/org/htmlunit/protocol/data/DataUrlDecoder.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@
2424
import java.nio.charset.UnsupportedCharsetException;
2525
import java.util.Base64;
2626

27-
import org.apache.commons.lang3.StringUtils;
2827
import org.htmlunit.util.MimeType;
28+
import org.htmlunit.util.StringUtils;
2929
import org.htmlunit.util.UrlUtils;
3030

3131
/**
@@ -124,7 +124,7 @@ private static Charset extractCharset(final String beforeData) {
124124
private static String extractMediaType(final String beforeData) {
125125
if (beforeData.contains("/")) {
126126
if (beforeData.contains(";")) {
127-
return org.htmlunit.util.StringUtils.substringBefore(beforeData, ";");
127+
return StringUtils.substringBefore(beforeData, ";");
128128
}
129129
return beforeData;
130130
}

src/main/java/org/htmlunit/util/StringUtils.java

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,11 @@
3333
*/
3434
public final class StringUtils {
3535

36+
/**
37+
* The empty String {@code ""}.
38+
*/
39+
public static final String EMPTY_STRING = "";
40+
3641
private static final Pattern HEX_COLOR = Pattern.compile("#([\\da-fA-F]{3}|[\\da-fA-F]{6})");
3742
private static final Pattern RGB_COLOR =
3843
Pattern.compile("rgb\\(\\s*(0|[1-9]\\d?|1\\d\\d?|2[0-4]\\d|25[0-5])%?\\s*,"
@@ -286,6 +291,34 @@ public static String replaceChars(final String str, final String searchChars, fi
286291
return buf.toString();
287292
}
288293

294+
/**
295+
* Gets the substring after the first occurrence of a separator. The separator is not returned.
296+
* <p>
297+
* A {@code null} string input will return {@code null}.
298+
* An empty ("") string input will return the empty string.
299+
* A {@code null} separator will return the empty string if the input string is not {@code null}.
300+
* <p>
301+
* If nothing is found, the empty string is returned.
302+
* <p>
303+
*
304+
* @param str the String to get a substring from, may be null.
305+
* @param find the String to find, may be null.
306+
* @return the substring after the first occurrence of the specified string, {@code null} if null String input.
307+
*/
308+
public static String substringAfter(final String str, final String find) {
309+
if (isEmptyOrNull(str)) {
310+
return str;
311+
}
312+
if (find == null) {
313+
return EMPTY_STRING;
314+
}
315+
final int pos = str.indexOf(find);
316+
if (pos == -1) {
317+
return EMPTY_STRING;
318+
}
319+
return str.substring(pos + find.length());
320+
}
321+
289322
/**
290323
* Escapes the characters '&lt;', '&gt;' and '&amp;' into their XML entity equivalents.
291324
*

src/test/java/org/htmlunit/util/StringUtilsTest.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,4 +530,30 @@ public void toFloat() throws Exception {
530530
assertEquals(21f, StringUtils.toFloat(" 21 ", 0));
531531
assertEquals(0, StringUtils.toFloat(" - 21 \t", 0f));
532532
}
533+
534+
/**
535+
* @throws Exception if the test fails
536+
*/
537+
@Test
538+
public void substringAfter() throws Exception {
539+
assertNull(StringUtils.substringAfter(null, null));
540+
assertNull(StringUtils.substringAfter(null, ""));
541+
assertNull(StringUtils.substringAfter(null, "abc"));
542+
543+
assertEquals(StringUtils.EMPTY_STRING, StringUtils.substringAfter("", null));
544+
assertEquals(StringUtils.EMPTY_STRING, StringUtils.substringAfter("", ""));
545+
assertEquals(StringUtils.EMPTY_STRING, StringUtils.substringAfter("", "abc"));
546+
547+
assertEquals(StringUtils.EMPTY_STRING, StringUtils.substringAfter(StringUtils.EMPTY_STRING, null));
548+
assertEquals(StringUtils.EMPTY_STRING, StringUtils.substringAfter(StringUtils.EMPTY_STRING, ""));
549+
assertEquals(StringUtils.EMPTY_STRING, StringUtils.substringAfter(StringUtils.EMPTY_STRING, "abc"));
550+
551+
assertEquals(StringUtils.EMPTY_STRING, StringUtils.substringAfter("abc", null));
552+
assertEquals("abc", StringUtils.substringAfter("abc", ""));
553+
554+
assertEquals("bc", StringUtils.substringAfter("abc", "a"));
555+
assertEquals("cba", StringUtils.substringAfter("abcba", "b"));
556+
assertEquals("", StringUtils.substringAfter("abc", "c"));
557+
assertEquals("", StringUtils.substringAfter("abc", "d"));
558+
}
533559
}

0 commit comments

Comments
 (0)