Skip to content

Commit 6f120fc

Browse files
committed
use our own StringUtils at more places
1 parent 431c16e commit 6f120fc

13 files changed

Lines changed: 154 additions & 27 deletions

File tree

src/main/java/org/htmlunit/WebResponseData.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@
2929
import org.apache.commons.io.IOUtils;
3030
import org.apache.commons.io.input.BOMInputStream;
3131
import org.apache.commons.lang3.ArrayUtils;
32-
import org.apache.commons.lang3.Strings;
3332
import org.apache.commons.logging.Log;
3433
import org.apache.commons.logging.LogFactory;
3534
import org.brotli.dec.BrotliInputStream;
3635
import org.htmlunit.util.MimeType;
3736
import org.htmlunit.util.NameValuePair;
37+
import org.htmlunit.util.StringUtils;
3838

3939
/**
4040
* Simple data object to simplify WebResponse creation.
@@ -101,7 +101,7 @@ private InputStream getStream(final ByteOrderMark... bomHeaders) throws IOExcept
101101
final List<NameValuePair> headers = getResponseHeaders();
102102
final String encoding = getHeader(headers, "content-encoding");
103103
if (encoding != null) {
104-
boolean isGzip = Strings.CI.contains(encoding, "gzip") && !"no-gzip".equals(encoding);
104+
boolean isGzip = StringUtils.containsIgnoreCase(encoding, "gzip") && !"no-gzip".equals(encoding);
105105
if ("gzip-only-text/html".equals(encoding)) {
106106
isGzip = MimeType.TEXT_HTML.equals(getHeader(headers, "content-type"));
107107
}
@@ -148,7 +148,7 @@ private InputStream getStream(final ByteOrderMark... bomHeaders) throws IOExcept
148148
return stream;
149149
}
150150

151-
if (Strings.CI.contains(encoding, "deflate")) {
151+
if (StringUtils.containsIgnoreCase(encoding, "deflate")) {
152152
boolean zlibHeader = false;
153153
if (stream.markSupported()) { // should be always the case as the content is in a byte[] or in a file
154154
stream.mark(2);

src/main/java/org/htmlunit/css/CssStyleSheet.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@
3939

4040
import org.apache.commons.io.IOUtils;
4141
import org.apache.commons.lang3.StringUtils;
42-
import org.apache.commons.lang3.Strings;
4342
import org.apache.commons.lang3.math.NumberUtils;
4443
import org.apache.commons.logging.Log;
4544
import org.apache.commons.logging.LogFactory;
@@ -564,7 +563,7 @@ static boolean selects(final BrowserVersion browserVersion,
564563
final String prefixValue = prefixAttributeCondition.getValue();
565564
if (prefixAttributeCondition.isCaseInSensitive()) {
566565
return !org.htmlunit.util.StringUtils.isEmptyString(prefixValue)
567-
&& Strings.CI.startsWith(
566+
&& org.htmlunit.util.StringUtils.startsWithIgnoreCase(
568567
element.getAttribute(prefixAttributeCondition.getLocalName()), prefixValue);
569568
}
570569
return !org.htmlunit.util.StringUtils.isEmptyString(prefixValue)
@@ -575,7 +574,7 @@ static boolean selects(final BrowserVersion browserVersion,
575574
final String suffixValue = suffixAttributeCondition.getValue();
576575
if (suffixAttributeCondition.isCaseInSensitive()) {
577576
return !org.htmlunit.util.StringUtils.isEmptyString(suffixValue)
578-
&& Strings.CI.endsWith(
577+
&& org.htmlunit.util.StringUtils.endsWithIgnoreCase(
579578
element.getAttribute(suffixAttributeCondition.getLocalName()), suffixValue);
580579
}
581580
return !org.htmlunit.util.StringUtils.isEmptyString(suffixValue)
@@ -586,7 +585,7 @@ static boolean selects(final BrowserVersion browserVersion,
586585
final String substringValue = substringAttributeCondition.getValue();
587586
if (substringAttributeCondition.isCaseInSensitive()) {
588587
return !org.htmlunit.util.StringUtils.isEmptyString(substringValue)
589-
&& Strings.CI.contains(
588+
&& org.htmlunit.util.StringUtils.containsIgnoreCase(
590589
element.getAttribute(substringAttributeCondition.getLocalName()), substringValue);
591590
}
592591
return !org.htmlunit.util.StringUtils.isEmptyString(substringValue)

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424

2525
import org.apache.commons.lang3.ArrayUtils;
2626
import org.apache.commons.lang3.StringUtils;
27-
import org.apache.commons.lang3.Strings;
2827
import org.apache.commons.logging.Log;
2928
import org.apache.commons.logging.LogFactory;
3029
import org.htmlunit.BrowserVersion;
@@ -117,7 +116,7 @@ protected void doClickStateUpdate(final boolean shiftKey, final boolean ctrlKey,
117116
}
118117
final String downloadAttribute = getDownloadAttribute();
119118
HtmlPage page = (HtmlPage) getPage();
120-
if (Strings.CI.startsWith(href, JavaScriptURLConnection.JAVASCRIPT_PREFIX)) {
119+
if (org.htmlunit.util.StringUtils.startsWithIgnoreCase(href, JavaScriptURLConnection.JAVASCRIPT_PREFIX)) {
121120
final StringBuilder builder = new StringBuilder(href.length());
122121
builder.append(JavaScriptURLConnection.JAVASCRIPT_PREFIX);
123122
for (int i = JavaScriptURLConnection.JAVASCRIPT_PREFIX.length(); i < href.length(); i++) {

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.util.Map;
2424

2525
import org.apache.commons.lang3.StringUtils;
26-
import org.apache.commons.lang3.Strings;
2726
import org.apache.commons.logging.Log;
2827
import org.apache.commons.logging.LogFactory;
2928
import org.htmlunit.SgmlPage;
@@ -81,7 +80,7 @@ protected boolean doClickStateUpdate(final boolean shiftKey, final boolean ctrlK
8180
final String href = getHrefAttribute().trim();
8281
if (!href.isEmpty()) {
8382
final HtmlPage page = (HtmlPage) getPage();
84-
if (Strings.CI.startsWith(href, JavaScriptURLConnection.JAVASCRIPT_PREFIX)) {
83+
if (org.htmlunit.util.StringUtils.startsWithIgnoreCase(href, JavaScriptURLConnection.JAVASCRIPT_PREFIX)) {
8584
page.executeJavaScript(
8685
href, "javascript url", getStartLineNumber());
8786
return false;

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434

3535
import org.apache.commons.lang3.ArrayUtils;
3636
import org.apache.commons.lang3.StringUtils;
37-
import org.apache.commons.lang3.Strings;
3837
import org.apache.commons.logging.Log;
3938
import org.apache.commons.logging.LogFactory;
4039
import org.htmlunit.BrowserVersion;
@@ -159,13 +158,15 @@ && getAttributeDirect(ATTRIBUTE_NOVALIDATE) != ATTRIBUTE_NOT_DEFINED) {
159158
}
160159

161160
final String action = getActionAttribute().trim();
162-
if (Strings.CI.startsWith(action, JavaScriptURLConnection.JAVASCRIPT_PREFIX)) {
161+
if (org.htmlunit.util.StringUtils.startsWithIgnoreCase(action,
162+
JavaScriptURLConnection.JAVASCRIPT_PREFIX)) {
163163
htmlPage.executeJavaScript(action, "Form action", getStartLineNumber());
164164
return;
165165
}
166166
}
167167
else {
168-
if (Strings.CI.startsWith(getActionAttribute(), JavaScriptURLConnection.JAVASCRIPT_PREFIX)) {
168+
if (org.htmlunit.util.StringUtils.startsWithIgnoreCase(getActionAttribute(),
169+
JavaScriptURLConnection.JAVASCRIPT_PREFIX)) {
169170
// The action is JavaScript but JavaScript isn't enabled.
170171
return;
171172
}

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@
4343
import java.util.concurrent.ConcurrentHashMap;
4444

4545
import org.apache.commons.lang3.StringUtils;
46-
import org.apache.commons.lang3.Strings;
4746
import org.apache.commons.logging.Log;
4847
import org.apache.commons.logging.LogFactory;
4948
import org.htmlunit.Cache;
@@ -939,7 +938,8 @@ public ScriptResult executeJavaScript(String sourceCode, final String sourceName
939938
return new ScriptResult(JavaScriptEngine.UNDEFINED);
940939
}
941940

942-
if (Strings.CI.startsWith(sourceCode, JavaScriptURLConnection.JAVASCRIPT_PREFIX)) {
941+
if (org.htmlunit.util.StringUtils.startsWithIgnoreCase(sourceCode,
942+
JavaScriptURLConnection.JAVASCRIPT_PREFIX)) {
943943
sourceCode = sourceCode.substring(JavaScriptURLConnection.JAVASCRIPT_PREFIX.length()).trim();
944944
if (sourceCode.startsWith("return ")) {
945945
sourceCode = sourceCode.substring("return ".length());

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.nio.charset.Charset;
2020

2121
import org.apache.commons.lang3.StringUtils;
22-
import org.apache.commons.lang3.Strings;
2322
import org.apache.commons.logging.Log;
2423
import org.apache.commons.logging.LogFactory;
2524
import org.htmlunit.FailingHttpStatusCodeException;
@@ -298,7 +297,7 @@ public static boolean isJavaScript(String typeAttribute, final String languageAt
298297
}
299298

300299
if (StringUtils.isNotEmpty(languageAttribute)) {
301-
return Strings.CI.startsWith(languageAttribute, "javascript");
300+
return org.htmlunit.util.StringUtils.startsWithIgnoreCase(languageAttribute, "javascript");
302301
}
303302
return true;
304303
}

src/main/java/org/htmlunit/javascript/host/Location.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
import java.net.URL;
2424

2525
import org.apache.commons.lang3.StringUtils;
26-
import org.apache.commons.lang3.Strings;
2726
import org.apache.commons.logging.Log;
2827
import org.apache.commons.logging.LogFactory;
2928
import org.htmlunit.BrowserVersion;
@@ -338,7 +337,7 @@ public String getSearch() {
338337
return "";
339338
}
340339

341-
if (Strings.CI.startsWith(url.getProtocol(), UrlUtils.ABOUT)
340+
if (org.htmlunit.util.StringUtils.startsWithIgnoreCase(url.getProtocol(), UrlUtils.ABOUT)
342341
&& window_.getWebWindow().getWebClient().getBrowserVersion()
343342
.hasFeature(JS_LOCATION_IGNORE_QUERY_FOR_ABOUT_PROTOCOL)) {
344343
return "";

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
import java.util.regex.Matcher;
1818
import java.util.regex.Pattern;
1919

20-
import org.apache.commons.lang3.Strings;
2120
import org.htmlunit.HttpHeader;
2221
import org.htmlunit.WebResponse;
2322

@@ -110,10 +109,10 @@ public static boolean containsMaxAge(final WebResponse response) {
110109
*/
111110
public static boolean containsMaxAgeOrSMaxage(final WebResponse response) {
112111
final String cacheControl = response.getResponseHeaderValue(HttpHeader.CACHE_CONTROL);
113-
if (Strings.CI.contains(cacheControl, CACHE_CONTROL_MAX_AGE)) {
112+
if (StringUtils.containsIgnoreCase(cacheControl, CACHE_CONTROL_MAX_AGE)) {
114113
return true;
115114
}
116-
return Strings.CI.contains(cacheControl, CACHE_CONTROL_S_MAXAGE);
115+
return StringUtils.containsIgnoreCase(cacheControl, CACHE_CONTROL_S_MAXAGE);
117116
}
118117

119118
/**
@@ -153,6 +152,6 @@ private static long directiveValue(final WebResponse response, final Pattern pat
153152

154153
private static boolean containsCacheControlValue(final WebResponse response, final String value) {
155154
final String cacheControl = response.getResponseHeaderValue(HttpHeader.CACHE_CONTROL);
156-
return Strings.CI.contains(cacheControl, value);
155+
return StringUtils.containsIgnoreCase(cacheControl, value);
157156
}
158157
}

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

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,8 +80,10 @@ public static boolean equalsChar(final char expected, final CharSequence s) {
8080
}
8181

8282
/**
83+
* Tests if a CharSequence starts with a specified prefix.
84+
*
8385
* @param s the string to check
84-
* @param expectedStart the string that we expect at the beginning
86+
* @param expectedStart the string that we expect at the beginning (has to be not null and not empty)
8587
* @return true if the provided string has only one char and this matches the expectation
8688
*/
8789
public static boolean startsWithIgnoreCase(final String s, final String expectedStart) {
@@ -99,6 +101,66 @@ public static boolean startsWithIgnoreCase(final String s, final String expected
99101
return s.regionMatches(true, 0, expectedStart, 0, expectedStart.length());
100102
}
101103

104+
/**
105+
* Tests if a CharSequence ends with a specified prefix.
106+
*
107+
* @param s the string to check
108+
* @param expectedEnd the string that we expect at the end (has to be not null and not empty)
109+
* @return true if the provided string has only one char and this matches the expectation
110+
*/
111+
public static boolean endsWithIgnoreCase(final String s, final String expectedEnd) {
112+
if (expectedEnd == null) {
113+
throw new IllegalArgumentException("Expected end string can't be null or empty");
114+
}
115+
116+
final int expectedEndLength = expectedEnd.length();
117+
if (expectedEndLength == 0) {
118+
throw new IllegalArgumentException("Expected end string can't be null or empty");
119+
}
120+
121+
if (s == null) {
122+
return false;
123+
}
124+
if (s == expectedEnd) {
125+
return true;
126+
}
127+
128+
return s.regionMatches(true, s.length() - expectedEndLength, expectedEnd, 0, expectedEndLength);
129+
}
130+
131+
/**
132+
* Tests if a CharSequence ends with a specified prefix.
133+
*
134+
* @param s the string to check
135+
* @param expected the string that we expect to be a substring (has to be not null and not empty)
136+
* @return true if the provided string has only one char and this matches the expectation
137+
*/
138+
public static boolean containsIgnoreCase(final String s, final String expected) {
139+
if (expected == null) {
140+
throw new IllegalArgumentException("Expected string can't be null or empty");
141+
}
142+
143+
final int expectedLength = expected.length();
144+
if (expectedLength == 0) {
145+
throw new IllegalArgumentException("Expected string can't be null or empty");
146+
}
147+
148+
if (s == null) {
149+
return false;
150+
}
151+
if (s == expected) {
152+
return true;
153+
}
154+
155+
final int max = s.length() - expectedLength;
156+
for (int i = 0; i <= max; i++) {
157+
if (s.regionMatches(true, i, expected, 0, expectedLength)) {
158+
return true;
159+
}
160+
}
161+
return false;
162+
}
163+
102164
/**
103165
* Escapes the characters '&lt;', '&gt;' and '&amp;' into their XML entity equivalents.
104166
*

0 commit comments

Comments
 (0)