1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.fourthline.cling.model.meta;
17
18
19 import org.fourthline.cling.model.Validatable;
20 import org.fourthline.cling.model.ValidationError;
21 import org.fourthline.cling.model.types.BinHexDatatype;
22 import org.seamless.util.io.IO;
23 import org.seamless.util.MimeType;
24 import org.seamless.util.URIUtil;
25
26 import java.io.File;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.net.MalformedURLException;
30 import java.net.URI;
31 import java.net.URL;
32 import java.util.ArrayList;
33 import java.util.List;
34 import java.util.logging.Logger;
35
36
37
38
39
40
41
42
43
44
45
46
47
48 public class Icon implements Validatable {
49
50 final private static Logger log = Logger.getLogger(StateVariable.class.getName());
51
52 final private MimeType mimeType;
53 final private int width;
54 final private int height;
55 final private int depth;
56 final private URI uri;
57 final private byte[] data;
58
59
60 private Device device;
61
62
63
64
65 public Icon(String mimeType, int width, int height, int depth, URI uri) {
66 this(mimeType != null && mimeType.length() > 0 ? MimeType.valueOf(mimeType) : null, width, height, depth, uri, null);
67 }
68
69
70
71
72
73
74
75 public Icon(String mimeType, int width, int height, int depth, URL url) throws IOException{
76 this(mimeType, width, height, depth, new File(URIUtil.toURI(url)));
77 }
78
79
80
81
82
83 public Icon(String mimeType, int width, int height, int depth, File file) throws IOException {
84 this(mimeType, width, height, depth, file.getName(), IO.readBytes(file));
85 }
86
87
88
89
90
91
92 public Icon(String mimeType, int width, int height, int depth, String uniqueName, InputStream is) throws IOException {
93 this(mimeType, width, height, depth, uniqueName, IO.readBytes(is));
94 }
95
96
97
98
99
100
101 public Icon(String mimeType, int width, int height, int depth, String uniqueName, byte[] data) {
102 this(mimeType != null && mimeType.length() > 0 ? MimeType.valueOf(mimeType) : null, width, height, depth, URI.create(uniqueName), data);
103 }
104
105
106
107
108
109
110
111 public Icon(String mimeType, int width, int height, int depth, String uniqueName, String binHexEncoded) {
112 this(
113 mimeType, width, height, depth, uniqueName,
114 binHexEncoded != null && !binHexEncoded.equals("") ? new BinHexDatatype().valueOf(binHexEncoded) : null
115 );
116 }
117
118 protected Icon(MimeType mimeType, int width, int height, int depth, URI uri, byte[] data) {
119 this.mimeType = mimeType;
120 this.width = width;
121 this.height = height;
122 this.depth = depth;
123 this.uri = uri;
124 this.data = data;
125 }
126
127 public MimeType getMimeType() {
128 return mimeType;
129 }
130
131 public int getWidth() {
132 return width;
133 }
134
135 public int getHeight() {
136 return height;
137 }
138
139 public int getDepth() {
140 return depth;
141 }
142
143 public URI getUri() {
144 return uri;
145 }
146
147 public byte[] getData() {
148 return data;
149 }
150
151 public Device getDevice() {
152 return device;
153 }
154
155 void setDevice(Device device) {
156 if (this.device != null)
157 throw new IllegalStateException("Final value has been set already, model is immutable");
158 this.device = device;
159 }
160
161 public List<ValidationError> validate() {
162 List<ValidationError> errors = new ArrayList<>();
163
164 if (getMimeType() == null) {
165 log.warning("UPnP specification violation of: " + getDevice());
166 log.warning("Invalid icon, missing mime type: " + this);
167 }
168 if (getWidth() == 0) {
169 log.warning("UPnP specification violation of: " + getDevice());
170 log.warning("Invalid icon, missing width: " + this);
171 }
172 if (getHeight() == 0) {
173 log.warning("UPnP specification violation of: " + getDevice());
174 log.warning("Invalid icon, missing height: " + this);
175 }
176 if (getDepth() == 0) {
177 log.warning("UPnP specification violation of: " + getDevice());
178 log.warning("Invalid icon, missing bitmap depth: " + this);
179 }
180
181 if (getUri() == null) {
182 errors.add(new ValidationError(
183 getClass(),
184 "uri",
185 "URL is required"
186 ));
187 } else {
188 try {
189 URL testURI = getUri().toURL();
190 if (testURI == null)
191 throw new MalformedURLException();
192 } catch (MalformedURLException ex) {
193 errors.add(new ValidationError(
194 getClass(),
195 "uri",
196 "URL must be valid: " + ex.getMessage())
197 );
198 } catch (IllegalArgumentException ex) {
199
200 }
201 }
202
203 return errors;
204 }
205
206 public Icon deepCopy() {
207 return new Icon(
208 getMimeType(),
209 getWidth(),
210 getHeight(),
211 getDepth(),
212 getUri(),
213 getData()
214 );
215 }
216
217 @Override
218 public String toString() {
219 return "Icon(" + getWidth() + "x" + getHeight() + ", MIME: " + getMimeType() + ") " + getUri();
220 }
221 }