diff --git a/src/org/openstreetmap/gui/jmapviewer/tilesources/BingAerialTileSource.java b/src/org/openstreetmap/gui/jmapviewer/tilesources/BingAerialTileSource.java
index bb94d5c..2eedd48 100644
|
a
|
b
|
package org.openstreetmap.gui.jmapviewer.tilesources;
|
| 3 | 3 | //License: GPL. |
| 4 | 4 | |
| 5 | 5 | import java.awt.Image; |
| 6 | | import java.io.IOException; |
| 7 | | import java.io.InputStream; |
| | 6 | import java.io.*; |
| 8 | 7 | import java.net.URL; |
| 9 | | import java.net.URLConnection; |
| 10 | 8 | import java.util.ArrayList; |
| 11 | 9 | import java.util.List; |
| 12 | 10 | import java.util.Locale; |
| … |
… |
import javax.xml.xpath.XPathExpressionException;
|
| 26 | 24 | import javax.xml.xpath.XPathFactory; |
| 27 | 25 | |
| 28 | 26 | import org.openstreetmap.gui.jmapviewer.Coordinate; |
| | 27 | import org.openstreetmap.josm.io.CacheCustomContent; |
| | 28 | import org.openstreetmap.josm.io.UTFInputStreamReader; |
| 29 | 29 | import org.w3c.dom.Document; |
| 30 | 30 | import org.w3c.dom.Node; |
| 31 | 31 | import org.w3c.dom.NodeList; |
| | 32 | import org.xml.sax.InputSource; |
| 32 | 33 | import org.xml.sax.SAXException; |
| 33 | 34 | |
| 34 | 35 | public class BingAerialTileSource extends AbstractTMSTileSource { |
| … |
… |
public class BingAerialTileSource extends AbstractTMSTileSource {
|
| 47 | 48 | |
| 48 | 49 | if (attributions == null) { |
| 49 | 50 | attributions = Executors.newSingleThreadExecutor().submit(new Callable<List<Attribution>>() { |
| 50 | | public List<Attribution> call() throws Exception { |
| 51 | | List<Attribution> attrs = null; |
| 52 | | int waitTime = 1; |
| 53 | | do { |
| 54 | 51 | |
| | 52 | @Override |
| | 53 | public List<Attribution> call() throws Exception { |
| | 54 | BingAttributionData attributionLoader = new BingAttributionData(); |
| | 55 | int waitTimeSec = 1; |
| | 56 | while (true) { |
| 55 | 57 | try { |
| 56 | | attrs = loadAttributionText(); |
| 57 | | System.out.println("Successfully loaded Bing attribution data."); |
| 58 | | return attrs; |
| 59 | | } catch(IOException e) { |
| 60 | | System.err.println("Could not connect to Bing API. Will retry in " + waitTime + " seconds."); |
| 61 | | Thread.sleep(waitTime * 1000L); |
| 62 | | waitTime *= 2; |
| | 58 | return parseAttributionText(attributionLoader.updateIfRequiredString()); |
| | 59 | } catch (IOException ex) { |
| | 60 | System.err.println("Could not connect to Bing API. Will retry in " + waitTimeSec + " seconds."); |
| | 61 | Thread.sleep(waitTimeSec * 1000L); |
| | 62 | waitTimeSec *= 2; |
| 63 | 63 | } |
| 64 | | |
| 65 | | } while(true); |
| | 64 | } |
| 66 | 65 | } |
| 67 | 66 | }); |
| 68 | 67 | } |
| 69 | 68 | } |
| 70 | 69 | |
| | 70 | private static class BingAttributionData extends CacheCustomContent<IOException> { |
| | 71 | |
| | 72 | public BingAttributionData() { |
| | 73 | super("bing.attribution.xml", CacheCustomContent.INTERVAL_WEEKLY); |
| | 74 | } |
| | 75 | |
| | 76 | @Override |
| | 77 | protected byte[] updateData() throws IOException { |
| | 78 | URL u = new URL("http://dev.virtualearth.net/REST/v1/Imagery/Metadata/Aerial?include=ImageryProviders&output=xml&key=" |
| | 79 | + API_KEY); |
| | 80 | BufferedReader in = new BufferedReader(new InputStreamReader(u.openStream())); |
| | 81 | StringBuilder content = new StringBuilder(4096); |
| | 82 | String line; |
| | 83 | while ((line = in.readLine()) != null) { |
| | 84 | content.append(line); |
| | 85 | } |
| | 86 | in.close(); |
| | 87 | System.out.println("Successfully loaded Bing attribution data."); |
| | 88 | return content.toString().getBytes(); |
| | 89 | } |
| | 90 | } |
| | 91 | |
| 71 | 92 | class Attribution { |
| 72 | 93 | String attribution; |
| 73 | 94 | int minZoom; |
| … |
… |
public class BingAerialTileSource extends AbstractTMSTileSource {
|
| 88 | 109 | return url; |
| 89 | 110 | } |
| 90 | 111 | |
| 91 | | private List<Attribution> loadAttributionText() throws IOException { |
| | 112 | private List<Attribution> parseAttributionText(String xml) throws IOException { |
| 92 | 113 | try { |
| 93 | | URL u = new URL("http://dev.virtualearth.net/REST/v1/Imagery/Metadata/Aerial?include=ImageryProviders&output=xml&key=" |
| 94 | | + API_KEY); |
| 95 | | URLConnection conn = u.openConnection(); |
| 96 | | |
| 97 | | InputStream stream = conn.getInputStream(); |
| 98 | | |
| 99 | 114 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
| 100 | 115 | DocumentBuilder builder = factory.newDocumentBuilder(); |
| 101 | | Document document = builder.parse(stream); |
| | 116 | Document document = builder.parse(new InputSource( |
| | 117 | UTFInputStreamReader.create(new ByteArrayInputStream(xml.getBytes()), "UTF-8"))); |
| 102 | 118 | |
| 103 | 119 | XPathFactory xPathFactory = XPathFactory.newInstance(); |
| 104 | 120 | XPath xpath = xPathFactory.newXPath(); |