View Javadoc
1   /*
2    * Copyright (C) 2013 4th Line GmbH, Switzerland
3    *
4    * The contents of this file are subject to the terms of either the GNU
5    * Lesser General Public License Version 2 or later ("LGPL") or the
6    * Common Development and Distribution License Version 1 or later
7    * ("CDDL") (collectively, the "License"). You may not use this file
8    * except in compliance with the License. See LICENSE.txt for more
9    * information.
10   *
11   * This program is distributed in the hope that it will be useful,
12   * but WITHOUT ANY WARRANTY; without even the implied warranty of
13   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14   */
15  
16  package org.fourthline.cling.support.model;
17  
18  import org.fourthline.cling.support.model.container.Album;
19  import org.fourthline.cling.support.model.container.Container;
20  import org.fourthline.cling.support.model.container.GenreContainer;
21  import org.fourthline.cling.support.model.container.MovieGenre;
22  import org.fourthline.cling.support.model.container.MusicAlbum;
23  import org.fourthline.cling.support.model.container.MusicArtist;
24  import org.fourthline.cling.support.model.container.MusicGenre;
25  import org.fourthline.cling.support.model.container.PersonContainer;
26  import org.fourthline.cling.support.model.container.PhotoAlbum;
27  import org.fourthline.cling.support.model.container.PlaylistContainer;
28  import org.fourthline.cling.support.model.container.StorageFolder;
29  import org.fourthline.cling.support.model.container.StorageSystem;
30  import org.fourthline.cling.support.model.container.StorageVolume;
31  import org.fourthline.cling.support.model.item.AudioBook;
32  import org.fourthline.cling.support.model.item.AudioBroadcast;
33  import org.fourthline.cling.support.model.item.AudioItem;
34  import org.fourthline.cling.support.model.item.ImageItem;
35  import org.fourthline.cling.support.model.item.Item;
36  import org.fourthline.cling.support.model.item.Movie;
37  import org.fourthline.cling.support.model.item.MusicTrack;
38  import org.fourthline.cling.support.model.item.MusicVideoClip;
39  import org.fourthline.cling.support.model.item.Photo;
40  import org.fourthline.cling.support.model.item.PlaylistItem;
41  import org.fourthline.cling.support.model.item.TextItem;
42  import org.fourthline.cling.support.model.item.VideoBroadcast;
43  import org.fourthline.cling.support.model.item.VideoItem;
44  
45  import java.util.ArrayList;
46  import java.util.List;
47  
48  /**
49   * @author Christian Bauer
50   */
51  public class DIDLContent {
52  
53      public static final String NAMESPACE_URI = "urn:schemas-upnp-org:metadata-1-0/DIDL-Lite/";
54      public static final String DESC_WRAPPER_NAMESPACE_URI = "urn:fourthline-org:cling:support:content-directory-desc-1-0";
55  
56      protected List<Container> containers = new ArrayList<>();
57      protected List<Item> items = new ArrayList<>();
58      protected List<DescMeta> descMetadata = new ArrayList<>();
59  
60      public Container getFirstContainer() {
61          return getContainers().get(0);
62      }
63  
64      public DIDLContent addContainer(Container container) {
65          getContainers().add(container);
66          return this;
67      }
68  
69      public List<Container> getContainers() {
70          return containers;
71      }
72  
73      public void setContainers(List<Container> containers) {
74          this.containers = containers;
75      }
76  
77      /**
78       * Adds {@link Item} or {@link Container} typed instances, ignores everything else.
79       */
80      public DIDLContent addObject(Object object) {
81          if(object instanceof Item) {
82          	addItem((Item)object);
83          } else if(object instanceof Container) {
84          	addContainer((Container)object);
85          }
86          return this;
87      }
88      
89      public DIDLContent addItem(Item item) {
90          getItems().add(item);
91          return this;
92      }
93  
94      public List<Item> getItems() {
95          return items;
96      }
97  
98      public void setItems(List<Item> items) {
99          this.items = items;
100     }
101 
102     public DIDLContent addDescMetadata(DescMeta descMetadata) {
103         getDescMetadata().add(descMetadata);
104         return this;
105     }
106 
107     public List<DescMeta> getDescMetadata() {
108         return descMetadata;
109     }
110 
111     public void setDescMetadata(List<DescMeta> descMetadata) {
112         this.descMetadata = descMetadata;
113     }
114 
115     public void replaceGenericContainerAndItems() {
116         setItems(replaceGenericItems(getItems()));
117         setContainers(replaceGenericContainers(getContainers()));
118     }
119 
120     protected List<Item> replaceGenericItems(List<Item> genericItems) {
121         List<Item> specificItems = new ArrayList<>();
122 
123         for (Item genericItem : genericItems) {
124             String genericType = genericItem.getClazz().getValue();
125 
126             if (AudioItem.CLASS.getValue().equals(genericType)) {
127                 specificItems.add(new AudioItem(genericItem));
128             } else if (MusicTrack.CLASS.getValue().equals(genericType)) {
129                 specificItems.add(new MusicTrack(genericItem));
130             } else if (AudioBook.CLASS.getValue().equals(genericType)) {
131                 specificItems.add(new AudioBook(genericItem));
132             } else if (AudioBroadcast.CLASS.getValue().equals(genericType)) {
133                 specificItems.add(new AudioBroadcast(genericItem));
134 
135             } else if (VideoItem.CLASS.getValue().equals(genericType)) {
136                 specificItems.add(new VideoItem(genericItem));
137             } else if (Movie.CLASS.getValue().equals(genericType)) {
138                 specificItems.add(new Movie(genericItem));
139             } else if (VideoBroadcast.CLASS.getValue().equals(genericType)) {
140                 specificItems.add(new VideoBroadcast(genericItem));
141             } else if (MusicVideoClip.CLASS.getValue().equals(genericType)) {
142                 specificItems.add(new MusicVideoClip(genericItem));
143 
144             } else if (ImageItem.CLASS.getValue().equals(genericType)) {
145                 specificItems.add(new ImageItem(genericItem));
146             } else if (Photo.CLASS.getValue().equals(genericType)) {
147                 specificItems.add(new Photo(genericItem));
148 
149             } else if (PlaylistItem.CLASS.getValue().equals(genericType)) {
150                 specificItems.add(new PlaylistItem(genericItem));
151 
152             } else if (TextItem.CLASS.getValue().equals(genericType)) {
153                 specificItems.add(new TextItem(genericItem));
154 
155             } else {
156                 specificItems.add(genericItem);
157             }
158         }
159 
160         return specificItems;
161     }
162 
163     protected List<Container> replaceGenericContainers(List<Container> genericContainers) {
164         List<Container> specificContainers = new ArrayList<>();
165 
166         for (Container genericContainer : genericContainers) {
167             String genericType = genericContainer.getClazz().getValue();
168 
169             Container specific;
170 
171             if (Album.CLASS.getValue().equals(genericType)) {
172                 specific = new Album(genericContainer);
173 
174             } else if (MusicAlbum.CLASS.getValue().equals(genericType)) {
175                 specific = new MusicAlbum(genericContainer);
176 
177             } else if (PhotoAlbum.CLASS.getValue().equals(genericType)) {
178                 specific = new PhotoAlbum(genericContainer);
179 
180             } else if (GenreContainer.CLASS.getValue().equals(genericType)) {
181                 specific = new GenreContainer(genericContainer);
182 
183             } else if (MusicGenre.CLASS.getValue().equals(genericType)) {
184                 specific = new MusicGenre(genericContainer);
185 
186             } else if (MovieGenre.CLASS.getValue().equals(genericType)) {
187                 specific = new MovieGenre(genericContainer);
188 
189             } else if (PlaylistContainer.CLASS.getValue().equals(genericType)) {
190                 specific = new PlaylistContainer(genericContainer);
191 
192             } else if (PersonContainer.CLASS.getValue().equals(genericType)) {
193                 specific = new PersonContainer(genericContainer);
194 
195             } else if (MusicArtist.CLASS.getValue().equals(genericType)) {
196                 specific = new MusicArtist(genericContainer);
197 
198             } else if (StorageSystem.CLASS.getValue().equals(genericType)) {
199                 specific = new StorageSystem(genericContainer);
200 
201             } else if (StorageVolume.CLASS.getValue().equals(genericType)) {
202                 specific = new StorageVolume(genericContainer);
203 
204             } else if (StorageFolder.CLASS.getValue().equals(genericType)) {
205                 specific = new StorageFolder(genericContainer);
206 
207             } else {
208                 specific = genericContainer;
209             }
210 
211             specific.setItems(replaceGenericItems(genericContainer.getItems()));
212             specificContainers.add(specific);
213         }
214 
215         return specificContainers;
216     }
217     
218     public long getCount() {
219     	return items.size() + containers.size();
220     }
221 }