KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > killingar > forum > internal > Utils


1 /* Copyright 2000-2005 Anders Hovmöller
2  *
3  * The person or persons who have associated their work with
4  * this document (the "Dedicator") hereby dedicate the entire
5  * copyright in the work of authorship identified below (the
6  * "Work") to the public domain.
7  *
8  * Dedicator makes this dedication for the benefit of the
9  * public at large and to the detriment of Dedicator's heirs
10  * and successors. Dedicator intends this dedication to be an
11  * overt act of relinquishment in perpetuity of all present
12  * and future rights under copyright law, whether vested or
13  * contingent, in the Work. Dedicator understands that such
14  * relinquishment of all rights includes the relinquishment of
15  * all rights to enforce (by lawsuit or otherwise) those
16  * copyrights in the Work.
17  *
18  * Dedicator recognizes that, once placed in the public
19  * domain, the Work may be freely reproduced, distributed,
20  * transmitted, used, modified, built upon, or otherwise
21  * exploited by anyone for any purpose, commercial or non-
22  * commercial, and in any way, including by methods that have
23  * not yet been invented or conceived.
24  */

25
26 /**
27  * Contains a few utility functions.
28  */

29 package net.killingar.forum.internal;
30
31 import it.unimi.dsi.fastutil.longs.LongArrayList;
32
33 import java.net.URLEncoder JavaDoc;
34 import java.sql.ResultSet JavaDoc;
35 import java.sql.SQLException JavaDoc;
36 import java.util.StringTokenizer JavaDoc;
37
38 public class Utils
39 {
40     /**
41      * Escapes a String to make it safe for passing to an SQL database.
42      */

43     public static String JavaDoc disableSQL(String JavaDoc s) throws NullPointerException JavaDoc
44     {
45         if (s == null)return "";//throw new NullPointerException();
46

47         char[] sa = s.toCharArray();
48
49         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(sa.length);
50         for (int i = 0; i < sa.length; i++)
51         {
52             switch(sa[i])
53             {
54             case '\\':
55                 sb.append("\\\\");
56                 break;
57             case '\'':
58                 sb.append("\\'");
59                 break;
60             case '\"':
61                 sb.append("\\\"");
62                 break;
63             /*case '(':
64                 sb.append("\\(");
65                 break;
66             case ')':
67                 sb.append("\\)");
68                 break;*/

69
70             /*
71             // not actual SQL escaping
72             //case 'Œ': // mac å
73             case 'å':
74                 sb.append("&aring;");
75                 break;
76             //case '?': // mac Å
77             case 'Å':
78                 sb.append("&Aring;");
79                 break;
80             //case 'Š': // mac ä
81             case 'ä':
82                 sb.append("&auml;");
83                 break;
84             //case '€': // mac Ä
85             case 'Ä':
86                 sb.append("&Auml;");
87                 break;
88             //case 'š': // mac ö
89             case 'ö':
90                 sb.append("&ouml;");
91                 break;
92             //case '…': // mac Ö
93             case 'Ö':
94                 sb.append("&Ouml;");
95                 break;*/

96             default:
97                 sb.append(sa[i]);
98                 break;
99             }
100         }
101         return sb.toString();
102     }
103
104
105     /**
106      * Escapes a String to make it safe for html viewing.
107      */

108     public static String JavaDoc disableHTML(String JavaDoc s)
109     {
110         if (s == null)return "";
111
112     char[] sa = s.toCharArray();
113
114         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(sa.length);
115         for (int i = 0; i < sa.length; i++)
116         {
117             switch(sa[i])
118             {
119             case '<':
120                 sb.append("&lt;");
121                 break;
122             case '>':
123                 sb.append("&gt;");
124                 break;
125             case '\"':
126                 sb.append("&quot;");
127                 break;
128             case '&':
129                 sb.append("&amp;");
130                 break;
131             default:
132                 sb.append(sa[i]);
133                 break;
134             }
135         }
136         return sb.toString();
137     }
138     /**
139      * Escapes a String to make it safe for html viewing.
140      */

141     public static String JavaDoc disableQuote(String JavaDoc s)
142     {
143         if (s == null)return "";
144
145         char[] sa = s.toCharArray();
146
147         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(sa.length);
148         for (int i = 0; i < sa.length; i++)
149         {
150             switch(sa[i])
151             {
152             case '\"':
153                 sb.append("&quot;");
154                 break;
155             default:
156                 sb.append(sa[i]);
157                 break;
158             }
159         }
160         return sb.toString();
161     }
162
163
164     /**
165      * Replaces html codes with their plain text equivalent in windows format (opposite of disableHTML(), also fixes &auml;&aring;&ouml;).
166      */

167     public static String JavaDoc unHTML(String JavaDoc s)
168     {
169         if (s == null)return "";
170
171         char[] sa = s.toCharArray();
172
173         StringBuffer JavaDoc sb = new StringBuffer JavaDoc(sa.length);
174         char c;
175         for (int i = 0; i < sa.length; i++)
176         {
177             c = sa[i];
178             switch(c)
179             {
180             case '&':
181                 {
182                     int foo = 3;
183                     String JavaDoc tmp = s.substring(i+1, i+foo+1);
184                     if (tmp.equals("lt;"))
185                         sb.append('<');
186                     else if (tmp.equals("gt;"))
187                         sb.append('>');
188                     else
189                     {
190                         foo = 4;
191                         tmp = s.substring(i+1, i+foo+1);
192
193
194                         if (tmp.equals("amp;"))
195                             sb.append('&');
196                         else
197                         {
198                             foo = 5;
199                             tmp = s.substring(i+1, i+foo+1);
200
201
202                             if (tmp.equals("quot;"))
203                                 sb.append('\"');
204                             else if (tmp.equals("auml;"))
205                                 sb.append('ä');
206                             else if (tmp.equals("Auml;"))
207                                 sb.append('Ä');
208                             else if (tmp.equals("Ouml;"))
209                                 sb.append('Ö');
210                             else if (tmp.equals("ouml;"))
211                                 sb.append('ö');
212                             else
213                             {
214                                 foo = 6;
215                                 tmp = s.substring(i+1, i+foo+1);
216
217
218                                 if (tmp.equals("aring;"))
219                                     sb.append('å');
220                                 else if (tmp.equals("Aring;"))
221                                     sb.append('Å');
222                                 else
223                                 {
224                                     sb.append(c);
225                                     break;
226                                 }
227                             }
228                         }
229                     }
230                     i+=foo;
231                 }
232                 break;
233             default:
234                 sb.append(c);
235                 break;
236             }
237         }
238         return sb.toString();
239     }
240
241
242     /**
243      * Returns true if the character is to be considered a space.
244      * This function is used by the nrWords() function.
245      */

246     public static boolean isspace(char c)
247     {
248         switch(c)
249         {
250         case ' ':
251         case '\n':
252         case '\r':
253         case '\t':
254         case '.':
255         case '?':
256         case '!':
257         case ',':
258         case ':':
259         case ';':
260         case '\'':
261         case '\"':
262         case '(':
263         case ')':
264             return true;
265         default:
266             return false;
267         }
268     }
269
270
271     /**
272      * Count the number of words in a String.
273      */

274     public static int nrWords(String JavaDoc s)
275     {
276         if (s == null)return 0;
277         int count = 0;
278         boolean run = false;
279
280         char[] sa = s.toCharArray();
281
282         for (int i = 0; i < sa.length; i++)
283         {
284             if (isspace(sa[i]))
285             {
286                 if (run)count++;
287                 run = false;
288             }
289             else run = true;
290         }
291
292         if (run)
293             count++;
294         return count;
295     }
296
297     /**
298      * Returns a list of longs from a ResultSet. It reads one long value per row.
299      */

300     public static long[] getLongsFromResult(ResultSet JavaDoc result) throws SQLException JavaDoc
301     {
302         LongArrayList a = new LongArrayList();
303
304         for(int i = 0; result.next(); i++)
305             a.add(result.getLong(1));
306
307         if (a.size() == 0)
308             return new long[0];
309
310         long r[] = new long[a.size()];
311         a.toArray(r);
312
313         return r;
314     }
315
316     private static long[] noResultLongs = new long[0];
317     public static long[] getLongsFromString(String JavaDoc inList)
318     {
319         if (inList == null)
320             return noResultLongs;
321
322         StringTokenizer JavaDoc t = new StringTokenizer JavaDoc(inList, ", \t");
323         long[] r = new long[t.countTokens()];
324         int i = 0;
325         while (t.hasMoreTokens())
326             r[i++] = Long.parseLong(t.nextToken());
327
328         return r;
329     }
330
331     public String JavaDoc URLEncodeParam(String JavaDoc name, Object JavaDoc value)
332     {
333         StringBuffer JavaDoc link = new StringBuffer JavaDoc();
334
335         if (value != null)
336         {
337             if (value instanceof String JavaDoc)
338             {
339                 link.append(name);
340                 link.append('=');
341                 link.append(URLEncoder.encode((String JavaDoc) value));
342             }
343             else
344             {
345                 String JavaDoc[] values = (String JavaDoc[]) value;
346                 link.append(name);
347                 link.append('=');
348                 link.append(URLEncoder.encode(values[0]));
349             }
350         }
351
352         return link.toString();
353     }
354
355     public String JavaDoc URLEncodeParams(java.util.Map JavaDoc m)
356     {
357         StringBuffer JavaDoc link = new StringBuffer JavaDoc();
358
359         if (link.toString().indexOf("?") == -1)
360             link.append('?');
361         else
362             link.append("&amp;");
363
364         // Set parameters
365
java.util.Iterator JavaDoc i = m.entrySet().iterator();
366         while (i.hasNext())
367         {
368             java.util.Map.Entry entry = (java.util.Map.Entry)i.next();
369             link.append(URLEncodeParam((String JavaDoc)entry.getKey(), entry.getValue()));
370
371         if (i.hasNext())
372                 link.append("&amp;");
373         }
374         return link.toString();
375     }
376 }
Popular Tags