/** * @author Hyacinthe MENIET * Created on 25 août 07 */ package net.dotmyself.restclient.yahoomaps; import java.io.UnsupportedEncodingException; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; import java.util.List; import javax.xml.bind.JAXBContext; import javax.xml.bind.JAXBException; import javax.xml.bind.Unmarshaller; /** * A class designed to wrap the Yahoo! Geocode service. */ public class GeocodeMapper { private static final String baseUrl = "http://api.local.yahoo.com/MapsService/V1/geocode"; private static final String appId = "YahooDemo"; //replace with your own code private static final String urlEncoding = "ISO-8859-1"; /** * Retrieves the location of an address. * @param street * The street address you are searching for. * @param city * The city for the location you are searching for. * @param state * The US state (if applicable) you are searching for. * @param zip * The US ZIP code (if applicable) you are searching for. * @return list of {@link ResultType} * @throws UnsupportedEncodingException * @throws JAXBException * @throws MalformedURLException */ public static List getResultSet(String street, String city, String state,String zip) throws UnsupportedEncodingException, MalformedURLException, JAXBException { StringBuilder sb = new StringBuilder(baseUrl); sb.append("?appid="); sb.append(URLEncoder.encode(appId, urlEncoding)); if(street != null && street.length() > 1) { sb.append("&street="); sb.append(URLEncoder.encode(street, urlEncoding)); } if(city != null && city.length() > 1) { sb.append("&city="); sb.append(URLEncoder.encode(city, urlEncoding)); } if(state != null && state.length() > 1) { sb.append("&state="); sb.append(URLEncoder.encode(state, urlEncoding)); } if(zip != null && zip.length() > 1) { sb.append("&zip="); sb.append(URLEncoder.encode(zip, urlEncoding)); } ResultSet rs = extractLocation(new URL(sb.toString())); return (rs != null) ? rs.getResult() : null; } /** * Retrieves the location of an address. * @param location * A free form field of address information. * @return list of {@link ResultType} * @throws UnsupportedEncodingException * @throws JAXBException * @throws MalformedURLException */ public static List getResultSet(String location) throws UnsupportedEncodingException, MalformedURLException, JAXBException { StringBuilder sb = new StringBuilder(baseUrl); sb.append("?appid="); sb.append(URLEncoder.encode(appId, urlEncoding)); if(location != null && location.length() > 1) { sb.append("&location="); sb.append(URLEncoder.encode(location, urlEncoding)); } ResultSet rs = extractLocation(new URL(sb.toString())); return (rs != null) ? rs.getResult() : null; } /** * Uses the JAXB classes to process the returned XML * and returns the corresponding {@link ResultSet} * @param url * The URL for the query. * @return {@link ResultSet} * @throws JAXBException * @throws MalformedURLException */ private static ResultSet extractLocation(URL url) throws JAXBException, MalformedURLException { JAXBContext context = JAXBContext.newInstance("net.dotmyself.restclient.yahoomaps"); Unmarshaller unmarshaller = context.createUnmarshaller(); return (ResultSet) unmarshaller.unmarshal(url); } }