| | |
| | | import java.util.regex.Matcher; |
| | | import java.util.regex.Pattern; |
| | | |
| | | public abstract class StringUtils |
| | | { |
| | | public abstract class StringUtils { |
| | | public static String ENCODING_US_ASCII = "US-ASCII"; |
| | | public static String ENCODING_ISO_8859_1 = "ISO-8859-1"; |
| | | public static String ENCODING_ISO_8859_2 = "ISO-8859-2"; |
| | |
| | | |
| | | private static final HtmlEncoderFallbackHandler HTML_ENCODER_FALLBACK = new HtmlEncoderFallbackHandler(); |
| | | |
| | | static |
| | | { |
| | | static { |
| | | // Html encoding mapping according to the HTML 4.0 spec |
| | | // http://www.w3.org/TR/REC-html40/sgml/entities.html |
| | | |
| | |
| | | DEFENSIVE_HTML_ENCODE_MAP.put('\u2666', "♦"); |
| | | |
| | | Set<Map.Entry<Character, String>> aggresive_entries = AGGRESSIVE_HTML_ENCODE_MAP.entrySet(); |
| | | for (Map.Entry<Character, String> entry : aggresive_entries) |
| | | { |
| | | for (Map.Entry<Character, String> entry : aggresive_entries) { |
| | | HTML_DECODE_MAP.put(entry.getValue(), entry.getKey()); |
| | | } |
| | | |
| | | Set<Map.Entry<Character, String>> defensive_entries = DEFENSIVE_HTML_ENCODE_MAP.entrySet(); |
| | | for (Map.Entry<Character, String> entry : defensive_entries) |
| | | { |
| | | for (Map.Entry<Character, String> entry : defensive_entries) { |
| | | HTML_DECODE_MAP.put(entry.getValue(), entry.getKey()); |
| | | } |
| | | |
| | |
| | | * @see #encodeRegexp(String) |
| | | * @since 1.0 |
| | | */ |
| | | public static String encodeClassname(String name) |
| | | { |
| | | if (null == name) |
| | | { |
| | | public static String encodeClassname(String name) { |
| | | if (null == name) { |
| | | return null; |
| | | } |
| | | |
| | |
| | | return matcher.replaceAll("_"); |
| | | } |
| | | |
| | | private static boolean needsUrlEncoding(String source) |
| | | { |
| | | if (null == source) |
| | | { |
| | | private static boolean needsUrlEncoding(String source) { |
| | | if (null == source) { |
| | | return false; |
| | | } |
| | | |
| | |
| | | // string is returned as-is |
| | | boolean encode = false; |
| | | char ch; |
| | | for (int i = 0; i < source.length(); i++) |
| | | { |
| | | for (int i = 0; i < source.length(); i++) { |
| | | ch = source.charAt(i); |
| | | |
| | | if (ch >= 'a' && ch <= 'z' || |
| | | ch >= 'A' && ch <= 'Z' || |
| | | ch >= '0' && ch <= '9' || |
| | | ch == '-' || ch == '_' || ch == '.' || ch == '*') |
| | | { |
| | | ch >= 'A' && ch <= 'Z' || |
| | | ch >= '0' && ch <= '9' || |
| | | ch == '-' || ch == '_' || ch == '.' || ch == '*') { |
| | | continue; |
| | | } |
| | | |
| | |
| | | * @see #encodeRegexp(String) |
| | | * @since 1.0 |
| | | */ |
| | | public static String encodeUrl(String source) |
| | | { |
| | | if (!needsUrlEncoding(source)) |
| | | { |
| | | public static String encodeUrl(String source) { |
| | | if (!needsUrlEncoding(source)) { |
| | | return source; |
| | | } |
| | | |
| | | try |
| | | { |
| | | try { |
| | | return URLEncoder.encode(source, ENCODING_ISO_8859_1); |
| | | } |
| | | ///CLOVER:OFF |
| | | catch (UnsupportedEncodingException e) |
| | | { |
| | | catch (UnsupportedEncodingException e) { |
| | | // this should never happen, ISO-8859-1 is a standard encoding |
| | | throw new RuntimeException(e); |
| | | } |
| | |
| | | * @see #encodeRegexp(String) |
| | | * @since 1.0 |
| | | */ |
| | | public static String encodeUrlValue(String source) |
| | | { |
| | | if (!needsUrlEncoding(source)) |
| | | { |
| | | public static String encodeUrlValue(String source) { |
| | | if (!needsUrlEncoding(source)) { |
| | | return source; |
| | | } |
| | | |
| | | // check if the string is valid US-ASCII encoding |
| | | boolean valid = true; |
| | | CharsetEncoder encoder = CHARSET_US_ASCII.newEncoder(); |
| | | try |
| | | { |
| | | try { |
| | | encoder.encode(CharBuffer.wrap(source)); |
| | | } |
| | | catch (CharacterCodingException e) |
| | | { |
| | | catch (CharacterCodingException e) { |
| | | valid = false; |
| | | } |
| | | |
| | | try |
| | | { |
| | | try { |
| | | // if it is valid US-ASCII, use the regular URL encoding method |
| | | if (valid) |
| | | { |
| | | if (valid) { |
| | | return URLEncoder.encode(source, ENCODING_US_ASCII); |
| | | } |
| | | // otherwise, base-64 encode the UTF-8 bytes and mark the string |
| | | // as being encoded in a special way |
| | | else |
| | | { |
| | | else { |
| | | StringBuilder encoded = new StringBuilder("%02%02"); |
| | | String base64 = Base64.encodeToString(source.getBytes(ENCODING_UTF_8), false); |
| | | String base64_urlsafe = replace(base64, "=", "%3D"); |
| | |
| | | } |
| | | } |
| | | ///CLOVER:OFF |
| | | catch (UnsupportedEncodingException e) |
| | | { |
| | | catch (UnsupportedEncodingException e) { |
| | | // this should never happen, ISO-8859-1 is a standard encoding |
| | | throw new RuntimeException(e); |
| | | } |
| | |
| | | * @see #doesUrlValueNeedDecoding(String) |
| | | * @since 1.0 |
| | | */ |
| | | public static String decodeUrlValue(String source) |
| | | { |
| | | try |
| | | { |
| | | public static String decodeUrlValue(String source) { |
| | | try { |
| | | byte[] decoded = Base64.decode(source.substring(2)); |
| | | if (null == decoded) |
| | | { |
| | | if (null == decoded) { |
| | | return null; |
| | | } else |
| | | { |
| | | } else { |
| | | return new String(decoded, StringUtils.ENCODING_UTF_8); |
| | | } |
| | | } |
| | | ///CLOVER:OFF |
| | | catch (UnsupportedEncodingException e) |
| | | { |
| | | catch (UnsupportedEncodingException e) { |
| | | // this should never happen, UTF-8 is a standard encoding |
| | | throw new RuntimeException(e); |
| | | } |
| | |
| | | * @see #decodeUrlValue(String) |
| | | * @since 1.0 |
| | | */ |
| | | public static boolean doesUrlValueNeedDecoding(String source) |
| | | { |
| | | public static boolean doesUrlValueNeedDecoding(String source) { |
| | | if (source != null && |
| | | source.length() > 2 && |
| | | source.startsWith("\u0002\u0002")) |
| | | { |
| | | source.length() > 2 && |
| | | source.startsWith("\u0002\u0002")) { |
| | | return true; |
| | | } |
| | | |
| | | return false; |
| | | } |
| | | |
| | | private static boolean needsHtmlEncoding(String source, boolean defensive) |
| | | { |
| | | if (null == source) |
| | | { |
| | | private static boolean needsHtmlEncoding(String source, boolean defensive) { |
| | | if (null == source) { |
| | | return false; |
| | | } |
| | | |
| | | boolean encode = false; |
| | | char ch; |
| | | for (int i = 0; i < source.length(); i++) |
| | | { |
| | | for (int i = 0; i < source.length(); i++) { |
| | | ch = source.charAt(i); |
| | | |
| | | if ((defensive || (ch != '\u0022' && ch != '\u0026' && ch != '\u003C' && ch != '\u003E')) && |
| | | ch < '\u00A0') |
| | | { |
| | | ch < '\u00A0') { |
| | | continue; |
| | | } |
| | | |
| | |
| | | /** |
| | | * @since 1.6 |
| | | */ |
| | | public static String decodeHtml(String source) |
| | | { |
| | | public static String decodeHtml(String source) { |
| | | if (null == source || |
| | | 0 == source.length()) |
| | | { |
| | | 0 == source.length()) { |
| | | return source; |
| | | } |
| | | |
| | |
| | | |
| | | StringBuilder result = null; |
| | | |
| | | while (current_index <= source.length()) |
| | | { |
| | | while (current_index <= source.length()) { |
| | | delimiter_start_index = source.indexOf('&', current_index); |
| | | if (delimiter_start_index != -1) |
| | | { |
| | | if (delimiter_start_index != -1) { |
| | | delimiter_end_index = source.indexOf(';', delimiter_start_index + 1); |
| | | if (delimiter_end_index != -1) |
| | | { |
| | | if (delimiter_end_index != -1) { |
| | | // ensure that the string builder is setup correctly |
| | | if (null == result) |
| | | { |
| | | if (null == result) { |
| | | result = new StringBuilder(); |
| | | } |
| | | |
| | | // add the text that leads up to this match |
| | | if (delimiter_start_index > current_index) |
| | | { |
| | | if (delimiter_start_index > current_index) { |
| | | result.append(source.substring(current_index, delimiter_start_index)); |
| | | } |
| | | |
| | |
| | | current_index = delimiter_end_index + 1; |
| | | |
| | | // try to decoded numeric entities |
| | | if (entity.charAt(1) == '#') |
| | | { |
| | | if (entity.charAt(1) == '#') { |
| | | int start = 2; |
| | | int radix = 10; |
| | | // check if the number is hexadecimal |
| | | if (entity.charAt(2) == 'X' || entity.charAt(2) == 'x') |
| | | { |
| | | if (entity.charAt(2) == 'X' || entity.charAt(2) == 'x') { |
| | | start++; |
| | | radix = 16; |
| | | } |
| | | try |
| | | { |
| | | try { |
| | | Character c = new Character((char) Integer.parseInt(entity.substring(start, entity.length() - 1), radix)); |
| | | result.append(c); |
| | | } |
| | | // when the number of the entity can't be parsed, add the entity as-is |
| | | catch (NumberFormatException e) |
| | | { |
| | | catch (NumberFormatException e) { |
| | | result.append(entity); |
| | | } |
| | | } else |
| | | { |
| | | } else { |
| | | // try to decode the entity as a literal |
| | | Character decoded = HTML_DECODE_MAP.get(entity); |
| | | if (decoded != null) |
| | | { |
| | | if (decoded != null) { |
| | | result.append(decoded); |
| | | } |
| | | // if there was no match, add the entity as-is |
| | | else |
| | | { |
| | | else { |
| | | result.append(entity); |
| | | } |
| | | } |
| | | } else |
| | | { |
| | | } else { |
| | | break; |
| | | } |
| | | } else |
| | | { |
| | | } else { |
| | | break; |
| | | } |
| | | } |
| | | |
| | | if (null == result) |
| | | { |
| | | if (null == result) { |
| | | return source; |
| | | } else if (current_index < source.length()) |
| | | { |
| | | } else if (current_index < source.length()) { |
| | | result.append(source.substring(current_index)); |
| | | } |
| | | |
| | |
| | | * @see #encodeRegexp(String) |
| | | * @since 1.0 |
| | | */ |
| | | public static String encodeHtml(String source) |
| | | { |
| | | if (needsHtmlEncoding(source, false)) |
| | | { |
| | | public static String encodeHtml(String source) { |
| | | if (needsHtmlEncoding(source, false)) { |
| | | return encode(source, HTML_ENCODER_FALLBACK, AGGRESSIVE_HTML_ENCODE_MAP, DEFENSIVE_HTML_ENCODE_MAP); |
| | | } |
| | | return source; |
| | |
| | | * @see #encodeRegexp(String) |
| | | * @since 1.0 |
| | | */ |
| | | public static String encodeHtmlDefensive(String source) |
| | | { |
| | | if (needsHtmlEncoding(source, true)) |
| | | { |
| | | public static String encodeHtmlDefensive(String source) { |
| | | if (needsHtmlEncoding(source, true)) { |
| | | return encode(source, null, DEFENSIVE_HTML_ENCODE_MAP); |
| | | } |
| | | return source; |
| | |
| | | * @see #encodeRegexp(String) |
| | | * @since 1.0 |
| | | */ |
| | | public static String encodeXml(String source) |
| | | { |
| | | public static String encodeXml(String source) { |
| | | return encode(source, null, XML_ENCODE_MAP); |
| | | } |
| | | |
| | |
| | | * @see #encodeRegexp(String) |
| | | * @since 1.0 |
| | | */ |
| | | public static String encodeString(String source) |
| | | { |
| | | public static String encodeString(String source) { |
| | | return encode(source, null, STRING_ENCODE_MAP); |
| | | } |
| | | |
| | |
| | | * @see #encodeRegexp(String) |
| | | * @since 1.0 |
| | | */ |
| | | public static String encodeUnicode(String source) |
| | | { |
| | | if (null == source) |
| | | { |
| | | public static String encodeUnicode(String source) { |
| | | if (null == source) { |
| | | return null; |
| | | } |
| | | |
| | | StringBuilder encoded = new StringBuilder(); |
| | | String hexstring = null; |
| | | for (int i = 0; i < source.length(); i++) |
| | | { |
| | | for (int i = 0; i < source.length(); i++) { |
| | | hexstring = Integer.toHexString((int) source.charAt(i)).toUpperCase(); |
| | | encoded.append("\\u"); |
| | | // fill with zeros |
| | | for (int j = hexstring.length(); j < 4; j++) |
| | | { |
| | | for (int j = hexstring.length(); j < 4; j++) { |
| | | encoded.append("0"); |
| | | } |
| | | encoded.append(hexstring); |
| | |
| | | * @see #encodeRegexp(String) |
| | | * @since 1.0 |
| | | */ |
| | | public static String encodeSql(String source) |
| | | { |
| | | public static String encodeSql(String source) { |
| | | return encode(source, null, SQL_ENCODE_MAP); |
| | | } |
| | | |
| | |
| | | * @see #encodeRegexp(String) |
| | | * @since 1.0 |
| | | */ |
| | | public static String encodeLatex(String source) |
| | | { |
| | | if (null == source) |
| | | { |
| | | public static String encodeLatex(String source) { |
| | | if (null == source) { |
| | | return null; |
| | | } |
| | | |
| | |
| | | * @return The encoded <code>String</code> object. |
| | | * @since 1.0 |
| | | */ |
| | | private static String encode(String source, EncoderFallbackHandler fallbackHandler, Map<Character, String>... encodingTables) |
| | | { |
| | | if (null == source) |
| | | { |
| | | private static String encode(String source, EncoderFallbackHandler fallbackHandler, Map<Character, String>... encodingTables) { |
| | | if (null == source) { |
| | | return null; |
| | | } |
| | | |
| | | if (null == encodingTables || |
| | | 0 == encodingTables.length) |
| | | { |
| | | 0 == encodingTables.length) { |
| | | return source; |
| | | } |
| | | |
| | |
| | | char[] string_to_encode_array = source.toCharArray(); |
| | | int last_match = -1; |
| | | |
| | | for (int i = 0; i < string_to_encode_array.length; i++) |
| | | { |
| | | for (int i = 0; i < string_to_encode_array.length; i++) { |
| | | char char_to_encode = string_to_encode_array[i]; |
| | | for (Map<Character, String> encoding_table : encodingTables) |
| | | { |
| | | if (encoding_table.containsKey(char_to_encode)) |
| | | { |
| | | for (Map<Character, String> encoding_table : encodingTables) { |
| | | if (encoding_table.containsKey(char_to_encode)) { |
| | | encoded_string = prepareEncodedString(source, encoded_string, i, last_match, string_to_encode_array); |
| | | |
| | | encoded_string.append(encoding_table.get(char_to_encode)); |
| | |
| | | } |
| | | |
| | | if (fallbackHandler != null && |
| | | last_match < i && |
| | | fallbackHandler.hasFallback(char_to_encode)) |
| | | { |
| | | last_match < i && |
| | | fallbackHandler.hasFallback(char_to_encode)) { |
| | | encoded_string = prepareEncodedString(source, encoded_string, i, last_match, string_to_encode_array); |
| | | |
| | | fallbackHandler.appendFallback(encoded_string, char_to_encode); |
| | |
| | | } |
| | | } |
| | | |
| | | if (null == encoded_string) |
| | | { |
| | | if (null == encoded_string) { |
| | | return source; |
| | | } else |
| | | { |
| | | } else { |
| | | int difference = string_to_encode_array.length - (last_match + 1); |
| | | if (difference > 0) |
| | | { |
| | | if (difference > 0) { |
| | | encoded_string.append(string_to_encode_array, last_match + 1, difference); |
| | | } |
| | | return encoded_string.toString(); |
| | | } |
| | | } |
| | | |
| | | private static StringBuilder prepareEncodedString(String source, StringBuilder encodedString, int i, int lastMatch, char[] stringToEncodeArray) |
| | | { |
| | | if (null == encodedString) |
| | | { |
| | | private static StringBuilder prepareEncodedString(String source, StringBuilder encodedString, int i, int lastMatch, char[] stringToEncodeArray) { |
| | | if (null == encodedString) { |
| | | encodedString = new StringBuilder(source.length()); |
| | | } |
| | | |
| | | int difference = i - (lastMatch + 1); |
| | | if (difference > 0) |
| | | { |
| | | if (difference > 0) { |
| | | encodedString.append(stringToEncodeArray, lastMatch + 1, difference); |
| | | } |
| | | |
| | | return encodedString; |
| | | } |
| | | |
| | | private static interface EncoderFallbackHandler |
| | | { |
| | | private static interface EncoderFallbackHandler { |
| | | abstract boolean hasFallback(char character); |
| | | |
| | | abstract void appendFallback(StringBuilder encodedBuffer, char character); |
| | | } |
| | | |
| | | private static class HtmlEncoderFallbackHandler implements EncoderFallbackHandler |
| | | { |
| | | private static class HtmlEncoderFallbackHandler implements EncoderFallbackHandler { |
| | | private final static String PREFIX = "&#"; |
| | | private final static String SUFFIX = ";"; |
| | | |
| | | public boolean hasFallback(char character) |
| | | { |
| | | if (character < '\u00A0') |
| | | { |
| | | public boolean hasFallback(char character) { |
| | | if (character < '\u00A0') { |
| | | return false; |
| | | } |
| | | |
| | | return true; |
| | | } |
| | | |
| | | public void appendFallback(StringBuilder encodedBuffer, char character) |
| | | { |
| | | public void appendFallback(StringBuilder encodedBuffer, char character) { |
| | | encodedBuffer.append(PREFIX); |
| | | encodedBuffer.append((int) character); |
| | | encodedBuffer.append(SUFFIX); |
| | |
| | | * @see #encodeLatex(String) |
| | | * @since 1.3 |
| | | */ |
| | | public static String encodeRegexp(String source) |
| | | { |
| | | public static String encodeRegexp(String source) { |
| | | int regexp_quote_start = source.indexOf("\\E"); |
| | | if (-1 == regexp_quote_start) |
| | | { |
| | | if (-1 == regexp_quote_start) { |
| | | return "\\Q" + source + "\\E"; |
| | | } |
| | | |
| | |
| | | regexp_quote_start = 0; |
| | | |
| | | int current = 0; |
| | | while (-1 == (regexp_quote_start = source.indexOf("\\E", current))) |
| | | { |
| | | while (-1 == (regexp_quote_start = source.indexOf("\\E", current))) { |
| | | buffer.append(source.substring(current, regexp_quote_start)); |
| | | current = regexp_quote_start + 2; |
| | | buffer.append("\\E\\\\E\\Q"); |
| | |
| | | * of the substring. |
| | | * @since 1.0 |
| | | */ |
| | | public static int count(String source, String substring) |
| | | { |
| | | public static int count(String source, String substring) { |
| | | return count(source, substring, true); |
| | | } |
| | | |
| | |
| | | * of the substring. |
| | | * @since 1.0 |
| | | */ |
| | | public static int count(String source, String substring, boolean matchCase) |
| | | { |
| | | if (null == source) |
| | | { |
| | | public static int count(String source, String substring, boolean matchCase) { |
| | | if (null == source) { |
| | | return 0; |
| | | } |
| | | |
| | | if (null == substring) |
| | | { |
| | | if (null == substring) { |
| | | return 0; |
| | | } |
| | | |
| | |
| | | int substring_index = 0; |
| | | int count = 0; |
| | | |
| | | if (!matchCase) |
| | | { |
| | | if (!matchCase) { |
| | | source = source.toLowerCase(); |
| | | substring = substring.toLowerCase(); |
| | | } |
| | | |
| | | while (current_index < source.length() - 1) |
| | | { |
| | | while (current_index < source.length() - 1) { |
| | | substring_index = source.indexOf(substring, current_index); |
| | | |
| | | if (-1 == substring_index) |
| | | { |
| | | if (-1 == substring_index) { |
| | | break; |
| | | } else |
| | | { |
| | | } else { |
| | | current_index = substring_index + substring.length(); |
| | | count++; |
| | | } |
| | |
| | | * <code>String</code> objects. |
| | | * @since 1.0 |
| | | */ |
| | | public static ArrayList<String> split(String source, String seperator) |
| | | { |
| | | public static ArrayList<String> split(String source, String seperator) { |
| | | return split(source, seperator, true); |
| | | } |
| | | |
| | |
| | | * <code>String</code> objects. |
| | | * @since 1.0 |
| | | */ |
| | | public static ArrayList<String> split(String source, String seperator, boolean matchCase) |
| | | { |
| | | public static ArrayList<String> split(String source, String seperator, boolean matchCase) { |
| | | ArrayList<String> substrings = new ArrayList<String>(); |
| | | |
| | | if (null == source) |
| | | { |
| | | if (null == source) { |
| | | return substrings; |
| | | } |
| | | |
| | | if (null == seperator) |
| | | { |
| | | if (null == seperator) { |
| | | substrings.add(source); |
| | | return substrings; |
| | | } |
| | |
| | | String element = null; |
| | | |
| | | String source_lookup_reference = null; |
| | | if (!matchCase) |
| | | { |
| | | if (!matchCase) { |
| | | source_lookup_reference = source.toLowerCase(); |
| | | seperator = seperator.toLowerCase(); |
| | | } else |
| | | { |
| | | } else { |
| | | source_lookup_reference = source; |
| | | } |
| | | |
| | | while (current_index <= source_lookup_reference.length()) |
| | | { |
| | | while (current_index <= source_lookup_reference.length()) { |
| | | delimiter_index = source_lookup_reference.indexOf(seperator, current_index); |
| | | |
| | | if (-1 == delimiter_index) |
| | | { |
| | | if (-1 == delimiter_index) { |
| | | element = new String(source.substring(current_index, source.length())); |
| | | substrings.add(element); |
| | | current_index = source.length() + 1; |
| | | } else |
| | | { |
| | | } else { |
| | | element = new String(source.substring(current_index, delimiter_index)); |
| | | substrings.add(element); |
| | | current_index = delimiter_index + seperator.length(); |
| | |
| | | * @return A <code>String[]</code> array containing the seperated parts. |
| | | * @since 1.0 |
| | | */ |
| | | public static String[] splitToArray(String source, String seperator) |
| | | { |
| | | public static String[] splitToArray(String source, String seperator) { |
| | | return splitToArray(source, seperator, true); |
| | | } |
| | | |
| | |
| | | * @return A <code>String[]</code> array containing the seperated parts. |
| | | * @since 1.0 |
| | | */ |
| | | public static String[] splitToArray(String source, String seperator, boolean matchCase) |
| | | { |
| | | public static String[] splitToArray(String source, String seperator, boolean matchCase) { |
| | | ArrayList<String> substrings = split(source, seperator, matchCase); |
| | | String[] substrings_array = new String[substrings.size()]; |
| | | substrings_array = substrings.toArray(substrings_array); |
| | |
| | | * @return An <code>int[]</code> array containing the seperated parts. |
| | | * @since 1.0 |
| | | */ |
| | | public static int[] splitToIntArray(String source, String seperator) |
| | | { |
| | | public static int[] splitToIntArray(String source, String seperator) { |
| | | return splitToIntArray(source, seperator, true); |
| | | } |
| | | |
| | |
| | | * @return An <code>int[]</code> array containing the seperated parts. |
| | | * @since 1.0 |
| | | */ |
| | | public static int[] splitToIntArray(String source, String seperator, boolean matchCase) |
| | | { |
| | | public static int[] splitToIntArray(String source, String seperator, boolean matchCase) { |
| | | ArrayList<String> string_parts = split(source, seperator, matchCase); |
| | | int number_of_valid_parts = 0; |
| | | |
| | | for (String string_part : string_parts) |
| | | { |
| | | try |
| | | { |
| | | for (String string_part : string_parts) { |
| | | try { |
| | | Integer.parseInt(string_part); |
| | | number_of_valid_parts++; |
| | | } |
| | | catch (NumberFormatException e) |
| | | { |
| | | catch (NumberFormatException e) { |
| | | // just continue |
| | | } |
| | | } |
| | |
| | | int[] string_parts_int = (int[]) Array.newInstance(int.class, number_of_valid_parts); |
| | | int added_parts = 0; |
| | | |
| | | for (String string_part : string_parts) |
| | | { |
| | | try |
| | | { |
| | | for (String string_part : string_parts) { |
| | | try { |
| | | string_parts_int[added_parts] = Integer.parseInt(string_part); |
| | | added_parts++; |
| | | } |
| | | catch (NumberFormatException e) |
| | | { |
| | | catch (NumberFormatException e) { |
| | | // just continue |
| | | } |
| | | } |
| | |
| | | * @return A <code>byte[]</code> array containing the bytes. |
| | | * @since 1.0 |
| | | */ |
| | | public static byte[] splitToByteArray(String source, String seperator) |
| | | { |
| | | public static byte[] splitToByteArray(String source, String seperator) { |
| | | return splitToByteArray(source, seperator, true); |
| | | } |
| | | |
| | |
| | | * @return A <code>byte[]</code> array containing the bytes. |
| | | * @since 1.0 |
| | | */ |
| | | public static byte[] splitToByteArray(String source, String seperator, boolean matchCase) |
| | | { |
| | | public static byte[] splitToByteArray(String source, String seperator, boolean matchCase) { |
| | | ArrayList<String> string_parts = split(source, seperator, matchCase); |
| | | int number_of_valid_parts = 0; |
| | | for (String string_part : string_parts) |
| | | { |
| | | try |
| | | { |
| | | for (String string_part : string_parts) { |
| | | try { |
| | | Byte.parseByte(string_part); |
| | | number_of_valid_parts++; |
| | | } |
| | | catch (NumberFormatException e) |
| | | { |
| | | catch (NumberFormatException e) { |
| | | // just continue |
| | | } |
| | | } |
| | | |
| | | byte[] string_parts_byte = (byte[]) Array.newInstance(byte.class, number_of_valid_parts); |
| | | int added_parts = 0; |
| | | for (String string_part : string_parts) |
| | | { |
| | | try |
| | | { |
| | | for (String string_part : string_parts) { |
| | | try { |
| | | string_parts_byte[added_parts] = Byte.parseByte(string_part); |
| | | added_parts++; |
| | | } |
| | | catch (NumberFormatException e) |
| | | { |
| | | catch (NumberFormatException e) { |
| | | // just continue |
| | | } |
| | | } |
| | |
| | | * @return A new <code>String</code> containing the stripped result. |
| | | * @since 1.0 |
| | | */ |
| | | public static String stripFromFront(String source, String stringToStrip) |
| | | { |
| | | public static String stripFromFront(String source, String stringToStrip) { |
| | | return stripFromFront(source, stringToStrip, true); |
| | | } |
| | | |
| | |
| | | * @return A new <code>String</code> containing the stripping result. |
| | | * @since 1.0 |
| | | */ |
| | | public static String stripFromFront(String source, String stringToStrip, boolean matchCase) |
| | | { |
| | | if (null == source) |
| | | { |
| | | public static String stripFromFront(String source, String stringToStrip, boolean matchCase) { |
| | | if (null == source) { |
| | | return null; |
| | | } |
| | | |
| | | if (null == stringToStrip) |
| | | { |
| | | if (null == stringToStrip) { |
| | | return source; |
| | | } |
| | | |
| | |
| | | int last_index = 0; |
| | | |
| | | String source_lookup_reference = null; |
| | | if (!matchCase) |
| | | { |
| | | if (!matchCase) { |
| | | source_lookup_reference = source.toLowerCase(); |
| | | stringToStrip = stringToStrip.toLowerCase(); |
| | | } else |
| | | { |
| | | } else { |
| | | source_lookup_reference = source; |
| | | } |
| | | |
| | | new_index = source_lookup_reference.indexOf(stringToStrip); |
| | | if (0 == new_index) |
| | | { |
| | | do |
| | | { |
| | | if (0 == new_index) { |
| | | do { |
| | | last_index = new_index; |
| | | new_index = source_lookup_reference.indexOf(stringToStrip, new_index + strip_length); |
| | | } |
| | | while (new_index != -1 && |
| | | new_index == last_index + strip_length); |
| | | new_index == last_index + strip_length); |
| | | |
| | | return source.substring(last_index + strip_length); |
| | | } else |
| | | { |
| | | } else { |
| | | return source; |
| | | } |
| | | } |
| | |
| | | * @return A new <code>String</code> containing the stripped result. |
| | | * @since 1.0 |
| | | */ |
| | | public static String stripFromEnd(String source, String stringToStrip) |
| | | { |
| | | public static String stripFromEnd(String source, String stringToStrip) { |
| | | return stripFromEnd(source, stringToStrip, true); |
| | | } |
| | | |
| | |
| | | * @return A new <code>String</code> containing the stripped result. |
| | | * @since 1.0 |
| | | */ |
| | | public static String stripFromEnd(String source, String stringToStrip, boolean matchCase) |
| | | { |
| | | if (null == source) |
| | | { |
| | | public static String stripFromEnd(String source, String stringToStrip, boolean matchCase) { |
| | | if (null == source) { |
| | | return null; |
| | | } |
| | | |
| | | if (null == stringToStrip) |
| | | { |
| | | if (null == stringToStrip) { |
| | | return source; |
| | | } |
| | | |
| | |
| | | int last_index = 0; |
| | | |
| | | String source_lookup_reference = null; |
| | | if (!matchCase) |
| | | { |
| | | if (!matchCase) { |
| | | source_lookup_reference = source.toLowerCase(); |
| | | stringToStrip = stringToStrip.toLowerCase(); |
| | | } else |
| | | { |
| | | } else { |
| | | source_lookup_reference = source; |
| | | } |
| | | |
| | | new_index = source_lookup_reference.lastIndexOf(stringToStrip); |
| | | if (new_index != -1 && |
| | | source.length() == new_index + strip_length) |
| | | { |
| | | do |
| | | { |
| | | source.length() == new_index + strip_length) { |
| | | do { |
| | | last_index = new_index; |
| | | new_index = source_lookup_reference.lastIndexOf(stringToStrip, last_index - 1); |
| | | } |
| | | while (new_index != -1 && |
| | | new_index == last_index - strip_length); |
| | | new_index == last_index - strip_length); |
| | | |
| | | return source.substring(0, last_index); |
| | | } else |
| | | { |
| | | } else { |
| | | return source; |
| | | } |
| | | } |
| | |
| | | * result. |
| | | * @since 1.0 |
| | | */ |
| | | public static String replace(String source, String stringToReplace, String replacementString) |
| | | { |
| | | public static String replace(String source, String stringToReplace, String replacementString) { |
| | | return replace(source, stringToReplace, replacementString, true); |
| | | } |
| | | |
| | |
| | | * result. |
| | | * @since 1.0 |
| | | */ |
| | | public static String replace(String source, String stringToReplace, String replacementString, boolean matchCase) |
| | | { |
| | | if (null == source) |
| | | { |
| | | public static String replace(String source, String stringToReplace, String replacementString, boolean matchCase) { |
| | | if (null == source) { |
| | | return null; |
| | | } |
| | | |
| | | if (null == stringToReplace) |
| | | { |
| | | if (null == stringToReplace) { |
| | | return source; |
| | | } |
| | | |
| | | if (null == replacementString) |
| | | { |
| | | if (null == replacementString) { |
| | | return source; |
| | | } |
| | | |
| | | Iterator<String> string_parts = split(source, stringToReplace, matchCase).iterator(); |
| | | StringBuilder new_string = new StringBuilder(); |
| | | |
| | | while (string_parts.hasNext()) |
| | | { |
| | | while (string_parts.hasNext()) { |
| | | String string_part = string_parts.next(); |
| | | new_string.append(string_part); |
| | | if (string_parts.hasNext()) |
| | | { |
| | | if (string_parts.hasNext()) { |
| | | new_string.append(replacementString); |
| | | } |
| | | } |
| | |
| | | * concatenation result. |
| | | * @since 1.0 |
| | | */ |
| | | public static String repeat(String source, int count) |
| | | { |
| | | if (null == source) |
| | | { |
| | | public static String repeat(String source, int count) { |
| | | if (null == source) { |
| | | return null; |
| | | } |
| | | |
| | | StringBuilder new_string = new StringBuilder(); |
| | | while (count > 0) |
| | | { |
| | | while (count > 0) { |
| | | new_string.append(source); |
| | | count--; |
| | | } |
| | |
| | | * @return The new <code>String</code> array. |
| | | * @since 1.0 |
| | | */ |
| | | public static String[] toStringArray(Iterator<String> iterator) |
| | | { |
| | | if (null == iterator) |
| | | { |
| | | public static String[] toStringArray(Iterator<String> iterator) { |
| | | if (null == iterator) { |
| | | return new String[0]; |
| | | } |
| | | |
| | | ArrayList<String> strings = new ArrayList<String>(); |
| | | |
| | | while (iterator.hasNext()) |
| | | { |
| | | while (iterator.hasNext()) { |
| | | strings.add(iterator.next()); |
| | | } |
| | | |
| | |
| | | * <code>String</code> array. |
| | | * @since 1.0 |
| | | */ |
| | | public static ArrayList<String> toArrayList(String[] stringArray) |
| | | { |
| | | public static ArrayList<String> toArrayList(String[] stringArray) { |
| | | ArrayList<String> strings = new ArrayList<String>(); |
| | | |
| | | if (null == stringArray) |
| | | { |
| | | if (null == stringArray) { |
| | | return strings; |
| | | } |
| | | |
| | | for (String element : stringArray) |
| | | { |
| | | for (String element : stringArray) { |
| | | strings.add(element); |
| | | } |
| | | |
| | |
| | | * @return A new <code>String</code> with the join result. |
| | | * @since 1.0 |
| | | */ |
| | | public static String join(Collection collection, String seperator) |
| | | { |
| | | if (null == collection) |
| | | { |
| | | public static String join(Collection collection, String seperator) { |
| | | if (null == collection) { |
| | | return null; |
| | | } |
| | | |
| | | if (null == seperator) |
| | | { |
| | | if (null == seperator) { |
| | | seperator = ""; |
| | | } |
| | | |
| | | if (0 == collection.size()) |
| | | { |
| | | if (0 == collection.size()) { |
| | | return ""; |
| | | } else |
| | | { |
| | | } else { |
| | | StringBuilder result = new StringBuilder(); |
| | | for (Object element : collection) |
| | | { |
| | | for (Object element : collection) { |
| | | result.append(String.valueOf(element)); |
| | | result.append(seperator); |
| | | } |
| | |
| | | * @return A new <code>String</code> with the join result. |
| | | * @since 1.0 |
| | | */ |
| | | public static String join(Object[] array, String seperator) |
| | | { |
| | | public static String join(Object[] array, String seperator) { |
| | | return join(array, seperator, null, false); |
| | | } |
| | | |
| | |
| | | * @return A new <code>String</code> with the join result. |
| | | * @since 1.0 |
| | | */ |
| | | public static String join(Object[] array, String seperator, String delimiter) |
| | | { |
| | | public static String join(Object[] array, String seperator, String delimiter) { |
| | | return join(array, seperator, delimiter, false); |
| | | } |
| | | |
| | |
| | | * @return A new <code>String</code> with the join result. |
| | | * @since 1.0 |
| | | */ |
| | | public static String join(Object[] array, String seperator, String delimiter, boolean encodeStrings) |
| | | { |
| | | if (null == array) |
| | | { |
| | | public static String join(Object[] array, String seperator, String delimiter, boolean encodeStrings) { |
| | | if (null == array) { |
| | | return null; |
| | | } |
| | | |
| | | if (null == seperator) |
| | | { |
| | | if (null == seperator) { |
| | | seperator = ""; |
| | | } |
| | | |
| | | if (null == delimiter) |
| | | { |
| | | if (null == delimiter) { |
| | | delimiter = ""; |
| | | } |
| | | |
| | | if (0 == array.length) |
| | | { |
| | | if (0 == array.length) { |
| | | return ""; |
| | | } else |
| | | { |
| | | } else { |
| | | int current_index = 0; |
| | | String array_value = null; |
| | | StringBuilder result = new StringBuilder(); |
| | | while (current_index < array.length - 1) |
| | | { |
| | | if (null == array[current_index]) |
| | | { |
| | | while (current_index < array.length - 1) { |
| | | if (null == array[current_index]) { |
| | | result.append("null"); |
| | | } else |
| | | { |
| | | } else { |
| | | array_value = String.valueOf(array[current_index]); |
| | | if (encodeStrings) |
| | | { |
| | | if (encodeStrings) { |
| | | array_value = encodeString(array_value); |
| | | } |
| | | result.append(delimiter); |
| | |
| | | current_index++; |
| | | } |
| | | |
| | | if (null == array[current_index]) |
| | | { |
| | | if (null == array[current_index]) { |
| | | result.append("null"); |
| | | } else |
| | | { |
| | | } else { |
| | | array_value = String.valueOf(array[current_index]); |
| | | if (encodeStrings) |
| | | { |
| | | if (encodeStrings) { |
| | | array_value = encodeString(array_value); |
| | | } |
| | | result.append(delimiter); |
| | |
| | | * @return A new <code>String</code> with the join result. |
| | | * @since 1.0 |
| | | */ |
| | | public static String join(boolean[] array, String seperator) |
| | | { |
| | | if (null == array) |
| | | { |
| | | public static String join(boolean[] array, String seperator) { |
| | | if (null == array) { |
| | | return null; |
| | | } |
| | | |
| | | if (null == seperator) |
| | | { |
| | | if (null == seperator) { |
| | | seperator = ""; |
| | | } |
| | | |
| | | if (0 == array.length) |
| | | { |
| | | if (0 == array.length) { |
| | | return ""; |
| | | } else |
| | | { |
| | | } else { |
| | | int current_index = 0; |
| | | String result = ""; |
| | | while (current_index < array.length - 1) |
| | | { |
| | | while (current_index < array.length - 1) { |
| | | result = result + array[current_index] + seperator; |
| | | current_index++; |
| | | } |
| | |
| | | * @return A new <code>String</code> with the join result. |
| | | * @since 1.0 |
| | | */ |
| | | public static String join(byte[] array, String seperator) |
| | | { |
| | | if (null == array) |
| | | { |
| | | public static String join(byte[] array, String seperator) { |
| | | if (null == array) { |
| | | return null; |
| | | } |
| | | |
| | | if (null == seperator) |
| | | { |
| | | if (null == seperator) { |
| | | seperator = ""; |
| | | } |
| | | |
| | | if (0 == array.length) |
| | | { |
| | | if (0 == array.length) { |
| | | return ""; |
| | | } else |
| | | { |
| | | } else { |
| | | int current_index = 0; |
| | | String result = ""; |
| | | while (current_index < array.length - 1) |
| | | { |
| | | while (current_index < array.length - 1) { |
| | | result = result + array[current_index] + seperator; |
| | | current_index++; |
| | | } |
| | |
| | | * @return A new <code>String</code> with the join result. |
| | | * @since 1.0 |
| | | */ |
| | | public static String join(double[] array, String seperator) |
| | | { |
| | | if (null == array) |
| | | { |
| | | public static String join(double[] array, String seperator) { |
| | | if (null == array) { |
| | | return null; |
| | | } |
| | | |
| | | if (null == seperator) |
| | | { |
| | | if (null == seperator) { |
| | | seperator = ""; |
| | | } |
| | | |
| | | if (0 == array.length) |
| | | { |
| | | if (0 == array.length) { |
| | | return ""; |
| | | } else |
| | | { |
| | | } else { |
| | | int current_index = 0; |
| | | String result = ""; |
| | | while (current_index < array.length - 1) |
| | | { |
| | | while (current_index < array.length - 1) { |
| | | result = result + array[current_index] + seperator; |
| | | current_index++; |
| | | } |
| | |
| | | * @return A new <code>String</code> with the join result. |
| | | * @since 1.0 |
| | | */ |
| | | public static String join(float[] array, String seperator) |
| | | { |
| | | if (null == array) |
| | | { |
| | | public static String join(float[] array, String seperator) { |
| | | if (null == array) { |
| | | return null; |
| | | } |
| | | |
| | | if (null == seperator) |
| | | { |
| | | if (null == seperator) { |
| | | seperator = ""; |
| | | } |
| | | |
| | | if (0 == array.length) |
| | | { |
| | | if (0 == array.length) { |
| | | return ""; |
| | | } else |
| | | { |
| | | } else { |
| | | int current_index = 0; |
| | | String result = ""; |
| | | while (current_index < array.length - 1) |
| | | { |
| | | while (current_index < array.length - 1) { |
| | | result = result + array[current_index] + seperator; |
| | | current_index++; |
| | | } |
| | |
| | | * @return A new <code>String</code> with the join result. |
| | | * @since 1.0 |
| | | */ |
| | | public static String join(int[] array, String seperator) |
| | | { |
| | | if (null == array) |
| | | { |
| | | public static String join(int[] array, String seperator) { |
| | | if (null == array) { |
| | | return null; |
| | | } |
| | | |
| | | if (null == seperator) |
| | | { |
| | | if (null == seperator) { |
| | | seperator = ""; |
| | | } |
| | | |
| | | if (0 == array.length) |
| | | { |
| | | if (0 == array.length) { |
| | | return ""; |
| | | } else |
| | | { |
| | | } else { |
| | | int current_index = 0; |
| | | String result = ""; |
| | | while (current_index < array.length - 1) |
| | | { |
| | | while (current_index < array.length - 1) { |
| | | result = result + array[current_index] + seperator; |
| | | current_index++; |
| | | } |
| | |
| | | * @return A new <code>String</code> with the join result. |
| | | * @since 1.0 |
| | | */ |
| | | public static String join(long[] array, String seperator) |
| | | { |
| | | if (null == array) |
| | | { |
| | | public static String join(long[] array, String seperator) { |
| | | if (null == array) { |
| | | return null; |
| | | } |
| | | |
| | | if (null == seperator) |
| | | { |
| | | if (null == seperator) { |
| | | seperator = ""; |
| | | } |
| | | |
| | | if (0 == array.length) |
| | | { |
| | | if (0 == array.length) { |
| | | return ""; |
| | | } else |
| | | { |
| | | } else { |
| | | int current_index = 0; |
| | | String result = ""; |
| | | while (current_index < array.length - 1) |
| | | { |
| | | while (current_index < array.length - 1) { |
| | | result = result + array[current_index] + seperator; |
| | | current_index++; |
| | | } |
| | |
| | | * @return A new <code>String</code> with the join result. |
| | | * @since 1.0 |
| | | */ |
| | | public static String join(short[] array, String seperator) |
| | | { |
| | | if (null == array) |
| | | { |
| | | public static String join(short[] array, String seperator) { |
| | | if (null == array) { |
| | | return null; |
| | | } |
| | | |
| | | if (null == seperator) |
| | | { |
| | | if (null == seperator) { |
| | | seperator = ""; |
| | | } |
| | | |
| | | if (0 == array.length) |
| | | { |
| | | if (0 == array.length) { |
| | | return ""; |
| | | } else |
| | | { |
| | | } else { |
| | | int current_index = 0; |
| | | String result = ""; |
| | | while (current_index < array.length - 1) |
| | | { |
| | | while (current_index < array.length - 1) { |
| | | result = result + array[current_index] + seperator; |
| | | current_index++; |
| | | } |
| | |
| | | * @return A new <code>String</code> with the join result. |
| | | * @since 1.0 |
| | | */ |
| | | public static String join(char[] array, String seperator) |
| | | { |
| | | public static String join(char[] array, String seperator) { |
| | | return join(array, seperator, null); |
| | | } |
| | | |
| | |
| | | * @return A new <code>String</code> with the join result. |
| | | * @since 1.0 |
| | | */ |
| | | public static String join(char[] array, String seperator, String delimiter) |
| | | { |
| | | if (null == array) |
| | | { |
| | | public static String join(char[] array, String seperator, String delimiter) { |
| | | if (null == array) { |
| | | return null; |
| | | } |
| | | |
| | | if (null == seperator) |
| | | { |
| | | if (null == seperator) { |
| | | seperator = ""; |
| | | } |
| | | |
| | | if (null == delimiter) |
| | | { |
| | | if (null == delimiter) { |
| | | delimiter = ""; |
| | | } |
| | | |
| | | if (0 == array.length) |
| | | { |
| | | if (0 == array.length) { |
| | | return ""; |
| | | } else |
| | | { |
| | | } else { |
| | | int current_index = 0; |
| | | StringBuilder result = new StringBuilder(); |
| | | while (current_index < array.length - 1) |
| | | { |
| | | while (current_index < array.length - 1) { |
| | | result.append(delimiter); |
| | | result.append(array[current_index]); |
| | | result.append(delimiter); |
| | |
| | | * substring. |
| | | * @since 1.0 |
| | | */ |
| | | public static int[] indicesOf(String source, String substring) |
| | | { |
| | | public static int[] indicesOf(String source, String substring) { |
| | | return indicesOf(source, substring, true); |
| | | } |
| | | |
| | |
| | | * substring. |
| | | * @since 1.0 |
| | | */ |
| | | public static int[] indicesOf(String source, String substring, boolean matchCase) |
| | | { |
| | | public static int[] indicesOf(String source, String substring, boolean matchCase) { |
| | | if (null == source || |
| | | null == substring) |
| | | { |
| | | null == substring) { |
| | | return new int[0]; |
| | | } |
| | | |
| | | String source_lookup_reference = null; |
| | | if (!matchCase) |
| | | { |
| | | if (!matchCase) { |
| | | source_lookup_reference = source.toLowerCase(); |
| | | substring = substring.toLowerCase(); |
| | | } else |
| | | { |
| | | } else { |
| | | source_lookup_reference = source; |
| | | } |
| | | |
| | |
| | | int[] indices = new int[count]; |
| | | int counter = 0; |
| | | |
| | | while (current_index < source.length() - 1) |
| | | { |
| | | while (current_index < source.length() - 1) { |
| | | substring_index = source_lookup_reference.indexOf(substring, current_index); |
| | | |
| | | if (-1 == substring_index) |
| | | { |
| | | if (-1 == substring_index) { |
| | | break; |
| | | } else |
| | | { |
| | | } else { |
| | | current_index = substring_index + substring.length(); |
| | | indices[counter] = substring_index; |
| | | counter++; |
| | |
| | | * <p><code>null</code> if no match could be found. |
| | | * @since 1.0 |
| | | */ |
| | | public static Matcher getMatchingRegexp(String value, Collection<Pattern> regexps) |
| | | { |
| | | public static Matcher getMatchingRegexp(String value, Collection<Pattern> regexps) { |
| | | if (value != null && |
| | | value.length() > 0 && |
| | | regexps != null && |
| | | regexps.size() > 0) |
| | | { |
| | | value.length() > 0 && |
| | | regexps != null && |
| | | regexps.size() > 0) { |
| | | Matcher matcher = null; |
| | | for (Pattern regexp : regexps) |
| | | { |
| | | for (Pattern regexp : regexps) { |
| | | matcher = regexp.matcher(value); |
| | | if (matcher.matches()) |
| | | { |
| | | if (matcher.matches()) { |
| | | return matcher; |
| | | } |
| | | } |
| | |
| | | * <p><code>null</code> if no match could be found. |
| | | * @since 1.0 |
| | | */ |
| | | public static Matcher getRegexpMatch(Collection<String> values, Pattern regexp) |
| | | { |
| | | public static Matcher getRegexpMatch(Collection<String> values, Pattern regexp) { |
| | | if (values != null && |
| | | values.size() > 0 && |
| | | regexp != null) |
| | | { |
| | | values.size() > 0 && |
| | | regexp != null) { |
| | | Matcher matcher = null; |
| | | for (String value : values) |
| | | { |
| | | for (String value : values) { |
| | | matcher = regexp.matcher(value); |
| | | if (matcher.matches()) |
| | | { |
| | | if (matcher.matches()) { |
| | | return matcher; |
| | | } |
| | | } |
| | |
| | | * <p><code>false</code> otherwise. |
| | | * @since 1.0 |
| | | */ |
| | | public static boolean filter(String name, Pattern included, Pattern excluded) |
| | | { |
| | | public static boolean filter(String name, Pattern included, Pattern excluded) { |
| | | Pattern[] included_array = null; |
| | | if (included != null) |
| | | { |
| | | if (included != null) { |
| | | included_array = new Pattern[]{included}; |
| | | } |
| | | |
| | | Pattern[] excluded_array = null; |
| | | if (excluded != null) |
| | | { |
| | | if (excluded != null) { |
| | | excluded_array = new Pattern[]{excluded}; |
| | | } |
| | | |
| | |
| | | * <p><code>false</code> otherwise. |
| | | * @since 1.0 |
| | | */ |
| | | public static boolean filter(String name, Pattern[] included, Pattern[] excluded) |
| | | { |
| | | if (null == name) |
| | | { |
| | | public static boolean filter(String name, Pattern[] included, Pattern[] excluded) { |
| | | if (null == name) { |
| | | return false; |
| | | } |
| | | |
| | | boolean accepted = false; |
| | | |
| | | // retain only the includes |
| | | if (null == included) |
| | | { |
| | | if (null == included) { |
| | | accepted = true; |
| | | } else |
| | | { |
| | | for (Pattern pattern : included) |
| | | { |
| | | } else { |
| | | for (Pattern pattern : included) { |
| | | if (pattern != null && |
| | | pattern.matcher(name).matches()) |
| | | { |
| | | pattern.matcher(name).matches()) { |
| | | accepted = true; |
| | | break; |
| | | } |
| | |
| | | |
| | | // remove the excludes |
| | | if (accepted && |
| | | excluded != null) |
| | | { |
| | | for (Pattern pattern : excluded) |
| | | { |
| | | excluded != null) { |
| | | for (Pattern pattern : excluded) { |
| | | if (pattern != null && |
| | | pattern.matcher(name).matches()) |
| | | { |
| | | pattern.matcher(name).matches()) { |
| | | accepted = false; |
| | | break; |
| | | } |
| | |
| | | * @return The capitalized <code>String</code>. |
| | | * @since 1.0 |
| | | */ |
| | | public static String capitalize(String source) |
| | | { |
| | | if (source == null || source.length() == 0) |
| | | { |
| | | public static String capitalize(String source) { |
| | | if (source == null || source.length() == 0) { |
| | | return source; |
| | | } |
| | | |
| | | if (source.length() > 1 && |
| | | Character.isUpperCase(source.charAt(0))) |
| | | { |
| | | Character.isUpperCase(source.charAt(0))) { |
| | | return source; |
| | | } |
| | | |
| | |
| | | * @return The uncapitalized <code>String</code>. |
| | | * @since 1.5 |
| | | */ |
| | | public static String uncapitalize(String source) |
| | | { |
| | | if (source == null || source.length() == 0) |
| | | { |
| | | public static String uncapitalize(String source) { |
| | | if (source == null || source.length() == 0) { |
| | | return source; |
| | | } |
| | | |
| | | if (source.length() > 1 && |
| | | Character.isLowerCase(source.charAt(0))) |
| | | { |
| | | Character.isLowerCase(source.charAt(0))) { |
| | | return source; |
| | | } |
| | | |
| | |
| | | return new String(chars); |
| | | } |
| | | |
| | | private static String convertUrl(String source, Pattern pattern, boolean shorten, boolean sanitize, boolean no_follow) |
| | | { |
| | | private static String convertUrl(String source, Pattern pattern, boolean shorten, boolean sanitize, boolean no_follow) { |
| | | int max_length = 1024; |
| | | |
| | | String result = source; |
| | | |
| | | Matcher url_matcher = pattern.matcher(source); |
| | | boolean found = url_matcher.find(); |
| | | if (found) |
| | | { |
| | | if (found) { |
| | | String visual_url = null; |
| | | String actual_url = null; |
| | | int last = 0; |
| | | StringBuilder sb = new StringBuilder(); |
| | | do |
| | | { |
| | | do { |
| | | actual_url = url_matcher.group(1); |
| | | if (url_matcher.groupCount() > 1) |
| | | { |
| | | if (url_matcher.groupCount() > 1) { |
| | | visual_url = url_matcher.group(2); |
| | | } else |
| | | { |
| | | } else { |
| | | visual_url = actual_url; |
| | | } |
| | | |
| | | if (sanitize) |
| | | { |
| | | if (sanitize) { |
| | | // defang javascript |
| | | actual_url = StringUtils.replace(actual_url, "javascript:", ""); |
| | | |
| | | // fill in http:// for URLs that don't begin with / |
| | | if ((actual_url.indexOf("://") == -1) && |
| | | (!actual_url.startsWith("/"))) |
| | | { |
| | | (!actual_url.startsWith("/"))) { |
| | | actual_url = "http://" + actual_url; |
| | | } |
| | | } |
| | | |
| | | if (pattern.equals(BBCODE_BAREURL)) |
| | | { |
| | | if (pattern.equals(BBCODE_BAREURL)) { |
| | | sb.append(source.substring(last, url_matcher.start(1))); |
| | | } else |
| | | { |
| | | } else { |
| | | sb.append(source.substring(last, url_matcher.start(0))); |
| | | } |
| | | sb.append("<a href=\""); |
| | | sb.append(actual_url); |
| | | sb.append("\""); |
| | | if (actual_url.startsWith("http://") || |
| | | actual_url.startsWith("https://")) |
| | | { |
| | | actual_url.startsWith("https://")) { |
| | | sb.append(" target=\"_blank\""); |
| | | } |
| | | if (no_follow) |
| | | { |
| | | if (no_follow) { |
| | | sb.append(" rel=\"nofollow\""); |
| | | } |
| | | sb.append(">"); |
| | | if (visual_url.length() <= max_length || !shorten) |
| | | { |
| | | if (visual_url.length() <= max_length || !shorten) { |
| | | sb.append(visual_url); |
| | | } else |
| | | { |
| | | } else { |
| | | String ellipsis = "..."; |
| | | int query_index = visual_url.indexOf("?"); |
| | | |
| | | // hack query string off |
| | | // keep '?' |
| | | if (query_index != -1) |
| | | { |
| | | if (query_index != -1) { |
| | | visual_url = visual_url.substring(0, query_index + 1) + ellipsis; |
| | | } |
| | | |
| | | if (visual_url.length() >= max_length) |
| | | { |
| | | if (visual_url.length() >= max_length) { |
| | | int last_slash = visual_url.lastIndexOf("/"); |
| | | int start_slash = visual_url.indexOf("/", visual_url.indexOf("://") + 3); |
| | | |
| | | if (last_slash != start_slash) |
| | | { |
| | | if (last_slash != start_slash) { |
| | | visual_url = visual_url.substring(0, start_slash + 1) + ellipsis + visual_url.substring(last_slash); |
| | | } |
| | | } |
| | |
| | | } |
| | | sb.append("</a>"); |
| | | |
| | | if (pattern.equals(BBCODE_BAREURL)) |
| | | { |
| | | if (pattern.equals(BBCODE_BAREURL)) { |
| | | last = url_matcher.end(1); |
| | | } else |
| | | { |
| | | } else { |
| | | last = url_matcher.end(0); |
| | | } |
| | | |
| | |
| | | return result; |
| | | } |
| | | |
| | | private static String parseBBCode(String source, boolean shorten, boolean sanitize, boolean convert_bare, boolean no_follow) |
| | | { |
| | | private static String parseBBCode(String source, boolean shorten, boolean sanitize, boolean convert_bare, boolean no_follow) { |
| | | String result = source; |
| | | |
| | | result = StringUtils.replace(result, "[b]", "<b>", false); |
| | |
| | | StringBuilder buffer = new StringBuilder(); |
| | | int startIndex; |
| | | int endIndex; |
| | | while (-1 != (startIndex = resultLowerCopy.indexOf("[*]"))) |
| | | { |
| | | while (-1 != (startIndex = resultLowerCopy.indexOf("[*]"))) { |
| | | int begin = resultLowerCopy.indexOf("[list]", startIndex + 3); |
| | | int end = resultLowerCopy.indexOf("[/list]", startIndex + 3); |
| | | int next = resultLowerCopy.indexOf("[*]", startIndex + 3); // 3 == sizeof [*] |
| | | |
| | | if (begin == -1) |
| | | { |
| | | if (begin == -1) { |
| | | begin = Integer.MAX_VALUE; |
| | | } |
| | | |
| | | if (end == -1) |
| | | { |
| | | if (end == -1) { |
| | | end = Integer.MAX_VALUE; |
| | | } |
| | | |
| | | if (next == -1) |
| | | { |
| | | if (next == -1) { |
| | | next = Integer.MAX_VALUE; |
| | | } |
| | | |
| | | if (next < begin && next < end) |
| | | { |
| | | if (next < begin && next < end) { |
| | | endIndex = next; |
| | | } else if (begin < next && begin < end) |
| | | { |
| | | } else if (begin < next && begin < end) { |
| | | endIndex = begin; |
| | | } else if (end < next && end < begin) |
| | | { |
| | | } else if (end < next && end < begin) { |
| | | endIndex = end; |
| | | } else |
| | | { |
| | | } else { |
| | | endIndex = resultLowerCopy.length(); |
| | | } |
| | | |
| | | buffer |
| | | .append(resultCopy.substring(0, startIndex)) |
| | | .append("<li>") |
| | | .append(resultCopy.substring(startIndex + 3, endIndex)) // 3 == sizeof [*] |
| | | .append("</li>"); |
| | | .append(resultCopy.substring(0, startIndex)) |
| | | .append("<li>") |
| | | .append(resultCopy.substring(startIndex + 3, endIndex)) // 3 == sizeof [*] |
| | | .append("</li>"); |
| | | |
| | | resultCopy = resultCopy.substring(endIndex); |
| | | resultLowerCopy = resultLowerCopy.substring(endIndex); |
| | |
| | | result = convertUrl(result, BBCODE_URL_SHORT, shorten, sanitize, no_follow); |
| | | result = convertUrl(result, BBCODE_URL_LONG, shorten, sanitize, no_follow); |
| | | |
| | | if (convert_bare) |
| | | { |
| | | if (convert_bare) { |
| | | result = convertUrl(result, BBCODE_BAREURL, shorten, sanitize, no_follow); |
| | | } |
| | | |
| | |
| | | * @return The corresponding <code>boolean</code> value. |
| | | * @since 1.0 |
| | | */ |
| | | public static boolean convertToBoolean(String value) |
| | | { |
| | | if (null == value) |
| | | { |
| | | public static boolean convertToBoolean(String value) { |
| | | if (null == value) { |
| | | return false; |
| | | } |
| | | |
| | | if (value.equals("1") || |
| | | value.equalsIgnoreCase("t") || |
| | | value.equalsIgnoreCase("true") || |
| | | value.equalsIgnoreCase("y") || |
| | | value.equalsIgnoreCase("yes") || |
| | | value.equalsIgnoreCase("on")) |
| | | { |
| | | value.equalsIgnoreCase("t") || |
| | | value.equalsIgnoreCase("true") || |
| | | value.equalsIgnoreCase("y") || |
| | | value.equalsIgnoreCase("yes") || |
| | | value.equalsIgnoreCase("on")) { |
| | | return true; |
| | | } |
| | | |
| | |
| | | * replaced tabs. |
| | | * @since 1.0 |
| | | */ |
| | | public static String convertTabsToSpaces(String line, int tabWidth) |
| | | { |
| | | public static String convertTabsToSpaces(String line, int tabWidth) { |
| | | StringBuilder result = new StringBuilder(); |
| | | int tab_index = -1; |
| | | int last_tab_index = 0; |
| | | int added_chars = 0; |
| | | int tab_size; |
| | | while ((tab_index = line.indexOf("\t", last_tab_index)) != -1) |
| | | { |
| | | while ((tab_index = line.indexOf("\t", last_tab_index)) != -1) { |
| | | tab_size = tabWidth - ((tab_index + added_chars) % tabWidth); |
| | | if (0 == tab_size) |
| | | { |
| | | if (0 == tab_size) { |
| | | tab_size = tabWidth; |
| | | } |
| | | added_chars += tab_size - 1; |
| | |
| | | result.append(StringUtils.repeat(" ", tab_size)); |
| | | last_tab_index = tab_index + 1; |
| | | } |
| | | if (0 == last_tab_index) |
| | | { |
| | | if (0 == last_tab_index) { |
| | | return line; |
| | | } else |
| | | { |
| | | } else { |
| | | result.append(line.substring(last_tab_index)); |
| | | } |
| | | |
| | |
| | | * @return The trimmed <code>String</code>. |
| | | * @since 1.0 |
| | | */ |
| | | public static String trim(String source) |
| | | { |
| | | if (source == null || source.length() == 0) |
| | | { |
| | | public static String trim(String source) { |
| | | if (source == null || source.length() == 0) { |
| | | return source; |
| | | } |
| | | |
| | |
| | | * @param width the maximum length of any one line. |
| | | * @return a new String with reformatted as needed. |
| | | */ |
| | | public static String wordWrap(String input, int width, Locale locale) |
| | | { |
| | | public static String wordWrap(String input, int width, Locale locale) { |
| | | // handle invalid input |
| | | if (input == null) |
| | | { |
| | | if (input == null) { |
| | | return ""; |
| | | } else if (width < 5) |
| | | { |
| | | } else if (width < 5) { |
| | | return input; |
| | | } else if (width >= input.length()) |
| | | { |
| | | } else if (width >= input.length()) { |
| | | return input; |
| | | } |
| | | |
| | | // default locale |
| | | if (locale == null) |
| | | { |
| | | if (locale == null) { |
| | | locale = Locale.US; |
| | | } |
| | | |
| | |
| | | String line; |
| | | |
| | | // go over the input string and jump from line to line |
| | | while (current_index <= input.length()) |
| | | { |
| | | while (current_index <= input.length()) { |
| | | // look for the next linebreak |
| | | delimiter_index = input.indexOf(seperator, current_index); |
| | | |
| | | // get the line that corresponds to it |
| | | if (-1 == delimiter_index) |
| | | { |
| | | line = new String(input.substring(current_index, input.length())); |
| | | current_index = input.length() + 1; |
| | | } |
| | | else |
| | | { |
| | | line = new String(input.substring(current_index, delimiter_index)); |
| | | current_index = delimiter_index + seperator.length(); |
| | | } |
| | | if (-1 == delimiter_index) { |
| | | line = new String(input.substring(current_index, input.length())); |
| | | current_index = input.length() + 1; |
| | | } else { |
| | | line = new String(input.substring(current_index, delimiter_index)); |
| | | current_index = delimiter_index + seperator.length(); |
| | | } |
| | | |
| | | // handle the wrapping of the line |
| | | BreakIterator breaks = BreakIterator.getLineInstance(locale); |
| | | breaks.setText(line); |
| | | // handle the wrapping of the line |
| | | BreakIterator breaks = BreakIterator.getLineInstance(locale); |
| | | breaks.setText(line); |
| | | |
| | | int line_start = 0; |
| | | int start = breaks.first(); |
| | | int end = breaks.next(); |
| | | while (end != BreakIterator.DONE) |
| | | { |
| | | // check if the width has been exceeded |
| | | if (end - 1 - line_start >= width) |
| | | { |
| | | boolean break_line = true; |
| | | int line_start = 0; |
| | | int start = breaks.first(); |
| | | int end = breaks.next(); |
| | | while (end != BreakIterator.DONE) { |
| | | // check if the width has been exceeded |
| | | if (end - 1 - line_start >= width) { |
| | | boolean break_line = true; |
| | | |
| | | // first check if the last characters were spaces, |
| | | // if they were and by removing them the width is not |
| | | // exceeded, just continue |
| | | if (Character.isWhitespace(line.charAt(end - 1))) |
| | | { |
| | | for (int j = end - 1; j >= 0; j--) |
| | | { |
| | | if (!Character.isWhitespace(line.charAt(j))) |
| | | { |
| | | if (j - line_start < width) |
| | | { |
| | | break_line = false; |
| | | } |
| | | // first check if the last characters were spaces, |
| | | // if they were and by removing them the width is not |
| | | // exceeded, just continue |
| | | if (Character.isWhitespace(line.charAt(end - 1))) { |
| | | for (int j = end - 1; j >= 0; j--) { |
| | | if (!Character.isWhitespace(line.charAt(j))) { |
| | | if (j - line_start < width) { |
| | | break_line = false; |
| | | } |
| | | |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | break; |
| | | } |
| | | } |
| | | } |
| | | |
| | | if (break_line) |
| | | { |
| | | String line_breaked = line.substring(line_start, start); |
| | | // this can happen with trailing whitespace |
| | | if (line_breaked.length() > width) |
| | | { |
| | | line_breaked = line_breaked.substring(0, width); |
| | | } |
| | | buffer.append(line_breaked); |
| | | if (break_line) { |
| | | String line_breaked = line.substring(line_start, start); |
| | | // this can happen with trailing whitespace |
| | | if (line_breaked.length() > width) { |
| | | line_breaked = line_breaked.substring(0, width); |
| | | } |
| | | buffer.append(line_breaked); |
| | | |
| | | buffer.append("\n"); |
| | | buffer.append("\n"); |
| | | |
| | | line_start = start; |
| | | } |
| | | } |
| | | line_start = start; |
| | | } |
| | | } |
| | | |
| | | start = end; |
| | | end = breaks.next(); |
| | | } |
| | | start = end; |
| | | end = breaks.next(); |
| | | } |
| | | |
| | | if (line_start < line.length()) |
| | | { |
| | | buffer.append(line.substring(line_start)); |
| | | } |
| | | if (line_start < line.length()) { |
| | | buffer.append(line.substring(line_start)); |
| | | } |
| | | |
| | | if (delimiter_index != -1) |
| | | { |
| | | buffer.append("\n"); |
| | | } |
| | | } |
| | | if (delimiter_index != -1) { |
| | | buffer.append("\n"); |
| | | } |
| | | } |
| | | |
| | | return buffer.toString(); |
| | | return buffer.toString(); |
| | | } |
| | | } |