recommited file due to UTF-8 issues
This commit is contained in:
107
util/src/main/java/de/muehlencord/shared/util/StringUtil.java
Normal file
107
util/src/main/java/de/muehlencord/shared/util/StringUtil.java
Normal file
@ -0,0 +1,107 @@
|
|||||||
|
/*
|
||||||
|
* StringUtil.java
|
||||||
|
*
|
||||||
|
* Created on 4. Mai 2007, 12:36
|
||||||
|
*/
|
||||||
|
package de.muehlencord.shared.util;
|
||||||
|
|
||||||
|
import java.text.ParseException;
|
||||||
|
import org.apache.log4j.Logger;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author joern@muehlencord.de
|
||||||
|
*/
|
||||||
|
public abstract class StringUtil {
|
||||||
|
|
||||||
|
/** the logging object */
|
||||||
|
private static final Logger logger = Logger.getLogger(StringUtil.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* returns the given string in ISO-8859-1 encoding
|
||||||
|
*
|
||||||
|
* @param input the input string in UTF-8
|
||||||
|
* @return the converted string in
|
||||||
|
*
|
||||||
|
* @throws StringEncodingException if the string cannot be converted
|
||||||
|
*/
|
||||||
|
public static String getISOString(String input) throws StringEncodingException {
|
||||||
|
try {
|
||||||
|
byte[] b = input.getBytes("UTF-8");
|
||||||
|
return new String(b, "ISO-8859-1");
|
||||||
|
} catch (Exception ex) {
|
||||||
|
logger.debug(getStackTraceString(ex));
|
||||||
|
throw new StringEncodingException("Cannot convert string from UTF-8 to ISO-8859-1. Reason: " + ex.getMessage(), ex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* returns the stackstrace as one string
|
||||||
|
*
|
||||||
|
* @param ex the exception to return the string for
|
||||||
|
* @return the stackstrace as one string
|
||||||
|
*/
|
||||||
|
public static String getStackTraceString(Throwable ex) {
|
||||||
|
String logString = ex.toString() + "\n";
|
||||||
|
StackTraceElement[] stack = ex.getStackTrace();
|
||||||
|
for (int i = 0; i < stack.length; i++) {
|
||||||
|
logString += (stack[i].toString()) + "\n";
|
||||||
|
}
|
||||||
|
return logString;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* fills s with blanks if s < length
|
||||||
|
*
|
||||||
|
*
|
||||||
|
*
|
||||||
|
|
||||||
|
*
|
||||||
|
* @param length the needed length for this field
|
||||||
|
* @param s the field to extend with blanks
|
||||||
|
* @return s extended by trainling blanks. s.length == length
|
||||||
|
*/
|
||||||
|
public static String getBlankedString(int length, String s) {
|
||||||
|
if (s == null) {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
String returnValue = s;
|
||||||
|
while (s.length() < length) {
|
||||||
|
returnValue += " ";
|
||||||
|
}
|
||||||
|
return returnValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* returns the string located between the two given keywords
|
||||||
|
* @param content the string to get the value from
|
||||||
|
* @param keyWord1 the starting keyword
|
||||||
|
* @param keyWord2 the end keywod
|
||||||
|
* @return the string between keyword1 and keyword2
|
||||||
|
* @throws ParseException if the value cannot be determined - e.g. if one of the keywords is not found
|
||||||
|
*/
|
||||||
|
public static String getValueBetweenKeywords(String content, String keyWord1, String keyWord2) throws ParseException {
|
||||||
|
int pos1 = content.indexOf(keyWord1);
|
||||||
|
if (pos1 == -1) {
|
||||||
|
throw new ParseException("Keyword1=" + keyWord1 + " not found in content string",0);
|
||||||
|
}
|
||||||
|
|
||||||
|
int pos2 = content.indexOf(keyWord2, pos1);
|
||||||
|
if (pos2 == -1) {
|
||||||
|
throw new ParseException("Keyword2=" + keyWord2 + " not found in content string",0);
|
||||||
|
}
|
||||||
|
String returnValue = content.substring(pos1+keyWord1.length(), pos2);
|
||||||
|
returnValue = returnValue.trim();
|
||||||
|
return returnValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* returns true, if given string is either null or a blank string
|
||||||
|
* @param s the string to check
|
||||||
|
* @return true, if s is either null or s.equals("")
|
||||||
|
*/
|
||||||
|
public static boolean isEmpty(String s) {
|
||||||
|
return (s != null) && (!s.equals (""));
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user