Skip to content

Commit 69f1290

Browse files
committed
remove dependency to org.apache.commons.lang3.math.NumberUtils
1 parent 3f5b123 commit 69f1290

File tree

7 files changed

+90
-10
lines changed

7 files changed

+90
-10
lines changed

src/changes/changes.xml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,13 @@
77
</properties>
88

99
<body>
10+
<release version="4.19.0" date="November xx, 2025" description="Bugfixes">
11+
<action type="update" dev="rbri">
12+
No longer use org.apache.commons.lang3.math.NumberUtils.
13+
</action>
14+
</release>
15+
16+
1017
<release version="4.18.0" date="October 30, 2025" description="Chrome/Edge 141, Firefox 144, FirefoxESR 140, javascript, Bugfixes">
1118
<action type="update" dev="RhinoTeam">
1219
core-js: replace the NumberToString implementation with the Schubfach algorithm

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@
1818

1919
import java.util.regex.Pattern;
2020

21-
import org.apache.commons.lang3.math.NumberUtils;
2221
import org.htmlunit.html.DomElement;
2322
import org.htmlunit.html.DomNode;
2423
import org.htmlunit.html.HtmlCanvas;
2524
import org.htmlunit.html.HtmlHtml;
25+
import org.htmlunit.util.StringUtils;
2626

