1 | // |
2 | // (c) Copyright, Moebius Solutions, 2002 |
3 | // |
4 | // All Rights Reserved |
5 | // |
6 | // This material may be reproduced by or for the U. S. Government |
7 | // pursuant to the copyright license under the clause at |
8 | // DFARS 252.227-7013 (OCT 1988). |
9 | // |
10 | /* |
11 | * $Id: JavaValuePrinter.java,v 1.1.1.5 2004/05/25 20:23:30 hastings Exp $ |
12 | */ |
13 | package com.moesol.generator.printer; |
14 | |
15 | import java.io.*; |
16 | import java.lang.reflect.*; |
17 | |
18 | /** |
19 | * Print types out in a manner suitable for Java programs. |
20 | */ |
21 | public class JavaValuePrinter extends ValuePrinter { |
22 | protected JavaValuePrinter() { } |
23 | private static JavaValuePrinter instance = new JavaValuePrinter(); |
24 | public static JavaValuePrinter getInstance() { |
25 | return instance; |
26 | } |
27 | public void print(Object value, Writer out) throws IOException { |
28 | if (value == null) { |
29 | out.write("null"); |
30 | return; |
31 | } |
32 | if (value instanceof String) { |
33 | printString(value.toString(), out); |
34 | return; |
35 | } |
36 | if (value instanceof Long) { |
37 | printLong((Long)value, out); |
38 | return; |
39 | } |
40 | if (value instanceof Integer) { |
41 | printInteger((Integer)value, out); |
42 | return; |
43 | } |
44 | if (value instanceof Short) { |
45 | printShort((Short)value, out); |
46 | return; |
47 | } |
48 | if (value instanceof Byte) { |
49 | printByte((Byte)value, out); |
50 | return; |
51 | } |
52 | if (value instanceof Character) { |
53 | printCharacter((Character)value, out); |
54 | return; |
55 | } |
56 | if (value instanceof Float) { |
57 | out.write(value.toString()); |
58 | return; |
59 | } |
60 | if (value instanceof Double) { |
61 | out.write(value.toString()); |
62 | return; |
63 | } |
64 | if (value instanceof Boolean) { |
65 | out.write(value.toString()); |
66 | return; |
67 | } |
68 | if (value.getClass().isArray()) { |
69 | printArray(value, out); |
70 | return; |
71 | } |
72 | // Uncle |
73 | out.write("null /* unable to print object as value */"); |
74 | } |
75 | private void printString(String s, Writer out) throws IOException { |
76 | out.write('"'); |
77 | for (int i = 0; i < s.length(); i++) { |
78 | char c = s.charAt(i); |
79 | // TODO The escapes below are really for the generator not java. |
80 | if (c == '\\') { |
81 | out.write("\\\\\\"); |
82 | } else if (c == '"') { |
83 | out.write('\\'); |
84 | } |
85 | out.write(c); |
86 | } |
87 | out.write('"'); |
88 | return; |
89 | } |
90 | private void printLong(Long value, Writer out) throws IOException { |
91 | out.write("0x"); |
92 | out.write(Long.toHexString(value.longValue())); |
93 | out.write("L"); |
94 | } |
95 | private void printInteger(Integer value, Writer out) throws IOException { |
96 | out.write("0x"); |
97 | out.write(Integer.toHexString(value.intValue())); |
98 | } |
99 | private void printShort(Short value, Writer out) throws IOException { |
100 | out.write("0x"); |
101 | out.write(Integer.toHexString(value.intValue() & 0xFFFF)); |
102 | } |
103 | private void printByte(Byte value, Writer out) throws IOException { |
104 | out.write("0x"); |
105 | out.write(Integer.toHexString(value.intValue() & 0xFF)); |
106 | } |
107 | private void printCharacter(Character value, Writer out) throws IOException { |
108 | out.write("'"); |
109 | out.write(value.toString()); |
110 | out.write("'"); |
111 | } |
112 | private void printArray(Object value, Writer out) throws IOException { |
113 | out.write("{ "); |
114 | int length = Array.getLength(value); |
115 | if (0 < length) { |
116 | print(Array.get(value, 0), out); |
117 | } |
118 | for (int i = 1; i < length; i++) { |
119 | out.write(", "); |
120 | print(Array.get(value, i), out); |
121 | } |
122 | out.write(" }"); |
123 | } |
124 | } |