001: /***
002: * jwma Java WebMail
003: * Copyright (c) 2000-2003 jwma team
004: *
005: * jwma is free software; you can distribute and use this source
006: * under the terms of the BSD-style license received along with
007: * the distribution.
008: ***/package dtw.webmail.util;
009:
010: import java.util.*;
011: import java.text.SimpleDateFormat;
012: import java.io.File;
013:
014: /**
015: * Class that contains static utility methods to
016: * handle Strings.
017: *
018: * @author Dieter Wimberger
019: * @version 0.9.7 07/02/2003
020: */
021: public class StringUtil {
022:
023: /**
024: * Method that splits a string with delimited fields
025: * into an array of strings.
026: *
027: * @param str String with delimited fields.
028: * @param delim String that represents the delimiter.
029: *
030: * @return the String[] that contains all splitted fields.
031: */
032: public static String[] split(String str, String delim) {
033:
034: StringTokenizer strtok = new StringTokenizer(str, delim);
035: String[] result = new String[strtok.countTokens()];
036:
037: for (int i = 0; i < result.length; i++) {
038: result[i] = strtok.nextToken();
039: }
040:
041: return result;
042: }//split(String,String)
043:
044: /**
045: * Method that joins an array of strings into a string
046: * with delimited fields
047: *
048: * @param items an array of strings.
049: * @param delim String that represents the delimiter.
050: *
051: * @return the String that contains the delimited list.
052: */
053: public static String join(String[] items, String delim) {
054: StringBuffer sbuf = new StringBuffer();
055: for (int i = 0; i < items.length; i++) {
056: sbuf.append(items[i]);
057: if (i < items.length - 1) {
058: sbuf.append(delim);
059: }
060: }
061: return sbuf.toString();
062: }//join
063:
064: /**
065: * This method is used to circumvent a bug (or feature) in the
066: * URLClassLoader, which prevents it from being able to use file
067: * url's with path strings which contain a <tt>..</tt>
068: * Example:
069: * <tt>/usr/local/tomcat/bin/../webapps/webmail/WEB-INF/i18n/</tt>
070: * does not properly work, whereas
071: * <tt>/usr/local/tomcat/webapps/webmail/WEB-INF/i18n/</tt>
072: * which represents the same (for the <tt>File</tt> class it makes
073: * no difference whatsoever), but works.
074: */
075: public static String repairPath(String path) {
076: int idx = -1;
077: String newpath = "";
078:
079: idx = path.indexOf(File.separator + ".." + File.separator);
080: if (idx > 0) {
081: newpath = path.substring(0, idx);
082: newpath = newpath.substring(0, newpath
083: .lastIndexOf(File.separator));
084: newpath += path.substring(idx + 3, path.length());
085: return newpath;
086: } else {
087: return path;
088: }
089: }//repairPath
090:
091: public static boolean contains(String[] strs, String str) {
092: for (int i = 0; i < strs.length; i++) {
093: if (str.equals(strs[i])) {
094: return true;
095: }
096: }
097: return false;
098: }//contains
099:
100: /**
101: * Returns now, as <tt>String</tt> formatted
102: * with <tt>DEFAULT_DATEFORMAT</tt>.
103: *
104: * @return now as formatted <tt>String</tt>.
105: */
106: public static String getFormattedDate() {
107: return DEFAULT_DATEFORMAT.format(new Date());
108: }//getFormattedDate
109:
110: /**
111: * Defines a simple date format, to be used for returning the
112: * formatted date.
113: */
114: public static SimpleDateFormat DEFAULT_DATEFORMAT = new SimpleDateFormat(
115: "E, dd.MMM yyyy hh:mm:ss (z)");
116:
117: }//class StringUtil
|