1 package net.sourceforge.jparam;
2
3 import java.io.PrintStream;
4 import java.util.ArrayList;
5 import java.util.HashMap;
6 import java.util.Iterator;
7 import java.util.LinkedList;
8 import java.util.List;
9 import java.util.Map;
10 import java.util.Set;
11
12 import net.sourceforge.jparam.conversion.creators.CreatorRegistry;
13 import net.sourceforge.jparam.output.SerializerRegistry;
14 import net.sourceforge.jparam.typename.TypeNameRegistry;
15 import net.sourceforge.jparam.util.Utils;
16
17 public class BasicRegistration {
18
19 /***
20 * Perform basic registrations of default Java-JParam types, conversions, creators and serializers
21 * As well as some constants
22 */
23 static void doRegistration() {
24 registerNames();
25 registerCreators();
26 registerConstants();
27 registerSerializers();
28 registerStandardConverters();
29 }
30
31 private static void registerNames() {
32 TypeNameRegistry registry = TypeNameRegistry.getInstance();
33
34 registry.registerJParamAndJava(Boolean.class, "bool");
35 registry.registerJParamAndJava(Character.class, "char");
36 registry.registerJParamAndJava(Byte.class, "uchar");
37 registry.registerJParamAndJava(Short.class, "short");
38 registry.registerJParamAndJava(Integer.class, "int");
39 registry.registerJParamToJava("long", Integer.class);
40 registry.registerJParamAndJava(Long.class, "longlong");
41 registry.registerJParamAndJava(Float.class, "float");
42 registry.registerJParamAndJava(Double.class, "double");
43 registry.registerJParamAndJava(String.class, "string");
44
45 registry.registerJParamAndJava(HashMap.class, "map");
46 registry.registerJParamAndJava(LinkedList.class, "list");
47 registry.registerJParamAndJava(ArrayList.class, "vector");
48
49 registry.registerJavaToJParam(Object.class, "Object");
50 registry.registerJavaToJParam(Number.class, "Number");
51 }
52
53
54
55
56
57 private static void registerCreators() {
58 CreatorRegistry registry = CreatorRegistry.getInstance();
59
60
61
62 registry.registerHelperClass(VectorCreator.class);
63 }
64
65 public static class VectorCreator {
66 public static LinkedList create1(int num, Object o) {
67 LinkedList l = new LinkedList();
68
69 for (int i = 0; i < num; i++) {
70 l.add(o);
71 }
72
73 return l;
74 }
75
76 public static ArrayList create2(int num, Object o) {
77 ArrayList l = new ArrayList();
78
79 for (int i = 0; i < num; i++) {
80 l.add(o);
81 }
82
83 return l;
84 }
85 }
86
87
88
89
90
91 private static void registerConstants() {
92 ConstantRegistry registry = ConstantRegistry.getInstance();
93
94 registry.registerConstant("true", Boolean.TRUE);
95 registry.registerConstant("false", Boolean.FALSE);
96 registry.registerConstant("NULL", null);
97 }
98
99
100
101
102
103 private static void registerSerializers() {
104 SerializerRegistry registry = SerializerRegistry.getInstance();
105
106 registry.registerHelperClass(PrimitiveSerializers.class);
107 }
108
109 public static class PrimitiveSerializers {
110 public static void serialize(Boolean b, PrintStream out) {
111
112 String str = b.booleanValue() ? "true" : "false";
113 out.print(str);
114 }
115
116 public static void serialize(Character c, PrintStream out) {
117 out.print('\'');
118 charOutput(out, c.charValue(), false);
119 out.print('\'');
120 }
121
122 public static void serialize(String str, PrintStream out) {
123 out.print('"');
124 int len = str.length();
125 for (int i = 0; i < len; i++) {
126 charOutput(out, str.charAt(i), true);
127 }
128 out.print('"');
129 }
130
131 public static void serialize(Long obj, PrintStream out) {
132 out.print("longlong(" + obj + "L)");
133 }
134
135 public static void serialize(Byte obj, PrintStream out) {
136 out.print("uchar(" + obj + ")");
137
138 }
139
140 public static void serialize(Short obj, PrintStream out) {
141 out.print("short(" + obj + ")");
142 }
143
144 public static void serialize(Float obj, PrintStream out) {
145 out.print(obj + "f");
146 }
147
148 public static void serialize(Integer obj, PrintStream out) {
149 out.print(obj);
150 }
151
152 public static void serialize(Double obj, PrintStream out) {
153 out.print(obj);
154 }
155
156 public static void serialize(LinkedList v, PrintStream out)
157 throws JParamException {
158 serialize((List) v, out);
159 }
160
161 public static void serialize(ArrayList v, PrintStream out)
162 throws JParamException {
163 serialize((List) v, out);
164 }
165
166 public static void serialize(List v, PrintStream out)
167 throws JParamException {
168 out.print("[");
169 Iterator itr = v.iterator();
170 boolean isFirst = true;
171 while (itr.hasNext()) {
172 if (isFirst) {
173 isFirst = false;
174 } else {
175 out.print(",");
176 }
177 JParam.writeObject(itr.next(), out);
178 }
179 out.print("]");
180 }
181
182 public static void serialize(HashMap m, PrintStream out)
183 throws JParamException {
184 serialize((Map) m, out);
185 }
186
187 public static void serialize(Map m, PrintStream out)
188 throws JParamException {
189 Set entries = m.entrySet();
190
191 out.print("{");
192
193 Iterator itr = entries.iterator();
194 boolean isFirst = true;
195 while (itr.hasNext()) {
196 if (isFirst) {
197 isFirst = false;
198 } else {
199 out.print(", ");
200 }
201 Map.Entry entry = (Map.Entry) itr.next();
202 JParam.writeObject(entry.getKey(), out);
203 out.print(" => ");
204 JParam.writeObject(entry.getValue(), out);
205 }
206 out.print("}");
207 }
208 }
209
210 private static void charOutput(PrintStream out, char c, boolean isString) {
211 switch (c) {
212 case '\n':
213 out.print("//n");
214 return;
215 case '\t':
216 out.print("//t");
217 return;
218 case 11:
219 out.print("//v");
220 return;
221 case '\b':
222 out.print("//b");
223 return;
224 case '\r':
225 out.print("//r");
226 return;
227 case '\f':
228 out.print("//f");
229 return;
230 case 7:
231 out.print("//a");
232 return;
233 case '//':
234 out.print("////");
235 return;
236 case '"':
237 if (isString) {
238 out.print("//\"");
239 } else {
240 out.print('"');
241 }
242 return;
243 case '\'':
244 if (isString) {
245 out.print("'");
246 } else {
247 out.print("//'");
248 }
249 return;
250 }
251
252 if (isPrintable(c)) {
253 out.print(c);
254 return;
255 }
256
257
258 int high_digit = (c >> 4) & 0xF;
259 int low_digit = c & 0xF;
260 out.print("//x");
261 out.print(encodeHex(high_digit));
262 out.print(encodeHex(low_digit));
263 }
264
265 private static char encodeHex(int digit) {
266 Utils.assert(digit >= 0 && digit < 16, "invalid hex digit: " + digit);
267 if (digit < 10) {
268 return (char) ('0' + digit);
269 }
270
271 return (char) ('A' + digit - 10);
272 }
273
274 private static boolean isPrintable(char c) {
275 return (c >= 32) && (c <= 126);
276 }
277
278
279
280
281
282 private static void registerStandardConverters() {
283 JParam.registerHelperClass(StandardConverters.class);
284 }
285
286 public static class StandardConverters {
287 public static int promote_convert_char_to_int(char c) {
288 return c;
289 }
290
291 public static long promote_convert_char_to_long(char c) {
292 return c;
293 }
294
295 public static float promote_convert_char_to_float(char c) {
296 return c;
297 }
298
299 public static double promote_convert_char_to_double(char c) {
300 return c;
301 }
302
303 public static short promote_convert_byte_to_short(byte b) {
304 return b;
305 }
306
307 public static int promote_convert_byte_to_int(byte b) {
308 return b;
309 }
310
311 public static long promote_convert_byte_to_long(byte b) {
312 return b;
313 }
314
315 public static float promote_convert_byte_to_float(byte b) {
316 return b;
317 }
318
319 public static double promote_convert_byte_to_double(byte b) {
320 return b;
321 }
322
323 public static int promote_convert_short_to_int(short s) {
324 return s;
325 }
326
327 public static long promote_convert_short_to_long(short s) {
328 return s;
329 }
330
331 public static float promote_convert_short_to_float(short s) {
332 return s;
333 }
334
335 public static double promote_convert_short_to_double(short s) {
336 return s;
337 }
338
339 public static long promote_convert_int_to_long(int i) {
340 return i;
341 }
342
343 public static float std_convert_int_to_float(int i) {
344 return i;
345 }
346
347 public static double promote_convert_int_to_double(int i) {
348 return i;
349 }
350
351 public static double std_convert_long_to_double(long l) {
352 return l;
353 }
354
355 public static double promote_convert_float_to_double(float f) {
356 return f;
357 }
358 }
359 }