1 /***
2 * Java DAB EPG API - Serialize/Deserialize To/From POJOs to XML/Binary as per
3 * ETSI specifications TS 102 818 (XML Specification for DAB EPG) and TS 102
4 * 371 (Transportation and Binary Encoding Specification for EPG).
5 *
6 * Copyright (C) 2007 GCap Media PLC
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22 package com.gcapmedia.dab.epg.binary;
23
24 import java.io.UnsupportedEncodingException;
25
26 /***
27 * String data using ISO/IEC 10646 with UTF-8 encoding.
28 *
29 * TODO The ISO/IEC 10646 characters 0xE000 to xF8FF shall not be
30 * included within any binary encoded strings.
31 */
32 public class StringType implements Encodable {
33
34 /***
35 * Content value
36 */
37 private String value;
38
39 /***
40 * String encoding
41 */
42 private static final String charset = "UTF-8";
43
44 /***
45 * Create a new string type
46 * @param value Content value
47 */
48 public StringType(String value) {
49 this.value = value;
50 }
51
52 /***
53 * @see com.gcapmedia.dab.epg.binary.Encodable#getBytes()
54 */
55 public byte[] getBytes() {
56 try {
57 return value.getBytes(charset);
58 } catch (UnsupportedEncodingException e) {
59 throw new IllegalArgumentException("Requested character set not supported: " + charset);
60 }
61 }
62
63 /***
64 * Parse a string type from its byte array representation
65 */
66 public static StringType fromBytes(byte[] bytes) {
67 StringType result = null;
68 try {
69 result = new StringType(new String(bytes, charset));
70 } catch (UnsupportedEncodingException e) {
71 throw new IllegalArgumentException("Requested character set not supported: " + charset);
72 }
73 return result;
74 }
75
76 /***
77 * @see com.gcapmedia.dab.epg.binary.Encodable#getLength()
78 */
79 public int getLength() {
80 return getBytes().length;
81 }
82
83 /***
84 * @see java.lang.Object#toString()
85 */
86 public String toString() {
87 return value;
88 }
89
90 /***
91 * @see java.lang.Object#equals(java.lang.Object)
92 */
93 @Override
94 public boolean equals(Object obj) {
95 if(!(obj instanceof StringType)) {
96 return false;
97 }
98 StringType that = (StringType)obj;
99 return this.value.equals(that.value);
100 }
101
102
103
104 }