1
2
3
4
5
6
7
8
9
10 package it.imolinfo.jbi4cics.typemapping.cobol;
11
12 import it.imolinfo.jbi4cics.Logger;
13 import it.imolinfo.jbi4cics.LoggerFactory;
14 import it.imolinfo.jbi4cics.exception.FormatException;
15 import java.lang.reflect.Array;
16 import java.math.BigDecimal;
17 import java.math.BigInteger;
18
19
20
21
22
23
24
25
26 public enum CobolType {
27
28
29 STRING {
30 Class getPreferredJavaType(CobolTypeDescriptor desc) {
31 return String.class;
32 }
33
34 int getBufferedLength(CobolTypeDescriptor desc) throws FormatException {
35 return desc.getStringLength();
36 }
37
38 void format(Object value, byte[] buffer, CobolTypeDescriptor desc,
39 int startingOffset) throws FormatException {
40 if (value == null) {
41
42
43
44
45
46 value = "";
47 } else if (!(value instanceof String)) {
48 LOG.error("CIC002104_String_not_found", value.getClass());
49 throw new FormatException("CIC002104_String_not_found",
50 new Object[] { value.getClass() });
51 }
52 StringMarshaller.marshallStringIntoBuffer((String) value, buffer,
53 startingOffset, desc.getCodePage(),
54 desc.getBufferedLength(), desc.getJustification(),
55 desc.getPadCharacter());
56 }
57
58 Object unformat(byte[] buffer, CobolTypeDescriptor desc,
59 int startingOffset) throws FormatException {
60 return StringMarshaller.unmarshallString(buffer, startingOffset,
61 desc.getCodePage(), desc.getBufferedLength()).trim();
62 }
63 },
64
65
66 INTEGER {
67 Class getPreferredJavaType(CobolTypeDescriptor desc) {
68 return BigInteger.class;
69 }
70
71 int getBufferedLength(CobolTypeDescriptor desc) throws FormatException {
72 int length = desc.getIntegerPartLength();
73
74 if ((length < 0) || (length >= BUFFERED_LENGTH.length)) {
75 LOG.error("CIC002109_Integer_too_much_long", length);
76 throw new FormatException("CIC002109_Integer_too_much_long",
77 new Object[] { length });
78 }
79 return BUFFERED_LENGTH[length];
80 }
81
82 void format(Object value, byte[] buffer, CobolTypeDescriptor desc,
83 int startingOffset) throws FormatException {
84 if (value == null) {
85
86
87
88
89
90 value = BigInteger.ZERO;
91 }
92 CobolFieldFormatter.formatInteger(value, buffer, desc,
93 startingOffset);
94 }
95
96 Object unformat(byte[] buffer, CobolTypeDescriptor desc,
97 int startingOffset) throws FormatException {
98 return IntegerMarshaller.unmarshallInteger(buffer, startingOffset,
99 desc.getBufferedLength(), desc.isBigEndian());
100 }
101 },
102
103
104 PACKED_DECIMAL {
105 Class getPreferredJavaType(CobolTypeDescriptor desc) {
106 return BigDecimal.class;
107 }
108
109 int getBufferedLength(CobolTypeDescriptor desc) throws FormatException {
110 return Math.round(((float) (desc.getIntegerPartLength()
111 + desc.getDecimalPartLength() + 1)) / 2);
112 }
113
114 void format(Object value, byte[] buffer, CobolTypeDescriptor desc,
115 int startingOffset) throws FormatException {
116 if (value == null){
117
118
119
120
121
122 value = BigDecimal.ZERO;
123 }
124 CobolFieldFormatter.formatPackedDecimal(value, buffer, desc,
125 startingOffset);
126 }
127
128 Object unformat(byte[] buffer, CobolTypeDescriptor desc,
129 int startingOffset) throws FormatException {
130 return PackedDecimalMarshaller.unmarshallBigDecimal(buffer, desc,
131 startingOffset);
132 }
133 },
134
135
136 ZONED {
137 Class getPreferredJavaType(CobolTypeDescriptor desc) {
138 return BigDecimal.class;
139 }
140
141 int getBufferedLength(CobolTypeDescriptor desc) throws FormatException {
142
143
144 int length = desc.getIntegerPartLength()
145 + desc.getDecimalPartLength();
146
147 if (desc.getZonedSignFormat()
148 == CobolTypeDescriptor.SIGN_FORMAT_LEADING_SEPARATE) {
149 length++;
150 }
151 return length;
152
153 }
154
155 void format(Object value, byte[] buffer, CobolTypeDescriptor desc,
156 int startingOffset) throws FormatException {
157 if (value == null) {
158
159
160
161
162
163 value = BigDecimal.ZERO;
164 }
165 CobolFieldFormatter.formatZoned(value, buffer, desc,
166 startingOffset);
167 }
168
169 Object unformat(byte[] buffer, CobolTypeDescriptor desc,
170 int startingOffset) throws FormatException {
171 return ZonedMarshaller.unmarshallZoned(buffer, startingOffset,
172 desc.getBufferedLength(), desc.isSigned(),
173 desc.getVirtualDecimalPoint(), desc.getZonedSignFormat(),
174 desc.getCodePage());
175 }
176 },
177
178
179 NESTED_COMMAREA {
180 Class getPreferredJavaType(CobolTypeDescriptor desc) {
181 return desc.getNestedCommarea().getBeanClass();
182 }
183
184 int getBufferedLength(CobolTypeDescriptor desc) throws FormatException {
185 return desc.getNestedCommarea().getBufferedLength();
186 }
187
188 void format(Object value, byte[] buffer, CobolTypeDescriptor desc,
189 int startingOffset) throws FormatException {
190 if (value == null) {
191
192
193
194
195
196 throw new FormatException("CIC002100_Null_nested_commarea");
197 }
198 CobolFieldFormatter.formatNestedCommarea(value, buffer, desc,
199 startingOffset);
200 }
201
202 Object unformat(byte[] buffer, CobolTypeDescriptor desc,
203 int startingOffset) throws FormatException {
204 return CobolFieldFormatter.unformatNestedCommarea(buffer, desc,
205 startingOffset);
206 }
207 },
208
209
210
211
212 OCCURS {
213 Class getPreferredJavaType(CobolTypeDescriptor desc) {
214 Class beanClass = desc.getNestedCommarea().getBeanClass();
215
216 return Array.newInstance(beanClass, 0).getClass();
217 }
218
219 int getBufferedLength(CobolTypeDescriptor desc) throws FormatException {
220 return desc.getNestedCommarea().getBufferedLength()
221 * desc.getOccursSize();
222 }
223
224 void format(Object value, byte[] buffer, CobolTypeDescriptor desc,
225 int startingOffset) throws FormatException {
226 if (value == null) {
227
228
229
230
231
232 throw new FormatException("CIC002101_Null_occurs_value");
233 }
234 CobolFieldFormatter.formatOccurs(value, buffer, desc,
235 startingOffset);
236 }
237
238 Object unformat(byte[] buffer, CobolTypeDescriptor desc,
239 int startingOffset) throws FormatException {
240 return CobolFieldFormatter.unformatOccurs(buffer, desc,
241 startingOffset);
242 }
243 };
244
245
246
247
248
249
250 private static final int[] BUFFERED_LENGTH = new int[] {
251
252
253
254 1, 1, 1,
255
256
257
258 2, 2,
259
260
261
262 4, 4, 4, 4, 4,
263
264
265
266 8, 8, 8, 8, 8, 8, 8, 8, 8,
267
268
269
270 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
271 16
272 };
273
274
275
276
277 private static final Logger LOG = LoggerFactory.getLogger(CobolType.class);
278
279 abstract Class getPreferredJavaType(CobolTypeDescriptor desc);
280
281 abstract int getBufferedLength(CobolTypeDescriptor desc)
282 throws FormatException;
283
284 abstract void format(Object value, byte[] buffer, CobolTypeDescriptor desc,
285 int startingOffset) throws FormatException;
286
287 abstract Object unformat(byte[] buffer, CobolTypeDescriptor desc,
288 int startingOffset) throws FormatException;
289 }