X-Git-Url: http://erislabs.net/gitweb/?a=blobdiff_plain;f=source%2Fmir%2Fmisc%2FStringUtil.java;h=3daf5005b2d51a36f1f449cbec2c7bc71b4030cc;hb=3beba5fd39a5d60ba5b87064ddf11dea30b78413;hp=5ec18689e6b2633ae66d6b6ba8a4a8fc98f74432;hpb=8563841098b6ab3e6233f61519e58b41dcc30266;p=mir.git diff --git a/source/mir/misc/StringUtil.java b/source/mir/misc/StringUtil.java index 5ec18689..3daf5005 100755 --- a/source/mir/misc/StringUtil.java +++ b/source/mir/misc/StringUtil.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2001, 2002 The Mir-coders group + * Copyright (C) 2001, 2002 The Mir-coders group * * This file is part of Mir. * @@ -18,68 +18,61 @@ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * * In addition, as a special exception, The Mir-coders gives permission to link - * the code of this program with the com.oreilly.servlet library, any library - * licensed under the Apache Software License, The Sun (tm) Java Advanced - * Imaging library (JAI), The Sun JIMI library (or with modified versions of - * the above that use the same license as the above), and distribute linked - * combinations including the two. You must obey the GNU General Public - * License in all respects for all of the code used other than the above - * mentioned libraries. If you modify this file, you may extend this exception - * to your version of the file, but you are not obligated to do so. If you do - * not wish to do so, delete this exception statement from your version. + * the code of this program with any library licensed under the Apache Software License, + * The Sun (tm) Java Advanced Imaging library (JAI), The Sun JIMI library + * (or with modified versions of the above that use the same license as the above), + * and distribute linked combinations including the two. You must obey the + * GNU General Public License in all respects for all of the code used other than + * the above mentioned libraries. If you modify this file, you may extend this + * exception to your version of the file, but you are not obligated to do so. + * If you do not wish to do so, delete this exception statement from your version. */ - package mir.misc; -import java.io.*; -import java.lang.*; -import java.util.*; -import java.text.NumberFormat; -import gnu.regexp.*; +import gnu.regexp.RE; +import gnu.regexp.REException; + +import java.io.File; +import java.text.NumberFormat; +import java.util.Calendar; +import java.util.*; +import java.util.StringTokenizer; /** * Statische Hilfsmethoden zur Stringbehandlung * - * @version $Revision: 1.24 $ $Date: 2002/09/01 22:05:50 $ - * @author $Author: mh $ - * - * $Log: StringUtil.java,v $ - * Revision 1.24 2002/09/01 22:05:50 mh - * Mir goes GPL - * - * Revision 1.23.2.1 2002/09/01 21:31:40 mh - * Mir goes GPL - * - * Revision 1.23 2002/06/28 20:39:37 mh - * added numberformat helper. make webdbDate2readableDate use webdb_create instead. make the order and appearance of it more consistent. cvs macros. and finally code tidying - * + * @version $Id: StringUtil.java,v 1.33.2.1 2003/06/23 15:24:06 zapata Exp $ + * @author mir-coders group * */ 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()); - } - } + private static RE re_newline2br, re_brbr2p, re_mail, re_url, 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("(
\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); + re_tables = new RE("<[ \t\r\n/]*(table|td|tr)[ \t\r\n]*>",RE.REG_ICASE); + re_forbiddenTags = new RE("<[ \t\r\n/]*(body|head|script)[ \t\r\n]*>",RE.REG_ICASE); + } + catch (REException e){ + System.err.println("FATAL: StringUtil: could not precompile REGEX: "+e.toString()); + } + } /** * Formats a number with the specified minimum and maximum number of digits. **/ public static synchronized String zeroPaddingNumber(long value, int minDigits, - int maxDigits) + int maxDigits) { NumberFormat numberFormat = NumberFormat.getInstance(); numberFormat.setMinimumIntegerDigits(minDigits); @@ -87,896 +80,360 @@ public final class StringUtil { return numberFormat.format(value); } - /** - * Wandelt Datum in einen 8-ziffrigen String um (yyyymmdd) - * @param theDate - * @return 8-ziffriger String (yyyymmdd) - */ - - public static final String date2webdbDate (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))); - return webdbDate.toString(); - } - - /** - * 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 - */ - - public static final String date2w3DateTime (GregorianCalendar theDate) { - StringBuffer webdbDate = new StringBuffer(); - webdbDate.append(String.valueOf(theDate.get(Calendar.YEAR))); - webdbDate.append("-"); - webdbDate.append(pad2(theDate.get(Calendar.MONTH) + 1)); - webdbDate.append("-"); - webdbDate.append(pad2(theDate.get(Calendar.DATE))); - webdbDate.append("T"); - webdbDate.append(pad2(theDate.get(Calendar.HOUR))); - webdbDate.append(":"); - webdbDate.append(pad2(theDate.get(Calendar.MINUTE))); - webdbDate.append(":"); - webdbDate.append(pad2(theDate.get(Calendar.SECOND))); - //assumes you are an hour-multiple away from UTC.... - int offset=(theDate.get(Calendar.ZONE_OFFSET)/(60*60*1000)); - if (offset < 0){ - webdbDate.append("-"); - } - else{ - webdbDate.append("+"); - } - webdbDate.append(pad2(Math.abs(offset))); - webdbDate.append(":00"); - return webdbDate.toString(); - } - - /** - * wandelt Calendar in dd.mm.yyyy / hh.mm um - * @param theDate - * @return String mit (dd.mm.yyyy / hh.mm um) - */ - public static String date2readableDateTime (GregorianCalendar theDate) { - String readable = ""; - int hour; - readable += pad2(theDate.get(Calendar.DATE)); - readable += "." + pad2(theDate.get(Calendar.MONTH) + 1); - readable += "." + String.valueOf(theDate.get(Calendar.YEAR)); - hour = theDate.get(Calendar.HOUR); - if (theDate.get(Calendar.AM_PM) == Calendar.PM) - hour += 12; - readable += " / " + pad2(hour); - readable += ":" + pad2(theDate.get(Calendar.MINUTE)); - return readable; - } - - /** - * wandelt eine Datum in einen 8-buchstabigen String, der durch / - * getrennt ist. - * - * @param webdbDate - * @return String mit /yyyy/mm/dd - */ - public static final String webdbDate2path (String webdbDate) { - StringBuffer path = new StringBuffer(); - path.append("/").append(webdbDate.substring(0, 4)); - path.append("/").append(webdbDate.substring(4, 6)); - path.append("/"); - //who did this? - //path.append("/").append(webdbDate.substring(6, 8)); - return path.toString(); - } - - /** - * wandelt Calendar in dd.mm.yyyy um - * - * @param theDate - * @return String mit yyyy.mm.dd - */ - public static final String webdbDate2readableDate (String webdbDate) { - String date = ""; - date += webdbDate.substring(0, 4); - date += "-" + webdbDate.substring(5, 7); - date += "-"+webdbDate.substring(8, 10); - return date; - } - - - /** - * 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) { - - returnDate.append(date.substring(8,10)).append('.'); - returnDate.append(date.substring(5,7)).append('.'); - returnDate.append(date.substring(0,4)).append(' '); - returnDate.append(date.substring(11,16)); - } - return returnDate.toString(); - } - /** - * converts string from format: yyyy-mm-dd__hh:mm:ss.dddddd+TZ - * to yyyy-mm-ddThh:mm:ss+TZ:00 (w3 format for Dublin Core) - */ - public static String webdbdateToDCDate(String date) { - StringBuffer returnDate = new StringBuffer(); - if (date!=null) { - returnDate.append(date.substring(0,10)); - returnDate.append("T"); - returnDate.append(date.substring(11,19)); - //String tzInfo=date.substring(26,29); - //if (tzInfo.equals("+00")){ - //UTC gets a special code in w3 dates - // returnDate.append("Z"); - //} - //else{ - //need to see what a newfoundland postgres - //timestamp looks like before making this robust - // returnDate.append(tzInfo); - // returnDate.append(":00"); - //} - - } - 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; + * Wandelt Datum in einen 8-ziffrigen String um (yyyymmdd) + * @param theDate + * @return 8-ziffriger String (yyyymmdd) + */ + + public static final String date2webdbDate (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))); + return webdbDate.toString(); + } + + /** + * 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 + */ + + public static final String date2w3DateTime (GregorianCalendar theDate) { + StringBuffer webdbDate = new StringBuffer(); + webdbDate.append(String.valueOf(theDate.get(Calendar.YEAR))); + webdbDate.append("-"); + webdbDate.append(pad2(theDate.get(Calendar.MONTH) + 1)); + webdbDate.append("-"); + webdbDate.append(pad2(theDate.get(Calendar.DATE))); + webdbDate.append("T"); + webdbDate.append(pad2(theDate.get(Calendar.HOUR))); + webdbDate.append(":"); + webdbDate.append(pad2(theDate.get(Calendar.MINUTE))); + webdbDate.append(":"); + webdbDate.append(pad2(theDate.get(Calendar.SECOND))); + //assumes you are an hour-multiple away from UTC.... + int offset=(theDate.get(Calendar.ZONE_OFFSET)/(60*60*1000)); + if (offset < 0){ + webdbDate.append("-"); } + else{ + webdbDate.append("+"); + } + webdbDate.append(pad2(Math.abs(offset))); + webdbDate.append(":00"); + return webdbDate.toString(); + } + + /** + * wandelt Calendar in dd.mm.yyyy / hh.mm um + * @param theDate + * @return String mit (dd.mm.yyyy / hh.mm um) + */ + public static String date2readableDateTime (GregorianCalendar theDate) { + String readable = ""; + int hour; + readable += pad2(theDate.get(Calendar.DATE)); + readable += "." + pad2(theDate.get(Calendar.MONTH) + 1); + readable += "." + String.valueOf(theDate.get(Calendar.YEAR)); + hour = theDate.get(Calendar.HOUR); + if (theDate.get(Calendar.AM_PM) == Calendar.PM) + hour += 12; + readable += " / " + pad2(hour); + readable += ":" + pad2(theDate.get(Calendar.MINUTE)); + return readable; + } - /** - * 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(); + /** + * deleteForbiddenTags + * this method deletes all