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.util.*;
027:
028: import org.jdom.*;
029:
030: import org.wfp.vam.intermap.kernel.map.mapServices.wms.dimensions.time.WmsTime;
031:
032: public class TimeExtent {
033: private Vector dates = new Vector();
034:
035: public static void main(String args[]) {
036: TimeExtent te = new TimeExtent(new Element("asdfasdf"));
037:
038: te.getIntervalDates("2003-01-01/2003-12-01/P1M"); // DEBUG
039: System.out.println("\n\n\n"
040: + jeeves.utils.Xml.getString(te.getJdom())); // DEBUG
041: }
042:
043: public TimeExtent(Element extent) {
044: this (extent.getText());
045: }
046:
047: public TimeExtent(String value) {
048: // Element values = new Element("values");
049:
050: StringTokenizer s = new StringTokenizer(value, ",");
051:
052: // Time t = new Time("2003-12-23"); // DEBUG
053: // t.setPeriod("P1D"); // DEBUG
054: // t.increment();
055:
056: // getIntervalDates("2003-12-23T11:12:13/2003-12-23T11:12:20/PT1S"); // DEBUG
057: // getIntervalDates("2003-07/2003-12/P1M"); // DEBUG
058: // System.out.println("\n\n\n" + jeeves.utils.Xml.getString(getJdom())); // DEBUG
059:
060: while (s.hasMoreTokens()) {
061: String token = s.nextToken();
062:
063: if (token.indexOf("/") == -1) { // doesn't contain "/" so it is a date and not an interval
064: // values.addContent(new Element(token));
065: dates.add(token);
066: } else
067: getIntervalDates(token); // contains "/" so it is an interval (ex. 1995-01-01/2004-01-06/P1D)
068: }
069:
070: }
071:
072: /**
073: * Returns a Jdom Element containig all the dates
074: *
075: * @return an Element
076: *
077: */
078: public Element getJdom() {
079: Element elDates = new Element("extent").setAttribute("name",
080: "time");
081: for (Iterator i = dates.iterator(); i.hasNext();) {
082: String date = (String) i.next();
083: elDates.addContent(new Element("value").setText(date));
084: }
085:
086: return elDates;
087: }
088:
089: /**
090: * Returns all the dates contained in the given interval
091: *
092: * @param interval a String
093: *
094: */
095: private void getIntervalDates(String interval) {
096: System.out.println("interval: " + interval);
097:
098: StringTokenizer st = new StringTokenizer(interval, "/");
099: WmsTime start = new WmsTime(st.nextToken());
100: WmsTime stop = new WmsTime(st.nextToken());
101: start.setPeriod(st.nextToken());
102:
103: getDates(start, stop);
104: }
105:
106: private void getDates(WmsTime beginDate, WmsTime endDate) {
107: for (; !beginDate.after(endDate); beginDate.increment()) {
108: dates.add(beginDate.toString());
109: }
110: }
111:
112: }
|