001: /*
002: * Copyright 2004,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016:
017: package org.apache.bsf.util.type;
018:
019: import java.awt.Color;
020: import java.awt.Font;
021: import java.util.Hashtable;
022:
023: /**
024: * The <em>TypeConvertorRegistry</em> is the registry of type convertors.
025: * It has lookup and register capabilities based on the types to be
026: * converted as well as by some object key.
027: *
028: * @author Sanjiva Weerawarana
029: * @author Matthew J. Duftler
030: * @see TypeConvertorRegistry
031: */
032: public class TypeConvertorRegistry {
033: Hashtable reg = new Hashtable();
034: Hashtable keyedReg = new Hashtable();
035:
036: // register some standard convertors at construction time
037: public TypeConvertorRegistry() {
038: // no-op convertors: cvt from primitive wrappers to the object wrapper
039: TypeConvertor tc = new TypeConvertor() {
040: public Object convert(Class from, Class to, Object obj) {
041: return obj;
042: }
043:
044: public String getCodeGenString() {
045: return "(Class from, Class to, Object obj) {\n"
046: + "return obj;\n" + "}";
047: }
048: };
049: register(Boolean.class, boolean.class, tc);
050: register(boolean.class, Boolean.class, tc);
051: register(Byte.class, byte.class, tc);
052: register(byte.class, Byte.class, tc);
053: register(Character.class, char.class, tc);
054: register(char.class, Character.class, tc);
055: register(Short.class, short.class, tc);
056: register(short.class, Short.class, tc);
057: register(Integer.class, int.class, tc);
058: register(int.class, Integer.class, tc);
059: register(Long.class, long.class, tc);
060: register(long.class, Long.class, tc);
061: register(Float.class, float.class, tc);
062: register(float.class, Float.class, tc);
063: register(Double.class, double.class, tc);
064: register(double.class, Double.class, tc);
065:
066: // object to string: the registry special cases this one as the backup
067: // if the target is string and there is no special convertor available
068: // otherwise
069: tc = new TypeConvertor() {
070: public Object convert(Class from, Class to, Object obj) {
071: return (obj == null) ? "(null)" : obj.toString();
072: }
073:
074: public String getCodeGenString() {
075: return "(Class from, Class to, Object obj) {\n"
076: + "return (obj == null) ? \"(null)\" : obj.toString ();\n"
077: + "}";
078: }
079: };
080: register(Object.class, String.class, tc);
081:
082: // convert strings to various primitives (both their object versions
083: // and wrappers for primitive versions)
084: tc = new TypeConvertor() {
085: public Object convert(Class from, Class to, Object obj) {
086: String str = (String) obj;
087: if (to == Boolean.class || to == boolean.class) {
088: return Boolean.valueOf(str);
089: } else if (to == Byte.class || to == byte.class) {
090: return Byte.valueOf(str);
091: } else if (to == Character.class || to == char.class) {
092: return new Character(str.charAt(0));
093: } else if (to == Short.class || to == short.class) {
094: return Short.valueOf(str);
095: } else if (to == Integer.class || to == int.class) {
096: return Integer.valueOf(str);
097: } else if (to == Long.class || to == long.class) {
098: return Long.valueOf(str);
099: } else if (to == Float.class || to == float.class) {
100: return Float.valueOf(str);
101: } else if (to == Double.class || to == double.class) {
102: return Double.valueOf(str);
103: } else {
104: return null;
105: }
106: }
107:
108: public String getCodeGenString() {
109: return "(Class from, Class to, Object obj) {\n"
110: + "String str = (String) obj;\n"
111: + "if (to == Boolean.class || to == boolean.class) {\n"
112: + "return Boolean.valueOf (str);\n"
113: + "} else if (to == Byte.class || to == byte.class) {\n"
114: + "return Byte.valueOf (str);\n"
115: + "} else if (to == Character.class || to == char.class) {\n"
116: + "return new Character (str.charAt (0));\n"
117: + "} else if (to == Short.class || to == short.class) {\n"
118: + "return Short.valueOf (str);\n"
119: + "} else if (to == Integer.class || to == int.class) {\n"
120: + "return Integer.valueOf (str);\n"
121: + "} else if (to == Long.class || to == long.class) {\n"
122: + "return Long.valueOf (str);\n"
123: + "} else if (to == Float.class || to == float.class) {\n"
124: + "return Float.valueOf (str);\n"
125: + "} else if (to == Double.class || to == double.class) {\n"
126: + "return Double.valueOf (str);\n"
127: + "} else {\n" + "return null;\n" + "}\n" + "}";
128: }
129: };
130: register(String.class, boolean.class, tc);
131: register(String.class, Boolean.class, tc);
132: register(String.class, byte.class, tc);
133: register(String.class, Byte.class, tc);
134: register(String.class, char.class, tc);
135: register(String.class, Character.class, tc);
136: register(String.class, short.class, tc);
137: register(String.class, Short.class, tc);
138: register(String.class, int.class, tc);
139: register(String.class, Integer.class, tc);
140: register(String.class, long.class, tc);
141: register(String.class, Long.class, tc);
142: register(String.class, float.class, tc);
143: register(String.class, Float.class, tc);
144: register(String.class, double.class, tc);
145: register(String.class, Double.class, tc);
146:
147: // strings to fonts
148: tc = new TypeConvertor() {
149: public Object convert(Class from, Class to, Object obj) {
150: return Font.decode((String) obj);
151: }
152:
153: public String getCodeGenString() {
154: return "(Class from, Class to, Object obj) {\n"
155: + "return Font.decode ((String) obj);\n" + "}";
156: }
157: };
158: register(String.class, Font.class, tc);
159:
160: // strings to colors
161: tc = new TypeConvertor() {
162: public Object convert(Class from, Class to, Object obj) {
163: return Color.decode((String) obj);
164: }
165:
166: public String getCodeGenString() {
167: return "(Class from, Class to, Object obj) {\n"
168: + "return Color.decode ((String) obj);\n" + "}";
169: }
170: };
171: register(String.class, Color.class, tc);
172: }
173:
174: // lookup a convertor
175: public TypeConvertor lookup(Class from, Class to) {
176: String key = from.getName() + " -> " + to.getName();
177: TypeConvertor tc = (TypeConvertor) reg.get(key);
178: if (tc == null) {
179: if (from != void.class && from != Void.class
180: && to == String.class) {
181: // find the object -> string convertor
182: return lookup(Object.class, String.class);
183: }
184: }
185: return tc;
186: }
187:
188: // lookup a convertor by key
189: public TypeConvertor lookupByKey(Object key) {
190: return (TypeConvertor) keyedReg.get(key);
191: }
192:
193: // register a convertor
194: public void register(Class from, Class to, TypeConvertor convertor) {
195: String key = from.getName() + " -> " + to.getName();
196: reg.put(key, convertor);
197: }
198:
199: // register a convertor by key
200: public void registerByKey(Object key, TypeConvertor convertor) {
201: keyedReg.put(key, convertor);
202: }
203: }
|