Changeset 27507 in osm for applications


Ignore:
Timestamp:
2012-01-20T12:45:07+01:00 (13 years ago)
Author:
simon04
Message:

jmapviewer: remove JOSM specific stuff

File:
1 edited

Legend:

Unmodified
Added
Removed
  • applications/viewer/jmapviewer/src/org/openstreetmap/gui/jmapviewer/tilesources/BingAerialTileSource.java

    r27491 r27507  
    55import java.awt.Image;
    66import java.io.IOException;
    7 import java.io.StringReader;
     7import java.net.MalformedURLException;
    88import java.net.URL;
    99import java.util.ArrayList;
    1010import java.util.List;
    1111import java.util.Locale;
    12 import java.util.Scanner;
    1312import java.util.concurrent.Callable;
    1413import java.util.concurrent.Executors;
     
    2928
    3029import org.openstreetmap.gui.jmapviewer.Coordinate;
    31 import org.openstreetmap.josm.io.CacheCustomContent;
    32 import org.openstreetmap.josm.io.UTFInputStreamReader;
    3330import org.w3c.dom.Document;
    3431import org.w3c.dom.Node;
     
    5249    }
    5350
    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 {
    7252        String attribution;
    7353        int minZoom;
     
    8969    }
    9070
    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 {
    9277        try {
    9378            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    9479            DocumentBuilder builder = factory.newDocumentBuilder();
    95             Document document = builder.parse(new InputSource(new StringReader(xml)));
     80            Document document = builder.parse(xml);
    9681
    9782            XPathFactory xPathFactory = XPathFactory.newInstance();
     
    205190    }
    206191
     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
    207214    @Override
    208215    public String getAttributionText(int zoom, Coordinate topLeft, Coordinate botRight) {
    209216        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());
    228218        }
    229219        try {
Note: See TracChangeset for help on using the changeset viewer.