2727
/**
2828
* Utilities for css value handling.
@@ -85,7 +85,7 @@ public static String pixelString(final DomElement element, final CssValue value)
8585
* @see #pixelString(DomElement, CssValue)
8686
*/
8787
public static int pixelValue(final String value) {
88-
float i = NumberUtils.toFloat(TO_FLOAT_PATTERN.matcher(value).replaceAll("$1"), 0);
88+
float i = StringUtils.toFloat(TO_FLOAT_PATTERN.matcher(value).replaceAll("$1"), 0);
8989
if (value.length() < 2) {
9090
return Math.round(i);
9191
}
@@ -123,7 +123,7 @@ else if (value.endsWith("pc")) {
123123
private static int pixelValue(final DomElement element,
124124
final String styleValue, final CssValue value, final boolean percentMode) {
125125
if (styleValue.endsWith("%") || (styleValue.isEmpty() && element instanceof HtmlHtml)) {
126-
final float i = NumberUtils.toFloat(TO_FLOAT_PATTERN.matcher(styleValue).replaceAll("$1"), 100);
126+
final float i = StringUtils.toFloat(TO_FLOAT_PATTERN.matcher(styleValue).replaceAll("$1"), 100);
127127

128128
final DomNode parent = element.getParentNode();
129129
final int absoluteValue;

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
import java.util.regex.Pattern;
3939

4040
import org.apache.commons.io.IOUtils;
41-
import org.apache.commons.lang3.math.NumberUtils;
4241
import org.apache.commons.logging.Log;
4342
import org.apache.commons.logging.LogFactory;
4443
import org.htmlunit.BrowserVersion;
@@ -1023,15 +1022,15 @@ private static boolean getNthElement(final String nth, final int index) {
10231022
if (value.length() > 0 && value.charAt(0) == '+') {
10241023
value = value.substring(1);
10251024
}
1026-
denominator = NumberUtils.toInt(value, 1);
1025+
denominator = StringUtils.toInt(value, 1);
10271026
}
10281027
}
10291028

10301029
String value = nth.substring(nIndex + 1).trim();
10311030
if (value.length() > 0 && value.charAt(0) == '+') {
10321031
value = value.substring(1);
10331032
}
1034-
final int numerator = NumberUtils.toInt(value, 0);
1033+
final int numerator = StringUtils.toInt(value, 0);
10351034
if (denominator == 0) {
10361035
return index == numerator && numerator > 0;
10371036
}

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import java.io.IOException;
2222

23-
import org.apache.commons.lang3.math.NumberUtils;
2423
import org.htmlunit.html.DomElement;
2524
import org.htmlunit.html.DomNode;
2625
import org.htmlunit.html.HtmlCheckBoxInput;
@@ -314,7 +313,7 @@ public void setSelectionEnd(final int end) {
314313
@JsxGetter
315314
public int getMaxLength() {
316315
final String attrValue = getDomNodeOrDie().getAttribute("maxLength");
317-
return NumberUtils.toInt(attrValue, -1);
316+
return StringUtils.toInt(attrValue, -1);
318317
}
319318

320319
/**
@@ -333,7 +332,7 @@ public void setMaxLength(final int length) {
333332
@JsxGetter
334333
public int getMinLength() {
335334
final String attrValue = getDomNodeOrDie().getAttribute("minLength");
336-
return NumberUtils.toInt(attrValue, -1);
335+
return StringUtils.toInt(attrValue, -1);
337336
}
338337

339338
/**

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -848,4 +848,38 @@ public static String substringBefore(final String str, final String find) {
848848
}
849849
return str.substring(0, pos);
850850
}
851+
852+
/**
853+
* Tries to converts a {@link String} into an {@code int}, returning a default value if the conversion fails.
854+
* If the string is {@code null}, the default value is returned.
855+
*
856+
* @param str the string to convert, may be null.
857+
* @param defaultValue the default value.
858+
* @return the int represented by the string, or the default if conversion fails or the provides str is {@code null}
859+
*/
860+
public static int toInt(final String str, final int defaultValue) {
861+
try {
862+
return Integer.parseInt(str);
863+
}
864+
catch (final RuntimeException e) {
865+
return defaultValue;
866+
}
867+
}
868+
869+
/**
870+
* Tries to converts a {@link String} into an {@code float}, returning a default value if the conversion fails.
871+
* If the string is {@code null}, the default value is returned.
872+
*
873+
* @param str the string to convert, may be null.
874+
* @param defaultValue the default value.
875+
* @return the float represented by the string, or the default if conversion fails or the provides str is {@code null}
876+
*/
877+
public static float toFloat(final String str, final float defaultValue) {
878+
try {
879+
return Float.parseFloat(str);
880+
}
881+
catch (final RuntimeException e) {
882+
return defaultValue;
883+
}
884+
}
851885
}

src/test/java/org/htmlunit/archunit/ArchitectureTest.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ public class ArchitectureTest {
131131
*/
132132
@ArchTest
133133
public static final ArchRule apacheStringUtilsIsNotEmptyRule = noClasses()
134-
.should().callMethod(org.apache.commons.lang3.StringUtils.class, "isNotEmpty", CharSequence.class);
134+
.should().callMethod(org.apache.commons.lang3.math.NumberUtils.class, "toInt", String.class, Integer.class);
135135

136136
/**
137137
* Do not use org.apache.commons.lang3.Strings.startsWith(CharSequence, CharSequence).
@@ -155,6 +155,13 @@ public class ArchitectureTest {
155155
.should().callMethod(org.apache.commons.lang3.Strings.class, "contains", CharSequence.class, CharSequence.class);
156156

157157

158+
/**
159+
* Do not use org.apache.commons.lang3.math.NumberUtils.
160+
*/
161+
@ArchTest
162+
public static final ArchRule apacheNumberUtilsRule = noClasses()
163+
.should().dependOnClassesThat().haveFullyQualifiedName("org.apache.commons.lang3.math.NumberUtils");
164+
158165
/**
159166
* The jetty websocket stuff is only used by one class.
160167
*/

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,4 +496,38 @@ public void substringBefore() throws Exception {
496496
assertEquals("x", StringUtils.substringBefore("xaba", "a"));
497497
assertEquals(" ", StringUtils.substringBefore(" a", "a"));
498498
}
499+
500+
/**
501+
* @throws Exception if the test fails
502+
*/
503+
@Test
504+
public void toInt() throws Exception {
505+
assertEquals(17, StringUtils.toInt(null, 17));
506+
assertEquals(17, StringUtils.toInt("", 17));
507+
assertEquals(17, StringUtils.toInt(" ", 17));
508+
assertEquals(4, StringUtils.toInt("\t", 4));
509+
assertEquals(4, StringUtils.toInt("two", 4));
510+
511+
assertEquals(21, StringUtils.toInt("21", 4));
512+
assertEquals(-21, StringUtils.toInt("-21", 4));
513+
assertEquals(0, StringUtils.toInt(" 21 ", 0));
514+
assertEquals(0, StringUtils.toInt(" - 21 \t", 0));
515+
}
516+
517+
/**
518+
* @throws Exception if the test fails
519+
*/
520+
@Test
521+
public void toFloat() throws Exception {
522+
assertEquals(17.2f, StringUtils.toFloat(null, 17.2f));
523+
assertEquals(17.2f, StringUtils.toFloat("", 17.2f));
524+
assertEquals(17.2f, StringUtils.toFloat(" ", 17.2f));
525+
assertEquals(4f, StringUtils.toFloat("\t", 4f));
526+
assertEquals(4f, StringUtils.toFloat("two", 4));
527+
528+
assertEquals(21f, StringUtils.toFloat("21", 4.1f));
529+
assertEquals(-21f, StringUtils.toFloat("-21", 4.1f));
530+
assertEquals(21f, StringUtils.toFloat(" 21 ", 0));
531+
assertEquals(0, StringUtils.toFloat(" - 21 \t", 0f));
532+
}
499533
}

0 commit comments

Comments
 (0)