-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathSmil.java
More file actions
273 lines (198 loc) · 5.47 KB
/
Copy pathSmil.java
File metadata and controls
273 lines (198 loc) · 5.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
package pl.smsapi.message;
import java.io.DataInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.HashMap;
import java.util.logging.Level;
import java.util.logging.Logger;
import pl.smsapi.SmsapiException;
//import javax.activation.MimetypesFileTypeMap;
//import com.sun.xml.internal.messaging.saaj.util.Base64;
public final class Smil implements SmilInterface {
String content;
public static enum Region {
IMAGE("Image", "img"),
TEXT("Text", "text"),
AUDIO("Audio", "audio"),
VIDEO("Video", "video");
private final String id;
private final String tag;
Region(String id, String tag) {
this.id = id;
this.tag = tag;
}
public String id() {
return id;
}
public String tag() {
return tag;
}
};
public static enum ByteType {
TXT("text"),
JPG("image"),
PNG("image"),
GIF("image"),
WAV("audio"),
MP3("audio"),
OGG("audio"),
MP4("video"),
AVI("video");
private final String type;
ByteType(String type) {
this.type = type;
}
public String type() {
String tmp = this.name().toLowerCase();
if (tmp.equals("txt")) {
tmp = "plain";
} else if (tmp.equals("mp3")) {
tmp = "mpeg";
}
return type + "/" + tmp;
}
};
private HashMap<String, String> head = new HashMap<String, String>();
private HashMap<String, String> body = new HashMap<String, String>();
String width = "100%";
String height = "100%";
String dur;
Smil() {
}
Smil(String content) {
this.content = content;
}
public String getWidth() {
return width;
}
public void setWidth(String width) {
this.width = width;
}
public String getHeight() {
return height;
}
public void setHeight(String height) {
this.height = height;
}
public String getDur() {
return dur;
}
public void setDur(String dur) {
this.dur = dur;
}
@Override
public void createElement(Region region, String src, String height, String width, String top, String left, String fit) {
String item1 = regionTag(region, height, width, top, left, fit);
head.put(region.id(), item1);
String item2 = "<" + region.tag() + " src=\"" + src + "\" region=\"" + region.id() + "\" />";
body.put(region.id(), item2);
}
@Override
public void createElement(byte[] data, ByteType type, String height, String width, String top, String left, String fit) {
//String encodeText = new String(Base64.encode(data));
String encodeText = (String) Base64Coder.encodeLines(data);
Region region = getRegionByByteType(type);
String item1 = regionTag(region, height, width, top, left, fit);
head.put(region.id(), item1);
String item2 = "<" + region.tag() + " src=\"data:" + type.type() + ";base64," + encodeText + "\" region=\"" + region.id() + "\" />";
body.put(region.id(), item2);
}
private Region getRegionByByteType(ByteType byteType) {
Region region = null;
switch (byteType) {
case TXT:
region = Region.TEXT;
break;
case JPG:
region = Region.IMAGE;
break;
case PNG:
region = Region.IMAGE;
break;
case GIF:
region = Region.IMAGE;
break;
case WAV:
region = Region.AUDIO;
break;
case MP3:
region = Region.AUDIO;
break;
case OGG:
region = Region.AUDIO;
break;
case MP4:
region = Region.VIDEO;
break;
case AVI:
region = Region.VIDEO;
break;
}
return region;
}
@Override
public void createElement(File file, String height, String width, String top, String left, String fit) {
try {
String fileName = file.getName();
ByteType byteType;
final long length = file.length();
byte[] DataBa = new byte[(int) length];
DataInputStream dataIs = new DataInputStream(new FileInputStream(file));
dataIs.readFully(DataBa);
try {
fileName = fileName.substring(fileName.lastIndexOf(".") + 1);
byteType = ByteType.valueOf(fileName.toUpperCase());
createElement(DataBa, byteType, width, height, left, top, fit);
} catch (IllegalArgumentException ex) {
throw new SmsapiException("Not exists byteType");
} finally {
dataIs.close();
}
} catch (FileNotFoundException ex) {
Logger.getLogger(Smil.class.getName()).log(Level.SEVERE, null, ex);
throw new SmsapiException(ex.getMessage());
} catch (IOException ex) {
Logger.getLogger(Smil.class.getName()).log(Level.SEVERE, null, ex);
throw new SmsapiException(ex.getMessage());
}
}
private String regionTag(Region region, String height, String width, String top, String left, String fit) {
String item1 = "<region id=\"" + region.id() + "\"";
if (width != null && !width.isEmpty()) {
item1 += " width=\"" + width + "\"";
}
if (height != null && !height.isEmpty()) {
item1 += " height=\"" + height + "\"";
}
if (left != null && !left.isEmpty()) {
item1 += " left=\"" + top + "\"";
}
if (top != null && !top.isEmpty()) {
item1 += " top=\"" + top + "\"";
}
if (fit != null && !fit.isEmpty()) {
item1 += " fit=\"" + fit + "\"";
}
item1 += "/>";
return item1;
}
public final String render() {
if (content != null && !content.isEmpty()) {
return content;
}
String smil;
smil = "<smil><head><layout><root-layout height=\"" + height + "\" width=\"" + width + "\"/>";
for (String region : head.values()) {
smil += region;
}
smil += "</layout></head><body>";
smil += (dur == null) ? "<par>" : "<par dur=\"" + dur + "\">";
for (String item : body.values()) {
smil += item;
}
smil += "</par></body></smil>";
return smil;
}
}