KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > wcf > format > Formatter


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.wcf.format;
14
15 import java.util.ArrayList JavaDoc;
16 import java.util.HashMap JavaDoc;
17 import java.util.Iterator JavaDoc;
18 import java.util.List JavaDoc;
19 import java.util.Locale JavaDoc;
20 import java.util.Map JavaDoc;
21
22 /**
23  * parse/print java objects for user input/display.
24  */

25 public class Formatter implements BasicTypes {
26   List JavaDoc handlerList = new ArrayList JavaDoc(); // order matters!
27
Map JavaDoc handlerMap = new HashMap JavaDoc();
28   Locale JavaDoc locale;
29
30   /**
31    * Constructor Formatter.
32    */

33   public Formatter() {
34   }
35
36
37   public void setLocale(Locale JavaDoc locale) {
38     this.locale = locale;
39     for (Iterator JavaDoc it = handlerList.iterator(); it.hasNext(); ) {
40       FormatHandler fh = (FormatHandler)it.next();
41       fh.setLocale(locale);
42     }
43   }
44  
45   public Locale JavaDoc getLocale() {
46     return locale;
47   }
48   
49   /**
50    * adds a new handler for a specific data type.
51    */

52   public void addHandler(FormatHandler newHandler) {
53     String JavaDoc type = newHandler.getName();
54     FormatHandler oldHandler = (FormatHandler) handlerMap.get(type);
55     if (oldHandler != null) {
56       handlerMap.remove(type);
57       handlerList.remove(oldHandler);
58     }
59
60     handlerMap.put(type, newHandler);
61     handlerList.add(newHandler);
62   }
63
64   /**
65    * searches for the Format Handler that is registered for the given type
66    * @param type the requested type. if null or empty string, "string" is assumed
67    * @return the handler for type or null if there is no handler
68    */

69   public FormatHandler getHandler(String JavaDoc type) {
70     if (type == null || type.length() == 0)
71       type = BasicTypes.STRING_TYPE;
72     return (FormatHandler) handlerMap.get(type);
73   }
74
75   /**
76    * prints value
77    */

78   public String JavaDoc format(String JavaDoc type, Object JavaDoc value, String JavaDoc userPattern) {
79     FormatHandler fh = getHandler(type);
80     if (fh == null)
81       return String.valueOf(value);
82     return fh.format(value, userPattern);
83   }
84
85   /**
86    * parses value into an Object
87    */

88   public Object JavaDoc parse(String JavaDoc type, String JavaDoc value, String JavaDoc userPattern) {
89     FormatHandler fh = getHandler(type);
90     if (fh == null)
91       return value;
92     return fh.parse(value, userPattern);
93   }
94   
95   /**
96    * returns the the first renderer that can handle the object
97    */

98   public FormatHandler guessHandler(Object JavaDoc value) {
99     Iterator JavaDoc it = handlerList.iterator();
100     while (it.hasNext()) {
101       FormatHandler fh = (FormatHandler) it.next();
102       if (fh.canHandle(value))
103         return fh;
104     }
105     return null;
106   }
107   
108  
109   
110 }
Popular Tags