1 19 20 package org.netbeans.lib.ddl.util; 21 22 import java.text.ParseException ; 23 import java.util.Enumeration ; 24 import java.util.Map ; 25 import java.util.StringTokenizer ; 26 import java.util.Vector ; 27 28 import org.openide.util.MapFormat; 29 30 35 36 public class CommandFormatter { 37 38 Vector items; 39 40 45 public static String format(String pattern, Map arguments) throws ParseException , IllegalArgumentException { 46 CommandFormatter temp = new CommandFormatter(pattern); 47 return temp.format(arguments); 48 } 49 50 53 public CommandFormatter(String pattern) throws ParseException { 54 this(new StringTokenizer (pattern, "[]", true)); 55 } 56 57 60 private CommandFormatter(StringTokenizer tok) throws ParseException { 61 items = scan(tok); 62 } 63 64 67 private Vector scan(StringTokenizer tok) throws ParseException { 68 Vector objvec = new Vector (); 69 while (tok.hasMoreTokens()) { 70 String token = (String ) tok.nextElement(); 71 Object obj = token; 72 if (token.equals("[")) 73 obj = (Object )scan(tok); 74 else if (token.equals("]")) 75 break; 76 objvec.add(obj); 77 } 78 79 return objvec; 80 } 81 82 85 public String format(Map arguments) throws IllegalArgumentException { 86 return format(items, arguments); 87 } 88 89 93 private String format(Vector itemvec, Map arguments) throws IllegalArgumentException { 94 String retstr = ""; 95 Enumeration items_e = itemvec.elements(); 96 while (items_e.hasMoreElements()) { 97 Object e_item = items_e.nextElement(); 98 if (e_item instanceof Vector ) { 99 try { 100 e_item = format((Vector )e_item, arguments); 101 } catch (Exception e) { 102 } 104 } 105 106 if (e_item instanceof String ) { 107 MapFormat fmt = new MapFormat(arguments); 108 fmt.setThrowExceptionIfKeyWasNotFound(true); 109 String e_msg = fmt.format((String )e_item); 110 if (e_msg != null) 111 retstr = retstr + e_msg; 112 else 113 throw new IllegalArgumentException (); 114 } 115 } 116 117 return retstr; 118 } 119 } 120 | Popular Tags |