001: /**
002: * GeoRSSCodec.java
003: */package org.wfp.vam.intermap.kernel.map.mapServices.wmc;
004:
005: /**
006: * This is a poor man's implementation of georss, adapted on just what gn needs.
007: * Please feel free to use some real georss library.
008: *
009: * A simple GeoRSS Atom feed looks like this:
010: *
011: <?xml version="1.0" encoding="utf-8"?>
012: <feed xmlns="http://www.w3.org/2005/Atom"
013: xmlns:georss="http://www.georss.org/georss">
014: <title>Earthquakes</title>
015:
016: <subtitle>International earthquake observation labs</subtitle>
017: <link href="http://example.org/"/>
018: <updated>2005-12-13T18:30:02Z</updated>
019: <author>
020: <name>Dr. Thaddeus Remor</name>
021:
022: <email>tremor@quakelab.edu</email>
023: </author>
024: <id>urn:uuid:60a76c80-d399-11d9-b93C-0003939e0af6</id>
025: <entry>
026: <title>M 3.2, Mona Passage</title>
027:
028: <link href="http://example.org/2005/09/09/atom01"/>
029: <id>urn:uuid:1225c695-cfb8-4ebb-aaaa-80da344efa6a</id>
030: <updated>2005-08-17T07:02:32Z</updated>
031: <summary>We just had a big one.</summary>
032:
033: <georss:point>45.256 -71.92</georss:point>
034: </entry>
035: </feed>
036:
037: *
038: * @author ETj
039: */
040:
041: import java.util.List;
042: import org.jdom.Element;
043: import org.jdom.Namespace;
044: import org.wfp.vam.intermap.kernel.marker.Marker;
045: import org.wfp.vam.intermap.kernel.marker.MarkerSet;
046:
047: public class GeoRSSCodec {
048: private final static Namespace NS_ATOM = Namespace.getNamespace(
049: "atom", "http://www.w3.org/2005/Atom");
050: private final static Namespace NS_GEORSS = Namespace.getNamespace(
051: "georss", "http://www.georss.org/georss");
052:
053: public static Element getGeoRSS(MarkerSet ms) {
054: return getGeoRSS(ms, "intermap markerset");
055: }
056:
057: public static Element getGeoRSS(MarkerSet ms, String title) {
058: Element feed = new Element("feed", NS_ATOM);
059: feed.addContent(new Element("title", NS_ATOM).setText(title));
060:
061: for (Marker marker : ms) {
062: feed.addContent(getGeoRSSEntry(marker));
063: }
064:
065: return feed;
066: }
067:
068: private static Element getGeoRSSEntry(Marker marker) {
069: Element entry = new Element("entry", NS_ATOM);
070: entry.addContent(new Element("title", NS_ATOM).setText(marker
071: .getTitle()));
072: entry.addContent(new Element("summary", NS_ATOM).setText(marker
073: .getDesc()));
074: entry.addContent(new Element("point", NS_GEORSS).setText(marker
075: .getLat()
076: + " " + marker.getLon()));
077: return entry;
078: }
079:
080: public static MarkerSet parseGeoRSS(Element feed) {
081: MarkerSet ms = new MarkerSet();
082:
083: if (feed.getName().equals("feed")) {
084: List<Element> entries = (List<Element>) feed.getChildren(
085: "entry", NS_ATOM);
086: for (Element entry : entries) {
087: ms.add(parseGeoRSSEntry(entry));
088: }
089: } else {
090: throw new IllegalArgumentException(
091: "feed element not found. Found '" + feed.getName()
092: + "'");
093: }
094:
095: return ms;
096: }
097:
098: /**
099: * Method parseGeoRSSEntry
100: *
101: * @param entry an Element
102: *
103: * @return a Marker
104: */
105: private static Marker parseGeoRSSEntry(Element entry) {
106: String title = entry.getChildTextTrim("title", NS_ATOM);
107: String summary = entry.getChildTextTrim("summary", NS_ATOM);
108: String scoords = entry.getChildTextTrim("point", NS_GEORSS);
109:
110: String coords[] = scoords.split(" ");
111: Double lat = new Double(coords[0]);
112: Double lon = new Double(coords[1]);
113:
114: return new Marker(lat, lon, title, summary);
115: }
116:
117: }
|