001: //=============================================================================
002: //=== Copyright (C) 2001-2007 Food and Agriculture Organization of the
003: //=== United Nations (FAO-UN), United Nations World Food Programme (WFP)
004: //=== and United Nations Environment Programme (UNEP)
005: //===
006: //=== This program is free software; you can redistribute it and/or modify
007: //=== it under the terms of the GNU General Public License as published by
008: //=== the Free Software Foundation; either version 2 of the License, or (at
009: //=== your option) any later version.
010: //===
011: //=== This program is distributed in the hope that it will be useful, but
012: //=== WITHOUT ANY WARRANTY; without even the implied warranty of
013: //=== MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: //=== General Public License for more details.
015: //===
016: //=== You should have received a copy of the GNU General Public License
017: //=== along with this program; if not, write to the Free Software
018: //=== Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
019: //===
020: //=== Contact: Jeroen Ticheler - FAO - Viale delle Terme di Caracalla 2,
021: //=== Rome - Italy. email: geonetwork@osgeo.org
022: //==============================================================================
023:
024: package org.wfp.vam.intermap.kernel.map.mapServices.wms.dimensions.time;
025:
026: import java.text.*;
027: import java.util.*;
028:
029: public class WmsTime {
030: //private String date;
031:
032: private Calendar c;
033:
034: private Integer century;
035: private Integer year;
036: private String weekDay; // TODO: to be implemented
037: private String zone; // TODO: to be implemented
038:
039: private Hashtable htTime = new Hashtable(); // Hashtable containing the time fields
040: private Hashtable htPeriod = new Hashtable(); // Hashtable containing the time period fields
041:
042: /**
043: * Constructor
044: *
045: * @param t a String
046: *
047: */
048: public WmsTime(String t) {
049: // separate the date from the time
050: String date = getDate(t);
051: String time = getTime(t);
052:
053: parseDate(date);
054: parseTime(time);
055:
056: // set the internal Calendar variable
057: setCalendar();
058: }
059:
060: /**
061: * Sets the period for incrementing the time
062: *
063: * @param p the WMS representation of the interval (P1D, PT1H...)
064: *
065: */
066: public void setPeriod(String p) {
067: // remove the "P" from the string
068: p = p.substring(1);
069:
070: // separate the date from the time
071: String datePeriod = getDate(p);
072: String timePeriod = getTime(p);
073:
074: // set the period
075: setDatePeriod(datePeriod);
076: setTimePeriod(timePeriod);
077: }
078:
079: /**
080: * Sets the date part of the period
081: *
082: * @param period a String
083: *
084: */
085: private void setDatePeriod(String period) {
086: StringTokenizer st = new StringTokenizer(period, "YMD", true);
087: while (st.hasMoreTokens()) {
088: String value = st.nextToken();
089: String unit = st.nextToken();
090: Integer intValue = new Integer(Integer.parseInt(value));
091: Integer calUnit = null;
092:
093: if (unit.equals("D"))
094: calUnit = new Integer(Calendar.DAY_OF_YEAR);
095: if (unit.equals("M"))
096: calUnit = new Integer(Calendar.MONTH);
097: if (unit.equals("Y"))
098: calUnit = new Integer(Calendar.YEAR);
099:
100: if (calUnit != null)
101: htPeriod.put(calUnit, intValue);
102: }
103: }
104:
105: /**
106: * Sets the time part of the period
107: *
108: * @param period a String
109: *
110: */
111: private void setTimePeriod(String period) {
112: StringTokenizer st = new StringTokenizer(period, "HMS", true);
113: while (st.hasMoreTokens()) {
114: String value = st.nextToken();
115: String unit = st.nextToken();
116:
117: if (unit.equals("S")) {
118: setSecondsPeriod(value);
119: }
120:
121: else {
122: Integer intValue = new Integer(Integer.parseInt(value));
123: Integer calUnit = null;
124:
125: if (unit.equals("H"))
126: calUnit = new Integer(Calendar.HOUR_OF_DAY);
127: if (unit.equals("M"))
128: calUnit = new Integer(Calendar.MINUTE);
129:
130: if (calUnit != null)
131: htPeriod.put(calUnit, intValue);
132: }
133: }
134: }
135:
136: /**
137: * Set the seconds part of the period
138: *
139: * @param value a String
140: *
141: * @exception NumberFormatException
142: *
143: */
144: private void setSecondsPeriod(String value)
145: throws NumberFormatException {
146: int p = value.indexOf(".");
147: if (p == -1)
148: p = value.length();
149:
150: String stSec = value.substring(0, p);
151: String stMsec = value.substring(p + 1, value.length());
152:
153: if (!stSec.equals("")) {
154: Integer sec = new Integer(Integer.parseInt(stSec));
155: htPeriod.put(new Integer(Calendar.SECOND), sec);
156: }
157: if (!stMsec.equals("")) {
158: float fMsec = Float.parseFloat("." + stMsec) * 1000;
159: Integer msec = new Integer((int) Math.abs(fMsec));
160: htPeriod.put(new Integer(Calendar.MILLISECOND), msec);
161: }
162: }
163:
164: /**
165: * Increments the time of a period
166: *
167: */
168: public void increment() {
169: for (Enumeration e = htPeriod.keys(); e.hasMoreElements();) {
170: Integer key = (Integer) e.nextElement();
171: Integer period = (Integer) htPeriod.get(key);
172:
173: c.add(key.intValue(), period.intValue());
174: }
175: }
176:
177: public boolean before(WmsTime t) {
178: return c.before(t.c);
179: }
180:
181: public boolean after(WmsTime t) {
182: return c.after(t.c);
183: }
184:
185: /**
186: * Returns the wms representation of the time
187: *
188: * @return a String
189: *
190: */
191: public String toString() {
192: String date = "";
193:
194: String stYear = c.get(Calendar.YEAR) + "";
195:
196: // century
197: if (century == null)
198: date += "-";
199: else
200: date += stYear.substring(0, 2);
201:
202: // year
203: if (year == null)
204: date += "-";
205: else
206: date += stYear.substring(2, 4);
207:
208: // month
209: if (!htTime.containsKey(new Integer(Calendar.MONTH)))
210: return date;
211: DecimalFormat f = new DecimalFormat("00");
212: date += "-";
213: String month = (String) htTime.get(new Integer(Calendar.MONTH));
214: if (month.equals("-"))
215: date += "-";
216: else
217: date += f.format(c.get(Calendar.MONTH) + 1);
218:
219: // day
220: if (!htTime.containsKey(new Integer(Calendar.DAY_OF_YEAR)))
221: return date;
222: date += "-";
223: String day = (String) htTime.get(new Integer(
224: Calendar.DAY_OF_YEAR));
225: if (day.equals("-"))
226: date += "-";
227: else
228: date += f.format(c.get(Calendar.DAY_OF_MONTH));
229:
230: // hour
231: if (!htTime.containsKey(new Integer(Calendar.HOUR)))
232: return date;
233: date += "T";
234: String hour = (String) htTime.get(new Integer(Calendar.HOUR));
235: if (hour.equals("-"))
236: date += "-";
237: else
238: date += f.format(c.get(Calendar.HOUR));
239:
240: // minute
241: if (!htTime.containsKey(new Integer(Calendar.MINUTE)))
242: return date;
243: date += ":";
244: String minute = (String) htTime
245: .get(new Integer(Calendar.MINUTE));
246: if (minute.equals("-"))
247: date += "-";
248: else
249: date += f.format(c.get(Calendar.MINUTE));
250:
251: if (!htTime.containsKey(new Integer(Calendar.SECOND)))
252: return date;
253: date += ":";
254: String second = (String) htTime
255: .get(new Integer(Calendar.SECOND));
256: if (second.equals("-"))
257: date += "-";
258: else
259: date += f.format(c.get(Calendar.SECOND));
260:
261: if (!htTime.containsKey(new Integer(Calendar.MILLISECOND)))
262: return date;
263: f = new DecimalFormat("000");
264: date += ".";
265: String millisecond = (String) htTime.get(new Integer(
266: Calendar.MILLISECOND));
267: if (millisecond.equals("-"))
268: date += "-";
269: else
270: date += f.format(c.get(Calendar.MILLISECOND));
271:
272: return date;
273: }
274:
275: /**
276: * Method getDate
277: *
278: * @param s a String
279: *
280: * @return the date portion of the given string
281: *
282: */
283: private String getDate(String s) {
284: int t = s.indexOf("T");
285: if (t == -1)
286: return s;
287:
288: return s.substring(0, t);
289: }
290:
291: /**
292: * Method getTime
293: *
294: * @param s a String
295: *
296: * @return the time portion of the given string
297: *
298: */
299: private String getTime(String s) {
300: int t = s.indexOf("T");
301: if (t == -1)
302: return "";
303:
304: return s.substring(t + 1);
305: }
306:
307: /**
308: * Parses the date portion
309: *
310: * @param date a String
311: *
312: */
313: private void parseDate(String date) {
314: // String[] weekDays = { "MON", "TUE", "WED", "THU", "FRI", "SAT", "SUN" };
315:
316: if (date.equals(""))
317: return;
318:
319: // century
320: int t = date.indexOf("-");
321: if (t == 0) {
322: century = null;
323: date = date.substring(1);
324: } else {
325: String stCentury = date.substring(0, 2);
326: century = new Integer(Integer.parseInt(stCentury));
327: date = date.substring(2);
328: }
329:
330: if (date.equals(""))
331: return;
332:
333: // year
334: t = date.indexOf("-");
335: if (t == 0) {
336: year = null;
337: date = date.substring(1);
338: } else {
339: String stYear = date.substring(0, 2);
340: year = new Integer(Integer.parseInt(stYear));
341: date = date.substring(2);
342: }
343:
344: // month
345: if (date.equals(""))
346: return;
347: t = date.indexOf("-", 1);
348: if (t == -1)
349: t = date.length();
350: String value;
351: if (t == 1)
352: htTime.put(new Integer(Calendar.MONTH), "-");
353: else {
354: value = date.substring(1, t);
355: htTime.put(new Integer(Calendar.MONTH), value);
356: }
357: date = date.substring(t);
358:
359: // day
360: if (date.equals(""))
361: return;
362: t = date.indexOf("-", 1);
363: if (t == -1)
364: t = date.length();
365: if (t == 1)
366: htTime.put(new Integer(Calendar.DAY_OF_YEAR), "-");
367: else {
368: value = date.substring(1, t);
369: htTime.put(new Integer(Calendar.DAY_OF_YEAR), value);
370: }
371:
372: date = date.substring(t);
373: }
374:
375: private void setCalendar() {
376: c = Calendar.getInstance();
377:
378: // set the year
379: int iYear;
380: if (century != null)
381: iYear = century.intValue() * 100;
382: else
383: iYear = 1900; // set an arbitrary century
384: if (year != null)
385: iYear += year.intValue();
386:
387: c.set(Calendar.YEAR, iYear);
388:
389: // set the month
390: String month = (String) htTime.get(new Integer(Calendar.MONTH));
391: if (month != null && !month.equals("-"))
392: c.set(Calendar.MONTH, Integer.parseInt(month) - 1);
393:
394: // set the day
395: String day = (String) htTime.get(new Integer(
396: Calendar.DAY_OF_YEAR));
397: if (day != null && !day.equals("-"))
398: c.set(Calendar.DAY_OF_MONTH, Integer.parseInt(day));
399:
400: // parsing of time is not yet implemented
401: // set the hour
402: String hour = (String) htTime.get(new Integer(Calendar.HOUR));
403: if (hour != null && !hour.equals("-"))
404: c.set(Calendar.HOUR, Integer.parseInt(hour));
405:
406: // set the minute
407: String minute = (String) htTime
408: .get(new Integer(Calendar.MINUTE));
409: if (minute != null && !minute.equals("-"))
410: c.set(Calendar.MINUTE, Integer.parseInt(minute));
411:
412: // set the second
413: String second = (String) htTime
414: .get(new Integer(Calendar.SECOND));
415: if (second != null && !second.equals("-"))
416: c.set(Calendar.SECOND, Integer.parseInt(second));
417:
418: // set the millisecond
419: String millisecond = (String) htTime.get(new Integer(
420: Calendar.MILLISECOND));
421: if (millisecond != null && !millisecond.equals("")
422: && !millisecond.equals("-")) {
423:
424: float fMsec = Float.parseFloat("." + millisecond) * 1000;
425: int msec = (int) Math.abs(fMsec);
426: c.set(Calendar.MILLISECOND, msec);
427:
428: }
429: }
430:
431: /**
432: * Parses the time portion
433: *
434: * @param time a String
435: *
436: */
437: private void parseTime(String time) {
438: StringTokenizer tok = new StringTokenizer(time, ":");
439: if (!tok.hasMoreTokens())
440: return;
441:
442: // hour
443: String t = tok.nextToken();
444: addTime(t, Calendar.HOUR);
445: if (!tok.hasMoreTokens())
446: return;
447:
448: // minute
449: t = tok.nextToken();
450: addTime(t, Calendar.MINUTE);
451: if (!tok.hasMoreTokens())
452: return;
453:
454: // second
455: t = tok.nextToken();
456: int p = t.indexOf(".");
457: if (p == -1)
458: p = t.length();
459:
460: String stSec = t.substring(0, p);
461: String stMsec = t.substring(p + 1, t.length());
462:
463: if (stSec != null)
464: addTime(stSec, Calendar.SECOND);
465: if (stMsec != null && !stMsec.equals(""))
466: addTime(stMsec, Calendar.MILLISECOND);
467: }
468:
469: private void addTime(String s, int i) throws NumberFormatException {
470: htTime.put(new Integer(i), s);
471: }
472:
473: }
|