1
2
3
4
5
6
7
8
9
10
11 package com.legstar.coxb.convert.simple;
12
13 import java.io.UnsupportedEncodingException;
14 import java.util.ArrayList;
15 import java.util.List;
16
17 import com.legstar.coxb.CobolContext;
18 import com.legstar.coxb.ICobolArrayNationalBinding;
19 import com.legstar.coxb.ICobolNationalBinding;
20 import com.legstar.coxb.convert.ICobolNationalConverter;
21 import com.legstar.coxb.convert.CobolConversionException;
22 import com.legstar.coxb.host.HostData;
23 import com.legstar.coxb.host.HostException;
24
25
26
27
28
29
30
31
32 public class CobolNationalSimpleConverter extends CobolSimpleConverter
33 implements ICobolNationalConverter {
34
35
36 private static final byte[] SPACE_UTF_16 = {0x00, 0x20};
37
38
39 private static final String NATIONAL_CHARSET = "UTF-16BE";
40
41
42
43
44 public CobolNationalSimpleConverter(final CobolContext cobolContext) {
45 super(cobolContext);
46 }
47
48
49 public int toHost(
50 final ICobolNationalBinding ce,
51 final byte[] hostTarget,
52 final int offset)
53 throws HostException {
54 int newOffset = 0;
55 try {
56 newOffset = toHostSingle(ce.getStringValue(),
57 ce.getByteLength(),
58 ce.isJustifiedRight(),
59 hostTarget,
60 offset);
61 } catch (CobolConversionException e) {
62 throwHostException(ce, e);
63 }
64 return newOffset;
65 }
66
67
68 public int toHost(
69 final ICobolArrayNationalBinding ce,
70 final byte[] hostTarget,
71 final int offset,
72 final int currentOccurs)
73 throws HostException {
74 int newOffset = offset;
75 try {
76 for (String javaSource : ce.getStringList()) {
77 newOffset = toHostSingle(javaSource,
78 ce.getItemByteLength(),
79 ce.isJustifiedRight(),
80 hostTarget,
81 newOffset);
82 }
83
84 for (int i = ce.getStringList().size();
85 i < currentOccurs; i++) {
86 newOffset = toHostSingle("",
87 ce.getItemByteLength(),
88 ce.isJustifiedRight(),
89 hostTarget,
90 newOffset);
91 }
92 } catch (CobolConversionException e) {
93 throwHostException(ce, e);
94 }
95 return newOffset;
96 }
97
98
99 public int fromHost(
100 final ICobolNationalBinding ce,
101 final byte[] hostSource,
102 final int offset)
103 throws HostException {
104 int newOffset = offset;
105 try {
106 String javaString = fromHostSingle(
107 ce.getByteLength(),
108 hostSource,
109 newOffset);
110 ce.setStringValue(javaString);
111 newOffset += ce.getByteLength();
112 } catch (CobolConversionException e) {
113 throwHostException(ce, e);
114 }
115 return newOffset;
116 }
117
118
119 public int fromHost(
120 final ICobolArrayNationalBinding ce,
121 final byte[] hostSource,
122 final int offset,
123 final int currentOccurs)
124 throws HostException {
125 List < String > lArray = new ArrayList < String >();
126 int newOffset = offset;
127 try {
128 for (int i = 0; i < currentOccurs; i++) {
129 String javaString = fromHostSingle(
130 ce.getItemByteLength(),
131 hostSource,
132 newOffset);
133 lArray.add(javaString);
134 newOffset += ce.getItemByteLength();
135 }
136 ce.setStringList(lArray);
137 } catch (CobolConversionException e) {
138 throwHostException(ce, e);
139 }
140 return newOffset;
141 }
142
143
144
145
146
147
148
149
150
151
152
153
154 public static final int toHostSingle(
155 final String javaString,
156 final int cobolByteLength,
157 final boolean isJustifiedRight,
158 final byte[] hostTarget,
159 final int offset)
160 throws CobolConversionException {
161
162
163 int lastOffset = offset + cobolByteLength;
164 if (lastOffset > hostTarget.length) {
165 throw (new CobolConversionException(
166 "Attempt to write past end of host source buffer",
167 new HostData(hostTarget), offset, cobolByteLength));
168 }
169
170
171
172 byte[] hostSource;
173
174
175
176 try {
177 if (javaString == null) {
178 hostSource =
179 "".getBytes(NATIONAL_CHARSET);
180 } else {
181 hostSource =
182 javaString.getBytes(NATIONAL_CHARSET);
183 }
184 } catch (UnsupportedEncodingException uee) {
185 throw new CobolConversionException(
186 "UnsupportedEncodingException:" + uee.getMessage());
187 }
188
189 int iSource = 0;
190 int hsl = hostSource.length;
191
192
193
194 int iTarget = offset;
195 boolean flip = true;
196
197
198 if (isJustifiedRight) {
199 while (iTarget < (lastOffset - hsl)) {
200 hostTarget[iTarget] = SPACE_UTF_16[(flip) ? 0 : 1];
201 iTarget++;
202 flip = !flip;
203 }
204 }
205
206
207 while (iSource < hostSource.length && iTarget < lastOffset) {
208 hostTarget[iTarget] = hostSource[iSource];
209 iSource++;
210 iTarget++;
211 }
212
213
214 flip = true;
215 if (!isJustifiedRight) {
216 while (iTarget < lastOffset) {
217 hostTarget[iTarget] = SPACE_UTF_16[(flip) ? 0 : 1];
218 iTarget++;
219 flip = !flip;
220 }
221 }
222
223 return lastOffset;
224 }
225
226
227
228
229
230
231
232
233
234 public static final String fromHostSingle(
235 final int cobolByteLength,
236 final byte[] hostSource,
237 final int offset)
238 throws CobolConversionException {
239
240 String javaString = null;
241 int javaStringLength = cobolByteLength;
242
243
244
245
246
247 int lastOffset = offset + cobolByteLength;
248 if (lastOffset > hostSource.length) {
249 if (offset >= hostSource.length) {
250 return javaString;
251 } else {
252 javaStringLength = hostSource.length - offset;
253 }
254 }
255
256
257
258 try {
259 javaString = new String(
260 hostSource, offset, javaStringLength,
261 NATIONAL_CHARSET);
262
263
264 if (javaString.indexOf("\0") != -1) {
265 javaString = javaString.replace('\0', ' ');
266 }
267 } catch (UnsupportedEncodingException uee) {
268 throw new CobolConversionException(
269 "UnsupportedEncodingException:" + uee.getMessage());
270 }
271
272 return javaString;
273 }
274 }