1 11 package org.eclipse.core.internal.content; 12 13 import java.util.*; 14 15 public class Util { 16 public static String [] parseItems(String string) { 17 return parseItems(string, ","); } 19 20 public static String [] parseItems(String string, String separator) { 21 if (string == null) 22 return new String [0]; 23 StringTokenizer tokenizer = new StringTokenizer(string, separator, true); 24 if (!tokenizer.hasMoreTokens()) 25 return new String [] {string.trim()}; 26 String first = tokenizer.nextToken().trim(); 27 boolean wasSeparator = false; 28 if (first.equals(separator)) { 29 first = ""; wasSeparator = true; 32 } 33 if (!tokenizer.hasMoreTokens()) 35 return wasSeparator ? new String [] {first, first} : new String [] {first}; 36 ArrayList items = new ArrayList(); 37 items.add(first); 38 String current; 39 do { 40 current = tokenizer.nextToken().trim(); 41 boolean isSeparator = current.equals(separator); 42 if (isSeparator) { 43 if (wasSeparator) 44 items.add(""); } else 46 items.add(current); 47 wasSeparator = isSeparator; 48 } while (tokenizer.hasMoreTokens()); 49 if (wasSeparator) 50 items.add(""); return (String []) items.toArray(new String [items.size()]); 53 } 54 55 public static List parseItemsIntoList(String string) { 56 return parseItemsIntoList(string, ","); } 58 59 public static List parseItemsIntoList(String string, String 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 first = tokenizer.nextToken().trim(); 69 boolean wasSeparator = false; 70 if (first.equals(separator)) { 71 first = ""; wasSeparator = true; 74 } 75 items.add(first); 76 if (!tokenizer.hasMoreTokens()) 77 return items; 78 String current; 79 do { 80 current = tokenizer.nextToken().trim(); 81 boolean isSeparator = current.equals(separator); 82 if (isSeparator) { 83 if (wasSeparator) 84 items.add(""); } else 86 items.add(current); 87 wasSeparator = isSeparator; 88 } while (tokenizer.hasMoreTokens()); 89 if (wasSeparator) 90 items.add(""); return items; 93 } 94 95 public static String toListString(Object [] list) { 96 return toListString(list, ","); } 98 99 public static String toListString(Object [] list, String separator) { 100 if (list == null || list.length == 0) 101 return null; 102 StringBuffer result = new StringBuffer (); 103 for (int i = 0; i < list.length; i++) { 104 result.append(list[i]); 105 result.append(separator); 106 } 107 return result.substring(0, result.length() - 1); 109 } 110 } 111 | Popular Tags |