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.util.Locale;
25
26 import com.gcapmedia.dab.epg.*;
27
28 /***
29 *
30 */
31 public class ProgrammeDeconstructor implements Deconstructor<Programme> {
32
33 /***
34 * @see com.gcapmedia.dab.epg.binary.Deconstructor#deconstruct(com.gcapmedia.dab.epg.binary.Element)
35 */
36 public Programme deconstruct(Element element) {
37
38 if(!element.hasAttribute(AttributeTag.programmeShortId)) {
39 throw new IllegalArgumentException("A Short CRID must be specified for this programme");
40 }
41
42 Attribute scridAttribute = element.getAttribute(AttributeTag.programmeShortId);
43 ShortCridType scrid = (ShortCridType)scridAttribute.getValue();
44 Programme programme = new Programme(scrid.getShortCrid());
45
46
47 if(element.hasAttribute(AttributeTag.programmeId)) {
48 Attribute attribute = element.getAttribute(AttributeTag.programmeId);
49 CridType type = (CridType)attribute.getValue();
50 programme.setId(type.getCrid());
51 }
52 if(element.hasAttribute(AttributeTag.programmeVersion)) {
53 Attribute attribute = element.getAttribute(AttributeTag.programmeVersion);
54 NumericType type = (NumericType)attribute.getValue();
55 programme.setVersion(type.getNumber());
56 }
57 if(element.hasAttribute(AttributeTag.programmeRecommendation)) {
58 Attribute attribute = element.getAttribute(AttributeTag.programmeRecommendation);
59 EnumType type = (EnumType)attribute.getValue();
60 if(type == EnumType.programmeRecommendationYes) {
61 programme.setRecommendation(Recommendation.YES);
62 } else {
63 programme.setRecommendation(Recommendation.NO);
64 }
65 }
66 if(element.hasAttribute(AttributeTag.programmeBroadcast)) {
67 Attribute attribute = element.getAttribute(AttributeTag.programmeBroadcast);
68 EnumType type = (EnumType)attribute.getValue();
69 if(type == EnumType.programmeBroadcastOnair) {
70 programme.setBroadcastFlag(BroadcastFlag.ON_AIR);
71 } else {
72 programme.setBroadcastFlag(BroadcastFlag.OFF_AIR);
73 }
74 }
75 if(element.hasAttribute(AttributeTag.programmeBitRate)) {
76 Attribute attribute = element.getAttribute(AttributeTag.programmeBitRate);
77 NumericType type = (NumericType)attribute.getValue();
78 programme.setBitrate(type.getNumber());
79 }
80 if(element.hasAttribute(AttributeTag.programmeLang)) {
81 Attribute attribute = element.getAttribute(AttributeTag.programmeLang);
82 LanguageType type = (LanguageType)attribute.getValue();
83 programme.setLocale(type.getLocale());
84 }
85
86
87 if(!element.hasChildren(ElementTag.location)) {
88 throw new IllegalArgumentException("Programme must be found by at least one location");
89 }
90 if(element.hasChildren(ElementTag.shortName)) {
91 for(Element child : element.getChildren(ElementTag.shortName)) {
92 Locale locale = Locale.getDefault();
93 if(child.hasAttribute(AttributeTag.shortNameLang)) {
94 Attribute attribute = child.getAttribute(AttributeTag.shortNameLang);
95 LanguageType type = (LanguageType)attribute.getValue();
96 locale = type.getLocale();
97 }
98 String text = child.getCData().getString().toString();
99 ShortName name = new ShortName(text, locale);
100 programme.getNames().addShortName(name);
101 }
102 }
103 if(!element.hasChildren(ElementTag.mediumName)) {
104 throw new IllegalArgumentException("At least one programme medium name must be specified");
105 }
106 for(Element child : element.getChildren(ElementTag.mediumName)) {
107 Locale locale = Locale.getDefault();
108 if(child.hasAttribute(AttributeTag.mediumNameLang)) {
109 Attribute attribute = child.getAttribute(AttributeTag.mediumNameLang);
110 LanguageType type = (LanguageType)attribute.getValue();
111 locale = type.getLocale();
112 }
113 String text = child.getCData().getString().toString();
114 MediumName name = new MediumName(text, locale);
115 programme.getNames().addMediumName(name);
116 }
117 if(element.hasChildren(ElementTag.longName)) {
118 for(Element child : element.getChildren(ElementTag.longName)) {
119 Locale locale = Locale.getDefault();
120 if(child.hasAttribute(AttributeTag.longNameLang)) {
121 Attribute attribute = child.getAttribute(AttributeTag.longNameLang);
122 LanguageType type = (LanguageType)attribute.getValue();
123 locale = type.getLocale();
124 }
125 String text = child.getCData().getString().toString();
126 LongName name = new LongName(text, locale);
127 programme.getNames().addLongName(name);
128 }
129 }
130 for(Element child : element.getChildren(ElementTag.location)) {
131 LocationDeconstructor deconstructor = new LocationDeconstructor();
132 Location location = deconstructor.deconstruct(child);
133 programme.addLocation(location);
134 }
135 for(Element child : element.getChildren(ElementTag.mediaDescription)) {
136 MediaGroupDeconstructor deconstructor = new MediaGroupDeconstructor();
137 MediaGroup media = deconstructor.deconstruct(child);
138 for(ShortDescription description : media.getShortDescriptions()) {
139 programme.getMedia().addShortDescription(description);
140 }
141 for(LongDescription description : media.getLongDescriptions()) {
142 programme.getMedia().addLongDescription(description);
143 }
144 for(Multimedia multimedia : media.getMultimedia()) {
145 programme.getMedia().addMultimedia(multimedia);
146 }
147 }
148 for(Element child : element.getChildren(ElementTag.genre)) {
149 GenreDeconstructor deconstructor = new GenreDeconstructor();
150 Genre genre = deconstructor.deconstruct(child);
151 programme.addGenre(genre);
152 }
153 if(element.hasChildren(ElementTag.CA)) {
154
155 }
156 return programme;
157 }
158
159 }