package de.hsadmin.core.util;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

public class TextUtil {

	private static final DateFormat df = SimpleDateFormat.getDateInstance(DateFormat.SHORT);
	
	public static synchronized String replaceUmlautCharacters(String umlautString) {
		StringBuffer buffer = new StringBuffer();
		for (char c : umlautString.toCharArray()) {
			if (	(c >= 'a' && c <= 'z') ||
					(c >= 'A' && c <= 'Z') ||	
					(c >= '0' && c <= '9') || 	
					(c == ' ') ) {
						buffer.append(c);
			} else {
				switch (c) {
				case 'ä':
					buffer.append("ae");
					break;
				case 'ö':
					buffer.append("oe");
					break;
				case 'ü':
					buffer.append("ue");
					break;
				case 'Ä':
					buffer.append("Ae");
					break;
				case 'Ö':
					buffer.append("Oe");
					break;
				case 'Ü':
					buffer.append("Ue");
					break;
				case 'ß':
					buffer.append("ss");
					break;
				default:
					buffer.append('-');
					break;
				}
			}
		}
		return buffer.toString();
	}

	public static synchronized String format(Date date) {
		return df.format(date);
	}

	public static synchronized Date parse(String dateString) {
		try {
			return df.parse(dateString);
		} catch (ParseException e) {
			try {
				return df.parse("01.01.1970");
			} catch (ParseException e1) {
				// don't care
				return null;
			}
		}
	}

}
