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