summaryrefslogtreecommitdiffstats
path: root/src/org/uic/barcode/staticFrame/UTLAYDataRecord.java
blob: 2e9a2dc13b2d9f15f1de1d67a8bce9e921e24b86 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
/*
 * 
 */
package org.uic.barcode.staticFrame;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;

import org.uic.barcode.staticHeader.ticketLayoutBarcode.FormatType;
import org.uic.barcode.staticHeader.ticketLayoutBarcode.LayoutElement;
import org.uic.barcode.staticHeader.ticketLayoutBarcode.TicketLayout;
import org.uic.ticket.EncodingFormatException;

/**
 * The Class UTLAYDataRecord implements a bar code data record containing the ticket layout.
 */
public class UTLAYDataRecord extends DataRecord {
	
	/** The ticket layout. */
	private TicketLayout layout;
	
	/**
	 * Instantiates a new empty UTLAY data record.
	 */
	public UTLAYDataRecord() {
		super("U_TLAY","01");
	}

	/**
	 * Decode utf-8 string.
	 *
	 * @param byteData the byte data
	 * @param offset the offset
	 * @param length the length
	 * @return the string
	 * @throws UnsupportedEncodingException the unsupported encoding exception
	 */
	private static String decodeUtf8String(byte[] byteData, int offset, int length) throws UnsupportedEncodingException {	
		byte[] bytes = new byte[length];
		for (int i = 0; i < length; i++){
			bytes[i] = byteData[i + offset];
		}
		return StandardCharsets.UTF_8.decode(ByteBuffer.wrap(bytes)).toString();
	}

	/**
	 * Decode string.
	 *
	 * @param byteData the byte data
	 * @param offset the offset
	 * @param length the length
	 * @return the string
	 */
	private static String decodeString(byte[] byteData, int offset, int length) {
		byte[] bytes = new byte[length];
		for (int i = 0; i < length; i++){
			bytes[i] = byteData[i + offset];
		}
		return StandardCharsets.ISO_8859_1.decode(ByteBuffer.wrap(bytes)).toString();
	}
	
	/**
	 * Encode utf-8.
	 *
	 * @param value the value
	 * @return the byte[]
	 */
	private static byte[] encodeUtf8(String value) {
		
		try {
			return value.getBytes("UTF-8");
		} catch (UnsupportedEncodingException e) {			
			throw new RuntimeException("UTF8 String encoding wrong!",e);
		}		
	}
	
	/**
	 * To string.
	 *
	 * @return the string
	 */
	public String toString() {
		
		StringBuilder sb = new StringBuilder();
		
		sb.append("TLB: ").append(layout.getLayoutStandard()).append('\n');
				
		for (LayoutElement e : layout.getElements()){
			sb.append("column: ").append(e.getColumn()).append(" - ");
			sb.append("line: ").append(e.getLine()).append(" - ");
			sb.append("width: ").append(e.getWidth()).append(" - ");
			sb.append("heigth: ").append(e.getHeight()).append(" - ");
			sb.append("text: ").append(e.getText()).append(" - ");
			sb.append("format: ").append(e.getFormat().toString()).append('\n');
		}
			
		return sb.toString();
		
	}

	/**
	 * Decode content.
	 *
	 * @throws IOException Signals that an I/O exception has occurred.
	 * @throws EncodingFormatException the encoding format exception
	 */
	@Override
	protected void decodeContent() throws IOException, EncodingFormatException {
		
		layout = new TicketLayout();
		
		if (content == null || content.length == 0 ) return;
			
		int offset = 0;
		
		String layoutType = decodeString(content, offset , 4);
		layout.setLayoutStandard(layoutType);
		offset = offset + 4;
				
		String numberValue = decodeString(content, offset , 4);
		offset = offset + 4;
		
		int elements = 0;
		try {
			elements = Integer.parseInt(numberValue);			
		} catch(NumberFormatException e){
			//Do Nothing			
		}
		
		int remainingBytes = content.length - offset; 
		
		for (int i = 0; i < elements && remainingBytes > 0 ;i++){
			
			String lineValue = decodeString(content, offset , 2);
			offset = offset + 2;			
			int line = 0;
			try {
				line = Integer.parseInt(lineValue);			
			} catch(NumberFormatException e){
				//Do Nothing			
			}			
			String columnValue = decodeString(content, offset , 2);
			offset = offset + 2;
			int column = 0;
			try {
				column = Integer.parseInt(columnValue);			
			} catch(NumberFormatException e){
				//Do Nothing			
			}			
			String heightValue = decodeString(content, offset , 2);
			offset = offset + 2;
			int height = 0;
			try {
				height = Integer.parseInt(heightValue);			
			} catch(NumberFormatException e){
				//Do Nothing			
			}			
			String widthValue = decodeString(content, offset , 2);
			offset = offset + 2;
			int width = 0;
			try {
				width = Integer.parseInt(widthValue);			
			} catch(NumberFormatException e){
				//Do Nothing			
			}			
			String formatValue = decodeString(content, offset , 1);
			offset = offset + 1;
			int format = 0;
			try {
				format = Integer.parseInt(formatValue);			
			} catch(NumberFormatException e){
				//Do Nothing			
			}			
			String lengthValue = decodeString(content, offset , 4);
			offset = offset + 4;
			int length = 0;
			try {
				length = Integer.parseInt(lengthValue);			
			} catch(NumberFormatException e){
				//Do Nothing			
			}			

			String text;
			try {
				text = decodeUtf8String(content, offset ,length);
			} catch (UnsupportedEncodingException e) {
				text = "unsupported character set";
			}
			offset = offset + length;
			
			LayoutElement layoutElement = new LayoutElement();   
			
			layoutElement.setColumn(column);
			layoutElement.setLine(line);
			layoutElement.setHeight(height);
			layoutElement.setWidth(width);
			layoutElement.setText(text);
			
			layoutElement.setFormat(FormatType.values()[format]);
						
			layout.addLayoutElement(layoutElement);
			
		}

	}

	/**
	 * Encode content.
	 *
	 * @throws IOException Signals that an I/O exception has occurred.
	 * @throws EncodingFormatException the encoding format exception
	 */
	@Override
	protected void encodeContent() throws IOException, EncodingFormatException {
		
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        
        if (layout == null || layout.getElements() == null || layout.getElements().isEmpty()) {
        	return;
        }

		//number of text elements
		String numberOfFields = String.format("%04d",layout.getElements().size());
			
		outputStream.write(layout.getLayoutStandard().getBytes());
		outputStream.write(numberOfFields.getBytes());
			
		for (LayoutElement e : layout.getElements()){
				
			String line   = String.format("%02d",e.getLine());
			String column = String.format("%02d",e.getColumn());
			String heigth = String.format("%02d",e.getHeight());
			String width  = String.format("%02d",e.getWidth());
			String format = String.format("%01d",e.getFormat().ordinal());
			String size   = String.format("%04d",encodeUtf8(e.getText()).length);

			outputStream.write(line.getBytes());
			outputStream.write(column.getBytes());
			outputStream.write(heigth.getBytes());
			outputStream.write(width.getBytes());
			outputStream.write(format.getBytes());
			outputStream.write(size.getBytes());
			outputStream.write(encodeUtf8(e.getText()));
				
		}			      
        
		content = outputStream.toByteArray();
	}
	
	/**
	 * Sets the layout.
	 *
	 * @param layout the new layout
	 */
	public void setLayout(TicketLayout layout) {
		this.layout = layout;
	}

	
	/**
	 * Gets the layout.
	 *
	 * @return the layout
	 */
	public TicketLayout getLayout() {
		return layout;
	}

}