1 package com.lamatek.tags.google; 2 3 import java.io.InputStream ; 4 import java.io.Serializable ; 5 import java.net.URL ; 6 import java.net.URLConnection ; 7 import java.util.Date ; 8 import java.util.Vector ; 9 10 import javax.servlet.jsp.tagext.BodyTagSupport ; 11 import javax.servlet.jsp.tagext.Tag ; 12 import javax.xml.parsers.DocumentBuilder ; 13 import javax.xml.parsers.DocumentBuilderFactory ; 14 15 import org.w3c.dom.Document ; 16 import org.w3c.dom.NamedNodeMap ; 17 import org.w3c.dom.Node ; 18 import org.w3c.dom.NodeList ; 19 20 import com.lamatek.tags.google.beans.TrafficEventBean; 21 22 31 public class GoogleMapTrafficTag extends BodyTagSupport implements Serializable { 32 33 String id = null; 34 String point = null; 35 double radius = 0.00d; 36 Vector events = null; 37 int severity = 1; 38 String type = "both"; GoogleMapIconTag icon = null; 40 41 49 public boolean geocode(double longitude, double latitude) { 50 boolean coded = true; 51 String url = "http://api.local.yahoo.com/MapsService/V1/trafficData?appid=google-jsp-taglib"; 52 url += "&longitude=" + longitude + "&latitude=" + latitude; 53 url += "&severity=" + severity + "&radius=" + radius; 54 url += "&include_map=0"; 55 try { 56 URLConnection con = new URL (url).openConnection(); 57 if (con != null) { 58 InputStream input = con.getInputStream(); 59 DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 60 Document elements = parser.parse(input); 61 NodeList results = elements.getElementsByTagName("Result"); 62 for (int i = 0; i < results.getLength(); i++) { 63 Node result = results.item(i); 64 NamedNodeMap attributes = result.getAttributes(); 65 String eventType = attributes.getNamedItem("type").getNodeValue(); 66 if (type.equals("both") || type.equals(eventType)) { 67 if (events == null) 68 events = new Vector (); 69 TrafficEventBean event = new TrafficEventBean(); 70 event.setType(eventType); 71 NodeList children = result.getChildNodes(); 72 for (int j = 0; j < children.getLength(); j++) { 73 Node child = children.item(j); 74 Node text = null; 75 NodeList sub = child.getChildNodes(); 76 for (int c = 0; c < sub.getLength(); c++) { 77 if (sub.item(c).getNodeName().equals("#text")) { 78 text = sub.item(c); 79 } 80 } 81 if (child.getNodeName().equals("Title")) { 82 if (text != null) 83 event.setTitle(text.getNodeValue()); 84 } 85 else if (child.getNodeName().equals("Description")) { 86 if (text != null) 87 event.setDescription(text.getNodeValue()); 88 } 89 else if (child.getNodeName().equals("Longitude")) { 90 if (text != null) 91 event.setLongitude(Double.parseDouble(text.getNodeValue())); 92 } 93 else if (child.getNodeName().equals("Latitude")) { 94 if (text != null) 95 event.setLatitude(Double.parseDouble(text.getNodeValue())); 96 } 97 else if (child.getNodeName().equals("Severity")) { 98 if (text != null) 99 event.setSeverity(Integer.parseInt(text.getNodeValue())); 100 } 101 else if (child.getNodeName().equals("Direction")) { 102 if (text != null) 103 event.setDirection(text.getNodeValue()); 104 } 105 else if (child.getNodeName().equals("ReportDate")) { 106 if (text != null) 107 event.setReported(new Date (Long.parseLong(text.getNodeValue()))); 108 } 109 else if (child.getNodeName().equals("UpdateDate")) { 110 if (text != null) 111 event.setUpdated(new Date (Long.parseLong(text.getNodeValue()))); 112 } 113 else if (child.getNodeName().equals("EndDate")) { 114 if (text != null) 115 event.setEnding(new Date (Long.parseLong(text.getNodeValue()))); 116 } 117 } 118 events.addElement(event); 119 } 120 } 121 input.close(); 122 } 123 } 124 catch(Exception ex) { 125 ex.printStackTrace(System.err); 126 coded = false; 127 } 128 return coded; 129 } 130 134 public int doStartTag() { 135 return EVAL_BODY_INCLUDE; 136 } 137 143 public int eventCount() { 144 if (events == null) { 145 return 0; 146 } 147 else { 148 return events.size(); 149 } 150 } 151 156 public TrafficEventBean getEvent(int i) { 157 if (events == null) { 158 return null; 159 } 160 else { 161 return (TrafficEventBean) events.elementAt(i); 162 } 163 } 164 168 public int doEndTag() { 169 addTag(); 170 return EVAL_PAGE; 171 } 172 175 private void addTag() { 176 Tag tag = this; 177 while (tag.getParent() != null) { 178 if (tag.getParent() instanceof GoogleMapTag) { 179 ((GoogleMapTag) tag.getParent()).addTraffic_tag(this); 180 return; 181 } 182 tag = tag.getParent(); 183 } 184 } 185 190 public String getId() { 191 return id; 192 } 193 198 public void setId(String id) { 199 this.id = id; 200 } 201 206 public String getPoint() { 207 return point; 208 } 209 215 public void setPoint(String point) { 216 this.point = point; 217 } 218 224 public double getRadius() { 225 return radius; 226 } 227 233 public void setRadius(double radius) { 234 this.radius = radius; 235 } 236 242 public int getSeverity() { 243 return severity; 244 } 245 252 public void setType(String type) { 253 this.type = type; 254 } 255 262 public String getType() { 263 return type; 264 } 265 271 public void setSeverity(int severity) { 272 this.severity = severity; 273 } 274 279 public void setIcon(GoogleMapIconTag icon) { 280 this.icon = icon; 281 } 282 288 public GoogleMapIconTag getIcon() { 289 return icon; 290 } 291 } 292 | Popular Tags |