Ticket #7284: 7284_v2.patch

File 7284_v2.patch, 5.4 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..d722c49 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 {  
    4445
    4546    public BingAerialTileSource() {
    4647        super("Bing Aerial Maps", "http://example.com/");
     48    }
    4749
    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> {
    5451
    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        }
    6455
    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();
    6869        }
    6970    }
    7071
    public class BingAerialTileSource extends AbstractTMSTileSource {  
    8889        return url;
    8990    }
    9091
    91     private List<Attribution> loadAttributionText() throws IOException {
     92    private List<Attribution> parseAttributionText(String xml) throws IOException {
    9293        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 
    9994            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    10095            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")));
    10298
    10399            XPathFactory xPathFactory = XPathFactory.newInstance();
    104100            XPath xpath = xPathFactory.newXPath();
    public class BingAerialTileSource extends AbstractTMSTileSource {  
    211207
    212208    @Override
    213209    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        }
    214229        try {
    215230            if (!attributions.isDone())
    216231                return "Loading Bing attribution data...";