/*
* Copyright 2002-2007 the original author or authors.
*
* Licensed under the Apache license, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.internna.iwebmvc.utils;
import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.codec.binary.Hex;
/**
* Miscellaneous {@link String} utility methods.
* @author Jose Noheda
* @since 1.0
*/
public final class StringUtils extends org.springframework.util.StringUtils {
/**
* Default private constructor for a Utility class.
*/
private StringUtils() { }
/**
* Check a Hexadecimal UUID string.
* Example: <pre class="code">StringUtils.isUUID("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0a");</pre>
* @param uuid a string
* @return <code>true</code> if the string is of length 32 and matches [a-zA-Z0-9]
*/
public static boolean isUUID(String uuid) {
return hasText(uuid) ? uuid.matches("[a-zA-Z0-9]{32,32}") : false;
}
/**
* Encode a string as a byte[] trying to generate an hexadecimal array ([0-9a-f]) if possible.
* Example: <pre class="code">StringUtils.encodeHex("0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0a");</pre>
* @param hex a string
* @return if hex matches [a-zA-Z0-9]{32,32} an hexadecimal array. Otherwise just a byte[]
*/
public static byte[] encodeHex(String hex) {
byte[] encoded = null;
if (hasText(hex)) {
try {
encoded = Hex.decodeHex(hex.toCharArray());
} catch (Exception ex) {
encoded = hex.getBytes();
}
}
return encoded;
}
public static String getFilename(String path) {
String res = org.springframework.util.StringUtils.getFilename(path);
if (hasText(res)) {
int separatorIndex = res.lastIndexOf("\\");
if (separatorIndex != -1) res = res.substring(separatorIndex + 1);
}
return res;
}
/**
* Splits a String into three parts breaking it using the provided init and
* end delimiters.
*
* @param string the string to split
* @param initDelimiter the first character to search for
* @param endDelimiter the last character
* @return 3 strings if both delimiters where found
*/
public static List<String> splitBetweenDelimiters(String string, String initDelimiter, String endDelimiter) {
List<String> split = new ArrayList<String>(3);
String[] firstSplit = split(string, initDelimiter);
if (firstSplit != null) {
String[] secondSplit = split(firstSplit[1], endDelimiter);
if (secondSplit != null) {
split.add(firstSplit[0]);
split.add(secondSplit[0]);
split.add(secondSplit[1]);
}
}
return split;
}
/**
* Pads with 0 a positive integer.
*
* @param i any integer
* @return a Hex string padded with 0 if needed
*/
public static String toPaddedHexString(int i) {
String s = Integer.toHexString(i);
if (s.length() % 2 != 0) s = "0" + s;
return s;
}
public static String MD5(String toEncode) throws NoSuchAlgorithmException, UnsupportedEncodingException {
MessageDigest digest = MessageDigest.getInstance("MD5");
digest.update(toEncode.getBytes("ISO-8859-1"));
return ByteUtils.toHex(digest.digest());
}
}