KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > ui > internal > intro > impl > util > StringUtil


1 /*******************************************************************************
2  * Copyright (c) 2004, 2006 IBM Corporation and others.
3  * All rights reserved. This program and the accompanying materials
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  *
8  * Contributors:
9  * IBM Corporation - initial API and implementation
10  *******************************************************************************/

11 package org.eclipse.ui.internal.intro.impl.util;
12
13 import java.io.UnsupportedEncodingException JavaDoc;
14 import java.lang.reflect.Method JavaDoc;
15 import java.net.URLDecoder JavaDoc;
16 import java.util.ArrayList JavaDoc;
17 import java.util.List JavaDoc;
18 import java.util.StringTokenizer JavaDoc;
19
20
21
22 public class StringUtil {
23
24     public static StringBuffer JavaDoc concat(String JavaDoc string1, String JavaDoc string2,
25             String JavaDoc string3) {
26         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc(string1);
27         buffer.append(string2);
28         buffer.append(string3);
29         return buffer;
30     }
31
32     public static StringBuffer JavaDoc concat(String JavaDoc string1, String JavaDoc string2,
33             String JavaDoc string3, String JavaDoc string4) {
34         StringBuffer JavaDoc buffer = concat(string1, string2, string3);
35         buffer.append(string4);
36         return buffer;
37     }
38
39     public static StringBuffer JavaDoc concat(String JavaDoc string1, String JavaDoc string2,
40             String JavaDoc string3, String JavaDoc string4, String JavaDoc string5) {
41         StringBuffer JavaDoc buffer = concat(string1, string2, string3, string4);
42         buffer.append(string5);
43         return buffer;
44     }
45
46     public static StringBuffer JavaDoc concat(String JavaDoc string1, String JavaDoc string2,
47             String JavaDoc string3, String JavaDoc string4, String JavaDoc string5, String JavaDoc string6) {
48         StringBuffer JavaDoc buffer = concat(string1, string2, string3, string4,
49             string5);
50         buffer.append(string6);
51         return buffer;
52     }
53
54     /*
55      * Helper method for String#split to handle the case where we
56      * might be running on Foundation class libraries instead of 1.4.
57      */

58     public static String JavaDoc[] split(String JavaDoc string, String JavaDoc delimiters) {
59         try {
60             return string.split(delimiters);
61         } catch (NoSuchMethodError JavaDoc e) {
62             // not running 1.4 so try a string tokenizer
63
List JavaDoc result = new ArrayList JavaDoc();
64             for (StringTokenizer JavaDoc tokenizer = new StringTokenizer JavaDoc(string, delimiters); tokenizer.hasMoreTokens(); )
65                 result.add(tokenizer.nextToken());
66             return (String JavaDoc[]) result.toArray(new String JavaDoc[result.size()]);
67         }
68     }
69
70     /*
71      * Helper method for URLDecoder#decode to handle the case where we
72      * might be running on Foundation class libraries instead of 1.4.
73      */

74     public static String JavaDoc decode(String JavaDoc s, String JavaDoc enc) throws UnsupportedEncodingException JavaDoc {
75         try {
76             return URLDecoder.decode(s, enc);
77         } catch (NoSuchMethodError JavaDoc e) {
78             // not running 1.4 so try the old method without an encoding
79
try {
80                 // use reflection to avoid deprecation warning
81
Method JavaDoc m = URLDecoder JavaDoc.class.getMethod("decode", new Class JavaDoc[] { String JavaDoc.class }); //$NON-NLS-1$
82
return (String JavaDoc)m.invoke(null, new Object JavaDoc[] {s});
83             }
84             catch (Exception JavaDoc ex) {
85                 // fall back to original string
86
return s;
87             }
88         }
89     }
90
91 }
92
Popular Tags