001: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
002: * This code is licensed under the GPL 2.0 license, availible at the root
003: * application directory.
004: */
005: package org.vfny.geoserver.wms.responses.map.georss;
006:
007: import java.io.IOException;
008: import java.util.Date;
009: import java.util.Iterator;
010: import java.util.List;
011: import java.util.logging.Level;
012:
013: import org.geotools.feature.Feature;
014: import org.geotools.feature.FeatureCollection;
015: import org.geotools.feature.FeatureIterator;
016: import org.geotools.xml.transform.Translator;
017: import org.vfny.geoserver.wms.WMSMapContext;
018: import org.xml.sax.ContentHandler;
019: import org.xml.sax.helpers.AttributesImpl;
020:
021: public class AtomGeoRSSTransformer extends GeoRSSTransformerBase {
022: public Translator createTranslator(ContentHandler handler) {
023: return new AtomGeoRSSTranslator(handler);
024: }
025:
026: public class AtomGeoRSSTranslator extends GeoRSSTranslatorSupport {
027: public AtomGeoRSSTranslator(ContentHandler contentHandler) {
028: super (contentHandler, null, "http://www.w3.org/2005/Atom");
029:
030: nsSupport.declarePrefix("georss",
031: "http://www.georss.org/georss");
032: }
033:
034: public void encode(Object o) throws IllegalArgumentException {
035: WMSMapContext map = (WMSMapContext) o;
036:
037: start("feed");
038:
039: //title
040: element("title", map.getTitle());
041:
042: //link
043: //TODO: make getFeature request
044: AttributesImpl atts = new AttributesImpl();
045: atts.addAttribute(null, "href", "href", null,
046: "http://localhost...");
047: element("link", null, atts);
048:
049: //updated
050: //TODO: proper xml encoding of date
051: element("updated", new Date().toString());
052:
053: //entries
054: try {
055: encodeEntries(map);
056: } catch (IOException e) {
057: throw new RuntimeException(e);
058: }
059:
060: end("feed");
061: }
062:
063: void encodeEntries(WMSMapContext map) throws IOException {
064: List featureCollections = loadFeatureCollections(map);
065: for (Iterator f = featureCollections.iterator(); f
066: .hasNext();) {
067: FeatureCollection features = (FeatureCollection) f
068: .next();
069: FeatureIterator iterator = null;
070:
071: try {
072: iterator = features.features();
073:
074: while (iterator.hasNext()) {
075: Feature feature = iterator.next();
076: try {
077: encodeEntry(feature);
078: } catch (Exception e) {
079: LOGGER
080: .warning("Encoding failed for feature: "
081: + feature.getID());
082: LOGGER.log(Level.FINE, "", e);
083: }
084:
085: }
086: } finally {
087: if (iterator != null) {
088: features.close(iterator);
089: }
090: }
091: }
092:
093: }
094:
095: void encodeEntry(Feature feature) {
096: start("entry");
097:
098: //title
099: element("title", feature.getID());
100:
101: //link
102: //TODO: make getFeature with fid filter
103: AttributesImpl atts = new AttributesImpl();
104: atts.addAttribute(null, "href", "href", null,
105: "http://localhost...");
106: element("link", null, atts);
107:
108: //id
109: element("id", feature.getID());
110:
111: //updated
112: //TODO: proper xml encoding of date
113: element("updated", new Date().toString());
114:
115: //content
116: element("content", "some content");
117:
118: //where
119: start("georss:where");
120: encodeGeometry(feature);
121: end("georss:where");
122:
123: end("entry");
124: }
125: }
126: }
|