KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > eclipse > swt > tools > internal > ItemData


1 /*******************************************************************************
2  * Copyright (c) 2004 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.swt.tools.internal;
12
13 import java.util.ArrayList JavaDoc;
14 import java.util.Arrays JavaDoc;
15 import java.util.HashMap JavaDoc;
16 import java.util.HashSet JavaDoc;
17 import java.util.Set JavaDoc;
18 import java.util.StringTokenizer JavaDoc;
19
20 public abstract class ItemData {
21
22     HashMap JavaDoc params;
23
24 public ItemData(String JavaDoc str) {
25     parse(str);
26 }
27
28 static String JavaDoc[] split(String JavaDoc str, String JavaDoc separator) {
29     StringTokenizer JavaDoc tk = new StringTokenizer JavaDoc(str, separator);
30     ArrayList JavaDoc result = new ArrayList JavaDoc();
31     while (tk.hasMoreElements()) {
32         result.add(tk.nextElement());
33     }
34     return (String JavaDoc[])result.toArray(new String JavaDoc[result.size()]);
35 }
36
37 public String JavaDoc[] getFlags() {
38     Object JavaDoc flags = getParam("flags");
39     if (flags == null) return new String JavaDoc[0];
40     if (flags instanceof String JavaDoc[]) return (String JavaDoc[])flags;
41     String JavaDoc[] result = split((String JavaDoc)flags, " ");
42     params.put("flags", result);
43     return result;
44 }
45
46 public boolean getFlag(String JavaDoc flag) {
47     String JavaDoc[] 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 JavaDoc getParam(String JavaDoc key) {
55     Object JavaDoc 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 JavaDoc str) {
64     this.params = new HashMap JavaDoc();
65     if (str.length() == 0) return;
66     String JavaDoc[] params = split(str, ",");
67     for (int i = 0; i < params.length; i++) {
68         String JavaDoc param = params[i];
69         int equals = param.indexOf('=');
70         if (equals == -1) {
71             System.out.println("Error: " + str + " param " + param);
72         }
73         String JavaDoc key = param.substring(0, equals).trim();
74         String JavaDoc value = param.substring(equals + 1).trim();
75         setParam(key, value);
76     }
77 }
78
79 public void setFlags(String JavaDoc[] flags) {
80     setParam("flags", flags);
81 }
82
83 public void setFlag(String JavaDoc flag, boolean value) {
84     String JavaDoc[] flags = getFlags();
85     HashSet JavaDoc set = new HashSet JavaDoc(Arrays.asList(flags));
86     if (value) {
87         set.add(flag);
88     } else {
89         set.remove(flag);
90     }
91     setFlags((String JavaDoc[])set.toArray(new String JavaDoc[set.size()]));
92 }
93
94 public void setGenerate(boolean value) {
95     setFlag("no_gen", !value);
96 }
97
98 public void setParam(String JavaDoc key, Object JavaDoc value) {
99     params.put(key, value);
100 }
101
102 public String JavaDoc toString() {
103     StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
104     Set JavaDoc set = params.keySet();
105     String JavaDoc[] keys = (String JavaDoc[])set.toArray(new String JavaDoc[set.size()]);
106     Arrays.sort(keys);
107     for (int j = 0; j < keys.length; j++) {
108         String JavaDoc key = keys[j];
109         Object JavaDoc value = params.get(key);
110         String JavaDoc valueStr = "";
111         if (value instanceof String JavaDoc) {
112             valueStr = (String JavaDoc)value;
113         } else if (value instanceof String JavaDoc[]) {
114             String JavaDoc[] values = (String JavaDoc[])value;
115             StringBuffer JavaDoc valueBuffer = new StringBuffer JavaDoc();
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