1 19 package org.openide.util; 20 21 import java.text.DateFormat ; 22 import java.text.FieldPosition ; 23 import java.text.Format ; 24 import java.text.MessageFormat ; 25 import java.text.NumberFormat ; 26 import java.text.ParsePosition ; 27 28 import java.util.Date ; 29 import java.util.Iterator ; 30 import java.util.Locale ; 31 import java.util.Map ; 32 33 34 51 public class MapFormat extends Format { 52 private static final int BUFSIZE = 255; 53 54 55 56 static final long serialVersionUID = -7695811542873819435L; 58 59 60 private Locale locale = Locale.getDefault(); 61 62 63 private String ldel = "{"; 65 66 private String rdel = "}"; 68 69 private Map argmap; 70 71 72 private int[] offsets; 73 74 75 private String [] arguments; 76 77 78 private int maxOffset; 79 80 81 private boolean throwex = false; 82 83 84 private boolean exactmatch = true; 85 86 91 public MapFormat(Map arguments) { 92 super(); 93 setMap(arguments); 94 } 95 96 105 public static String format(String pattern, Map arguments) { 106 MapFormat temp = new MapFormat(arguments); 107 108 return temp.format(pattern); 109 } 110 111 113 137 144 protected Object processKey(String key) { 145 return argmap.get(key); 146 } 147 148 155 public String processPattern(String newPattern) throws IllegalArgumentException { 156 int idx = 0; 157 int offnum = -1; 158 StringBuffer outpat = new StringBuffer (); 159 offsets = new int[BUFSIZE]; 160 arguments = new String [BUFSIZE]; 161 maxOffset = -1; 162 163 while (true) { 167 int ridx = -1; 168 int lidx = newPattern.indexOf(ldel, idx); 169 170 177 if (lidx >= 0) { 178 ridx = newPattern.indexOf(rdel, lidx + ldel.length()); 179 } else { 180 break; 181 } 182 183 if (++offnum >= BUFSIZE) { 184 throw new IllegalArgumentException ( 185 NbBundle.getBundle(MapFormat.class).getString("MSG_TooManyArguments") 186 ); 187 } 188 189 if (ridx < 0) { 190 if (exactmatch) { 191 throw new IllegalArgumentException ( 192 NbBundle.getBundle(MapFormat.class).getString("MSG_UnmatchedBraces") + " " + lidx 193 ); 194 } else { 195 break; 196 } 197 } 198 199 outpat.append(newPattern.substring(idx, lidx)); 200 offsets[offnum] = outpat.length(); 201 arguments[offnum] = newPattern.substring(lidx + ldel.length(), ridx); 202 idx = ridx + rdel.length(); 203 maxOffset++; 204 } 205 206 outpat.append(newPattern.substring(idx)); 207 208 return outpat.toString(); 209 } 210 211 216 private String formatObject(Object obj) { 217 if (obj == null) { 218 return null; 219 } 220 221 if (obj instanceof Number ) { 222 return NumberFormat.getInstance(locale).format(obj); } else if (obj instanceof Date ) { 224 return DateFormat.getDateTimeInstance(DateFormat.SHORT, DateFormat.SHORT, locale).format(obj); } else if (obj instanceof String ) { 226 return (String ) obj; 227 } 228 229 return obj.toString(); 230 } 231 232 239 public StringBuffer format(Object pat, StringBuffer result, FieldPosition fpos) { 240 String pattern = processPattern((String ) pat); 241 int lastOffset = 0; 242 243 for (int i = 0; i <= maxOffset; ++i) { 244 int offidx = offsets[i]; 245 result.append(pattern.substring(lastOffset, offsets[i])); 246 lastOffset = offidx; 247 248 String key = arguments[i]; 249 String obj; 250 if (key.length() > 0) { 251 obj = formatObject(processKey(key)); 252 } else { 253 result.append(this.ldel); 255 result.append(this.rdel); 256 continue; 257 } 258 259 if (obj == null) { 260 String lessgreedy = ldel + key; 265 int fromright = lessgreedy.lastIndexOf(ldel); 266 267 if (fromright > 0) { 268 String newkey = lessgreedy.substring(fromright + ldel.length()); 269 String newsubst = formatObject(processKey(newkey)); 270 271 if (newsubst != null) { 272 obj = lessgreedy.substring(0, fromright) + newsubst; 273 } 274 } 275 } 276 277 if (obj == null) { 278 if (throwex) { 279 throw new IllegalArgumentException ( 280 MessageFormat.format( 281 NbBundle.getBundle(MapFormat.class).getString("MSG_FMT_ObjectForKey"), 282 new Object [] { new Integer (key) } 283 ) 284 ); 285 } else { 286 obj = ldel + key + rdel; 287 } 288 } 289 290 result.append(obj); 291 } 292 293 result.append(pattern.substring(lastOffset, pattern.length())); 294 295 return result; 296 } 297 298 302 public Object parseObject(String text, ParsePosition status) { 303 return parse(text); 304 } 305 306 311 public String parse(String source) { 312 StringBuffer sbuf = new StringBuffer (source); 313 Iterator key_it = argmap.keySet().iterator(); 314 315 while (key_it.hasNext()) { 319 String it_key = (String ) key_it.next(); 320 String it_obj = formatObject(argmap.get(it_key)); 321 int it_idx = -1; 322 323 do { 324 it_idx = sbuf.toString().indexOf(it_obj, ++it_idx); 325 326 if (it_idx >= 0 ) { 327 sbuf.replace(it_idx, it_idx + it_obj.length(), ldel + it_key + rdel); 328 329 } 333 } while (it_idx != -1); 334 } 335 336 return sbuf.toString(); 337 } 338 339 343 public boolean willThrowExceptionIfKeyWasNotFound() { 344 return throwex; 345 } 346 347 352 public void setThrowExceptionIfKeyWasNotFound(boolean flag) { 353 throwex = flag; 354 } 355 356 360 public boolean isExactMatch() { 361 return exactmatch; 362 } 363 364 369 public void setExactMatch(boolean flag) { 370 exactmatch = flag; 371 } 372 373 374 public String getLeftBrace() { 375 return ldel; 376 } 377 378 381 public void setLeftBrace(String delimiter) { 382 ldel = delimiter; 383 } 384 385 386 public String getRightBrace() { 387 return rdel; 388 } 389 390 393 public void setRightBrace(String delimiter) { 394 rdel = delimiter; 395 } 396 397 398 public Map getMap() { 399 return argmap; 400 } 401 402 410 public void setMap(Map map) { 411 argmap = map; 412 } 413 414 416 516 } 517 | Popular Tags |