public final class StringUtil {
private static TimeZone UTC = TimeZone.getTimeZone("UTC");
- private static RE re_newline2br, re_brbr2p, re_mail, re_url, re_tags,
- re_tables, re_forbiddenTags;
+ private static RE re_tags, re_tables, re_forbiddenTags;
private StringUtil() { } // this avoids contruction
static {
try {
//precompile regex
- re_newline2br = new RE("(\r?\n){1}");
- re_brbr2p = new RE("(<br>\r?\n<br>){1,}");
- re_mail = new RE("\\b([a-zA-Z0-9_.-]+)@([a-zA-Z0-9_-]+)\\.([a-zA-Z0-9_.-]+)\\b");
- re_url = new RE("((https://)|(http://)|(ftp://)){1}([a-zA-Z0-9_-]+).([a-zA-Z0-9_.:-]+)/?([^ \t\r\n<>\\)\\]]+[^ \t\r\n.,<>\\)\\]])");
re_tags = new RE("<[^>]*>",RE.REG_ICASE);
re_tables = new RE("<[ \t\r\n/]*(table|td|tr)[ \t\r\n]*>",RE.REG_ICASE);
re_forbiddenTags = new RE("<[ \t\r\n/]*(html|meta|body|head|script)[ \t\r\n]*>",RE.REG_ICASE);
}
/**
- * Wandelt Calendar in einen 12-ziffrigen String um (yyyymmddhhmm)
- * @param theDate
- * @return 12-ziffriger String (yyyymmdd)
- */
-
- public static final String date2webdbDateTime (GregorianCalendar theDate) {
- StringBuffer webdbDate = new StringBuffer();
- webdbDate.append(String.valueOf(theDate.get(Calendar.YEAR)));
- webdbDate.append(pad2(theDate.get(Calendar.MONTH) + 1));
- webdbDate.append(pad2(theDate.get(Calendar.DATE)));
- webdbDate.append(pad2(theDate.get(Calendar.HOUR)));
- webdbDate.append(pad2(theDate.get(Calendar.MINUTE)));
-
- return webdbDate.toString();
- }
-
- /**
* Return a http://www.w3.org/TR/NOTE-datetime formatted date (yyyy-mm-ddThh:mm:ssTZ)
* @param theDate
* @return w3approved datetime
}
/**
- * deleteForbiddenTags
- * this method deletes all <script>, <body> and <head>-tags
- */
- public static final String deleteForbiddenTags(String haystack) {
- return re_forbiddenTags.substituteAll(haystack,"");
- }
-
- /**
- * deleteHTMLTableTags
- * this method deletes all <table>, <tr> and <td>-tags
- */
- public static final String deleteHTMLTableTags(String haystack) {
- return re_tables.substituteAll(haystack,"");
- }
-
- /**
* wandelt eine Datum in einen 8-buchstabigen String, der durch <code>/</code>
* getrennt ist.
*
}
/**
- * Replaces in <code>haystack</code> matching <code>pattern</code> by <code>substitute</code>
- * @param haystack
- * @param pattern
- * @param substitute
- * @return String with replacements.
- */
- public static String regexpReplace(String haystack, String pattern, String substitute) {
- try {
- RE regex = new RE(pattern);
- return regex.substituteAll(haystack,substitute);
- } catch(REException ex){
- return null;
- }
- }
-
- /**
* L?scht <code>/</code> am Ende des Strings, falls vorhanden
* @param path
* @return String ohne <code>/</code> am Ende
/**
- * convertNewline2P ist eine regex-routine zum umwandeln von 2 oder mehr newlines (\n)
- * in den html-tag <p>
- * nur sinnvoll, wenn text nicht im html-format eingegeben
- */
- private static String convertNewline2P(String haystack) {
- return re_brbr2p.substituteAll(haystack,"\n</p><p>");
- }
-
- /**
- * convertNewline2Break ist eine regex-routine zum umwandeln von 1 newline (\n)
- * in den html-tag <br>
- * nur sinnvoll, wenn text nicht im html-format eingegeben
- */
- private static String convertNewline2Break(String haystack) {
- return re_newline2br.substituteAll(haystack,"$0<br />");
- }
-
- /**
- * createMailLinks wandelt text im email-adressenformat
- * in einen klickbaren link um
- * nur sinnvoll, wenn text nicht im html-format eingegeben
- */
- private static String createMailLinks(String haystack) {
- return re_mail.substituteAll(haystack,"<a href=\"mailto:$0\">$0</a>");
- }
-
-
- /**
- * createMailLinks wandelt text im email-adressenformat
- * in einen klickbaren link um
- * nur sinnvoll, wenn text nicht im html-format eingegeben
- */
- private static String createMailLinks(String haystack, String imageRoot, String mailImage) {
- return re_mail.substituteAll(haystack,"<img src=\""+imageRoot+"/"+mailImage+"\" border=\"0\"/> <a href=\"mailto:$0\">$0</a>");
- }
-
-
- /**
- * createURLLinks wandelt text im url-format
- * in einen klickbaren link um
- * nur sinnvoll, wenn text nicht im html-format eingegeben
- */
- private static String createURLLinks(String haystack) {
- return re_url.substituteAll(haystack,"<a href=\"$0\">$0</a>");
- }
-
- /**
- * this routine takes text in url format and makes
- * a clickaeble "<href>" link removing any "illegal" html tags
- * @param haystack the url
- * @param title the href link text
- * @param imageRoot the place to find icons
- * @param extImage the url of the icon to show next to the link
- * @return a String containing the url
- */
- private static String createURLLinks(String haystack, String title, String imageRoot,String extImage) {
- if (title == null) {
- return re_url.substituteAll(haystack,"<img src=\""+imageRoot+"/"+extImage+"\" border=\"0\"/> <a href=\"$0\">$0</a>");
- }
- title = removeHTMLTags(title);
- return re_url.substituteAll(haystack,"<img src=\""+imageRoot+"/"+extImage+"\" border=\"0\"/> <a href=\"$0\">"+title+"</a>");
- }
-
- /**
- * this routine takes text in url format and makes
- * a clickaeble "<href>" link removing any "illegal" html tags
- * @param haystack the url
- * @param imageRoot the place to find icons
- * @param extImage the url of the icon to show next to the link
- * @param intImage unused
- * @return a String containing the url
- */
- private static String createURLLinks(String haystack, String title, String imageRoot,String extImage,String intImage) {
- return createURLLinks(haystack, title, imageRoot, extImage);
- }
-
- /**
- * this method deletes all html tags
- */
- public static String removeHTMLTags(String haystack){
- return re_tags.substituteAll(haystack,"");
- }
-
-
- /**
- * createHTML ruft alle regex-methoden zum unwandeln eines nicht
- * htmlcodierten string auf und returnt einen htmlcodierten String
- */
- public static String createHTML(String content){
- content=convertNewline2Break(content);
- content=convertNewline2P(content);
- content=createMailLinks(content);
- content=createURLLinks(content);
- return content;
- }
-
-
- /**
- * createHTML ruft alle regex-methoden zum unwandeln eines nicht
- * htmlcodierten string auf und returnt einen htmlcodierten String
- */
- public static String createHTML(String content,String producerDocRoot,String mailImage,String extImage,String intImage){
- content=convertNewline2Break(content);
- content=convertNewline2P(content);
- content=createMailLinks(content, producerDocRoot,mailImage);
- content=createURLLinks(content, null, producerDocRoot, extImage, intImage);
-
- return content;
- }
-
- /**
* Converts mir's horrible internal date format (yyyy-MM-dd HH:mm:ss+zz) into a java Date
*
* @param anInternalDate