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.binding.staging;
17  
18  import org.fourthline.cling.model.ValidationException;
19  import org.fourthline.cling.model.meta.Device;
20  import org.fourthline.cling.model.meta.DeviceDetails;
21  import org.fourthline.cling.model.meta.Icon;
22  import org.fourthline.cling.model.meta.ManufacturerDetails;
23  import org.fourthline.cling.model.meta.ModelDetails;
24  import org.fourthline.cling.model.meta.Service;
25  import org.fourthline.cling.model.meta.UDAVersion;
26  import org.fourthline.cling.model.types.DLNACaps;
27  import org.fourthline.cling.model.types.DLNADoc;
28  import org.fourthline.cling.model.types.DeviceType;
29  import org.fourthline.cling.model.types.UDN;
30  
31  import java.net.URI;
32  import java.net.URL;
33  import java.util.ArrayList;
34  import java.util.List;
35  
36  /**
37   * @author Christian Bauer
38   */
39  public class MutableDevice {
40  
41      public UDN udn;
42      public MutableUDAVersion udaVersion = new MutableUDAVersion();
43      public URL baseURL;
44      public String deviceType;
45      public String friendlyName;
46      public String manufacturer;
47      public URI manufacturerURI;
48      public String modelName;
49      public String modelDescription;
50      public String modelNumber;
51      public URI modelURI;
52      public String serialNumber;
53      public String upc;
54      public URI presentationURI;
55      public List<DLNADoc> dlnaDocs = new ArrayList<>();
56      public DLNACaps dlnaCaps;
57      public List<MutableIcon> icons = new ArrayList<>();
58      public List<MutableService> services = new ArrayList<>();
59      public List<MutableDevice> embeddedDevices = new ArrayList<>();
60      public MutableDevice parentDevice;
61  
62      public Device build(Device prototype) throws ValidationException {
63          // Note how all embedded devices inherit the version and baseURL of the root!
64          return build(prototype, createDeviceVersion(), baseURL);
65      }
66  
67      public Device build(Device prototype, UDAVersion deviceVersion, URL baseURL) throws ValidationException {
68  
69          List<Device> embeddedDevicesList = new ArrayList<>();
70          for (MutableDevice embeddedDevice : embeddedDevices) {
71              embeddedDevicesList.add(embeddedDevice.build(prototype, deviceVersion, baseURL));
72          }
73          return prototype.newInstance(
74                  udn,
75                  deviceVersion,
76                  createDeviceType(),
77                  createDeviceDetails(baseURL),
78                  createIcons(),
79                  createServices(prototype),
80                  embeddedDevicesList
81          );
82      }
83  
84      public UDAVersion createDeviceVersion() {
85          return new UDAVersion(udaVersion.major, udaVersion.minor);
86      }
87  
88      public DeviceType createDeviceType() {
89          return DeviceType.valueOf(deviceType);
90      }
91  
92      public DeviceDetails createDeviceDetails(URL baseURL) {
93          return new DeviceDetails(
94                  baseURL,
95                  friendlyName,
96                  new ManufacturerDetails(manufacturer, manufacturerURI),
97                  new ModelDetails(modelName, modelDescription, modelNumber, modelURI),
98                  serialNumber, upc, presentationURI, dlnaDocs.toArray(new DLNADoc[dlnaDocs.size()]), dlnaCaps
99          );
100     }
101 
102     public Icon[] createIcons() {
103         Icon[] iconArray = new Icon[icons.size()];
104         int i = 0;
105         for (MutableIcon icon : icons) {
106             iconArray[i++] = icon.build();
107         }
108         return iconArray;
109     }
110 
111     public Service[] createServices(Device prototype) throws ValidationException {
112         Service[] services = prototype.newServiceArray(this.services.size());
113         int i = 0;
114         for (MutableService service : this.services) {
115             services[i++] = service.build(prototype);
116         }
117         return services;
118     }
119 
120 }