View Javadoc
1   /*
2    * Licensed to The Apereo Foundation under one or more contributor license
3    * agreements. See the NOTICE file distributed with this work for additional
4    * information regarding copyright ownership.
5    *
6    *
7    * The Apereo Foundation licenses this file to you under the Educational
8    * Community License, Version 2.0 (the "License"); you may not use this file
9    * except in compliance with the License. You may obtain a copy of the License
10   * at:
11   *
12   *   http://opensource.org/licenses/ecl2.txt
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
17   * License for the specific language governing permissions and limitations under
18   * the License.
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   * TODO: Comment me!
35   */
36  public class MediaTimePointImpl implements MediaTimePoint {
37  
38    /** The reference time point */
39    private MediaTimePoint referenceTimePoint = null;
40  
41    /** Time delimiter */
42    private static final String TimeSpecDelimiter = "T";
43  
44    /** Fraction delimiter */
45    private static final String FractionSpecDelimiter = "F";
46  
47    /** Time separator */
48    private static final String TimeSeparator = ":";
49  
50    /** Date separator */
51    private static final String DateSeparator = "-";
52  
53    /** The year */
54    protected int year = 0;
55  
56    /** The month */
57    protected int month = 0;
58  
59    /** The day of month */
60    protected int day = 0;
61  
62    /** The number of hour */
63    protected int hour = 0;
64  
65    /** The nubmer of minutes */
66    protected int minute = 0;
67  
68    /** The number of seconds */
69    protected int second = 0;
70  
71    /** The fraction */
72    protected int fractions = 0;
73  
74    /** The number of fractions per second */
75    protected int fractionsPerSecond = 0;
76  
77    /** The time zone */
78    protected String timeZone = "+00:00";
79  
80    /** Number of milliseconds per day */
81    private static final long MS_PER_DAY = 86400000L;
82  
83    /** Number of milliseconds per hour */
84    private static final long MS_PER_HOUR = 3600000L;
85  
86    /** Number of milliseconds per minute */
87    private static final long MS_PER_MINUTE = 60000L;
88  
89    /** Number of milliseconds per second */
90    private static final long MS_PER_SECOND = 1000L;
91  
92    /**
93     * Creates a new media time point representing <code>T00:00:00:0F0</code>
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    * Creates a new timepoint representing the given time.
108    *
109    * @param milliseconds
110    *          the number of milliseconds
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    * @see org.opencastproject.metadata.mpeg7.MediaTimePoint
128    */
129   public int getDay() {
130     return day;
131   }
132 
133   /**
134    * @see org.opencastproject.metadata.mpeg7.MediaTimePoint#getHour()
135    */
136   public int getHour() {
137     return hour;
138   }
139 
140   /**
141    * @see org.opencastproject.metadata.mpeg7.MediaTimePoint#getMinutes()
142    */
143   public int getMinutes() {
144     return minute;
145   }
146 
147   /**
148    * @see org.opencastproject.metadata.mpeg7.MediaTimePoint#getMonth()
149    */
150   public int getMonth() {
151     return month;
152   }
153 
154   /**
155    * @see org.opencastproject.metadata.mpeg7.MediaTimePoint#getNFractions()
156    */
157   public int getNFractions() {
158     return fractions;
159   }
160 
161   /**
162    * @see org.opencastproject.metadata.mpeg7.MediaTimePoint#getSeconds()
163    */
164   public int getSeconds() {
165     return second;
166   }
167 
168   /**
169    * @see org.opencastproject.metadata.mpeg7.MediaTimePoint#getYear()
170    */
171   public int getYear() {
172     return year;
173   }
174 
175   /**
176    * @see org.opencastproject.metadata.mpeg7.MediaTimePoint#getFractionsPerSecond()
177    */
178   public int getFractionsPerSecond() {
179     return fractionsPerSecond;
180   }
181 
182   /**
183    * @see org.opencastproject.metadata.mpeg7.MediaTimePoint#getTimeInMilliseconds()
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     return milliseconds;
193   }
194 
195   /**
196    * @see org.opencastproject.metadata.mpeg7.MediaTimePoint#isRelative()
197    */
198   public boolean isRelative() {
199     return referenceTimePoint != null;
200   }
201 
202   /**
203    * Sets a reference time point which makes this time point relative to the given one.
204    *
205    * @param timePoint
206    *          the reference time point
207    */
208   public void setReferenceTimePoint(MediaTimePoint timePoint) {
209     this.referenceTimePoint = timePoint;
210   }
211 
212   public void setFractionsPerSecond(int fractionsPerSecond) {
213     this.fractionsPerSecond = fractionsPerSecond;
214   }
215 
216   public String getTimeZone() {
217     return timeZone;
218   }
219 
220   public void setTimeZone(String timeZone) {
221     this.timeZone = timeZone;
222   }
223 
224   public void setDay(int day) {
225     this.day = day;
226   }
227 
228   public void setHour(int hour) {
229     this.hour = hour;
230   }
231 
232   public void setMinutes(int minutes) {
233     this.minute = minutes;
234   }
235 
236   public void setMonth(int month) {
237     this.month = month;
238   }
239 
240   public void setFractions(int fractions) {
241     this.fractions = fractions;
242   }
243 
244   public void setSeconds(int seconds) {
245     this.second = seconds;
246   }
247 
248   public void setYear(int year) {
249     this.year = year;
250   }
251 
252   /**
253    * Creates a timepoint representation from the given time point string.
254    *
255    * @param text
256    *          the timepoint text representation
257    * @return the the timepoint
258    * @throws IllegalArgumentException
259    *           if <code>text</code> is malformed
260    */
261   public static MediaTimePointImpl parseTimePoint(String text) throws IllegalArgumentException {
262     MediaTimePointImpl timePoint = new MediaTimePointImpl();
263     timePoint.parse(text);
264     return timePoint;
265   }
266 
267   /**
268    * Parses a timepoint string.
269    */
270   private void parse(String text) throws IllegalArgumentException {
271     String date = null;
272     String time = null;
273     String fractions = null;
274     date = text.substring(0, text.indexOf(TimeSpecDelimiter));
275     time = text.substring(text.indexOf(TimeSpecDelimiter) + 1, text.indexOf(TimeSpecDelimiter) + 9);
276     fractions = text.substring(text.indexOf(TimeSpecDelimiter) + time.length() + 2);
277     if (fractions.contains(TimeSeparator)) {
278       timeZone = fractions.substring(fractions.length() - 6);
279       fractions = fractions.substring(0, fractions.length() - 7);
280     }
281     parseDate(date);
282     parseTime(time);
283     parseFractions(fractions);
284   }
285 
286   /**
287    * Parses the date portion of a time point.
288    *
289    * @param date
290    *          the date
291    */
292   protected void parseDate(String date) {
293     int firstDateSeparator = date.indexOf(DateSeparator);
294     int lastDateSeparator = date.lastIndexOf(DateSeparator);
295     if (firstDateSeparator > -1) {
296       year = Integer.parseInt(date.substring(0, firstDateSeparator));
297       month = Short.parseShort(date.substring(firstDateSeparator + 1, lastDateSeparator));
298       day = Short.parseShort(date.substring(lastDateSeparator + 1));
299     } else {
300       year = 0;
301       month = 0;
302       day = 0;
303     }
304   }
305 
306   /**
307    * Parses the time portion of a time point
308    *
309    * @param time
310    *          the time
311    */
312   protected void parseTime(String time) {
313     int firstTimeSeparator = time.indexOf(TimeSeparator);
314     int lastTimeSeparator = time.lastIndexOf(TimeSeparator);
315     if (firstTimeSeparator > -1) {
316       hour = Short.parseShort(time.substring(0, firstTimeSeparator));
317       minute = Short.parseShort(time.substring(firstTimeSeparator + 1, lastTimeSeparator));
318       second = Short.parseShort(time.substring(lastTimeSeparator + 1));
319     }
320   }
321 
322   /**
323    * Parses the fractions of a time point.
324    *
325    * @param fractions
326    *          the fractions
327    */
328   private void parseFractions(String fractions) {
329     this.fractions = Integer.parseInt(fractions.substring(0, fractions.indexOf(FractionSpecDelimiter)));
330     this.fractionsPerSecond = Integer.parseInt(fractions.substring(fractions.indexOf(FractionSpecDelimiter) + 1));
331   }
332 
333   /**
334    * @see java.lang.Object#toString()
335    */
336   @Override
337   public String toString() {
338     Formatter f = new Formatter();
339     String result = null;
340     if (referenceTimePoint == null && year != 0) {
341       result = f.format("%04d-%02d-%02dT%02d:%02d:%02d:%dF%d", year, month, day, hour, minute, second, fractions,
342               fractionsPerSecond).toString();
343     } else {
344       result = f.format("T%02d:%02d:%02d:%dF%d", hour, minute, second, fractions, fractionsPerSecond).toString();
345     }
346     f.close();
347     return result;
348   }
349 
350   /**
351    * @see org.opencastproject.mediapackage.XmlElement#toXml(org.w3c.dom.Document)
352    */
353   public Node toXml(Document document) {
354     Element node = null;
355     if (referenceTimePoint != null)
356       node = document.createElement("MediaRelTimePoint");
357     else
358       node = document.createElement("MediaTimePoint");
359     node.setTextContent(toString());
360     return node;
361   }
362 
363 }