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     }
193     return milliseconds;
194   }
195 
196   /**
197    * @see org.opencastproject.metadata.mpeg7.MediaTimePoint#isRelative()
198    */
199   public boolean isRelative() {
200     return referenceTimePoint != null;
201   }
202 
203   /**
204    * Sets a reference time point which makes this time point relative to the given one.
205    *
206    * @param timePoint
207    *          the reference time point
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    * Creates a timepoint representation from the given time point string.
255    *
256    * @param text
257    *          the timepoint text representation
258    * @return the the timepoint
259    * @throws IllegalArgumentException
260    *           if <code>text</code> is malformed
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    * Parses a timepoint string.
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    * Parses the date portion of a time point.
289    *
290    * @param date
291    *          the date
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    * Parses the time portion of a time point
309    *
310    * @param time
311    *          the time
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    * Parses the fractions of a time point.
325    *
326    * @param fractions
327    *          the fractions
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    * @see java.lang.Object#toString()
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    * @see org.opencastproject.mediapackage.XmlElement#toXml(org.w3c.dom.Document)
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 }