View Javadoc

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.net.MalformedURLException;
25  import java.net.URL;
26  import java.util.Locale;
27  
28  import com.gcapmedia.dab.epg.*;
29  import com.gcapmedia.dab.epg.Multimedia.Type;
30  
31  /***
32   * 
33   */
34  public class MediaGroupDeconstructor implements Deconstructor<MediaGroup> {
35  
36  	/***
37  	 * @see com.gcapmedia.dab.epg.binary.Deconstructor#deconstruct(com.gcapmedia.dab.epg.binary.Element)
38  	 */
39  	public MediaGroup deconstruct(Element element) {
40  		
41  		MediaGroup group = new MediaGroup();
42  
43  		// attributes
44  		
45  		// elements
46  		if(element.hasChildren(ElementTag.shortDescription)) {
47  			for(Element child : element.getChildren(ElementTag.shortDescription)) {
48  				Locale locale = Locale.getDefault();
49  				if(child.hasAttribute(AttributeTag.shortDescriptionLang)) {
50  					Attribute attribute = child.getAttribute(AttributeTag.shortDescriptionLang);
51  					LanguageType type = (LanguageType)attribute.getValue();
52  					locale = type.getLocale();
53  				}
54  				String text = child.getCData().toString();
55  				ShortDescription description = new ShortDescription(text, locale);
56  				group.addShortDescription(description);
57  			}
58  		}
59  		if(element.hasChildren(ElementTag.longDescription)) {
60  			for(Element child : element.getChildren(ElementTag.longDescription)) {
61  				Locale locale = Locale.getDefault();
62  				if(child.hasAttribute(AttributeTag.longDescriptionLang)) {
63  					Attribute attribute = child.getAttribute(AttributeTag.longDescriptionLang);
64  					LanguageType type = (LanguageType)attribute.getValue();
65  					locale = type.getLocale();
66  				}
67  				String text = child.getCData().toString();
68  				LongDescription description = new LongDescription(text, locale);
69  				group.addLongDescription(description);
70  			}
71  		}
72  		if(element.hasChildren(ElementTag.multimedia)) {
73  			for(Element child : element.getChildren(ElementTag.multimedia)) {
74  				
75  				if(!child.hasAttribute(AttributeTag.multimediaUrl)) {
76  					throw new IllegalArgumentException("Must specify a multimedia URL");
77  				} 
78  				Attribute urlAttribute = child.getAttribute(AttributeTag.multimediaUrl);
79  				StringType urlType = (StringType)urlAttribute.getValue();
80  				URL url = null;
81  				try {
82  					url = new URL(urlType.toString());
83  				} catch (MalformedURLException e) {
84  					throw new IllegalArgumentException("Error creating multimedia URL: " + e.getMessage());
85  				}
86  				Locale locale = Locale.getDefault();
87  				if(child.hasAttribute(AttributeTag.multimediaLan)) {
88  					Attribute attribute = child.getAttribute(AttributeTag.multimediaLan);
89  					LanguageType type = (LanguageType)attribute.getValue();
90  					locale = type.getLocale();
91  				}
92  				Multimedia multimedia = new Multimedia(url, locale);
93  				
94  				if(child.hasAttribute(AttributeTag.multimediaMimeValue)) {
95  					Attribute attribute = child.getAttribute(AttributeTag.multimediaMimeValue);
96  					StringType type = (StringType)attribute.getValue();
97  					String mimeValue = type.toString();
98  					multimedia.setMimeType(mimeValue);
99  				}
100 				
101 				if(child.hasAttribute(AttributeTag.multimediaType)) {
102 					Attribute attribute = child.getAttribute(AttributeTag.multimediaType);
103 					EnumType type = (EnumType)attribute.getValue();
104 					if(type == EnumType.multimediaTypeLogoColourRectangle) {
105 						multimedia.setType(Type.LOGO_COLOUR_RECTANGLE);
106 					} else if(type == EnumType.multimediaTypeLogoColourSquare) {
107 						multimedia.setType(Type.LOGO_COLOUR_SQUARE);
108 					} else if(type == EnumType.multimediaTypeLogoMonoRectangle) {
109 						multimedia.setType(Type.LOGO_MONO_RECTANGLE);
110 					} else if(type == EnumType.multimediaTypeLogoMonoSquare) {
111 						multimedia.setType(Type.LOGO_MONO_SQUARE);
112 					} else if(type == EnumType.multimediaTypeLogoUnrestricted) {
113 						multimedia.setType(Type.LOGO_UNRESTRICTED);
114 					} else {
115 						throw new IllegalArgumentException("Unknown multimedia type specified: " + type);
116 					}
117 				}
118 				
119 				if(child.hasAttribute(AttributeTag.multimediaHeight)) {
120 					Attribute attribute = child.getAttribute(AttributeTag.multimediaHeight);
121 					NumericType type = (NumericType)attribute.getValue();
122 					multimedia.setHeight(type.getNumber());
123 				}
124 				
125 				if(child.hasAttribute(AttributeTag.multimediaWidth)) {
126 					Attribute attribute = child.getAttribute(AttributeTag.multimediaWidth);
127 					NumericType type = (NumericType)attribute.getValue();
128 					multimedia.setWidth(type.getNumber());
129 				}
130 				
131 				group.addMultimedia(multimedia);
132 			}
133 		}
134 	
135 		return group;
136 	}
137 
138 }