diff --git a/src/org/openstreetmap/gui/jmapviewer/tilesources/BingAerialTileSource.java b/src/org/openstreetmap/gui/jmapviewer/tilesources/BingAerialTileSource.java
index bb94d5c..d722c49 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 {
|
| 44 | 45 | |
| 45 | 46 | public BingAerialTileSource() { |
| 46 | 47 | super("Bing Aerial Maps", "http://example.com/"); |
| | 48 | } |
| 47 | 49 | |
| 48 | | if (attributions == null) { |
| 49 | | 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 { |
| | 50 | private static class BingAttributionData extends CacheCustomContent<IOException> { |
| 54 | 51 | |
| 55 | | 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; |
| 63 | | } |
| | 52 | public BingAttributionData() { |
| | 53 | super("bing.attribution.xml", CacheCustomContent.INTERVAL_WEEKLY); |
| | 54 | } |
| 64 | 55 | |
| 65 | | } while(true); |
| 66 | | } |
| 67 | | }); |
| | 56 | @Override |
| | 57 | protected byte[] updateData() throws IOException { |
| | 58 | URL u = new URL("http://dev.virtualearth.net/REST/v1/Imagery/Metadata/Aerial?include=ImageryProviders&output=xml&key=" |
| | 59 | + API_KEY); |
| | 60 | BufferedReader in = new BufferedReader(new InputStreamReader(u.openStream())); |
| | 61 | StringBuilder content = new StringBuilder(4096); |
| | 62 | String line; |
| | 63 | while ((line = in.readLine()) != null) { |
| | 64 | content.append(line); |
| | 65 | } |
| | 66 | in.close(); |
| | 67 | System.out.println("Successfully loaded Bing attribution data."); |
| | 68 | return content.toString().getBytes(); |
| 68 | 69 | } |
| 69 | 70 | } |
| 70 | 71 | |
| … |
… |
public class BingAerialTileSource extends AbstractTMSTileSource {
|
| 88 | 89 | return url; |
| 89 | 90 | } |
| 90 | 91 | |
| 91 | | private List<Attribution> loadAttributionText() throws IOException { |
| | 92 | private List<Attribution> parseAttributionText(String xml) throws IOException { |
| 92 | 93 | 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 | 94 | DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); |
| 100 | 95 | DocumentBuilder builder = factory.newDocumentBuilder(); |
| 101 | | Document document = builder.parse(stream); |
| | 96 | Document document = builder.parse(new InputSource( |
| | 97 | UTFInputStreamReader.create(new ByteArrayInputStream(xml.getBytes()), "UTF-8"))); |
| 102 | 98 | |
| 103 | 99 | XPathFactory xPathFactory = XPathFactory.newInstance(); |
| 104 | 100 | XPath xpath = xPathFactory.newXPath(); |
| … |
… |
public class BingAerialTileSource extends AbstractTMSTileSource {
|
| 211 | 207 | |
| 212 | 208 | @Override |
| 213 | 209 | public String getAttributionText(int zoom, Coordinate topLeft, Coordinate botRight) { |
| | 210 | if (attributions == null) { |
| | 211 | attributions = Executors.newSingleThreadExecutor().submit(new Callable<List<Attribution>>() { |
| | 212 | |
| | 213 | @Override |
| | 214 | public List<Attribution> call() throws Exception { |
| | 215 | BingAttributionData attributionLoader = new BingAttributionData(); |
| | 216 | int waitTimeSec = 1; |
| | 217 | while (true) { |
| | 218 | try { |
| | 219 | return parseAttributionText(attributionLoader.updateIfRequiredString()); |
| | 220 | } catch (IOException ex) { |
| | 221 | System.err.println("Could not connect to Bing API. Will retry in " + waitTimeSec + " seconds."); |
| | 222 | Thread.sleep(waitTimeSec * 1000L); |
| | 223 | waitTimeSec *= 2; |
| | 224 | } |
| | 225 | } |
| | 226 | } |
| | 227 | }); |
| | 228 | } |
| 214 | 229 | try { |
| 215 | 230 | if (!attributions.isDone()) |
| 216 | 231 | return "Loading Bing attribution data..."; |