Skip to content

Commit

Permalink
micro optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
rbri committed Nov 12, 2024
1 parent c751592 commit 527b720
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 68 deletions.
7 changes: 2 additions & 5 deletions src/main/java/org/htmlunit/FormEncodingType.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package org.htmlunit;

import java.io.Serializable;
import java.util.Locale;

import org.htmlunit.util.MimeType;

Expand Down Expand Up @@ -59,13 +58,11 @@ public String getName() {
* @return the constant corresponding to the specified name, {@link #URL_ENCODED} if none match.
*/
public static FormEncodingType getInstance(final String name) {
final String lowerCaseName = name.toLowerCase(Locale.ROOT);

if (MULTIPART.getName().equals(lowerCaseName)) {
if (MULTIPART.getName().equalsIgnoreCase(name)) {
return MULTIPART;
}

if (TEXT_PLAIN.getName().equals(lowerCaseName)) {
if (TEXT_PLAIN.getName().equalsIgnoreCase(name)) {
return TEXT_PLAIN;
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/htmlunit/attachment/AttachmentHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,12 @@
package org.htmlunit.attachment;

import java.io.Serializable;
import java.util.Locale;

import org.htmlunit.HttpHeader;
import org.htmlunit.Page;
import org.htmlunit.WebResponse;
import org.htmlunit.util.MimeType;
import org.htmlunit.util.StringUtils;

/**
* <p>A handler for attachments, which represent pages received from the server which contain
Expand Down Expand Up @@ -89,8 +89,8 @@ default boolean isAttachment(final WebResponse response) {
// They treat it as if the Content-Disposition header was set to attachment, and propose a "Save As" dialog.
final String contentType = response.getResponseHeaderValue(HttpHeader.CONTENT_TYPE);
return contentType != null
&& MimeType.APPLICATION_OCTET_STREAM.equals(contentType.toLowerCase(Locale.ROOT));
&& MimeType.APPLICATION_OCTET_STREAM.equalsIgnoreCase(contentType);
}
return disp.toLowerCase(Locale.ROOT).startsWith("attachment");
return StringUtils.startsWithIgnoreCase(disp, "attachment");
}
}
21 changes: 10 additions & 11 deletions src/main/java/org/htmlunit/html/HtmlArea.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import java.net.MalformedURLException;
import java.net.URL;
import java.util.Arrays;
import java.util.Locale;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -219,23 +218,23 @@ public final String getTargetAttribute() {
* @return {@code true} if the point is contained in this area
*/
boolean containsPoint(final int x, final int y) {
final String shape = StringUtils.defaultIfEmpty(getShapeAttribute(), SHAPE_RECT).toLowerCase(Locale.ROOT);
final String shape = StringUtils.defaultIfEmpty(getShapeAttribute(), SHAPE_RECT);

if ("default".equals(shape) && getCoordsAttribute() != null) {
if ("default".equalsIgnoreCase(shape) && getCoordsAttribute() != null) {
return true;
}

if (SHAPE_RECT.equals(shape) && getCoordsAttribute() != null) {
if (SHAPE_RECT.equalsIgnoreCase(shape) && getCoordsAttribute() != null) {
final Shape2D rectangle = parseRect();
return rectangle.contains(x, y);
}

if (SHAPE_CIRCLE.equals(shape) && getCoordsAttribute() != null) {
if (SHAPE_CIRCLE.equalsIgnoreCase(shape) && getCoordsAttribute() != null) {
final Shape2D circle = parseCircle();
return circle.contains(x, y);
}

if (SHAPE_POLY.equals(shape) && getCoordsAttribute() != null) {
if (SHAPE_POLY.equalsIgnoreCase(shape) && getCoordsAttribute() != null) {
final Shape2D path = parsePoly();
return path.contains(x, y);
}
Expand Down Expand Up @@ -362,23 +361,23 @@ public boolean handles(final Event event) {
}

private boolean isEmpty() {
final String shape = StringUtils.defaultIfEmpty(getShapeAttribute(), SHAPE_RECT).toLowerCase(Locale.ROOT);
final String shape = StringUtils.defaultIfEmpty(getShapeAttribute(), SHAPE_RECT);

if ("default".equals(shape) && getCoordsAttribute() != null) {
if ("default".equalsIgnoreCase(shape) && getCoordsAttribute() != null) {
return false;
}

if (SHAPE_RECT.equals(shape) && getCoordsAttribute() != null) {
if (SHAPE_RECT.equalsIgnoreCase(shape) && getCoordsAttribute() != null) {
final Shape2D rectangle = parseRect();
return rectangle.isEmpty();
}

if (SHAPE_CIRCLE.equals(shape) && getCoordsAttribute() != null) {
if (SHAPE_CIRCLE.equalsIgnoreCase(shape) && getCoordsAttribute() != null) {
final Shape2D circle = parseCircle();
return circle.isEmpty();
}

if (SHAPE_POLY.equals(shape) && getCoordsAttribute() != null) {
if (SHAPE_POLY.equalsIgnoreCase(shape) && getCoordsAttribute() != null) {
final Shape2D path = parsePoly();
return path.isEmpty();
}
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/org/htmlunit/html/HtmlButton.java
Original file line number Diff line number Diff line change
Expand Up @@ -259,11 +259,10 @@ public final String getTypeAttribute() {
* @return the normalized type value (submit|reset|button).
*/
public String getType() {
final String type = getTypeAttribute().toLowerCase(Locale.ROOT);
if (TYPE_RESET.equals(type)) {
if (TYPE_RESET.equalsIgnoreCase(type)) {
return TYPE_RESET;
}
if (TYPE_BUTTON.equals(type)) {
if (TYPE_BUTTON.equalsIgnoreCase(type)) {
return TYPE_BUTTON;
}
return TYPE_SUBMIT;
Expand Down
96 changes: 55 additions & 41 deletions src/main/java/org/htmlunit/html/HtmlLink.java
Original file line number Diff line number Diff line change
Expand Up @@ -274,55 +274,57 @@ public void onAllChildrenAddedToPage(final boolean postponed) {
LOG.debug("Link node added: " + asXml());
}

if (!isStyleSheetLink()) {
if (LOG.isDebugEnabled()) {
LOG.debug("Link type '" + getRelAttribute() + "' not supported ("
+ asXml().replaceAll("[\\r\\n]", "") + ").");
final boolean isStyleSheetLink = isStyleSheetLink();

if (isStyleSheetLink) {
final WebClient webClient = getPage().getWebClient();
if (!webClient.getOptions().isCssEnabled()) {
if (LOG.isDebugEnabled()) {
LOG.debug("Stylesheet Link found but ignored because css support is disabled ("
+ asXml().replaceAll("[\\r\\n]", "") + ").");
}
return;
}

return;
}

final WebClient webClient = getPage().getWebClient();
if (!webClient.getOptions().isCssEnabled()) {
if (LOG.isDebugEnabled()) {
LOG.debug("Stylesheet Link found but ignored because css support is disabled ("
+ asXml().replaceAll("[\\r\\n]", "") + ").");
if (!webClient.isJavaScriptEngineEnabled()) {
if (LOG.isDebugEnabled()) {
LOG.debug("Stylesheet Link found but ignored because javascript engine is disabled ("
+ asXml().replaceAll("[\\r\\n]", "") + ").");
}
return;
}
return;
}

if (!webClient.isJavaScriptEngineEnabled()) {
if (LOG.isDebugEnabled()) {
LOG.debug("Stylesheet Link found but ignored because javascript engine is disabled ("
+ asXml().replaceAll("[\\r\\n]", "") + ").");
}
return;
}
final PostponedAction action = new PostponedAction(getPage(), "Loading of link " + this) {
@Override
public void execute() {
final HTMLLinkElement linkElem = HtmlLink.this.getScriptableObject();
// force loading, caching inside the link
linkElem.getSheet();
}
};

final PostponedAction action = new PostponedAction(getPage(), "Loading of link " + this) {
@Override
public void execute() {
final HTMLLinkElement linkElem = HtmlLink.this.getScriptableObject();
// force loading, caching inside the link
linkElem.getSheet();
final AbstractJavaScriptEngine<?> engine = webClient.getJavaScriptEngine();
if (postponed) {
engine.addPostponedAction(action);
}
else {
try {
action.execute();
}
catch (final RuntimeException e) {
throw e;
}
catch (final Exception e) {
throw new RuntimeException(e);
}
}
};

final AbstractJavaScriptEngine<?> engine = webClient.getJavaScriptEngine();
if (postponed) {
engine.addPostponedAction(action);
return;
}
else {
try {
action.execute();
}
catch (final RuntimeException e) {
throw e;
}
catch (final Exception e) {
throw new RuntimeException(e);
}

if (LOG.isDebugEnabled()) {
LOG.debug("Link type '" + getRelAttribute() + "' not supported ("
+ asXml().replaceAll("[\\r\\n]", "") + ").");
}
}

Expand Down Expand Up @@ -350,6 +352,18 @@ public boolean isStyleSheetLink() {
return false;
}

/**
* @return true if the rel attribute is 'modulepreload'
*/
public boolean isModulePreloadLink() {
String rel = getRelAttribute();
if (rel != null) {
rel = rel.toLowerCase(Locale.ROOT);
return ArrayUtils.contains(StringUtils.splitAtBlank(rel), "modulepreload");
}
return false;
}

/**
* <p><span style="color:red">Experimental API: May be changed in next release
* and may not yet work perfectly!</span></p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
import java.nio.charset.Charset;
import java.util.ArrayDeque;
import java.util.Deque;
import java.util.Locale;

import org.apache.commons.lang3.ArrayUtils;
import org.htmlunit.BrowserVersion;
Expand Down Expand Up @@ -712,13 +711,12 @@ public void ignoredStartElement(final QName elem, final XMLAttributes attrs, fin
// when multiple html/body elements are encountered, the attributes of the discarded
// elements are used when not previously defined
if (attrs != null && body_ != null) {
String lp = elem.getLocalpart();
final String lp = elem.getLocalpart();
if (lp != null && lp.length() == 4) {
lp = lp.toLowerCase(Locale.ROOT);
if ("body".equals(lp)) {
if ("body".equalsIgnoreCase(lp)) {
copyAttributes(body_, attrs);
}
else if ("html".equals(lp)) {
else if ("html".equalsIgnoreCase(lp)) {
final DomNode parent = body_.getParentNode();
if (parent instanceof DomElement) {
copyAttributes((DomElement) parent, attrs);
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/org/htmlunit/util/StringUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,26 @@ public static boolean equalsChar(final char expected, final CharSequence s) {
return s != null && s.length() == 1 && expected == s.charAt(0);
}

/**
* @param s the string to check
* @param expectedStart the string that we expect at the beginning
* @return true if the provided string has only one char and this matches the expectation
*/
public static boolean startsWithIgnoreCase(final String s, final String expectedStart) {
if (expectedStart == null || expectedStart.length() == 0) {
throw new IllegalArgumentException("Expected start string can't be null or empty");
}

if (s == null) {
return false;
}
if (s == expectedStart) {
return true;
}

return s.regionMatches(true, 0, expectedStart, 0, expectedStart.length());
}

/**
* Escapes the characters '&lt;', '&gt;' and '&amp;' into their XML entity equivalents. Note that
* sometimes we have to use this method instead of
Expand Down
23 changes: 23 additions & 0 deletions src/test/java/org/htmlunit/util/StringUtilsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package org.htmlunit.util;

import static java.nio.charset.StandardCharsets.UTF_8;
import static org.junit.Assert.assertThrows;

import org.htmlunit.SimpleWebTestCase;
import org.htmlunit.html.impl.Color;
Expand Down Expand Up @@ -55,6 +56,28 @@ public void equalsChar() throws Exception {
assertFalse(StringUtils.equalsChar('#', "# "));
}

/**
* @throws Exception if the test fails
*/
@Test
public void startsWithIgnoreCase() throws Exception {
assertTrue(StringUtils.startsWithIgnoreCase("abcd", "ab"));
assertTrue(StringUtils.startsWithIgnoreCase("abcd", "abcd"));
assertTrue(StringUtils.startsWithIgnoreCase("abcd", "AB"));
assertTrue(StringUtils.startsWithIgnoreCase("Abcd", "abCd"));

assertFalse(StringUtils.startsWithIgnoreCase("AB", "x"));
assertFalse(StringUtils.startsWithIgnoreCase("AB", "xxzOO"));
assertFalse(StringUtils.startsWithIgnoreCase("", "x"));
assertFalse(StringUtils.startsWithIgnoreCase("abcd", "bc"));

assertFalse(StringUtils.startsWithIgnoreCase(null, "x"));

assertThrows(IllegalArgumentException.class, () -> StringUtils.startsWithIgnoreCase("AB", null));
assertThrows(IllegalArgumentException.class, () -> StringUtils.startsWithIgnoreCase("AB", ""));
assertThrows(IllegalArgumentException.class, () -> StringUtils.startsWithIgnoreCase("", ""));
}

/**
* @throws Exception if the test fails
*/
Expand Down

0 comments on commit 527b720

Please sign in to comment.