KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > core > internal > content > Util


1 /*******************************************************************************
2  * Copyright (c) 2005 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.core.internal.content;
12
13 import java.util.*;
14
15 public class Util {
16     public static String JavaDoc[] parseItems(String JavaDoc string) {
17         return parseItems(string, ","); //$NON-NLS-1$
18
}
19
20     public static String JavaDoc[] parseItems(String JavaDoc string, String JavaDoc separator) {
21         if (string == null)
22             return new String JavaDoc[0];
23         StringTokenizer tokenizer = new StringTokenizer(string, separator, true);
24         if (!tokenizer.hasMoreTokens())
25             return new String JavaDoc[] {string.trim()};
26         String JavaDoc first = tokenizer.nextToken().trim();
27         boolean wasSeparator = false;
28         if (first.equals(separator)) {
29             // leading separator
30
first = ""; //$NON-NLS-1$
31
wasSeparator = true;
32         }
33         // simple cases, do not create temporary list
34
if (!tokenizer.hasMoreTokens())
35             return wasSeparator ? /* two empty strings */new String JavaDoc[] {first, first} : /*single non-empty element */new String JavaDoc[] {first};
36         ArrayList items = new ArrayList();
37         items.add(first);
38         String JavaDoc current;
39         do {
40             current = tokenizer.nextToken().trim();
41             boolean isSeparator = current.equals(separator);
42             if (isSeparator) {
43                 if (wasSeparator)
44                     items.add(""); //$NON-NLS-1$
45
} else
46                 items.add(current);
47             wasSeparator = isSeparator;
48         } while (tokenizer.hasMoreTokens());
49         if (wasSeparator)
50             // trailing separator
51
items.add(""); //$NON-NLS-1$
52
return (String JavaDoc[]) items.toArray(new String JavaDoc[items.size()]);
53     }
54
55     public static List parseItemsIntoList(String JavaDoc string) {
56         return parseItemsIntoList(string, ","); //$NON-NLS-1$
57
}
58
59     public static List parseItemsIntoList(String JavaDoc string, String JavaDoc separator) {
60         List items = new ArrayList(5);
61         if (string == null)
62             return items;
63         StringTokenizer tokenizer = new StringTokenizer(string, separator, true);
64         if (!tokenizer.hasMoreTokens()) {
65             items.add(string.trim());
66             return items;
67         }
68         String JavaDoc first = tokenizer.nextToken().trim();
69         boolean wasSeparator = false;
70         if (first.equals(separator)) {
71             // leading separator
72
first = ""; //$NON-NLS-1$
73
wasSeparator = true;
74         }
75         items.add(first);
76         if (!tokenizer.hasMoreTokens())
77             return items;
78         String JavaDoc current;
79         do {
80             current = tokenizer.nextToken().trim();
81             boolean isSeparator = current.equals(separator);
82             if (isSeparator) {
83                 if (wasSeparator)
84                     items.add(""); //$NON-NLS-1$
85
} else
86                 items.add(current);
87             wasSeparator = isSeparator;
88         } while (tokenizer.hasMoreTokens());
89         if (wasSeparator)
90             // trailing separator
91
items.add(""); //$NON-NLS-1$
92
return items;
93     }
94
95     public static String JavaDoc toListString(Object JavaDoc[] list) {
96         return toListString(list, ","); //$NON-NLS-1$
97
}
98
99     public static String JavaDoc toListString(Object JavaDoc[] list, String JavaDoc separator) {
100         if (list == null || list.length == 0)
101             return null;
102         StringBuffer JavaDoc result = new StringBuffer JavaDoc();
103         for (int i = 0; i < list.length; i++) {
104             result.append(list[i]);
105             result.append(separator);
106         }
107         // ignore last comma
108
return result.substring(0, result.length() - 1);
109     }
110 }
111
Popular Tags