001 /*
002 * kiuruString.java
003 *
004 * Created on 27. marraskuuta 2002, 17:15
005 */
006
007 package kiurubeans;
008 import kotkabeans.*;
009 import java.util.StringTokenizer;
010 import java.util.ArrayList;
011 /** Contains useful String handling routines.
012 * @author jusmaki
013 */
014 public class KiuruString {
015
016 /** Creates a new instance of KiuruString */
017 public KiuruString() {
018 }
019
020
021 /** Converts a comma separated string into an array of objects.
022 * @return (actual) string array of tokens
023 * @param csvString String that is going to be separated.
024 * @param separator Separator of the tokens.
025 */
026 public static Object[] csvToArray(String csvString,String separator) {
027 if ((csvString==null) || (csvString.length()==0)) return null;
028 StringTokenizer sT = new StringTokenizer(csvString,separator);
029 ArrayList result = new ArrayList(10);
030 while(sT.hasMoreElements()) {
031 result.add(sT.nextElement());
032 }
033 Object[] res = result.toArray();
034 return res;
035 }
036
037 /** Converts a comma separated string into a separated string with another separator
038 * @return Separated string containing tokens separated with new separators.
039 * @param CSVString String to be converted
040 * @param separator New separator
041 */
042 public static String CSVToGeneralCSV(String CSVString,String separator) {
043 if ((CSVString==null) || (CSVString.length()==0)) return "";
044 StringTokenizer sT = new StringTokenizer(CSVString,",");
045 StringBuffer result = new StringBuffer(50);
046 while (sT.hasMoreElements()) {
047 if (result.length()>0) result.append(separator);
048 result.append((String)sT.nextElement());
049 }
050 return result.toString();
051 }
052
053 /** Converts a string array into one "comma separated string" with optional separator
054 * @return <CODE>String</CODE> that contains the strings from the string array separated with
055 * <CODE>separator</CODE>.
056 * @param strings Strings to be combined
057 * @param separator Separator between different tokens
058 */
059 public static String arrayToGeneralCSV(String[] strings,String separator) {
060 if ((strings==null) || (strings.length==0)) return "";
061 StringBuffer result = new StringBuffer();
062 for (int i=0; i<strings.length; i++) {
063 if (i>0) result.append(separator);
064 result.append(strings[i]);
065 }
066 return result.toString();
067 }
068
069 /** Converts a string array into one Comma separated string.
070 * @return Comma separated <CODE>string</CODE>
071 * @param strings String array containing Strings
072 */
073 public static String arrayToCSV(java.lang.String[] strings) {
074 return arrayToCSV(strings,"");
075 }
076
077 /** Converts a string array into one sql query.
078 * @return Returns String: "<CODE>sqlfield in (string1,string2,...)</CODE>
079 * @param strings String array containing possible SQL values
080 * @param fieldname The name of the sqlfield that can be equal with Strings in String array.
081 */
082 public static String arrayToCSV(java.lang.String[] strings,String fieldname) {
083 if ((strings==null) ||(strings.length==0))
084 return fieldname + " IS NOT NULL";
085 boolean empty=true;
086 StringBuffer sb=new StringBuffer(1000);
087 for (int i=0;i<strings.length;i++) {
088 if (!empty)
089 sb.append(",");
090 if (strings[i]!=null) {
091 if (!strings[i].equals("")) {
092 empty=false;
093 sb.append(strings[i]);
094 }
095 }
096 }
097 if (fieldname.equals("")) {
098 return new String(sb);
099 }
100 if (empty==true) {
101 return fieldname + "IS NOT NULL";
102 }
103 return fieldname + " IN ("+new String(sb)+")";
104 }
105
106 /** Converts s to \"s\".
107 * @return The quoted string.
108 * @param s String to be quoted
109 */
110 public static String quote(String s) {
111 return "\""+s+"\"";
112 }
113
114 /** Converts s to \\\"s\"\\
115 * @param s String to be quoted
116 * @return Quoted string that can be used in JavaScript code.
117 */
118 public static String jsQuote(String s) {
119 return "\\\""+s+"\\\"";
120 }
121
122 /**
123 * Converts s to 's'.
124 * @param s String to be surrounded with "'"s.
125 * @return String that begins and ends with '.
126 */
127 public static String sqlQuote(String s) {
128 return "'"+s+"'";
129 }
130
131 /**
132 * Converts s to 's'.
133 * @param i int to be surrounded with "'"s.
134 * @return String that begins and ends with '.
135 */
136 public static String sqlQuote(int i) {
137 return "'"+Integer.toString(i)+"'";
138 }
139
140 /** Returns the trimmed sql String.
141 * @return <CODE>Null</CODE> if <CODE>s</CODE> is <CODE>null</CODE>. Else it returns
142 * trimmed sql String.
143 * @param s <CODE>String</CODE> to be trimmed
144 */
145 public static String sqlTrim(String s) {
146 if (s==null) return null;
147 return kotkabeans.Encoder.SQLEncode(s).trim();
148 }
149
150 /** Returns current page filename, no request parameters or path.
151 * @deprecated @see HtmlBean.getCurrentPage
152 * @return The filename of the current page, no request parameters or path.
153 * @param r <CODE>request</CODE>-object from the JSP-page.
154 * @throws MalformedURLException If current page is not valid URL.
155 */
156 /* public static String getCurrentPage(javax.servlet.http.HttpServletRequest r) throws java.net.MalformedURLException {
157 return HtmlBean.getCurrentPage(r);
158 }*/
159
160 /** Tests if a given String is null or empty.
161 *
162 * @param s String to be tested
163 * @return true if String is null or empty
164 */
165 public static boolean isEmpty(String s) {
166 return ((s==null) || (s.length()==0));
167 }
168
169 /** Returns first occurence of any character of test in src.
170 * IE. returns -1 if strings have no common characters!
171 * @return Index of the first same character in two strings.
172 * -1, if strings have no common characters!
173 * @param src First string
174 * @param test Second string
175 */
176 public static int anyCharIndexOf(String src,String test) {
177 if ((src==null) || (test==null)) throw
178 new IllegalArgumentException("src or test may not be null!");
179 if (src.length()==0) return -1;
180 int result=test.length();
181 for (int i=0; i<test.length(); i++) {
182 int temp = src.indexOf(test.charAt(i));
183 if ((temp>-1) && (temp<result)) result = temp;
184 }
185 if (result==test.length()) result=-1;
186 return result;
187 }
188
189 /** Deletes characters of c from src string.
190 * @param src String we are manipulating.
191 * @param c Characters to be removed
192 * @return String that doesn't contain characters from <CODE>c</CODE>
193 */
194 public static String stripChars(String src,String c) {
195 if (src==null) return null;
196 StringBuffer result = new StringBuffer(src.length());
197 StringTokenizer t = new java.util.StringTokenizer(src,c);
198 while (t.hasMoreElements()) {
199 result.append(t.nextToken());
200 }
201 return result.toString();
202 }
203
204 }