1 /* -------------------------------------------------------------------
2 * Java source file for the class DataConverter
3 *
4 * Copyright (c), 2002, Masahiro Takatsuka.
5 * All Rights Researved.
6 *
7 * Original Author: Masahiro Takatsuka (masa@jbeans.net)
8 * $Author: takatsukam $
9 *
10 * $Date: 2003/07/25 04:51:45 $
11 *
12 * $Id: DataConverter.java,v 1.1.1.1 2003/07/25 04:51:45 takatsukam Exp $
13 *
14 * Reference: Document no:
15 * ___ ___
16 *
17 * To Do:
18 * ___
19 *
20 ------------------------------------------------------------------- */
21
22 /* --------------------------- Package ---------------------------- */
23 package net.jbeans.data.converter;
24
25 /* ------------------ Import classes (packages) ------------------- *//package-summary/html">color="#329900"> ------------------ Import classes (packages) ------------------- *//package-summary.html">color="#329900">/* ------------------ Import classes (packages) ------------------- *//package-summary.html">color="#329900"> ------------------ Import classes (packages) ------------------- */
26 import java.io.*;
27
28 import net.jbeans.lang.*;
29 import net.jbeans.data.*;
30
31 /*====================================================================
32 Implementation of class DataConverter
33 ====================================================================*/
34 /***
35 * generally describe DataConverter in here
36 *
37 * @version $Revision: 1.1.1.1 $
38 * @author Masahiro Takatsuka (masa@jbeans.net)
39 * @see Serializable
40 */
41
42 public abstract class DataConverter implements Serializable {
43 // these arrays will be written explicitly because of primitive type class.
44 transient protected Class[] sourceTypes;
45 transient protected Class[] targetTypes;
46
47 /***
48 * Constracts a new MtDataConverter
49 */
50 public DataConverter() {
51 super();
52 setDataTypes();
53 }
54
55 /***
56 * Serialization methods
57 */
58 private void writeObject(ObjectOutputStream s) throws IOException {
59 s.defaultWriteObject();
60 s.writeInt(this.sourceTypes.length);
61 for (int i = 0; i < this.sourceTypes.length; i++) {
62 if (this.sourceTypes[i].isPrimitive()) { // just write a name in String
63 s.writeObject(this.sourceTypes[i].getName());
64 } else {
65 s.writeObject(this.sourceTypes[i]);
66 }
67 }
68 s.writeInt(this.targetTypes.length);
69 for (int i = 0; i < this.targetTypes.length; i++) {
70 if (this.targetTypes[i].isPrimitive()) { // just write a name in String
71 s.writeObject(this.targetTypes[i].getName());
72 } else {
73 s.writeObject(this.targetTypes[i]);
74 }
75 }
76 }
77
78 /***
79 * Serialization methods
80 */
81 private void readObject(ObjectInputStream s) throws ClassNotFoundException, IOException {
82 s.defaultReadObject();
83 int count = s.readInt();
84 this.sourceTypes = new Class[count];
85 for (int i = 0; i < count; i++) {
86 Object obj = s.readObject();
87 if (obj instanceof String) { // this is a primitie type class
88 String name = (String) obj;
89 if (name.equals("byte")) {
90 this.sourceTypes[i] = Byte.TYPE;
91 } else if (name.equals("double")) {
92 this.sourceTypes[i] = Double.TYPE;
93 } else if (name.equals("float")) {
94 this.sourceTypes[i] = Float.TYPE;
95 } else if (name.equals("int")) {
96 this.sourceTypes[i] = Integer.TYPE;
97 } else if (name.equals("long")) {
98 this.sourceTypes[i] = Long.TYPE;
99 } else if (name.equals("short")) {
100 this.sourceTypes[i] = Short.TYPE;
101 } else if (name.equals("boolean")) {
102 this.sourceTypes[i] = Boolean.TYPE;
103 }
104 } else {
105 this.sourceTypes[i] = (Class) obj;
106 }
107 }
108
109 count = s.readInt();
110 this.targetTypes = new Class[count];
111 for (int i = 0; i < count; i++) {
112 Object obj = s.readObject();
113 if (obj instanceof String) { // this is a primitie type class
114 String name = (String) obj;
115 if (name.equals("byte")) {
116 this.targetTypes[i] = Byte.TYPE;
117 } else if (name.equals("double")) {
118 this.targetTypes[i] = Double.TYPE;
119 } else if (name.equals("float")) {
120 this.targetTypes[i] = Float.TYPE;
121 } else if (name.equals("int")) {
122 this.targetTypes[i] = Integer.TYPE;
123 } else if (name.equals("long")) {
124 this.targetTypes[i] = Long.TYPE;
125 } else if (name.equals("short")) {
126 this.targetTypes[i] = Short.TYPE;
127 } else if (name.equals("boolean")) {
128 this.targetTypes[i] = Boolean.TYPE;
129 }
130 } else {
131 this.targetTypes[i] = (Class) obj;
132 }
133 }
134 }
135
136 /***
137 * Initializes acceptable soruce and target types.
138 */
139 abstract protected void setDataTypes();
140
141 /***
142 * Returns a list of class which represent classes of source data.
143 */
144 public Class[] getSourceDataTypes() {
145 return this.sourceTypes;
146 }
147
148 /***
149 * Returns a class of target data.
150 */
151 public Class[] getTargetDataTypes() {
152 return this.targetTypes;
153 }
154
155 /***
156 * Checks whether the specified class is supported as a source data type.
157 */
158 public boolean isSourceDataTypeSupported(Class class1) {
159 for (int i = 0; i < this.sourceTypes.length; i++) {
160 if (ClassUtil.isAssignable(this.sourceTypes[i], class1)) {
161 return true;
162 }
163 }
164 return false;
165 }
166
167 /***
168 * Checks whether the specified class is supported as a target data type.
169 */
170 public boolean isTargetDataTypeSupported(Class class1) {
171 for (int i = 0; i < this.targetTypes.length; i++) {
172 if (ClassUtil.isAssignable(this.targetTypes[i], class1)) {
173 return true;
174 }
175 }
176 return false;
177 }
178
179 /***
180 * Converts the specified object "obj" into an object of a class "class1".
181 */
182 public Object convert(Object obj, String classname) throws InvalidDataTypeException, UnsupportedTypeException {
183 try {
184 return convert(obj, Class.forName(classname));
185 } catch (ClassNotFoundException ex) {
186 ex.printStackTrace();
187 System.out.println(ex);
188 throw new UnsupportedTypeException("BasicTypeConverter: Unsupported output type");
189 }
190 }
191
192 /***
193 * Converts the specified object "obj" into an object of a class "class1".
194 */
195 abstract protected Object convert(Object obj, Class class1) throws InvalidDataTypeException, UnsupportedTypeException;
196 }
This page was automatically generated by Maven