X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=source%2Fmir%2Fmisc%2FStringUtil.java;h=24de3ab6e79595e5bbb8de62866d17e8159ced2f;hb=c6a2204d8d75293256fd17c07e54971d7672359a;hp=d5685fe2c2055957a5d4aa06f912f268e30487be;hpb=5bf2a1fa11c1529a731ab6b4521b8254c1fa100f;p=mir.git
diff --git a/source/mir/misc/StringUtil.java b/source/mir/misc/StringUtil.java
index d5685fe2..24de3ab6 100755
--- a/source/mir/misc/StringUtil.java
+++ b/source/mir/misc/StringUtil.java
@@ -18,6 +18,25 @@ import gnu.regexp.*;
*/
public final class StringUtil {
+ private static RE re_newline2br, re_brbr2p, re_mail, re_url, re_tags;
+
+ private StringUtil() { } // this avoids contruction
+
+ static {
+ try {
+ //precompile regex
+ re_newline2br = new RE("(\r?\n){1}");
+ re_brbr2p = new RE("(
\r?\n
){1,}");
+ re_mail = new RE("([a-zA-Z0-9_.-]+)@([a-zA-Z0-9_-]+).([a-zA-Z0-9_.-]+)");
+ 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);
+ }
+ catch (REException e){
+ System.err.println("FATAL: StringUtil: could not precompile REGEX: "+e.toString());
+ }
+ }
+
+
/**
* Wandelt Datum in einen 8-ziffrigen String um (yyyymmdd)
* @param theDate
@@ -33,6 +52,22 @@ public final class StringUtil {
}
/**
+ * 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();
+ }
+
+ /**
* wandelt Calendar in dd.mm.yyyy / hh.mm um
* @param theDate
* @return String mit (dd.mm.yyyy / hh.mm um)
@@ -62,7 +97,9 @@ public final class StringUtil {
StringBuffer path = new StringBuffer();
path.append("/").append(webdbDate.substring(0, 4));
path.append("/").append(webdbDate.substring(4, 6));
- path.append("/").append(webdbDate.substring(6, 8));
+ path.append("/");
+ //who did this?
+ //path.append("/").append(webdbDate.substring(6, 8));
return path.toString();
}
@@ -81,10 +118,10 @@ public final class StringUtil {
}
- /**
- * converts string from format: yyyy-mm-dd__hh:mm:ss.d
- * to dd.mm.yyyy hh:mm
- */
+ /**
+ * converts string from format: yyyy-mm-dd__hh:mm:ss.d
+ * to dd.mm.yyyy hh:mm
+ */
public static String dateToReadableDate(String date) {
StringBuffer returnDate = new StringBuffer();
if (date!=null) {
@@ -97,6 +134,100 @@ public final class StringUtil {
return returnDate.toString();
}
+ /**
+ * converts string from format: yyyy-mm-dd__hh:mm:ss.d
+ * to yyyy
+ */
+ public static String dateToYear (String date) {
+ StringBuffer returnDate = new StringBuffer();
+ if (date!=null) {
+
+ returnDate.append(date.substring(0,4));
+ }
+ return returnDate.toString();
+ }
+
+ /**
+ * converts string from format: yyyy-mm-dd__hh:mm:ss.d
+ * to [m]m
+ */
+ public static String dateToMonth (String date) {
+ StringBuffer returnDate = new StringBuffer();
+ if (date!=null) {
+ if (!date.substring(5,6).equalsIgnoreCase("0")) returnDate.append(date.substring(5,7));
+ else returnDate.append(date.substring(6,7));
+ }
+ return returnDate.toString();
+ }
+
+ /**
+ * converts string from format: yyyy-mm-dd__hh:mm:ss.d
+ * to [d]d
+ */
+ public static String dateToDayOfMonth (String date) {
+ StringBuffer returnDate = new StringBuffer();
+ if (date!=null) {
+ if (!date.substring(8,9).equalsIgnoreCase("0")) returnDate.append(date.substring(8,10));
+ else returnDate.append(date.substring(9,10));
+ }
+ return returnDate.toString();
+ }
+
+ /**
+ * converts string from format: yyyy-mm-dd__hh:mm:ss.d
+ * to hh:mm
+ */
+ public static String dateToTime (String date) {
+ StringBuffer returnDate = new StringBuffer();
+ if (date!=null) {
+ returnDate.append(date.substring(11,16));
+ }
+ return returnDate.toString();
+ }
+
+ /**
+ * Splits the provided CSV text into a list. stolen wholesale from
+ * from Jakarta Turbine StrinUtils.java -mh
+ *
+ * @param text The CSV list of values to split apart.
+ * @param separator The separator character.
+ * @return The list of values.
+ */
+ public static String[] split(String text, String separator)
+ {
+ StringTokenizer st = new StringTokenizer(text, separator);
+ String[] values = new String[st.countTokens()];
+ int pos = 0;
+ while (st.hasMoreTokens())
+ {
+ values[pos++] = st.nextToken();
+ }
+ return values;
+ }
+
+ /**
+ * Joins the elements of the provided array into a single string
+ * containing a list of CSV elements. Stolen wholesale from Jakarta
+ * Turbine StringUtils.java. -mh
+ *
+ * @param list The list of values to join together.
+ * @param separator The separator character.
+ * @return The CSV text.
+ */
+ public static String join(String[] list, String separator)
+ {
+ StringBuffer csv = new StringBuffer();
+ for (int i = 0; i < list.length; i++)
+ {
+ if (i > 0)
+ {
+ csv.append(separator);
+ }
+ csv.append(list[i]);
+ }
+ return csv.toString();
+ }
+
/**
* schließt einen String in Anführungsszeichen ein, falls er Leerzeichen o.ä. enthält
@@ -119,22 +250,22 @@ public final class StringUtil {
*/
public static String quote(String s)
{
- //String s2 = quote(s, '\'');
-
- //Quickhack
- //Because of '?-Bug in Postgresql-JDBC-Driver
- StringBuffer temp = new StringBuffer();
- for(int i=0;is
*
*/
@@ -194,8 +325,8 @@ public final class StringUtil {
String crlf = System.getProperty("line.separator");
if (!crlf.equals("\n"))
s = replace(s, "\n", crlf);
- // byte[] buf = new byte[s.length()];
- byte[] buf = s.getBytes();
+ // byte[] buf = new byte[s.length()];
+ byte[] buf = s.getBytes();
return buf;
}
@@ -223,6 +354,24 @@ public final class StringUtil {
return buf.toString();
}
+ /**
+ * Ersetzt in String s
das Regexp pattern
durch substitute
+ * @param s
+ * @param pattern
+ * @param substitute
+ * @return String mit den Ersetzungen
+ */
+ 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;
+ }
+ }
+
+
+
/**
* Fügt einen Separator an den Pfad an
@@ -254,6 +403,16 @@ public final class StringUtil {
}
/**
+ * Checks to see if the path is absolute by looking for a leading file
+ * separater
+ * @param path
+ * @return
+ */
+ public static boolean isAbsolutePath (String path) {
+ return path.startsWith(File.separator);
+ }
+
+ /**
* Löscht Slash am Anfang des Strings
* @param path
* @return
@@ -342,7 +501,8 @@ public final class StringUtil {
StringBuffer buf = new StringBuffer();
for(int i=0;i < s.length(); i++ ) {
- /** @todo looks inefficient */
+ /** @todo looks inefficient, to ask for index of every char, in
+ * case of failure it runs to the end.*/
if (s.charAt(i)=='&') {
// convert html to xml-parsable representation
if( s.indexOf( "ö", i ) == i ) {
@@ -379,50 +539,50 @@ public final class StringUtil {
if( s.indexOf( """, i ) == i ) {
buf.append( "ß" ); i += 5;
continue;
- }
+ }
if( s.indexOf( "–", i ) == i ) {
buf.append( "–" ); i += 6;
continue;
- }
- if( s.indexOf( "—", i ) == i ) {
+ }
+ if( s.indexOf( "—", i ) == i ) {
buf.append( "—" ); i += 6;
continue;
- }
- if( s.indexOf( "“", i ) == i ) {
+ }
+ if( s.indexOf( "“", i ) == i ) {
buf.append( "“" ); i += 6;
continue;
- }
- if( s.indexOf( "”", i ) == i ) {
+ }
+ if( s.indexOf( "”", i ) == i ) {
buf.append( "”" ); i += 6;
continue;
- }
- if( s.indexOf( "„", i ) == i ) {
+ }
+ if( s.indexOf( "„", i ) == i ) {
buf.append( "„" ); i += 6;
continue;
- }
+ }
- //looks pretty stupid
- if( s.indexOf( "<", i ) == i ) {
+ //looks pretty stupid
+ if( s.indexOf( "<", i ) == i ) {
buf.append( "<" ); i += 3;
continue;
- }
- if( s.indexOf( ">", i ) == i ) {
+ }
+ if( s.indexOf( ">", i ) == i ) {
buf.append( ">" ); i += 3;
continue;
- }
- if( s.indexOf( "´", i ) == i ) {
+ }
+ if( s.indexOf( "´", i ) == i ) {
buf.append( "´" ); i += 6;
continue;
- }
- if( s.indexOf( " ", i ) == i ) {
+ }
+ if( s.indexOf( " ", i ) == i ) {
buf.append( " " ); i += 5;
continue;
- }
- //has to be the last
- if( s.indexOf( "&", i ) == i ) {
- buf.append( "&" ); i += 0;
- continue;
- }
+ }
+ //has to be the last
+ //if( s.indexOf( "&", i ) == i ) {
+ // buf.append( "&" ); i += 0;
+ // continue;
+ //}
}
// convert umlauts an other special charakters
switch( s.charAt(i) ) {
@@ -512,14 +672,14 @@ public final class StringUtil {
case 'ï': buf.append( "ï" ); break;
case 'Ï': buf.append( "Ï" ); break;
case '«': buf.append( "«" ); break;
- case '»': buf.append( "»" ); break;
- case '\'': buf.append( "´" ); break;
- case '\"': buf.append( """ ); break;
- //case '\u8211': buf.append( "–" ); break;
- //case '\u8212': buf.append( "—" ); break;
- //case '\u8220': buf.append( "“" ); break;
- //case '\u8221': buf.append( "”" ); break;
- //case '\u8222': buf.append( "„" ); break;
+ case '»': buf.append( "»" ); break;
+ case '\'': buf.append( "´" ); break;
+ case '\"': buf.append( """ ); break;
+ //case '\u8211': buf.append( "–" ); break;
+ //case '\u8212': buf.append( "—" ); break;
+ //case '\u8220': buf.append( "“" ); break;
+ //case '\u8221': buf.append( "”" ); break;
+ //case '\u8222': buf.append( "„" ); break;
//case '\"': buf.append( """ ); break;
default: buf.append( s.charAt(i) );
}
@@ -529,51 +689,56 @@ public final class StringUtil {
}
- public static String decodeHTMLinTags(String s){
- StringBuffer buffer = new StringBuffer();
- boolean start = false;
- boolean stop = false;
- int startIndex = 0;
- int stopIndex = 0;
- int temp = 0;
-
- for(int i=0;itext
ab startIndex
* @param text
* @param startIndex
@@ -736,35 +901,6 @@ public final class StringUtil {
return Math.min(i, j);
}
- /**
- * Diese Routine macht aus links in reinem text browsbare links
- * @param text
- * @return Konvertierter String
- */
- public static String makeLinks(String text) {
- int i = 0;
- StringBuffer buf = new StringBuffer(text.length());
- while (true) {
- int j = text.indexOf("http://", i);
- if (j < 0) {
- buf.append(text.substring(i));
- break;
- } else {
- buf.append(text.substring(i, j));
- int k = findEndOfWord(text,j+7);
- String url="";
- if (k<0) url = text.substring(j);
- else url = text.substring(j,k);
-
- buf.append(""+url+"");
- //System.out.println("url mark: " + url);
- i = j+url.length();
- }
- }
- return buf.toString();
-
- }
-
/**
* convertNewline2P ist eine regex-routine zum umwandeln von 2 oder mehr newlines (\n)
@@ -772,12 +908,7 @@ public final class StringUtil {
* nur sinnvoll, wenn text nicht im html-format eingegeben
*/
public static String convertNewline2P(String haystack) {
- try {
- RE regex = new RE("(
\r?\n
){1,}");
- return regex.substituteAll(haystack,"\n
"); - } catch(REException ex){ - return null; - } + return re_brbr2p.substituteAll(haystack,"\n
");
}
/**
@@ -786,12 +917,7 @@ public final class StringUtil {
* nur sinnvoll, wenn text nicht im html-format eingegeben
*/
public static String convertNewline2Break(String haystack) {
- try {
- RE regex = new RE("(\r?\n){1}");
- return regex.substituteAll(haystack,"$0
");
- } catch(REException ex){
- return null;
- }
+ return re_newline2br.substituteAll(haystack,"$0
");
}
/**
@@ -800,12 +926,7 @@ public final class StringUtil {
* nur sinnvoll, wenn text nicht im html-format eingegeben
*/
public static String createMailLinks(String haystack) {
- try {
- RE regex = new RE("([a-zA-Z0-9_.-]+)@([a-zA-Z0-9_-]+).([a-zA-Z0-9_.-]+)");
- return regex.substituteAll(haystack,"$0");
- } catch(REException ex){
- return null;
- }
+ return re_mail.substituteAll(haystack,"$0");
}
@@ -815,12 +936,7 @@ public final class StringUtil {
* nur sinnvoll, wenn text nicht im html-format eingegeben
*/
public static String createMailLinks(String haystack, String imageRoot, String mailImage) {
- try {
- RE regex = new RE("([a-zA-Z0-9_.-]+)@([a-zA-Z0-9_-]+).([a-zA-Z0-9_.-]+)");
- return regex.substituteAll(haystack," $0");
- } catch(REException ex){
- return null;
- }
+ return re_mail.substituteAll(haystack,"
$0");
}
@@ -830,35 +946,77 @@ public final class StringUtil {
* nur sinnvoll, wenn text nicht im html-format eingegeben
*/
public static String createURLLinks(String haystack) {
- try {
- //dieser Ausdruck brauch dringend fachliche Beratung
- RE regex = new RE("((https://)|(http://)|(ftp://))+([a-zA-Z0-9_-]+).([a-zA-Z0-9_.:-]+)/([^ \t\r\n<>]+[^ \t\r\n.,<>])");
- return regex.substituteAll(haystack,"$0");
- } catch(REException ex){
- return null;
- }
+ return re_url.substituteAll(haystack,"$0");
}
/**
- * createURLLinks wandelt text im url-format
- * in einen klickbaren link um
- * nur sinnvoll, wenn text nicht im html-format eingegeben
+ * this routine takes text in url format and makes
+ * a clickaeble "
$0");
- } catch(REException ex){
- return null;
+ public static String createURLLinks(String haystack, String title, String imageRoot,String extImage) {
+ if (title == null) {
+ return re_url.substituteAll(haystack,"
$0");
+ } else {
+ title = removeHTMLTags(title);
+ return re_url.substituteAll(haystack,"
"+title+"");
}
}
/**
+ * this routine takes text in url format and makes
+ * a clickaeble "
"+
+ title+"");
+ } else {
+ return re_url.substituteAll(haystack,
+ " ");
+ }
+ }
+
+
+ /**
* deleteForbiddenTags
* this method deletes all