📄 mapsourceloader.java
字号:
/* * Created on Aug 24, 2004 * */package org.placelab.mapper.loader;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.util.Enumeration;import java.util.Hashtable;import java.util.Vector;import org.placelab.core.TwoDCoordinate;import org.placelab.mapper.Beacon;import org.placelab.mapper.MapLoader;import org.placelab.mapper.MapUtils;import org.placelab.mapper.Mapper;import org.placelab.util.StringUtil;/** * */public class MapSourceLoader extends MapLoader implements Runnable { protected Vector sources; protected boolean deleteFirst; protected TwoDCoordinate coord1, coord2; protected Hashtable loadedAPs; protected boolean lastSource; protected boolean done, die; protected StringBuffer currentStatus; protected String currentError; protected int beaconsLoaded; public final static String SOURCES_URL = "http://www.placelab.org/data/sources.txt"; public MapSourceLoader(Mapper m, boolean delete) { super(m); sources = new Vector(); deleteFirst = delete; loadedAPs = new Hashtable(); done = die = false; currentStatus = new StringBuffer(); currentError = null; } public void addSource(MapSource source) { sources.addElement(source); } public void setArea(TwoDCoordinate one, TwoDCoordinate two) { coord1 = one; coord2 = two; } protected void setStatus(String s) { synchronized(currentStatus) { currentStatus.setLength(0); currentStatus.append(s); } } public boolean isDone() { return done; } public void die() { die = true; } public String getCurrentStatus() { synchronized(currentStatus) { return currentStatus.toString(); } } public String getError() { return currentError; } public int getBeaconCount() { return beaconsLoaded; } public void run() { setStatus("Opening map..."); if (!mapper.isOpened() && !mapper.open()) { setStatus("Map failed to open."); done = true; return; } if (deleteFirst) { setStatus("Clearing map..."); mapper.deleteAll(); } mapper.startBulkPuts(); Enumeration iter = sources.elements(); while (iter.hasMoreElements()) { MapSource source = (MapSource)iter.nextElement(); setStatus("Loading from " + source.getName()); lastSource = !iter.hasMoreElements(); try { loadMap(source); } catch (MapSourceException e) { currentError = e.getMessage(); setStatus("Loading error."); done = true; return; } } setStatus("Closing map..."); mapper.endBulkPuts(); mapper.close(); setStatus("Done."); done = true; } public void loadMap(MapSource source) throws MapSourceException { Enumeration iter = source.query(coord1, coord2); if (iter == null) { System.err.println(source.getName() + " returned null iterator"); return; } while (iter.hasMoreElements()) { String beacon = (String)iter.nextElement(); loadBeacon(source, beacon); if (die) break; } } public void loadBeacon(MapSource source, String s) { Beacon beacon = mapper.createBeacon(s); if (beacon == null || beacon.getId() == null) { System.err.println("Bad line: '" + s + "'"); return; } MapSource previous = (MapSource)loadedAPs.get(beacon.getId()); if (previous == null) { //never seen this ID in the mapper before mapper.putBeacon(beacon.getId(), beacon); if (!lastSource) loadedAPs.put(beacon.getId(), source); beaconsLoaded++; } else if (previous != null && previous == source) { // seen this ID and with this source so we add it if (mapper.overrideOnPut()) { Vector l = mapper.findBeacons(beacon.getId()); l.addElement(beacon); mapper.putBeacons(beacon.getId(), l); } else { mapper.putBeacon(beacon.getId(), beacon); } beaconsLoaded++; } // else System.out.println("Skipping duplicate"); } public static Vector getDefaultSources() { InputStream stream = MapUtils.getHttpStream(SOURCES_URL); if(stream == null) { System.err.println("Cannot open URL: " + SOURCES_URL); return null; } BufferedReader reader = new BufferedReader(new InputStreamReader(stream)); Vector sources = new Vector(); try { for (;;) { String line = reader.readLine(); if (line == null) break; String[] fields = StringUtil.split(line); if (fields.length < 2) continue; else if ("placelab".equals(fields[1])) sources.addElement(new PlacelabMapSource()); else if ("wigle".equals(fields[1])) sources.addElement(new WigleMapSource()); else if ("url".equals(fields[1]) && fields.length >= 3) sources.addElement(new URLMapSource(fields[0], fields[2])); } } catch (IOException e) {} return sources; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -