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;
23
24 import java.net.URI;
25 import java.net.URISyntaxException;
26
27 /***
28 * <p>
29 * Indicates the genre of a programme, group or service (audio or data). The
30 * genre scheme is based on that used by TV-Anytime. The supported classification
31 * schemes are:
32 *
33 * <ul>
34 * <li>IntentionCS</li>
35 * <li>FormatCS</li>
36 * <li>ContentCS</li>
37 * <li>OriginationCS</li>
38 * <li>IntendedAudienceCS</li>
39 * <li>ContentAlert</li>
40 * <li>MediaTypeCS</li>
41 * <li>AtmosphereCS</li>
42 * </ul>
43 * </p>
44 *
45 * <p>
46 * The <b>href</b> is the only required element and this specifies the genre, the
47 * Classification Scheme (CS) and the genre scheme used.
48 * </p>
49 *
50 * <p>
51 * The <b>definition</b> element, if used, should contain a description of the
52 * genre.
53 * </p>
54 *
55 * <p>
56 * Both of the above elements are intended to make the element more readable for
57 * humans.
58 * </p>
59 *
60 * <p>The <b>type</b> attribute indicates the type of the genre. The types of genres
61 * are defined as follows:
62 *
63 * <ul>
64 * <li><b>main</b>: The specified genre is the main, or primary. This is the default
65 * value.</li>
66 *
67 * <li><b>secondary</b>: The specified genre is a secondary genre, such as a subgenre.</li>
68 *
69 * <li><b>other</b>: The specified genre is an alternative genre, such as one defined
70 * or used by third parties.</li>
71 *
72 * </ul>
73 */
74 public class Genre {
75
76 /***
77 * Genre description
78 */
79 private String definition;
80
81 /***
82 * Genre type
83 */
84 private Type type = Type.MAIN;
85
86 private Scheme scheme;
87
88 private int level1;
89
90 private int level2;
91
92 private int level3;
93
94 private final static int YEAR = 2005;
95
96 /***
97 * Create a new genre
98 * @param href Genre HREF
99 */
100 public Genre(Scheme scheme) {
101 this.type = Type.MAIN;
102 this.scheme = scheme;
103 }
104
105 /***
106 * Create a new genre
107 * @param scheme Genre scheme
108 * @param definition Descriptive definition
109 * @param type Genre type
110 * @param level1
111 * @param level2
112 * @param level3
113 */
114 public Genre(Scheme scheme, String definition, Type type, int level1, int level2, int level3) {
115 this.scheme = scheme;
116 this.type = type;
117 this.definition = definition;
118 this.level1 = level1;
119 this.level2 = level2;
120 this.level3 = level3;
121 }
122
123 /***
124 * Set the genre type
125 * @param type Genre type
126 */
127 public void setType(Type type) {
128 this.type = type;
129 }
130
131 /***
132 * @return Returns the genre scheme
133 */
134 public Scheme getScheme() {
135 return scheme;
136 }
137
138 public void setLevel1(int level1) {
139 this.level1 = level1;
140 }
141
142 public int getLevel1() {
143 return level1;
144 }
145
146 public boolean hasLevel1() {
147 return level1 > 0;
148 }
149
150 public void setLevel2(int level2) {
151 this.level2 = level2;
152 }
153
154 public int getLevel2() {
155 return level2;
156 }
157
158 public boolean hasLevel2() {
159 return level2 > 0;
160 }
161
162 public void setLevel3(int level3) {
163 this.level3 = level3;
164 }
165
166 public int getLevel3() {
167 return level3;
168 }
169
170 public boolean hasLevel3() {
171 return level3 > 0;
172 }
173
174 /***
175 * @return Returns the Genre HREF
176 */
177 public URI getHref() throws URISyntaxException {
178 StringBuilder buf = new StringBuilder("urn:tva:metadata:cs:" + scheme.name + ":" + YEAR + ":" + scheme.id);
179 if(hasLevel1()) {
180 buf.append("." + level1);
181 }
182 if(hasLevel2()) {
183 buf.append("." + level2);
184 }
185 if(hasLevel3()) {
186 buf.append("." + level3);
187 }
188 return new URI(buf.toString());
189 }
190
191 /***
192 * @return Returns the Genre definition
193 */
194 public String getDefinition() {
195 return definition;
196 }
197
198 /***
199 * @return Returns the genre type
200 */
201 public Type getType() {
202 return type;
203 }
204
205 /***
206 * @see java.lang.Object#equals(java.lang.Object)
207 */
208 @Override
209 public boolean equals(Object obj) {
210 if(!(obj instanceof Genre)) {
211 return false;
212 }
213 Genre that = (Genre)obj;
214
215
216 if(this.type != that.type) {
217 return false;
218 }
219
220
221 if(this.scheme != that.scheme) {
222 return false;
223 }
224
225
226 if((this.hasLevel1() && !that.hasLevel1()) ||
227 (!this.hasLevel1() && that.hasLevel1())) {
228 return false;
229 } else {
230 if(this.getLevel1() != that.getLevel1()) {
231 return false;
232 }
233 }
234 if((this.hasLevel2() && !that.hasLevel2()) ||
235 (!this.hasLevel2() && that.hasLevel2())) {
236 return false;
237 } else {
238 if(this.getLevel2() != that.getLevel2()) {
239 return false;
240 }
241 }
242 if((this.hasLevel3() && !that.hasLevel3()) ||
243 (!this.hasLevel3() && that.hasLevel3())) {
244 return false;
245 } else {
246 if(this.getLevel3() != that.getLevel3()) {
247 return false;
248 }
249 }
250
251 return true;
252 }
253
254 /***
255 * @see java.lang.Object#toString()
256 */
257 public String toString() {
258 String result = null;
259 try {
260 result = getHref().toString();
261 } catch (URISyntaxException e) {
262 }
263 return result;
264 }
265
266 /***
267 * Genre type
268 */
269 public enum Type {
270
271 /***
272 * The specified genre is the main, or primary.
273 */
274 MAIN("main"),
275
276 /***
277 * The specified genre is a secondary genre
278 */
279 SECONDARY("secondary"),
280
281 /***
282 * The specified genre is an alternative
283 */
284 OTHER("other");
285
286 private String value;
287
288 private Type(String value) {
289 this.value = value;
290 }
291
292 public String toString() {
293 return value;
294 }
295
296 }
297
298 public enum Scheme {
299
300 INTENTION(1, "IntentionCS"),
301
302 FORMAT(2, "FormatCS"),
303
304 CONTENT(3, "ContentCS"),
305
306 INTENDED_AUDIENCE(4, "IntendedAudienceCS"),
307
308 ORIGINATION(5, "OriginationCS"),
309
310 CONTENT_ALERT(6, "ContentAlertCS"),
311
312 MEDIA_TYPE(7, "MediaTypeCS"),
313
314 ATMOSPHERE(8, "AtmosphereCS");
315
316 private int id;
317
318 private String name;
319
320 Scheme(int id, String name) {
321 this.id = id;
322 this.name = name;
323 }
324
325 public int getId() {
326 return id;
327 }
328
329 public String getName() {
330 return name;
331 }
332
333 public static Scheme fromId(int id) {
334 Scheme result = null;
335 for(Scheme scheme : values()) {
336 if(scheme.id == id) {
337 result = scheme;
338 break;
339 }
340 }
341 if(result == null) {
342 throw new IllegalArgumentException("Scheme not found with id " + id);
343 }
344 return result;
345 }
346
347 }
348 }