1
2
3
4
5
6
7 package org.astrogrid.dataservice.metadata;
8
9 import java.util.Date;
10
11
12 /***
13 * Defines Data types for the Virtual Observatory, and useful translation methods.
14 * These are primarily based on the types defined for VOTables.
15 *
16 * @author M Hill
17 */
18
19 public class VoTypes {
20
21
22
23 public final static String BOOLEAN = "boolean";
24 public final static String BIT = "bit";
25 public final static String UBYTE = "unsignedByte";
26 public final static String SHORT = "short";
27 public final static String INT = "int";
28 public final static String LONG = "long";
29 public final static String CHAR = "char";
30 public final static String UNICHAR = "unicodeChar";
31 public final static String DOUBLE = "double";
32 public final static String FLOAT = "float";
33 public final static String FLOATCOMPLEX = "floatComplex";
34 public final static String DOUBLECOMPLEX = "doubleComplex";
35
36 public final static String[] TYPES = new String[] {
37 BOOLEAN, BIT, UBYTE, SHORT, INT, LONG, CHAR, UNICHAR, DOUBLE, FLOAT, FLOATCOMPLEX, DOUBLECOMPLEX
38 };
39
40
41 /***
42 * Returns a VODataService <datatype> fragment for the given java class.
43 * Patch fix - Full description needs the arraysize.
44 *
45 */
46 public static String getVoTypeXml(Class javatype) {
47 if (javatype == null) {
48 throw new IllegalArgumentException("Null type given to work out VoType");
49 }
50 else if (javatype == Byte.class) {
51 return "<dataType>" + SHORT + "</dataType>";
52 }
53 else if (javatype == Short.class) {
54 return "<dataType>" + SHORT + "</dataType>";
55 }
56 else if (javatype == Integer.class) {
57 return "<dataType>" + INT + "</dataType>";
58 }
59 else if (javatype == Long.class) {
60 return "<dataType>" + LONG + "</dataType>";
61 }
62 else if (javatype == Float.class) {
63 return "<dataType>" + FLOAT + "</dataType>";
64 }
65 else if (javatype == Double.class) {
66 return "<dataType>" + DOUBLE + "</dataType>";
67 }
68 else if (javatype == Boolean.class) {
69 return "<dataType>" + BOOLEAN + "</dataType>";
70 }
71 else if (javatype == Character.class) {
72
73 return "<dataType arraysize='1'>" + CHAR + "</dataType>";
74 }
75 else if (javatype == String.class) {
76 return "<dataType arraysize='*'>" + CHAR + "</dataType>";
77 }
78 else if (javatype == Date.class) {
79 return "<dataType arraysize='*'>" + CHAR + "</dataType>";
80 }
81 else {
82 throw new IllegalArgumentException("Don't know what VOType the java class "+javatype+" maps to");
83 }
84 }
85
86
87 /***Returns the 'VO Type' for the given java class. NOTE that this is not
88 * sufficient for Votable, which needs the arraysize also set for strings
89 */
90 public static String getVoType(Class javatype) {
91 if (javatype == null) {
92 throw new IllegalArgumentException("Null type given to work out VoType");
93 }
94 else if (javatype == Byte.class) {
95 return "datatype='"+SHORT+"'";
96 }
97 else if (javatype == Short.class) {
98 return "datatype='"+SHORT+"'";
99 }
100 else if (javatype == Integer.class) {
101 return "datatype='"+INT+"'";
102 }
103 else if (javatype == Long.class) {
104 return "datatype='"+LONG+"'";
105 }
106 else if (javatype == Float.class) {
107 return "datatype='"+FLOAT+"'";
108 }
109 else if (javatype == Double.class) {
110 return "datatype='"+DOUBLE+"'";
111 }
112 else if (javatype == Boolean.class) {
113 return "datatype='"+BOOLEAN+"'";
114 }
115 else if (javatype == Character.class) {
116
117 return "datatype='"+CHAR+"' arraysize='1'";
118 }
119 else if (javatype == String.class) {
120 return "datatype='"+CHAR+"' arraysize='*'";
121 }
122 else if (javatype == Date.class) {
123 return "datatype='"+CHAR+"' arraysize='*'";
124 }
125 else {
126 throw new IllegalArgumentException("Don't know what VOType the java class "+javatype+" maps to");
127 }
128 }
129
130 /***Returns the VO Type as a string of two attributes (datatype and arraysize)
131 * for the given java class type.
132 */
133 public static String getVoTableTypeAttributes(Class javatype) {
134 if (javatype == null) {
135 throw new IllegalArgumentException("Null type given to work out VoType");
136 }
137 else if (javatype == Byte.class) {
138 return "datatype='"+SHORT+"'";
139 }
140 else if (javatype == Short.class) {
141 return "datatype='"+SHORT+"'";
142 }
143 else if (javatype == Integer.class) {
144 return "datatype='"+INT+"'";
145 }
146 else if (javatype == Long.class) {
147 return "datatype='"+LONG+"'";
148 }
149 else if (javatype == Float.class) {
150 return "datatype='"+FLOAT+"'";
151 }
152 else if (javatype == Double.class) {
153 return "datatype='"+FLOAT+"'";
154 }
155 else if (javatype == Boolean.class) {
156 return "datatype='"+BOOLEAN+"'";
157 }
158 else if (javatype == Character.class) {
159
160 return "datatype='"+CHAR+"' arraysize='1'";
161 }
162 else if (javatype == String.class) {
163 return "datatype='"+CHAR+"' arraysize='*'";
164 }
165 else if (javatype == Date.class) {
166 return "datatype='"+CHAR+"' arraysize='*'";
167 }
168 else {
169 throw new IllegalArgumentException("Don't know what VOType the java class "+javatype+" maps to");
170 }
171 }
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199 }
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249