📄 waddata.java
字号:
originLon, pixelsPerLat, pixelsPerLon, name); } } private void loadPlaces() throws IOException, WadDataFormatException { ZipEntry placeIndexEntry = wad.getEntry(placesIndexKey); if(placeIndexEntry == null || placeIndexEntry.isDirectory()) { // there is no requirement that any places be defined return; } InputStream indexStream = wad.getInputStream(placeIndexEntry); SectionedFileParser indexParser = null; try { indexParser = new SectionedFileParser( indexStream); } catch (SectionedFileFormatException sffe) { throw new WadDataFormatException(placesIndexKey, WadDataFormatException.BAD_RESOURCE_ERROR, sffe, ""); } Hashtable placeSection; if((placeSection = indexParser.getSection("Places")) == null) { throw new WadDataFormatException(placesIndexKey, WadDataFormatException.BAD_RESOURCE_ERROR, "No Places Section"); } Enumeration e = placeSection.keys(); while(e.hasMoreElements()) { String placeSetName = (String)e.nextElement(); String setPath = placesDirKey + placeSection.get(placeSetName); loadPlaceSet(placeSetName, setPath); } } private void loadPlaceSet(String placeSetName, String setPath) throws WadDataFormatException, IOException { Hashtable placeSet = new Hashtable(); String placeFilePath = setPath; ZipEntry placeFileEntry = wad.getEntry(placeFilePath); if(placeFileEntry == null) { throw new WadDataFormatException(placesIndexKey, placeFilePath, WadDataFormatException.MISSING_RESOURCE_ERROR, ""); } InputStream placeFile = wad.getInputStream(placeFileEntry); SectionedFileParser p = null; try { p = new SectionedFileParser(placeFile); } catch(SectionedFileFormatException sffe) { throw new WadDataFormatException(placesIndexKey, placeFilePath, WadDataFormatException.BAD_RESOURCE_ERROR, sffe, ""); } Hashtable setContents = p.allSections(); Enumeration e = setContents.keys(); while(e.hasMoreElements()) { String placeName = (String)e.nextElement(); Hashtable placeHash = (Hashtable)setContents.get(placeName); String placeType; String placeUrl; String placeText; double placeLat; double placeLon; String placeImageName; String placeImagePath; placeImagePath = getStringOrFail(placeHash, "icon_file", placesIndexKey, placeFilePath); // test that the image at least exists if(!containsResource(placeImagePath)) { throw new WadDataFormatException(setPath, placeImagePath, WadDataFormatException.MISSING_RESOURCE_ERROR, ""); } placeImageName = basename(placeImagePath); placeLat = getDoubleOrFail(placeHash, "lat", placesIndexKey, placeFilePath); placeLon = getDoubleOrFail(placeHash, "lon", placesIndexKey, placeFilePath); placeType = getStringOrBlank(placeHash, "type"); placeText = getStringOrBlank(placeHash, "text"); placeUrl = getStringOrBlank(placeHash, "url"); PlaceBacking place = new PlaceBacking(placeType, placeName, placeUrl, placeText, placeLat, placeLon, placeImageName, placeImagePath, this); placeSet.put(placeName, place); } places.put(placeSetName, placeSet); } /* helpers for parsing the sectioned files */ private String getStringOrFail(Hashtable inHash, String forKey, String referencedFrom, String entry) throws WadDataFormatException { if(inHash.containsKey(forKey)) { return (String)inHash.get(forKey); } else { throw new WadDataFormatException(referencedFrom, entry, WadDataFormatException.BAD_RESOURCE_ERROR, ""); } } private double getDoubleOrFail(Hashtable inHash, String forKey, String referencedFrom, String entry) throws WadDataFormatException { try { return Double.parseDouble(getStringOrFail(inHash, forKey, referencedFrom, entry)); } catch (NumberFormatException nfe) { throw new WadDataFormatException(referencedFrom, entry, WadDataFormatException.BAD_RESOURCE_ERROR, ""); } } private String getStringOrBlank(Hashtable inHash, String forKey) { String ret; ret = (String)inHash.get(forKey); if(ret != null) return ret; else return ""; } /** * Returns an ImageData for the file at path in the wad */ public ImageData getImageData(String path) throws IOException { ImageData data = (ImageData)imageResources.get(path); if(data == null) { ZipEntry e = wad.getEntry(path); if(e != null) { data = new ImageData(wad.getInputStream(e)); imageResources.put(path, data); } } return data; } /* methods for dealing with the ap cache */ /** * Find out whether the map wad contains an ap cache or not */ public boolean containsAPCache() { return containsResource(myApCacheKey); } /** * Uses JDBMMapLoader to load the ap cache into the WifiMapper */ public JDBMMapper loadAPCacheIntoMapper(String dbPath) throws IOException, WadDataFormatException { if(!containsAPCache() || (dbPath == null)) { return null; } MapLoader mapLoader; mapLoader = new MapLoader(new JDBMMapper(dbPath)); mapLoader.createNewMap(); mapLoader.loadMap(wad.getInputStream(wad.getEntry(myApCacheKey))); return (JDBMMapper)mapLoader.getMapper(); } /** * Loads the ap cache into a special place set accessable under * the place set name APs. This is used so we can display the * aps on maps like we do with places. * @param useIcon you must provide an ImageData to use as an * icon for the place, because every place must have an icon, and * I surely don't have one laying around. * @return the place set for the aps */ public Hashtable loadAPCacheAsPlaces(ImageData useIcon) throws IOException, WadDataFormatException { if(!containsAPCache()) { throw new WadDataFormatException(myApCacheKey, WadDataFormatException.MISSING_RESOURCE_ERROR, "cannot load the ap cache if it doesn't exist."); } Hashtable apSet = new Hashtable(); BufferedReader br = new BufferedReader(new InputStreamReader( wad.getInputStream(wad.getEntry(myApCacheKey)))); String line; while((line = br.readLine()) != null) { String tokens[] = StringUtil.split(line); if(tokens.length != 4) { // its too draconian to ask that the ap caches // have every line be perfect, because they just don't // in practice. No, I have no idea why. continue; } double apLat; double apLon; try { apLat = Double.parseDouble(tokens[0]); apLon = Double.parseDouble(tokens[1]); } catch(NumberFormatException nfe) { continue; } String name = tokens[2]; // mac address isn't really interesting for a place // but I use it as the key in the hashtable, since that // way it doesn't get the same ap twice String mac = StringUtil.canonicalizeBSSID(tokens[3]); PlaceBacking apPlace = new PlaceBacking("AP", name, "", "wireless access point: " + name, apLat, apLon, "ap icon", useIcon); apSet.put(mac, apPlace); } places.put("APs", apSet); return apSet; } /* methods for accessing/mutating the WadData */ public MapBacking getMap(String mapName) { return (MapBacking)maps.get(mapName); } public Hashtable getMaps() { return maps; } public void putMap(BitmapMapBacking map) { maps.put(map.getName(), map); } public Enumeration getPlaceSetNames() { return places.keys(); } /** * @return a Hashtable where place set names => * Hashtables where place names => PlaceBackings */ public Hashtable allPlaceSets() { return this.places; } public Hashtable getPlaceSet(String setName) { return (Hashtable)places.get(setName); } public PlaceBacking getPlace(String setName, String placeName) { Hashtable set = getPlaceSet(setName); if(set != null) { return (PlaceBacking)set.get(placeName); } else { return null; } } public void putPlaceSet(String setName, Hashtable set) { places.put(setName, set); } public void addPlace(String setName, PlaceBacking place) { Hashtable set = getPlaceSet(setName); if(set != null) { set.put(place.name, place); } } /** * Returns the MapBacking specified as the default for the wad * or null if no MapBacking is specified */ public MapBacking getDefaultMap() { if(defaultMap == null) return null; else return getMap(defaultMap); } /** * Returns a Hashtable containing the default place sets * (Hashtables of PlaceBackings) for the * wad or null if no default place sets are specified. */ public Hashtable getDefaultPlaceSets() { return defaultPlaces; } /* methods for writing WadData */ /** * Writes everything that is currently in the WadData out to * a map wad at path. A note on how images are written: all images * are written out to an entry in the zip with the filename * chosen by the wadRelativeImagePath attribute of the item being written. If I have * already written that image file, I do nothing. Despite the fact that * it would not increase the wad file size as a result of having two copies * of the same image and making the copies ensures that two different images * with the same name could coexist, this would defeat my attempt to only * have at most one copy of each image in memory. */ /*public void write(String path) throws IOException { ZipOutputStream out = new ZipOutputStream( new FileOutputStream(path)); // don't want to write the same entries twice HashSet entriesWritten = new HashSet(); Properties mapIndex = new Properties(); Properties placesIndex = new Properties(); Enumeration m = maps.elements(); while(m.hasMoreElements()) { MapBacking map = (MapBacking)m.nextElement(); // since the names are already unique in the Hashtable // i know I haven't already written a .meta file with this // name String mapMetaPath = mapsDirKey + map.name + ".meta"; Properties mapMeta = map.getProperties(); ZipEntry metaEntry = new ZipEntry(mapMetaPath); out.putNextEntry(metaEntry); mapMeta.store(out, map.name); out.closeEntry(); // now do the map image String imagePath = map.getWadRelativeImagePath(); // i must check that I haven't already written this image if(!(entriesWritten.contains(imagePath))) { ZipEntry imageEntry = new ZipEntry(imagePath); saveImage(out, imageEntry, map.getImageResource()); } // add the entry for this map to the index mapIndex.put(map.name, imagePath); } // put the map index ZipEntry mapIndexEntry = new ZipEntry(mapsIndexKey); out.putNextEntry(mapIndexEntry); mapIndex.store(out, "Hashtable Index"); out.closeEntry(); // now do the places }*/ // /* little helpers */// private void saveImage(ZipOutputStream out, ZipEntry at, ImageData image)// throws IOException {// ImageLoader imageWriter = new ImageLoader();// imageWriter.data = new ImageData[1];// imageWriter.data[0] = image;// out.putNextEntry(at);// try {// imageWriter.save(out, image.type);// } catch(SWTException swte) {// throw new IOException("couldn't write image " +// at.getName() + "with error: " + swte.toString());// }// out.closeEntry();// } private String basename(String path) { return path.substring(path.lastIndexOf('/') + 1, path.length() - 1); } private boolean containsResource(String path) { ZipEntry entry = wad.getEntry(path); return !(entry == null || entry.isDirectory()); } public InputStream getArbitraryResource(String path) throws IOException { if(!containsResource(path)) return null; return wad.getInputStream(wad.getEntry(path)); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -