1 11 package org.eclipse.swt.tools.internal; 12 13 import java.util.ArrayList ; 14 import java.util.Arrays ; 15 import java.util.HashMap ; 16 import java.util.HashSet ; 17 import java.util.Set ; 18 import java.util.StringTokenizer ; 19 20 public abstract class ItemData { 21 22 HashMap params; 23 24 public ItemData(String str) { 25 parse(str); 26 } 27 28 static String [] split(String str, String separator) { 29 StringTokenizer tk = new StringTokenizer (str, separator); 30 ArrayList result = new ArrayList (); 31 while (tk.hasMoreElements()) { 32 result.add(tk.nextElement()); 33 } 34 return (String [])result.toArray(new String [result.size()]); 35 } 36 37 public String [] getFlags() { 38 Object flags = getParam("flags"); 39 if (flags == null) return new String [0]; 40 if (flags instanceof String []) return (String [])flags; 41 String [] result = split((String )flags, " "); 42 params.put("flags", result); 43 return result; 44 } 45 46 public boolean getFlag(String flag) { 47 String [] flags = getFlags(); 48 for (int i = 0; i < flags.length; i++) { 49 if (flags[i].equals(flag)) return true; 50 } 51 return false; 52 } 53 54 public Object getParam(String key) { 55 Object value = params.get(key); 56 return value == null ? "" : value; 57 } 58 59 public boolean getGenerate() { 60 return !getFlag("no_gen"); 61 } 62 63 public void parse(String str) { 64 this.params = new HashMap (); 65 if (str.length() == 0) return; 66 String [] params = split(str, ","); 67 for (int i = 0; i < params.length; i++) { 68 String param = params[i]; 69 int equals = param.indexOf('='); 70 if (equals == -1) { 71 System.out.println("Error: " + str + " param " + param); 72 } 73 String key = param.substring(0, equals).trim(); 74 String value = param.substring(equals + 1).trim(); 75 setParam(key, value); 76 } 77 } 78 79 public void setFlags(String [] flags) { 80 setParam("flags", flags); 81 } 82 83 public void setFlag(String flag, boolean value) { 84 String [] flags = getFlags(); 85 HashSet set = new HashSet (Arrays.asList(flags)); 86 if (value) { 87 set.add(flag); 88 } else { 89 set.remove(flag); 90 } 91 setFlags((String [])set.toArray(new String [set.size()])); 92 } 93 94 public void setGenerate(boolean value) { 95 setFlag("no_gen", !value); 96 } 97 98 public void setParam(String key, Object value) { 99 params.put(key, value); 100 } 101 102 public String toString() { 103 StringBuffer buffer = new StringBuffer (); 104 Set set = params.keySet(); 105 String [] keys = (String [])set.toArray(new String [set.size()]); 106 Arrays.sort(keys); 107 for (int j = 0; j < keys.length; j++) { 108 String key = keys[j]; 109 Object value = params.get(key); 110 String valueStr = ""; 111 if (value instanceof String ) { 112 valueStr = (String )value; 113 } else if (value instanceof String []) { 114 String [] values = (String [])value; 115 StringBuffer valueBuffer = new StringBuffer (); 116 for (int i = 0; i < values.length; i++) { 117 if (i != 0) valueBuffer.append(" "); 118 valueBuffer.append(values[i]); 119 } 120 valueStr = valueBuffer.toString(); 121 } else { 122 valueStr = value.toString(); 123 } 124 if (valueStr.length() > 0) { 125 if (buffer.length() != 0) buffer.append(","); 126 buffer.append(key); 127 buffer.append("="); 128 buffer.append(valueStr); 129 } 130 } 131 return buffer.toString(); 132 } 133 134 } 135 | Popular Tags |