1 19 20 package org.apache.cayenne.dataview; 21 22 import java.util.*; 23 import java.text.*; 24 25 public class FormatFactory { 26 private Map builders = new HashMap(); 27 28 public FormatFactory() { 29 registerBuilder(DecimalFormat.class, new DecimalFormatBuilder()); 30 registerBuilder(NumberFormat.class, new DecimalFormatBuilder()); 31 registerBuilder(SimpleDateFormat.class, new SimpleDateFormatBuilder()); 32 registerBuilder(DateFormat.class, new SimpleDateFormatBuilder()); 33 registerBuilder(ChoiceFormat.class, new ChoiceFormatBuilder()); 34 registerBuilder(MessageFormat.class, new MessageFormatBuilder()); 35 registerBuilder(MapFormat.class, new MapFormatBuilder()); 36 } 37 38 public Format createFormat( 39 Class formatClass, 40 Locale locale, 41 Map parameters) { 42 if (locale == null) 43 locale = Locale.getDefault(); 44 Builder builder = getBuilder(formatClass); 45 Format format = builder.create(locale, parameters); 46 return format; 47 } 48 49 public Builder registerBuilder(Class formatClass, Builder builder) { 50 return (Builder)builders.put(formatClass, builder); 51 } 52 53 public Builder unregisterBuilder(Class formatClass) { 54 return (Builder)builders.remove(formatClass); 55 } 56 57 public Builder getBuilder(Class formatClass) { 58 return (Builder)builders.get(formatClass); 59 } 60 61 public static interface Builder { 62 Format create(Locale locale, Map parameters); 63 } 64 65 public static class DecimalFormatBuilder implements Builder { 66 public Format create(Locale locale, Map parameters) { 67 String pattern = (String )parameters.get("pattern"); 68 DecimalFormatSymbols sym = new DecimalFormatSymbols(locale); 69 DecimalFormat format = new DecimalFormat(); 70 format.setDecimalFormatSymbols(sym); 71 if (pattern != null) 72 format.applyPattern(pattern); 73 return format; 74 } 75 } 76 77 public static class SimpleDateFormatBuilder implements Builder { 78 public Format create(Locale locale, Map parameters) { 79 String pattern = (String )parameters.get("pattern"); 80 DateFormatSymbols sym = new DateFormatSymbols(locale); 81 SimpleDateFormat format = new SimpleDateFormat(); 82 format.setDateFormatSymbols(sym); 83 if (pattern != null) 84 format.applyPattern(pattern); 85 return format; 86 } 87 } 88 89 public static class ChoiceFormatBuilder implements Builder { 90 public Format create(Locale locale, Map parameters) { 91 String pattern = (String )parameters.get("pattern"); 92 ChoiceFormat format = new ChoiceFormat(pattern); 93 return format; 94 } 95 } 96 97 public static class MessageFormatBuilder implements Builder { 98 public Format create(Locale locale, Map parameters) { 99 String pattern = (String )parameters.get("pattern"); 100 MessageFormat format = new MessageFormat(pattern, locale); 101 return format; 102 } 103 } 104 105 public static class MapFormatBuilder implements Builder { 106 public Format create(Locale locale, Map parameters) { 107 MapFormat format = new MapFormat(); 108 String pattern = (String )parameters.get("pattern"); 109 String valueClassName = (String )parameters.get("value-class"); 110 String nullValueDesignation = (String )parameters.get("null-value"); 111 String entryDelimiter = (String )parameters.get("entry-delimiter"); 112 String valueFormatDelimiter = (String )parameters.get("value-delimiter"); 113 if (entryDelimiter != null && entryDelimiter.length() > 0) 114 format.setEntryDelimiter(entryDelimiter.charAt(0)); 115 if (valueFormatDelimiter != null && valueFormatDelimiter.length() > 0) 116 format.setValueFormatDelimiter(valueFormatDelimiter.charAt(0)); 117 if (nullValueDesignation != null) 118 format.setNullValueDesignation(nullValueDesignation); 119 Class valueClass; 120 try { 121 valueClass = Class.forName(valueClassName); 122 format.applyPattern(pattern, valueClass); 123 return format; 124 } 125 catch (ClassNotFoundException ex) { 126 throw new IllegalArgumentException ("Value class " + valueClassName + " not found"); 127 } 128 } 129 } 130 } 131 | Popular Tags |