// Copyright (C) 2006 Andreas J. Resch
//
// This program is free software; you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free Software
// Foundation; either version 2 of the License, or (at your option) any later
// version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License along with
// this program; if not, write to the Free Software Foundation, Inc.,
// 51 Franklin St, Fifth Floor, Boston, MA 02110, USA
/*
* StringUtil.java
* Created on Mar 20, 2004
*
* $Author: andi $
* $Date: 2006-07-21 17:56:39 $
*
*/
package at.anzac.util;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.StringTokenizer;
/********************************************************************
* StringUtil is a collection of utility functions that handle strings.
*******************************************************************/
public class StringUtil
{
/** CVS-tag that starts dates */
public final static String CVS_DATE_PREFIX = "$Date: ";
/** CVS-tag that starts revisions */
public final static String CVS_REVISION_PREFIX = "$Revision: ";
/** symbol that concludes all CVS-tags */
public final static String CVS_POSTFIX = " $";
/** date formatter to get norm date */
public static SimpleDateFormat dateFormat=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/*********************************************************************************
* get current date & time as as string in format "yyyy-MM-dd HH:mm:ss".
* @return date & time as string
********************************************************************************/
public static String getDateTime()
{
return dateFormat.format(new Date(System.currentTimeMillis()));
}
/*********************************************************************************
* get a specific date & time as as string in format "yyyy-MM-dd HH:mm:ss".
* @param time date&time to convert to string
* @return date & time as string
********************************************************************************/
public static String getDateTime(long time)
{
return dateFormat.format(new Date(time));
}
/********************************************************************
* get the stacktrace of a {@link Throwable} as {@link String}.
* @param t throwable to get the stacktarce of
* @return stacktrace as string
*******************************************************************/
public static String getStackTrace(Throwable t)
{
StringWriter sw;
sw = new StringWriter();
t.printStackTrace(new PrintWriter(sw));
return sw.toString();
}
/********************************************************************
* extract the date from a substituted CVS date keyword.
* on error an empty string is returned.
* @param cvsDate string containg the CVS date keyword.
* @return date extracted from cvsDate
*******************************************************************/
public static String extractCVSDate(String cvsDate)
{
return extractEnclosedString(cvsDate,CVS_DATE_PREFIX,CVS_POSTFIX).replace('/','-');
}
/********************************************************************
* extract the revision from a substituted CVS revision keyword.
* on error an empty string is returned.
* @param cvsRevision string containg the CVS revision keyword.
* @return revision extracted from cvs revision
*******************************************************************/
public static String extractCVSRevision(String cvsRevision)
{
return extractEnclosedString(cvsRevision,CVS_REVISION_PREFIX,CVS_POSTFIX);
}
/********************************************************************
* extract a substring that is enclosed between a prefix i.e. leading
* string and postfix i.e. trailing string.
* on error an empty string is returned.
* @param str string to extract the substring from
* @param prefix string the precedes the desired sunstring
* @param postfix string the follows the desired string
* @return extracted substring
*******************************************************************/
public static String extractEnclosedString(String str,String prefix,String postfix)
{
int first, last;
if(str == null)
{
return "";
}
first = str.indexOf(prefix);
if(first < 0)
{
return "";
}
first += prefix.length();
if(first >= str.length())
{
return "";
}
last = str.indexOf(postfix, first + 1);
if(last < 0)
{
return "";
}
return str.substring(first, last);
}
/**********************************************************************************
* remove all occurances of certain character from a string.
* @param src string to remove characters from
* @param chars characters to remove
* @return string bereft of all caharcters in chars
*********************************************************************************/
public static String remove(String src, String chars)
{
StringBuffer buff;
StringTokenizer tok;
buff = new StringBuffer();
tok = new StringTokenizer(src, chars);
while(tok.hasMoreTokens())
{
buff.append(tok.nextToken());
}
return buff.toString();
}
/**********************************************************************************
* convert a german formatted number so that it can be parsed by Double and Integer.
* converts numbers like 60.578.648,12 to 60578648.12 or 60.578.648,-- to 60578648.00
* so they can be processed by methods like {@link Integer#parseInt(java.lang.String)}.
* @param number to be reformatted
* @return reformatted number
*********************************************************************************/
public static String convertNumberString(String number)
{
return remove(number, ".").replace(',', '.').replace('-', '0');
}
/**********************************************************************************
* get the first len
characters of a string.
* if the sthe string is shorter than len
the original string is
* returned thus this is like the standard substr(0,len) but it does not produce
* an error when the string is too short.
* @param str string to get a substring from
* @param len number of characters to get
* @return first len
characters or less
*********************************************************************************/
public static String substr(String str,int len)
{
if(str.length()<=len)
{
return str;
}
return str.substring(0,len);
}
/**********************************************************************************
* replace all occurances of a substring.
* @param src string in which to replace
* @param a substring that is to be replaced
* @param b substring to replace with
* @return copy of src where all occurances of a are replaced by b
*********************************************************************************/
public static String replaceAll(String src,String a,String b)
{
int i,lenA,lenB;
String str;
str=src;
lenA=a.length();
lenB=b.length();
i=str.indexOf(a);
while(i>=0)
{
str=str.substring(0,i)+b+str.substring(i+lenA);
i=str.indexOf(a,i+lenB);
}
return str;
}
/**********************************************************************************
* remove all characters from a string that may not be contained in file names.
* @param str string to sanitize
* @return str
with all caharcters, that are not allowed in filenames
* replaced by "_"
*********************************************************************************/
public static String sanitizeFileName(String str)
{
String sane;
sane=replaceAll(str,"/","_");
sane=replaceAll(sane,"\\","_");
sane=replaceAll(sane,"?","_");
sane=replaceAll(sane,"*","_");
sane=replaceAll(sane,"\"","_");
sane=replaceAll(sane,"'","_");
sane=replaceAll(sane,":","_");
sane=replaceAll(sane,"<","_");
sane=replaceAll(sane,">","_");
sane=replaceAll(sane,"|","_");
return sane;
}
}
// ----------------------------------------------------------------------
//
// $Log: StringUtil.java,v $
// Revision 1.13 2006-07-21 17:56:39 andi
// added replaceAll().
// added sanitizeString().
//
// Revision 1.12 2006-02-03 15:03:59 AJR
// added GPL statement.
//
// Revision 1.11 2005/01/05 18:21:07 AJR
// added substr().
//
// Revision 1.10 2004/12/21 17:53:55 AJR
// added getDateTime(long).
//
// Revision 1.9 2004/12/13 13:10:46 EXTCVS
// added getDateTime().
//
// Revision 1.8 2004/11/05 15:47:19 EXTCVS
// added comments.
//
// Revision 1.7 2004/10/03 16:46:41 AJR
// fixed bug in extractEnclosedString().
//
// Revision 1.6 2004/10/03 13:39:08 AJR
// added extractEnclosedString().
// added extractCVSRevision().
//
// Revision 1.5 2004/10/03 10:46:49 AJR
// implemented compliance to stricter compiler settings.
//
// Revision 1.4 2004/08/21 21:19:55 AJR
// added comments.
// convertNumberString now alre replaces ,-- by .00 .
//
// Revision 1.3 2004/08/21 13:24:31 AJR
// added convertNumberString().
// added remove().
//
// Revision 1.2 2004/04/11 11:04:54 AJR
// added extractCVSDate().
//
// Revision 1.1 2004/03/22 16:51:17 AJR
// created.
//
//----------------------------------------------------------------------