1 56 package org.objectstyle.cayenne.dataview; 57 58 import java.text.*; 59 import java.util.*; 60 import java.lang.reflect.*; 61 62 public class MapFormat extends Format { 63 private LinkedHashMap formatMap; 64 private Object [] values; 65 private String [] formats; 66 private String entryDelimiter = "|"; 67 private String valueFormatDelimiter = "#"; 68 private String nullFormat = ""; 69 private String nullValueDesignation = "null"; 70 71 public MapFormat() { 72 } 73 74 public MapFormat(String pattern, Class valueClass) { 75 applyPattern(pattern, valueClass); 76 } 77 78 public MapFormat(Object [] values, String [] formats) { 79 setMap(values, formats); 80 } 81 82 public void setEntryDelimiter(char delimiter) { 83 entryDelimiter = String.valueOf(delimiter); 84 } 85 86 public char getEntryDelimiter() { 87 return entryDelimiter.charAt(0); 88 } 89 90 public void setValueFormatDelimiter(char delimiter) { 91 valueFormatDelimiter = String.valueOf(delimiter); 92 } 93 94 public char getValueFormatDelimiter() { 95 return valueFormatDelimiter.charAt(0); 96 } 97 98 public void setNullValueDesignation(String nullValueDesignation) { 99 this.nullValueDesignation = nullValueDesignation; 100 } 101 102 public String getNullValueDesignation() { 103 return nullValueDesignation; 104 } 105 106 public String getNullFormat() { 107 return nullFormat; 108 } 109 110 public void setMap(Object [] values, String [] formats) { 111 this.values = new Object [values.length]; 112 this.formats = new String [formats.length]; 113 System.arraycopy(values, 0, this.values, 0, formats.length); 114 System.arraycopy(formats, 0, this.formats, 0, formats.length); 115 formatMap = new LinkedHashMap(this.values.length + 1); 116 for (int i = 0; i < this.values.length; i++) { 117 if (this.formats[i] == null) 118 throw new NullPointerException ("format cannot be null: " + values[i]); 119 if (this.values[i] == null) 120 nullFormat = this.formats[i]; 121 else 122 formatMap.put(this.values[i], this.formats[i]); 123 } 124 } 125 126 public Object [] getValues() { 127 return values; 128 } 129 130 public String [] getFormats() { 131 return formats; 132 } 133 134 public void applyPattern(String pattern) { 135 applyPattern(pattern, String .class); 136 } 137 138 public void applyPattern(String pattern, Class valueClass) { 139 formatMap = new LinkedHashMap(); 140 Constructor stringConstructor; 141 try { 142 stringConstructor = 143 valueClass.getConstructor(new Class [] {String .class}); 144 } catch (Exception ex) { 145 throw new IllegalArgumentException (valueClass + " has no String cunstructor"); 146 } 147 StringTokenizer parser = new StringTokenizer(pattern, entryDelimiter); 148 ArrayList valueList = new ArrayList(); 149 ArrayList formatList = new ArrayList(); 150 while (parser.hasMoreTokens()) { 151 String pair = parser.nextToken().trim(); 152 int delimIndex = pair.indexOf(valueFormatDelimiter); 153 Object value; 154 String format = ""; 155 try { 156 String valueStr; 157 if (delimIndex < 0 || delimIndex >= pair.length() - 1) { 158 valueStr = pair; 159 } else { 160 valueStr = pair.substring(0, delimIndex); 161 format = pair.substring(delimIndex + 1); 162 } 163 if (nullValueDesignation.equals(valueStr)) { 164 nullFormat = format; 165 valueList.add(null); 166 formatList.add(nullFormat); 167 } else { 168 value = stringConstructor.newInstance(new Object [] {valueStr}); 169 formatMap.put(value, format); 170 valueList.add(value); 171 formatList.add(format); 172 } 173 } 174 catch (InstantiationException ex) { 175 throw new IllegalArgumentException (valueClass + " " + ex); 176 }catch (IllegalAccessException ex) { 177 throw new IllegalArgumentException (valueClass + " " + ex); 178 }catch (IllegalArgumentException ex) { 179 throw new IllegalArgumentException (valueClass + " " + ex); 180 }catch (InvocationTargetException ex) { 181 throw new IllegalArgumentException (pattern + " incorrect pattern: " + pair); 182 } 183 } 184 values = valueList.toArray(); 185 formats = (String [])formatList.toArray(new String [formatList.size()]); 186 } 187 188 public Object parseObject(String text, ParsePosition status) { 189 int start = status.getIndex(); 190 int furthest = start; 191 Object bestObject = null; 192 Object tempObject = null; 193 for (Iterator i = formatMap.entrySet().iterator(); i.hasNext();) { 194 Map.Entry entry = (Map.Entry)i.next(); 195 String tempString = (String )entry.getValue(); 196 if (text.regionMatches(start, tempString, 0, tempString.length())) { 197 status.setIndex(start + tempString.length()); 198 tempObject = entry.getKey(); 199 if (status.getIndex() > furthest) { 200 furthest = status.getIndex(); 201 bestObject = tempObject; 202 if (furthest == text.length()) break; 203 } 204 } 205 } 206 if (nullFormat != null && 207 text.regionMatches(start, nullFormat, 0, nullFormat.length())) { 208 status.setIndex(start + nullFormat.length()); 209 if (status.getIndex() > furthest) { 210 furthest = status.getIndex(); 211 bestObject = null; 212 } 213 } 214 status.setIndex(furthest); 215 if (status.getIndex() == start) { 216 status.setErrorIndex(furthest); 217 } 218 return bestObject; 219 } 220 221 public StringBuffer format(Object obj, StringBuffer toAppendTo, FieldPosition pos) { 222 if (obj == null) { 223 if (nullFormat == null) 224 throw new NullPointerException ("object to format cannot be null"); 225 else return toAppendTo.append(nullFormat); 226 } 227 String formatStr = (String )formatMap.get(obj); 228 if (formatStr == null) 229 throw new IllegalArgumentException ("cannot format the object " + obj); 230 toAppendTo.append(formatStr); 231 return toAppendTo; 232 } 233 234 public String toPattern() { 235 StringBuffer pattern = new StringBuffer (); 236 boolean notFirst = false; 237 for (Iterator i = formatMap.entrySet().iterator(); i.hasNext(); ) { 238 Map.Entry entry = (Map.Entry)i.next(); 239 if (notFirst) { 240 pattern.append(entryDelimiter); 241 } else notFirst = true; 242 pattern.append(entry.getKey()).append(valueFormatDelimiter).append(entry.getValue()); 243 } 244 if (nullValueDesignation != null && nullFormat != null) { 245 if (notFirst) 246 pattern.append(entryDelimiter); 247 pattern.append(nullValueDesignation).append(valueFormatDelimiter).append(nullFormat); 248 } 249 return pattern.toString(); 250 } 251 } | Popular Tags |