01: package org.vfny.geoserver.wms.responses.featureInfo;
02:
03: import java.io.IOException;
04:
05: import org.geotools.feature.Feature;
06:
07: /**
08: * Template which supports timestamps and timespans for features.
09: * <p>
10: *
11: * </p>
12: * @author Justin Deoliveira, The Open Planning Project, jdeolive@openplans.org
13: *
14: */
15: public class FeatureTimeTemplate {
16:
17: FeatureTemplate delegate;
18:
19: public FeatureTimeTemplate() {
20: this (new FeatureTemplate());
21: }
22:
23: public FeatureTimeTemplate(FeatureTemplate delegate) {
24: this .delegate = delegate;
25: }
26:
27: /**
28: * Executes the template against the feature.
29: * <p>
30: * This method returns:
31: * <ul>
32: * <li><code>{"01/01/07"}</code>: timestamp as 1 element array
33: * <li><code>{"01/01/07","01/12/07"}</code>: timespan as 2 element array
34: * <li><code>{null,"01/12/07"}</code>: open ended (start) timespan as 2 element array
35: * <li><code>{"01/12/07",null}</code>: open ended (end) timespan as 2 element array
36: * <li><code>{}</code>: no timestamp information as empty array
37: * </ul>
38: * </p>
39: * @param feature The feature to execute against.
40: */
41: public String[] execute(Feature feature) throws IOException {
42: String output = delegate.template(feature, "time.ftl",
43: getClass());
44:
45: if (output != null) {
46: output = output.trim();
47: }
48:
49: //case of nothing specified
50: if (output == null || "".equals(output)) {
51: return new String[] {};
52: }
53:
54: //JD: split() returns a single value when the delimiter is at the
55: // end... but two when at the start do another check
56: String[] timespan = output.split("\\|\\|");
57: if (output.endsWith("||")) {
58: timespan = new String[] { timespan[0], null };
59: }
60:
61: if (timespan.length > 2) {
62: String msg = "Incorrect time syntax. Should be: <date>||<date>";
63: throw new IllegalArgumentException(msg);
64: }
65:
66: //case of just a timestamp
67: if (timespan.length == 1) {
68: return timespan;
69: }
70:
71: //case of open ended timespan
72: if (timespan[0] == null || "".equals(timespan[0].trim())) {
73: timespan[0] = null;
74: }
75: if (timespan[1] == null || "".equals(timespan[1].trim())) {
76: timespan[1] = null;
77: }
78:
79: return timespan;
80: }
81: }
|