demo\GeorssTask.java
/* Copyright 2010 ESRI * * All rights reserved under the copyright laws of the United States * and applicable international laws, treaties, and conventions. * * You may freely redistribute and use this sample code, with or * without modification, provided you include the original copyright * notice and use restrictions. * * See the use restrictions. * */ package demo; import java.io.Serializable; import java.net.URL; import java.net.URLConnection; import java.util.ArrayList; import java.util.Enumeration; import java.util.HashMap; import java.util.Hashtable; import java.util.LinkedHashMap; import java.util.Map; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; import com.esri.adf.web.data.GraphicElement; import com.esri.adf.web.data.WebGraphics; import com.esri.adf.web.data.geometry.WebExtent; import com.esri.adf.web.data.geometry.WebGeometry; import com.esri.adf.web.data.geometry.WebPoint; import com.esri.adf.web.data.symbol.WebSimpleMarkerSymbol; import com.esri.adf.web.data.tasks.SimpleTaskInfo; import com.esri.adf.web.faces.event.MapEvent; import com.esri.adf.web.faces.event.TaskEvent; public class GeorssTask implements Serializable{ private static final long serialVersionUID = 1L; private String georssURL = "http://earthquake.usgs.gov/recenteqsww/catalogs/eqs7day-M2.5.xml"; private GeorssTaskInfo taskInfo = new GeorssTaskInfo(); private Hashtable hash; public void setGeorssURL(String georssURL) { this.georssURL = georssURL; } public String getGeorssURL() { return georssURL; } public void addGeorssSource(TaskEvent event) { try { getGeorssCoords(getGeorssURL()); if (hash.size() > 0) { WebSimpleMarkerSymbol pSymbol = new WebSimpleMarkerSymbol(); pSymbol.setColor("0,255,255"); pSymbol.setMarkerType(4); pSymbol.setOutlineColor("0,0,0"); pSymbol.setWidth(20); Enumeration locations = hash.keys(); while (locations.hasMoreElements()) { String[] strLoc = locations.nextElement().toString().split( ":"); if (strLoc.length >= 2) { WebPoint webPoint = new WebPoint(); webPoint.putCoords(Double.parseDouble(strLoc[0]), Double.parseDouble(strLoc[1])); if (webPoint != null) { WebGeometry webGeometry = (WebGeometry) webPoint; GraphicElement graphicElem = new GraphicElement(); graphicElem.setGeometry(webGeometry); graphicElem.setSymbol(pSymbol); event.getWebContext().getWebGraphics().addGraphics( graphicElem); } } } event.getWebContext().refresh(); } } catch (Exception e) { e.printStackTrace(); } } public void search(MapEvent event) { WebExtent ext = (WebExtent) event.getWebGeometry().toMapGeometry( event.getWebContext().getWebMap()); Enumeration locations = hash.keys(); ArrayList results = null; GeorssTaskResults geoRss = null; Map rssInfo = null; while (locations.hasMoreElements()) { String loc = locations.nextElement().toString(); WebPoint webpoint = new WebPoint(Double .parseDouble(loc.split(":")[0]), Double.parseDouble(loc .split(":")[1])); if (ext.contains(webpoint)) { rssInfo = (Map) hash.get(loc); if (results == null) results = new ArrayList(); geoRss = new GeorssTaskResults(event.getWebContext(), webpoint); geoRss.setRssName(rssInfo.get("title").toString()); geoRss.setRssDetails(rssInfo); results.add(geoRss); } } Map actions = new LinkedHashMap(); actions.put("Highlight", "highlight"); actions.put("Clear Highlight", "clearHighlight"); if (results != null) event.getWebContext().getWebResults().addResultsWithActionMap( "GeoRSS Search Results : ", results, "getRssName", "getRssDetails", actions); } public void clearGraphics(TaskEvent event) { WebGraphics graphics = event.getWebContext().getWebGraphics(); if (graphics != null) graphics.clearGraphics(); } public SimpleTaskInfo getTaskInfo() { return this.taskInfo; } public Hashtable getGeorssCoords(String urlPath) { Document doc; try { DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory .newInstance(); DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); URL url = new URL(urlPath); URLConnection urlConnection = url.openConnection(); urlConnection.connect(); doc = docBuilder.parse(urlConnection.getInputStream()); doc.normalize(); NodeList listOfFeatures = doc.getElementsByTagName("item"); if (hash == null) hash = new Hashtable(); for (int i = 0; i < listOfFeatures.getLength(); i++) { Element featElem = (Element) listOfFeatures.item(i); String coord = new String(featElem.getElementsByTagName( "geo:long").item(0).getFirstChild().getNodeValue() + ":" + featElem.getElementsByTagName("geo:lat").item(0) .getFirstChild().getNodeValue()); Map map = new HashMap(); for (int j = 0; j < featElem.getChildNodes().getLength(); j++) { NodeList pAttrList = featElem.getChildNodes(); Node attrNode = pAttrList.item(j); if (attrNode.getNodeType() == 1) { map.put(attrNode.getNodeName().toString(), attrNode .getFirstChild().getNodeValue()); } } hash.put(coord, map); } } catch (Exception e) { e.printStackTrace(); } return hash; } }