forked from jhy/jsoup
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Removed dependency on Apache Commons-lang. Jsoup now has no external …
…dependencies.
- Loading branch information
Showing
23 changed files
with
272 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
package org.jsoup.helper; | ||
|
||
import java.util.Collection; | ||
import java.util.Iterator; | ||
|
||
/** | ||
* A minimal String utility class. Designed for interal jsoup use only. | ||
*/ | ||
public final class StringUtil { | ||
// memoised padding up to 10 | ||
private static final String[] padding = {"", " ", " ", " ", " ", " ", " ", " ", " ", " ", " "}; | ||
|
||
/** | ||
* Join a collection of strings by a seperator | ||
* @param strings collection of string objects | ||
* @param sep string to place between strings | ||
* @return joined string | ||
*/ | ||
public static String join(Collection<String> strings, String sep) { | ||
return join(strings.iterator(), sep); | ||
} | ||
|
||
/** | ||
* Join a collection of strings by a seperator | ||
* @param strings iterator of string objects | ||
* @param sep string to place between strings | ||
* @return joined string | ||
*/ | ||
public static String join(Iterator<String> strings, String sep) { | ||
if (!strings.hasNext()) | ||
return ""; | ||
|
||
String start = strings.next(); | ||
if (!strings.hasNext()) // only one, avoid builder | ||
return start; | ||
|
||
StringBuilder sb = new StringBuilder(64).append(start); | ||
while (strings.hasNext()) { | ||
sb.append(sep); | ||
sb.append(strings.next()); | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
/** | ||
* Returns space padding | ||
* @param width amount of padding desired | ||
* @return string of spaces * width | ||
*/ | ||
public static String padding(int width) { | ||
if (width < 0) | ||
throw new IllegalArgumentException("width must be > 0"); | ||
|
||
if (width < padding.length) | ||
return padding[width]; | ||
|
||
char[] out = new char[width]; | ||
for (int i = 0; i < width; i++) | ||
out[i] = ' '; | ||
return String.valueOf(out); | ||
} | ||
|
||
/** | ||
* Tests if a string is blank: null, emtpy, or only whitespace (" ", \r\n, \t, etc) | ||
* @param string string to test | ||
* @return if string is blank | ||
*/ | ||
public static boolean isBlank(String string) { | ||
if (string == null || string.length() == 0) | ||
return true; | ||
|
||
int l = string.length(); | ||
for (int i = 0; i < l; i++) { | ||
if (!Character.isWhitespace(string.codePointAt(i))) | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* Tests if a string is numeric, i.e. contains only digit characters | ||
* @param string string to test | ||
* @return true if only digit chars, false if empty or null or contains non-digit chrs | ||
*/ | ||
public static boolean isNumeric(String string) { | ||
if (string == null || string.length() == 0) | ||
return false; | ||
|
||
int l = string.length(); | ||
for (int i = 0; i < l; i++) { | ||
if (!Character.isDigit(string.codePointAt(i))) | ||
return false; | ||
} | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package org.jsoup.helper; | ||
|
||
import java.util.Collection; | ||
|
||
/** | ||
* Simple validation methods. Designed for jsoup internal use | ||
*/ | ||
public final class Validate { | ||
|
||
/** | ||
* Validates that the obect is not null | ||
* @param obj object to test | ||
*/ | ||
public static void notNull(Object obj) { | ||
if (obj == null) | ||
throw new IllegalArgumentException("Object must not be null"); | ||
} | ||
|
||
/** | ||
* Validates that the object is not null | ||
* @param obj object to test | ||
* @param msg message to output if validation fails | ||
*/ | ||
public static void notNull(Object obj, String msg) { | ||
if (obj == null) | ||
throw new IllegalArgumentException(msg); | ||
} | ||
|
||
/** | ||
* Validates that the value is true | ||
* @param val object to test | ||
*/ | ||
public static void isTrue(boolean val) { | ||
if (!val) | ||
throw new IllegalArgumentException("Must be true"); | ||
} | ||
|
||
/** | ||
* Validates that the value is true | ||
* @param val object to test | ||
* @param msg message to output if validation fails | ||
*/ | ||
public static void isTrue(boolean val, String msg) { | ||
if (!val) | ||
throw new IllegalArgumentException(msg); | ||
} | ||
|
||
/** | ||
* Validates that the array contains no null elements | ||
* @param objects the array to test | ||
*/ | ||
public static void noNullElements(Object[] objects) { | ||
noNullElements(objects, "Array must not contain any null objects"); | ||
} | ||
|
||
/** | ||
* Validates that the array contains no null elements | ||
* @param objects the array to test | ||
* @param msg message to output if validation fails | ||
*/ | ||
public static void noNullElements(Object[] objects, String msg) { | ||
for (Object obj : objects) | ||
if (obj == null) | ||
throw new IllegalArgumentException(msg); | ||
} | ||
|
||
/** | ||
* Validates that the string is not empty | ||
* @param string the string to test | ||
*/ | ||
public static void notEmpty(String string) { | ||
if (string == null || string.length() == 0) | ||
throw new IllegalArgumentException("String must not be empty"); | ||
} | ||
|
||
/** | ||
* Validates that the string is not empty | ||
* @param string the string to test | ||
* @param msg message to output if validation fails | ||
*/ | ||
public static void notEmpty(String string, String msg) { | ||
if (string == null || string.length() == 0) | ||
throw new IllegalArgumentException(msg); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.