1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23 package org.opencastproject.metadata.mpeg7;
24
25 import org.w3c.dom.Document;
26 import org.w3c.dom.Element;
27 import org.w3c.dom.Node;
28
29 import java.util.Calendar;
30 import java.util.Formatter;
31 import java.util.TimeZone;
32
33
34
35
36 public class MediaTimePointImpl implements MediaTimePoint {
37
38
39 private MediaTimePoint referenceTimePoint = null;
40
41
42 private static final String TimeSpecDelimiter = "T";
43
44
45 private static final String FractionSpecDelimiter = "F";
46
47
48 private static final String TimeSeparator = ":";
49
50
51 private static final String DateSeparator = "-";
52
53
54 protected int year = 0;
55
56
57 protected int month = 0;
58
59
60 protected int day = 0;
61
62
63 protected int hour = 0;
64
65
66 protected int minute = 0;
67
68
69 protected int second = 0;
70
71
72 protected int fractions = 0;
73
74
75 protected int fractionsPerSecond = 0;
76
77
78 protected String timeZone = "+00:00";
79
80
81 private static final long MS_PER_DAY = 86400000L;
82
83
84 private static final long MS_PER_HOUR = 3600000L;
85
86
87 private static final long MS_PER_MINUTE = 60000L;
88
89
90 private static final long MS_PER_SECOND = 1000L;
91
92
93
94
95 public MediaTimePointImpl() {
96 year = 0;
97 month = 0;
98 day = 0;
99 hour = 0;
100 minute = 0;
101 second = 0;
102 fractions = 0;
103 fractionsPerSecond = 0;
104 }
105
106
107
108
109
110
111
112 public MediaTimePointImpl(long milliseconds) {
113 Calendar calendar = Calendar.getInstance();
114 calendar.setTimeInMillis(milliseconds);
115 calendar.setTimeZone(TimeZone.getTimeZone("UCT"));
116 second = calendar.get(Calendar.SECOND);
117 minute = calendar.get(Calendar.MINUTE);
118 hour = calendar.get(Calendar.HOUR_OF_DAY);
119 day = calendar.get(Calendar.DAY_OF_MONTH) - 1;
120 month = calendar.get(Calendar.MONTH);
121 year = calendar.get(Calendar.YEAR);
122 fractions = Math.round(milliseconds % 1000);
123 fractionsPerSecond = 1000;
124 }
125
126
127
128
129 public int getDay() {
130 return day;
131 }
132
133
134
135
136 public int getHour() {
137 return hour;
138 }
139
140
141
142
143 public int getMinutes() {
144 return minute;
145 }
146
147
148
149
150 public int getMonth() {
151 return month;
152 }
153
154
155
156
157 public int getNFractions() {
158 return fractions;
159 }
160
161
162
163
164 public int getSeconds() {
165 return second;
166 }
167
168
169
170
171 public int getYear() {
172 return year;
173 }
174
175
176
177
178 public int getFractionsPerSecond() {
179 return fractionsPerSecond;
180 }
181
182
183
184
185 public long getTimeInMilliseconds() {
186 long milliseconds = second * MS_PER_SECOND;
187 milliseconds += minute * MS_PER_MINUTE;
188 milliseconds += hour * MS_PER_HOUR;
189 milliseconds += day * MS_PER_DAY;
190 if (fractionsPerSecond > 0) {
191 milliseconds += (fractions * 1000L / fractionsPerSecond);
192 }
193 return milliseconds;
194 }
195
196
197
198
199 public boolean isRelative() {
200 return referenceTimePoint != null;
201 }
202
203
204
205
206
207
208
209 public void setReferenceTimePoint(MediaTimePoint timePoint) {
210 this.referenceTimePoint = timePoint;
211 }
212
213 public void setFractionsPerSecond(int fractionsPerSecond) {
214 this.fractionsPerSecond = fractionsPerSecond;
215 }
216
217 public String getTimeZone() {
218 return timeZone;
219 }
220
221 public void setTimeZone(String timeZone) {
222 this.timeZone = timeZone;
223 }
224
225 public void setDay(int day) {
226 this.day = day;
227 }
228
229 public void setHour(int hour) {
230 this.hour = hour;
231 }
232
233 public void setMinutes(int minutes) {
234 this.minute = minutes;
235 }
236
237 public void setMonth(int month) {
238 this.month = month;
239 }
240
241 public void setFractions(int fractions) {
242 this.fractions = fractions;
243 }
244
245 public void setSeconds(int seconds) {
246 this.second = seconds;
247 }
248
249 public void setYear(int year) {
250 this.year = year;
251 }
252
253
254
255
256
257
258
259
260
261
262 public static MediaTimePointImpl parseTimePoint(String text) throws IllegalArgumentException {
263 MediaTimePointImpl timePoint = new MediaTimePointImpl();
264 timePoint.parse(text);
265 return timePoint;
266 }
267
268
269
270
271 private void parse(String text) throws IllegalArgumentException {
272 String date = null;
273 String time = null;
274 String fractions = null;
275 date = text.substring(0, text.indexOf(TimeSpecDelimiter));
276 time = text.substring(text.indexOf(TimeSpecDelimiter) + 1, text.indexOf(TimeSpecDelimiter) + 9);
277 fractions = text.substring(text.indexOf(TimeSpecDelimiter) + time.length() + 2);
278 if (fractions.contains(TimeSeparator)) {
279 timeZone = fractions.substring(fractions.length() - 6);
280 fractions = fractions.substring(0, fractions.length() - 7);
281 }
282 parseDate(date);
283 parseTime(time);
284 parseFractions(fractions);
285 }
286
287
288
289
290
291
292
293 protected void parseDate(String date) {
294 int firstDateSeparator = date.indexOf(DateSeparator);
295 int lastDateSeparator = date.lastIndexOf(DateSeparator);
296 if (firstDateSeparator > -1) {
297 year = Integer.parseInt(date.substring(0, firstDateSeparator));
298 month = Short.parseShort(date.substring(firstDateSeparator + 1, lastDateSeparator));
299 day = Short.parseShort(date.substring(lastDateSeparator + 1));
300 } else {
301 year = 0;
302 month = 0;
303 day = 0;
304 }
305 }
306
307
308
309
310
311
312
313 protected void parseTime(String time) {
314 int firstTimeSeparator = time.indexOf(TimeSeparator);
315 int lastTimeSeparator = time.lastIndexOf(TimeSeparator);
316 if (firstTimeSeparator > -1) {
317 hour = Short.parseShort(time.substring(0, firstTimeSeparator));
318 minute = Short.parseShort(time.substring(firstTimeSeparator + 1, lastTimeSeparator));
319 second = Short.parseShort(time.substring(lastTimeSeparator + 1));
320 }
321 }
322
323
324
325
326
327
328
329 private void parseFractions(String fractions) {
330 this.fractions = Integer.parseInt(fractions.substring(0, fractions.indexOf(FractionSpecDelimiter)));
331 this.fractionsPerSecond = Integer.parseInt(fractions.substring(fractions.indexOf(FractionSpecDelimiter) + 1));
332 }
333
334
335
336
337 @Override
338 public String toString() {
339 Formatter f = new Formatter();
340 String result = null;
341 if (referenceTimePoint == null && year != 0) {
342 result = f.format("%04d-%02d-%02dT%02d:%02d:%02d:%dF%d", year, month, day, hour, minute, second, fractions,
343 fractionsPerSecond).toString();
344 } else {
345 result = f.format("T%02d:%02d:%02d:%dF%d", hour, minute, second, fractions, fractionsPerSecond).toString();
346 }
347 f.close();
348 return result;
349 }
350
351
352
353
354 public Node toXml(Document document) {
355 Element node = null;
356 if (referenceTimePoint != null) {
357 node = document.createElement("MediaRelTimePoint");
358 } else {
359 node = document.createElement("MediaTimePoint");
360 }
361 node.setTextContent(toString());
362 return node;
363 }
364
365 }