Changeset 27507 in osm for applications/viewer
- Timestamp:
- 2012-01-20T12:45:07+01:00 (13 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/tilesources/BingAerialTileSource.java
r27491 r27507 5 5 import java.awt.Image; 6 6 import java.io.IOException; 7 import java. io.StringReader;7 import java.net.MalformedURLException; 8 8 import java.net.URL; 9 9 import java.util.ArrayList; 10 10 import java.util.List; 11 11 import java.util.Locale; 12 import java.util.Scanner;13 12 import java.util.concurrent.Callable; 14 13 import java.util.concurrent.Executors; … … 29 28 30 29 import org.openstreetmap.gui.jmapviewer.Coordinate; 31 import org.openstreetmap.josm.io.CacheCustomContent;32 import org.openstreetmap.josm.io.UTFInputStreamReader;33 30 import org.w3c.dom.Document; 34 31 import org.w3c.dom.Node; … … 52 49 } 53 50 54 private static class BingAttributionData extends CacheCustomContent<IOException> { 55 56 public BingAttributionData() { 57 super("bing.attribution.xml", CacheCustomContent.INTERVAL_WEEKLY); 58 } 59 60 @Override 61 protected byte[] updateData() throws IOException { 62 URL u = new URL("http://dev.virtualearth.net/REST/v1/Imagery/Metadata/Aerial?include=ImageryProviders&output=xml&key=" 63 + API_KEY); 64 UTFInputStreamReader in = UTFInputStreamReader.create(u.openStream(), "utf-8"); 65 String r = new Scanner(in).useDelimiter("\\A").next(); 66 System.out.println("Successfully loaded Bing attribution data."); 67 return r.getBytes("utf-8"); 68 } 69 } 70 71 class Attribution { 51 protected class Attribution { 72 52 String attribution; 73 53 int minZoom; … … 89 69 } 90 70 91 private List<Attribution> parseAttributionText(String xml) throws IOException { 71 protected URL getAttributionUrl() throws MalformedURLException { 72 return new URL("http://dev.virtualearth.net/REST/v1/Imagery/Metadata/Aerial?include=ImageryProviders&output=xml&key=" 73 + API_KEY); 74 } 75 76 protected List<Attribution> parseAttributionText(InputSource xml) throws IOException { 92 77 try { 93 78 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 94 79 DocumentBuilder builder = factory.newDocumentBuilder(); 95 Document document = builder.parse( new InputSource(new StringReader(xml)));80 Document document = builder.parse(xml); 96 81 97 82 XPathFactory xPathFactory = XPathFactory.newInstance(); … … 205 190 } 206 191 192 protected Callable<List<Attribution>> getAttributionLoaderCallable() { 193 return new Callable<List<Attribution>>() { 194 195 @Override 196 public List<Attribution> call() throws Exception { 197 int waitTimeSec = 1; 198 while (true) { 199 try { 200 InputSource xml = new InputSource(getAttributionUrl().openStream()); 201 List<Attribution> r = parseAttributionText(xml); 202 System.out.println("Successfully loaded Bing attribution data."); 203 return r; 204 } catch (IOException ex) { 205 System.err.println("Could not connect to Bing API. Will retry in " + waitTimeSec + " seconds."); 206 Thread.sleep(waitTimeSec * 1000L); 207 waitTimeSec *= 2; 208 } 209 } 210 } 211 }; 212 } 213 207 214 @Override 208 215 public String getAttributionText(int zoom, Coordinate topLeft, Coordinate botRight) { 209 216 if (attributions == null) { 210 attributions = Executors.newSingleThreadExecutor().submit(new Callable<List<Attribution>>() { 211 212 @Override 213 public List<Attribution> call() throws Exception { 214 BingAttributionData attributionLoader = new BingAttributionData(); 215 int waitTimeSec = 1; 216 while (true) { 217 try { 218 return parseAttributionText(attributionLoader.updateIfRequiredString()); 219 } catch (IOException ex) { 220 System.err.println("Could not connect to Bing API. Will retry in " + waitTimeSec + " seconds."); 221 Thread.sleep(waitTimeSec * 1000L); 222 waitTimeSec *= 2; 223 ex.printStackTrace(); 224 } 225 } 226 } 227 }); 217 attributions = Executors.newSingleThreadExecutor().submit(getAttributionLoaderCallable()); 228 218 } 229 219 try {
Note:
See TracChangeset
for help on using the changeset viewer.