summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/uic/barcode/staticFrame/UHEADDataRecord.java
blob: 22ba8ce6536a974b78d1a7e9b062b74704a68b5e (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
273
274
275
276
277
278
279
280
281
package org.uic.barcode.staticFrame;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import org.uic.barcode.ticket.EncodingFormatException;

/**
 * The Class UHEADDataRecord implements the additional header record of a statis UIC bar code
 */
public class UHEADDataRecord extends DataRecord{
	
	
		/** The issuing date. */
		private Date issuingDate = null;
		
		/** The flags. */
		private int flags;
		
		/** The issuer. */
		private String issuer;
		
		/** The identifier. */
		private String identifier;
		
		/** The language. */
		private String language;
		
		/** The additional language. */
		private String additionalLanguage;
	
		/**
		 * Instantiates a new UHEAD data record.
		 */
		public UHEADDataRecord() {
			super("U_HEAD");
		}
		
		
		/**
		 * Gets the issuing date.
		 *
		 * @return the issuing date
		 */
		public Date getIssuingDate() {
			return issuingDate;
		}
		
		/**
		 * Sets the issuing date.
		 *
		 * @param issuingDate the new issuing date
		 */
		public void setIssuingDate(Date issuingDate) {
			this.issuingDate = issuingDate;
		}
		
		/**
		 * Gets the flags.
		 *
		 * @return the flags
		 */
		public int getFlags() {
			return flags;
		}
		
		/**
		 * Sets the flags.
		 *
		 * @param flags the new flags
		 */
		public void setFlags(int flags) {
			this.flags = flags;
		}
		
		/**
		 * Gets the issuer.
		 *
		 * @return the issuer
		 */
		public String getIssuer() {
			return issuer;
		}
		
		/**
		 * Sets the issuer.
		 *
		 * @param issuer the new issuer
		 */
		public void setIssuer(String issuer) {
			this.issuer = issuer;
		}
		
		/**
		 * Gets the identifier.
		 *
		 * @return the identifier
		 */
		public String getIdentifier() {
			return identifier;
		}
		
		/**
		 * Sets the identifier.
		 *
		 * @param identifier the new identifier
		 */
		public void setIdentifier(String identifier) {
			this.identifier = identifier;
		}
		
		/**
		 * Gets the language.
		 *
		 * @return the language
		 */
		public String getLanguage() {
			return language;
		}
		
		/**
		 * Sets the language.
		 *
		 * @param language the new language
		 */
		public void setLanguage(String language) {
			this.language = language;
		}
		
		/**
		 * Gets the additional language.
		 *
		 * @return the additional language
		 */
		public String getAdditionalLanguage() {
			return additionalLanguage;
		}
		
		/**
		 * Sets the additional language.
		 *
		 * @param additionalLanguage the new additional language
		 */
		public void setAdditionalLanguage(String additionalLanguage) {
			this.additionalLanguage = additionalLanguage;
		}
			

		/**
		 * Decode content.
		 *
		 * @throws IOException Signals that an I/O exception has occurred.
		 * @throws EncodingFormatException the encoding format exception
		 */
		protected void decodeContent() throws IOException, EncodingFormatException{
						
			if (content == null || content.length == 0 ) return;
			
			issuer = decodeString(content, 0 , 4);	

			identifier = decodeString(content, 4 , 20);		
			
			String issuingDateString = decodeString(content, 24 , 12);		

			String flagsString  = decodeString(content,36 , 1);
			
			language = decodeString(content, 37 , 2);

			additionalLanguage = decodeString(content,39 , 2);
			

			try {
				flags = Integer.parseInt(flagsString);
			} catch (Exception e) {
				flags = 9;
			}		

			// date format "DDMMYYYYHHMM"
			SimpleDateFormat formatter = new SimpleDateFormat("ddMMyyyyHHmm");
			try {
				issuingDate = formatter.parse(issuingDateString);
			} catch (ParseException e) {
				e.printStackTrace();
			}

		}
		

		/**
		 * Decode string.
		 *
		 * @param byteData the byte data
		 * @param offset the offset
		 * @param length the length
		 * @return the string
		 */
		private String decodeString(byte[] byteData, int offset, int length) {
			
			char[] chars = new char[length];
			
			for (int i = 0; i < length  && i < byteData.length;i++) {
				byte byteValue = byteData[offset + i];
				if (byteValue == '\n') {
					byteValue = ' ';
				}
				chars[i] = (char) byteValue;
			}
			
			return String.copyValueOf(chars);
		}

		/**
		 * Encode content.
		 *
		 * @throws IOException Signals that an I/O exception has occurred.
		 * @throws EncodingFormatException the encoding format exception
		 */
		protected void encodeContent() throws IOException, EncodingFormatException {
			
	        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
	    	
  	     	String issuerElement = String.format("%4s", this.issuer);

			String idElement = String.format("%20s", this.identifier);
			
			//DDMMYYYYHHMM"		
			Calendar now = Calendar.getInstance();
			
			// issuing date can be in the ticket or in the header
			if (this.issuingDate != null) {
				now.setTime(this.issuingDate);
			}
			
			String timeElement = String.format("%02d%02d%04d%02d%02d", 
					                           now.get(Calendar.DAY_OF_MONTH),
					                           now.get(Calendar.MONTH),
					                           now.get(Calendar.YEAR),
					                           now.get(Calendar.HOUR),
					                           now.get(Calendar.MINUTE));
			
				
			String flagsElement = String.format("%01d",this.flags);
			
			String language1 = null;
			String language2 = null;
			if (this.language != null) {
				language1 = String.format("%2s" ,this.language);
			} else {
				language1 = "  ";
			}
			if (this.additionalLanguage != null) {
				language2 = String.format("%2s" ,this.additionalLanguage);
			} else {
				language2 = "  ";
			}
			
			String languageElement =  language1 + language2;
			
			try {
				
				outputStream.write(issuerElement.getBytes());				
				outputStream.write(idElement.getBytes());	
				outputStream.write(timeElement.getBytes());		
				
				outputStream.write(flagsElement.getBytes());	
				outputStream.write(languageElement.getBytes());			
				
			} catch (IOException e) {
				e.printStackTrace();
			}
			
			super.setData(outputStream.toByteArray());

			
		}

}