KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cayenne > dataview > FormatFactory


1 /*****************************************************************
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  ****************************************************************/

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 JavaDoc 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 JavaDoc formatClass, Builder builder) {
50     return (Builder)builders.put(formatClass, builder);
51   }
52
53   public Builder unregisterBuilder(Class JavaDoc formatClass) {
54     return (Builder)builders.remove(formatClass);
55   }
56
57   public Builder getBuilder(Class JavaDoc 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 JavaDoc pattern = (String JavaDoc)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 JavaDoc pattern = (String JavaDoc)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 JavaDoc pattern = (String JavaDoc)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 JavaDoc pattern = (String JavaDoc)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 JavaDoc pattern = (String JavaDoc)parameters.get("pattern");
109       String JavaDoc valueClassName = (String JavaDoc)parameters.get("value-class");
110       String JavaDoc nullValueDesignation = (String JavaDoc)parameters.get("null-value");
111       String JavaDoc entryDelimiter = (String JavaDoc)parameters.get("entry-delimiter");
112       String JavaDoc valueFormatDelimiter = (String JavaDoc)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 JavaDoc valueClass;
120       try {
121         valueClass = Class.forName(valueClassName);
122         format.applyPattern(pattern, valueClass);
123         return format;
124       }
125       catch (ClassNotFoundException JavaDoc ex) {
126         throw new IllegalArgumentException JavaDoc("Value class " + valueClassName + " not found");
127       }
128     }
129   }
130 }
131
Popular Tags