View Javadoc

1   /*
2    * $Id: StringList.java,v 1.1 2008/07/21 11:08:01 oeuillot Exp $
3    */
4   package org.rcfaces.core.internal.util;
5   
6   import java.util.ArrayList;
7   import java.util.Collection;
8   import java.util.Collections;
9   import java.util.HashMap;
10  import java.util.Iterator;
11  import java.util.List;
12  import java.util.Map;
13  import java.util.Map.Entry;
14  
15  import org.apache.commons.logging.Log;
16  import org.apache.commons.logging.LogFactory;
17  import org.rcfaces.core.internal.lang.StringAppender;
18  
19  /**
20   * 
21   * @author Olivier Oeuillot (latest modification by $Author: oeuillot $)
22   * @version $Revision: 1.1 $ $Date: 2008/07/21 11:08:01 $
23   */
24  public class StringList {
25      private static final String REVISION = "$Revision: 1.1 $";
26  
27      private static final Log LOG = LogFactory.getLog(StringList.class);
28  
29      private static final char STRING_DEFAULT_SEPARATOR = ',';
30  
31      private static final char STRING_DEFAULT_ESCAPE = '\\';
32  
33      public static String[] parseTokensList(String value) {
34  
35          if (value == null) {
36              return null;
37          }
38          if (value.length() < 1) {
39              return new String[] { value };
40          }
41  
42          char chs[] = value.toCharArray();
43  
44          List l = null;
45  
46          StringAppender sa = new StringAppender(32);
47  
48          for (int i = 0; i < chs.length; i++) {
49              char c = chs[i];
50  
51              if (c == STRING_DEFAULT_ESCAPE) {
52                  i++;
53                  if (i == chs.length) {
54                      break;
55                  }
56                  sa.append(chs[i]);
57                  continue;
58              }
59  
60              if (c == STRING_DEFAULT_SEPARATOR) {
61                  if (l == null) {
62                      l = new ArrayList(value.length() / 8);
63                  }
64                  l.add(sa.toString().trim());
65  
66                  sa.setLength(0);
67                  continue;
68              }
69  
70              sa.append(chs[i]);
71          }
72  
73          if (l == null) {
74              return new String[] { sa.toString().trim() };
75          }
76  
77          l.add(sa.toString());
78  
79          return (String[]) l.toArray(new String[l.size()]);
80      }
81  
82      public static int countTokens(String value) {
83  
84          char chs[] = value.toCharArray();
85  
86          int cnt = 1;
87          for (int i = 0; i < chs.length; i++) {
88              char c = chs[i];
89  
90              if (c == STRING_DEFAULT_ESCAPE) {
91                  i++; // On ignore le suivant
92                  continue;
93              }
94  
95              if (c == STRING_DEFAULT_SEPARATOR) {
96                  cnt++;
97                  continue;
98              }
99          }
100 
101         return cnt;
102     }
103 
104     public static String getFirstToken(String value) {
105 
106         if (value == null || value.length() < 1) {
107             return value;
108         }
109 
110         StringAppender sa = new StringAppender(32);
111 
112         char chs[] = value.toCharArray();
113 
114         for (int i = 0; i < chs.length; i++) {
115             char c = chs[i];
116 
117             if (c == STRING_DEFAULT_ESCAPE) {
118                 i++;
119                 if (i == chs.length) {
120                     break;
121                 }
122                 sa.append(chs[i]);
123                 continue;
124             }
125 
126             if (c == STRING_DEFAULT_SEPARATOR) {
127                 break;
128             }
129 
130             sa.append(chs[i]);
131         }
132 
133         return sa.toString().trim();
134     }
135 
136     public static String joinTokens(Collection collection) {
137         StringAppender sa = new StringAppender(collection.size() * 16);
138 
139         for (Iterator it = collection.iterator(); it.hasNext();) {
140             String token = (String) it.next();
141 
142             if (sa.length() > 0) {
143                 sa.append(STRING_DEFAULT_SEPARATOR);
144             }
145 
146             char chs[] = token.trim().toCharArray();
147             for (int i = 0; i < chs.length; i++) {
148                 char c = chs[i];
149 
150                 if (c == STRING_DEFAULT_SEPARATOR || c == STRING_DEFAULT_ESCAPE
151                         || c == '=') {
152                     sa.append(STRING_DEFAULT_ESCAPE);
153                 }
154 
155                 sa.append(c);
156             }
157         }
158 
159         return sa.toString();
160     }
161 
162     public static Map parseTokensMap(String value) {
163 
164         if (value == null) {
165             return null;
166         }
167         if (value.length() < 1) {
168             return Collections.EMPTY_MAP;
169         }
170 
171         char chs[] = value.toCharArray();
172 
173         Map map = new HashMap();
174 
175         StringAppender sa = new StringAppender(32);
176         String key = null;
177 
178         for (int i = 0; i < chs.length; i++) {
179             char c = chs[i];
180 
181             if (c == '=') {
182                 key = sa.toString().trim();
183                 sa.setLength(0);
184                 continue;
185             }
186 
187             if (c == STRING_DEFAULT_ESCAPE) {
188                 i++;
189                 if (i == chs.length) {
190                     break;
191                 }
192 
193                 c = chs[i];
194                 sa.append(chs[i]);
195                 continue;
196             }
197 
198             if (c == STRING_DEFAULT_SEPARATOR) {
199                 if (key == null) {
200                     map.put(sa.toString().trim(), "");
201 
202                 } else {
203                     map.put(key, sa.toString().trim());
204                 }
205 
206                 key = null;
207                 sa.setLength(0);
208                 continue;
209             }
210 
211             sa.append(chs[i]);
212         }
213 
214         if (key == null) {
215             map.put(sa.toString().trim(), "");
216 
217         } else {
218             map.put(key, sa.toString().trim());
219         }
220 
221         return map;
222     }
223 
224     public static String joinTokens(Map collection) {
225         StringAppender sa = new StringAppender(collection.size() * 32);
226 
227         for (Iterator it = collection.entrySet().iterator(); it.hasNext();) {
228             Map.Entry entry = (Entry) it.next();
229             String token = (String) entry.getKey();
230             String value = (String) entry.getValue();
231 
232             if (sa.length() > 0) {
233                 sa.append(STRING_DEFAULT_SEPARATOR);
234             }
235 
236             char chs[] = token.trim().toCharArray();
237             for (int i = 0; i < chs.length; i++) {
238                 char c = chs[i];
239 
240                 if (c == STRING_DEFAULT_SEPARATOR || c == STRING_DEFAULT_ESCAPE
241                         || c == '=') {
242                     sa.append(STRING_DEFAULT_ESCAPE);
243                 }
244 
245                 sa.append(c);
246             }
247 
248             if (value != null && value.length() > 0) {
249                 sa.append('=');
250 
251                 chs = token.trim().toCharArray();
252                 for (int i = 0; i < chs.length; i++) {
253                     char c = chs[i];
254 
255                     if (c == STRING_DEFAULT_SEPARATOR
256                             || c == STRING_DEFAULT_ESCAPE || c == '=') {
257                         sa.append(STRING_DEFAULT_ESCAPE);
258                     }
259 
260                     sa.append(c);
261                 }
262             }
263         }
264 
265         return sa.toString();
266     }
267 }