Ticket #7284: 7284.patch

File 7284.patch, 4.9 KB (added by simon04, 14 years ago)
  • src/org/openstreetmap/gui/jmapviewer/tilesources/BingAerialTileSource.java

    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;  
    33//License: GPL.
    44
    55import java.awt.Image;
    6 import java.io.IOException;
    7 import java.io.InputStream;
     6import java.io.*;
    87import java.net.URL;
    9 import java.net.URLConnection;
    108import java.util.ArrayList;
    119import java.util.List;
    1210import java.util.Locale;
    import javax.xml.xpath.XPathExpressionException;  
    2624import javax.xml.xpath.XPathFactory;
    2725
    2826import org.openstreetmap.gui.jmapviewer.Coordinate;
     27import org.openstreetmap.josm.io.CacheCustomContent;
     28import org.openstreetmap.josm.io.UTFInputStreamReader;
    2929import org.w3c.dom.Document;
    3030import org.w3c.dom.Node;
    3131import org.w3c.dom.NodeList;
     32import org.xml.sax.InputSource;
    3233import org.xml.sax.SAXException;
    3334
    3435public class BingAerialTileSource extends AbstractTMSTileSource {
    public class BingAerialTileSource extends AbstractTMSTileSource {  
    4748
    4849        if (attributions == null) {
    4950            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 {
    5451
     52                @Override
     53                public List<Attribution> call() throws Exception {
     54                    BingAttributionData attributionLoader = new BingAttributionData();
     55                    int waitTimeSec = 1;
     56                    while (true) {
    5557                        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;
    6363                        }
    64 
    65                     } while(true);
     64                    }
    6665                }
    6766            });
    6867        }
    6968    }
    7069
     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
    7192    class Attribution {
    7293        String attribution;
    7394        int minZoom;
    public class BingAerialTileSource extends AbstractTMSTileSource {  
    88109        return url;
    89110    }
    90111
    91     private List<Attribution> loadAttributionText() throws IOException {
     112    private List<Attribution> parseAttributionText(String xml) throws IOException {
    92113        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 
    99114            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    100115            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")));
    102118
    103119            XPathFactory xPathFactory = XPathFactory.newInstance();
    104120            XPath xpath = xPathFactory.newXPath();