private void writeWaypoints(final cgCache cache) throws IOException { List<cgWaypoint> waypoints = cache.getWaypoints(); List<cgWaypoint> ownWaypoints = new ArrayList<cgWaypoint>(waypoints.size()); List<cgWaypoint> originWaypoints = new ArrayList<cgWaypoint>(waypoints.size()); for (cgWaypoint wp : cache.getWaypoints()) { if (wp.isUserDefined()) { ownWaypoints.add(wp); } else { originWaypoints.add(wp); } } int maxPrefix = 0; for (cgWaypoint wp : originWaypoints) { String prefix = wp.getPrefix(); try { maxPrefix = Math.max(Integer.parseInt(prefix), maxPrefix); } catch (NumberFormatException ex) { Log.e("Unexpected origin waypoint prefix='" + prefix + "'", ex); } writeCacheWaypoint(wp, prefix); } for (cgWaypoint wp : ownWaypoints) { maxPrefix++; String prefix = StringUtils.leftPad(String.valueOf(maxPrefix), 2, '0'); writeCacheWaypoint(wp, prefix); } }
public void testImportGpxZipAttachment() { Uri uri = Uri.parse("android.resource://cgeo.geocaching.test/raw/pq7545915"); GPXImporter.ImportGpxZipAttachmentThread importThread = new GPXImporter.ImportGpxZipAttachmentThread( uri, getInstrumentation().getContext().getContentResolver(), listId, importStepHandler, progressHandler); runImportThread(importThread); assertImportStepMessages( GPXImporter.IMPORT_STEP_START, GPXImporter.IMPORT_STEP_READ_FILE, GPXImporter.IMPORT_STEP_READ_WPT_FILE, GPXImporter.IMPORT_STEP_STORE_CACHES, GPXImporter.IMPORT_STEP_STORE_STATIC_MAPS, GPXImporter.IMPORT_STEP_FINISHED); SearchResult search = (SearchResult) importStepHandler.messages.get(5).obj; assertEquals(Collections.singletonList("GC31J2H"), new ArrayList<String>(search.getGeocodes())); cgCache cache = cgeoapplication.getInstance().loadCache("GC31J2H", LoadFlags.LOAD_CACHE_OR_DB); assertCacheProperties(cache); assertEquals( 1, cache .getWaypoints() .size()); // this is the original pocket query result without test waypoint }
public static void testGetOCCache() { String geoCode = "OU0331"; cgCache cache = OkapiClient.getCache(geoCode); assertNotNull(cache); assertEquals(geoCode, cache.getGeocode()); assertEquals("Oshkosh Municipal Tank", cache.getName()); }
private void writeAttributes(final cgCache cache) throws IOException { if (!cache.hasAttributes()) { return; } // TODO: Attribute conversion required: English verbose name, gpx-id gpx.write("<groundspeak:attributes>"); for (String attribute : cache.getAttributes()) { final CacheAttribute attr = CacheAttribute.getByGcRawName(CacheAttribute.trimAttributeName(attribute)); final boolean enabled = CacheAttribute.isEnabled(attribute); gpx.write("<groundspeak:attribute id=\""); gpx.write(Integer.toString(attr.id)); gpx.write("\" inc=\""); if (enabled) { gpx.write('1'); } else { gpx.write('0'); } gpx.write("\">"); gpx.write(StringEscapeUtils.escapeXml(attr.getL10n(enabled))); gpx.write("</groundspeak:attribute>"); } gpx.write("</groundspeak:attributes>"); }
public void testImportGpxZip() throws IOException { File pq7545915 = new File(tempDir, "7545915.zip"); copyResourceToFile(R.raw.pq7545915, pq7545915); GPXImporter.ImportGpxZipFileThread importThread = new GPXImporter.ImportGpxZipFileThread( pq7545915, listId, importStepHandler, progressHandler); runImportThread(importThread); assertImportStepMessages( GPXImporter.IMPORT_STEP_START, GPXImporter.IMPORT_STEP_READ_FILE, GPXImporter.IMPORT_STEP_READ_WPT_FILE, GPXImporter.IMPORT_STEP_STORE_CACHES, GPXImporter.IMPORT_STEP_STORE_STATIC_MAPS, GPXImporter.IMPORT_STEP_FINISHED); SearchResult search = (SearchResult) importStepHandler.messages.get(5).obj; assertEquals(Collections.singletonList("GC31J2H"), new ArrayList<String>(search.getGeocodes())); cgCache cache = cgeoapplication.getInstance().loadCache("GC31J2H", LoadFlags.LOAD_CACHE_OR_DB); assertCacheProperties(cache); assertEquals( 1, cache .getWaypoints() .size()); // this is the original pocket query result without test waypoint }
@Override public boolean accepts(final cgCache cache) { final cgCache fullCache = cgeoapplication .getInstance() .loadCache(cache.getGeocode(), EnumSet.of(LoadFlag.LOAD_ATTRIBUTES)); return fullCache.getAttributes().contains(attribute); }
private static int getFindsCount(cgCache cache) { if (cache.getLogCounts().isEmpty()) { cache.setLogCounts(cgData.loadLogCounts(cache.getGeocode())); } Integer logged = cache.getLogCounts().get(LogType.FOUND_IT); if (logged != null) { return logged; } return 0; }
private void setType(String parsedString) { final String knownType = cgBase.cacheTypes.get(parsedString); if (knownType != null) { cache.type = knownType; } else { if (cache.type == null || cache.type.length() == 0) { cache.type = "mystery"; // default for not recognized types } } }
private cgSearch storeParsedCaches(Collection<cgCache> caches) { final cgSearch search = new cgSearch(); final cgeoapplication app = cgeoapplication.getInstance(); int storedCaches = 0; for (cgCache cache : caches) { // remove from cache because a cache might be re-imported app.removeCacheFromCache(cache.getGeocode()); app.addCacheToSearch(search, cache); progressHandler.sendMessage(progressHandler.obtainMessage(0, ++storedCaches, 0)); } return search; }
/** * Return the first of the cache coordinates, the waypoint coordinates or the extra coordinates. * <code>null</code> entities are skipped. * * @param cache a cache * @param waypoint a waypoint * @param coords extra coordinates * @return the first non-null coordinates, or null if none are set */ private static Geopoint getCoordinates( final cgCache cache, final cgWaypoint waypoint, final Geopoint coords) { if (cache != null && cache.getCoords() != null) { return cache.getCoords(); } if (waypoint != null && waypoint.getCoords() != null) { return waypoint.getCoords(); } return coords; }
/** create a cache note from the UserData1 to UserData4 fields supported by GSAK */ private void createNoteFromGSAKUserdata() { if (StringUtils.isBlank(cache.getPersonalNote())) { final StringBuilder buffer = new StringBuilder(); for (final String anUserData : userData) { if (StringUtils.isNotBlank(anUserData)) { buffer.append(' ').append(anUserData); } } final String note = buffer.toString().trim(); if (StringUtils.isNotBlank(note)) { cache.setPersonalNote(note); } } }
/** create a cache note from the UserData1 to UserData4 fields supported by GSAK */ private void createNoteFromGSAKUserdata() { if (StringUtils.isBlank(cache.getPersonalNote())) { final StringBuilder buffer = new StringBuilder(); for (int i = 0; i < userData.length; i++) { if (StringUtils.isNotBlank(userData[i])) { buffer.append(' ').append(userData[i]); } } final String note = buffer.toString().trim(); if (StringUtils.isNotBlank(note)) { cache.setPersonalNote(note); } } }
public static cgCache getCache(final String geoCode) { final Parameters params = new Parameters("cache_code", geoCode, "fields", SERVICE_CACHE_FIELDS); final JSONObject data = request(ConnectorFactory.getConnector(geoCode), SERVICE_CACHE, params); if (data == null) { return null; } final cgCache cache = parseCache(data); long time = new Date().getTime(); cache.setUpdated(time); cache.setDetailedUpdate(time); return cache; }
private void writeLogs(final cgCache cache) throws IOException { final List<LogEntry> logs = cache.getLogs(); if (logs.size() <= 0) { return; } gpx.write("<groundspeak:logs>"); for (LogEntry log : logs) { gpx.write("<groundspeak:log id=\""); gpx.write(Integer.toString(log.id)); gpx.write("\">"); gpx.write("<groundspeak:date>"); gpx.write(StringEscapeUtils.escapeXml(dateFormatZ.format(new Date(log.date)))); gpx.write("</groundspeak:date>"); gpx.write("<groundspeak:type>"); gpx.write(StringEscapeUtils.escapeXml(log.type.type)); gpx.write("</groundspeak:type>"); gpx.write("<groundspeak:finder id=\"\">"); gpx.write(StringEscapeUtils.escapeXml(log.author)); gpx.write("</groundspeak:finder>"); gpx.write("<groundspeak:text encoded=\"False\">"); gpx.write(StringEscapeUtils.escapeXml(log.log)); gpx.write("</groundspeak:text>"); gpx.write("</groundspeak:log>"); } gpx.write("</groundspeak:logs>"); }
private void findGeoCode(final String input) { if (input == null || StringUtils.isNotBlank(cache.getGeocode())) { return; } final String trimmed = input.trim(); final MatcherWrapper matcherGeocode = new MatcherWrapper(patternGeocode, trimmed); if (matcherGeocode.find()) { final String geocode = matcherGeocode.group(1); // a geocode should not be part of a word if (geocode.length() == trimmed.length() || Character.isWhitespace(trimmed.charAt(geocode.length()))) { if (ConnectorFactory.canHandle(geocode)) { cache.setGeocode(geocode); } } } }
@Override protected void setUp() throws Exception { super.setUp(); premiumFilter = new StatePremiumFilter(); premiumCache = new cgCache(); premiumCache.setPremiumMembersOnly(true); }
/** * reset all fields that are used to store cache fields over the duration of parsing a single * cache */ private void resetCache() { type = null; sym = null; name = null; desc = null; cmt = null; cache = new cgCache(); cache.setReliableLatLon(true); for (int i = 0; i < userData.length; i++) { userData[i] = null; } }
private void findGeoCode(final String input) { if (input == null || (cache.geocode != null && cache.geocode.length() != 0)) { return; } final Matcher matcherGeocode = patternGeocode.matcher(input); if (matcherGeocode.find()) { if (matcherGeocode.groupCount() > 0) { final String geocode = matcherGeocode.group(1); if (ConnectorFactory.canHandle(geocode)) { cache.geocode = geocode; } } } }
public void testImportGpxWithWaypoints() throws IOException { File gc31j2h = new File(tempDir, "gc31j2h.gpx"); copyResourceToFile(R.raw.gc31j2h, gc31j2h); copyResourceToFile(R.raw.gc31j2h_wpts, new File(tempDir, "gc31j2h-wpts.gpx")); GPXImporter.ImportGpxFileThread importThread = new GPXImporter.ImportGpxFileThread(gc31j2h, listId, importStepHandler, progressHandler); runImportThread(importThread); assertImportStepMessages( GPXImporter.IMPORT_STEP_START, GPXImporter.IMPORT_STEP_READ_FILE, GPXImporter.IMPORT_STEP_READ_WPT_FILE, GPXImporter.IMPORT_STEP_STORE_CACHES, GPXImporter.IMPORT_STEP_STORE_STATIC_MAPS, GPXImporter.IMPORT_STEP_FINISHED); SearchResult search = (SearchResult) importStepHandler.messages.get(5).obj; assertEquals(Collections.singletonList("GC31J2H"), new ArrayList<String>(search.getGeocodes())); cgCache cache = cgeoapplication.getInstance().loadCache("GC31J2H", LoadFlags.LOAD_CACHE_OR_DB); assertCacheProperties(cache); assertEquals(2, cache.getWaypoints().size()); }
/** * Test {@link cgBase#parseCache(String, int) with "mocked" data * @param base */ @MediumTest public void testParseCache() { List<MockedCache> cachesToTest = new ArrayList<MockedCache>(); cachesToTest.add(new GC2CJPF()); cachesToTest.add(new GC1ZXX2()); for (MockedCache cache : cachesToTest) { cgCacheWrap caches = base.parseCache(cache.getData(), 0); cgCache cacheParsed = caches.cacheList.get(0); Assert.assertEquals(cacheParsed.getGeocode(), cache.getGeocode()); Assert.assertEquals(cacheParsed.getType(), cache.getType()); Assert.assertEquals(cacheParsed.getOwner(), cache.getOwner()); Assert.assertEquals(cacheParsed.getDifficulty(), cache.getDifficulty()); Assert.assertEquals(cacheParsed.getTerrain(), cache.getTerrain()); Assert.assertEquals(cacheParsed.getLatitude(), cache.getLatitude()); Assert.assertEquals(cacheParsed.getLongitude(), cache.getLongitude()); Assert.assertEquals(cacheParsed.isDisabled(), cache.isDisabled()); Assert.assertEquals(cacheParsed.isOwn(), cache.isOwn()); Assert.assertEquals(cacheParsed.isArchived(), cache.isArchived()); Assert.assertEquals(cacheParsed.isMembersOnly(), cache.isMembersOnly()); Assert.assertEquals(cacheParsed.getOwnerReal(), cache.getOwnerReal()); Assert.assertEquals(cacheParsed.getSize(), cache.getSize()); Assert.assertEquals(cacheParsed.getHint(), cache.getHint()); Assert.assertTrue(cacheParsed.getDescription().startsWith(cache.getDescription())); Assert.assertEquals(cacheParsed.getShortDescription(), cache.getShortDescription()); Assert.assertEquals(cacheParsed.getName(), cache.getName()); } }
@Override public boolean accepts(final cgCache cache) { final cgCache fullCache = cgeoapplication.getInstance().getCacheByGeocode(cache.getGeocode()); return fullCache.getAttributes().contains(attribute); }
@Override protected Boolean doInBackground(Void... params) { // quick check for being able to write the GPX file if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { return false; } try { final File exportLocation = new File(Settings.getGpxExportDir()); exportLocation.mkdirs(); final SimpleDateFormat fileNameDateFormat = new SimpleDateFormat("yyyyMMddHHmmss"); exportFile = new File( Settings.getGpxExportDir() + File.separatorChar + "export_" + fileNameDateFormat.format(new Date()) + ".gpx"); gpx = new BufferedWriter(new FileWriter(exportFile)); gpx.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); gpx.write( "<gpx version=\"1.0\" creator=\"c:geo - http://www.cgeo.org\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns=\"http://www.topografix.com/GPX/1/0\" xsi:schemaLocation=\"http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd http://www.topografix.com/GPX/1/0 http://www.topografix.com/GPX/1/0/gpx.xsd http://www.groundspeak.com/cache/1/0/1 http://www.groundspeak.com/cache/1/0/1/cache.xsd\">"); for (int i = 0; i < caches.size(); i++) { // reload the cache. otherwise logs, attributes and other detailed information is not // available final cgCache cache = cgeoapplication .getInstance() .loadCache(caches.get(i).getGeocode(), LoadFlags.LOAD_ALL_DB_ONLY); gpx.write("<wpt "); gpx.write("lat=\""); gpx.write(Double.toString(cache.getCoords().getLatitude())); gpx.write("\" "); gpx.write("lon=\""); gpx.write(Double.toString(cache.getCoords().getLongitude())); gpx.write("\">"); final Date hiddenDate = cache.getHiddenDate(); if (hiddenDate != null) { gpx.write("<time>"); gpx.write(StringEscapeUtils.escapeXml(dateFormatZ.format(hiddenDate))); gpx.write("</time>"); } gpx.write("<name>"); gpx.write(StringEscapeUtils.escapeXml(cache.getGeocode())); gpx.write("</name>"); gpx.write("<desc>"); gpx.write(StringEscapeUtils.escapeXml(cache.getName())); gpx.write("</desc>"); gpx.write("<url>"); gpx.write(cache.getUrl()); gpx.write("</url>"); gpx.write("<urlname>"); gpx.write(StringEscapeUtils.escapeXml(cache.getName())); gpx.write("</urlname>"); gpx.write("<sym>"); gpx.write(cache.isFound() ? "Geocache Found" : "Geocache"); gpx.write("</sym>"); gpx.write("<type>"); gpx.write(StringEscapeUtils.escapeXml("Geocache|" + cache.getType().pattern)); gpx.write("</type>"); gpx.write("<groundspeak:cache "); gpx.write("id=\""); gpx.write(cache.getCacheId()); gpx.write("\" available=\""); gpx.write(!cache.isDisabled() ? "True" : "False"); gpx.write("\" archived=\""); gpx.write(cache.isArchived() ? "True" : "False"); gpx.write("\" "); gpx.write("xmlns:groundspeak=\"http://www.groundspeak.com/cache/1/0/1\">"); gpx.write("<groundspeak:name>"); gpx.write(StringEscapeUtils.escapeXml(cache.getName())); gpx.write("</groundspeak:name>"); gpx.write("<groundspeak:placed_by>"); gpx.write(StringEscapeUtils.escapeXml(cache.getOwnerDisplayName())); gpx.write("</groundspeak:placed_by>"); gpx.write("<groundspeak:owner>"); gpx.write(StringEscapeUtils.escapeXml(cache.getOwnerUserId())); gpx.write("</groundspeak:owner>"); gpx.write("<groundspeak:type>"); gpx.write(StringEscapeUtils.escapeXml(cache.getType().pattern)); gpx.write("</groundspeak:type>"); gpx.write("<groundspeak:container>"); gpx.write(StringEscapeUtils.escapeXml(cache.getSize().id)); gpx.write("</groundspeak:container>"); writeAttributes(cache); gpx.write("<groundspeak:difficulty>"); gpx.write(Float.toString(cache.getDifficulty())); gpx.write("</groundspeak:difficulty>"); gpx.write("<groundspeak:terrain>"); gpx.write(Float.toString(cache.getTerrain())); gpx.write("</groundspeak:terrain>"); gpx.write("<groundspeak:country>"); gpx.write(StringEscapeUtils.escapeXml(cache.getLocation())); gpx.write("</groundspeak:country>"); gpx.write( "<groundspeak:state></groundspeak:state>"); // c:geo cannot manage 2 separate fields, // so we export as country gpx.write("<groundspeak:short_description html=\""); gpx.write(BaseUtils.containsHtml(cache.getShortDescription()) ? "True" : "False"); gpx.write("\">"); gpx.write(StringEscapeUtils.escapeXml(cache.getShortDescription())); gpx.write("</groundspeak:short_description>"); gpx.write("<groundspeak:long_description html=\""); gpx.write(BaseUtils.containsHtml(cache.getDescription()) ? "True" : "False"); gpx.write("\">"); gpx.write(StringEscapeUtils.escapeXml(cache.getDescription())); gpx.write("</groundspeak:long_description>"); gpx.write("<groundspeak:encoded_hints>"); gpx.write(StringEscapeUtils.escapeXml(cache.getHint())); gpx.write("</groundspeak:encoded_hints>"); writeLogs(cache); gpx.write("</groundspeak:cache>"); gpx.write("</wpt>"); writeWaypoints(cache); publishProgress(i + 1); } gpx.write("</gpx>"); gpx.close(); } catch (Exception e) { Log.e("GpxExport.ExportTask export", e); if (gpx != null) { try { gpx.close(); } catch (IOException ee) { } } // delete partial gpx file on error if (exportFile.exists()) { exportFile.delete(); } return false; } return true; }
private static cgCache parseCache(final JSONObject response) { final cgCache cache = new cgCache(); cache.setReliableLatLon(true); try { cache.setGeocode(response.getString(CACHE_CODE)); cache.setName(response.getString(CACHE_NAME)); // not used: names setLocation(cache, response.getString(CACHE_LOCATION)); cache.setType(getCacheType(response.getString(CACHE_TYPE))); final String status = response.getString(CACHE_STATUS); cache.setDisabled(status.equalsIgnoreCase("Temporarily unavailable")); cache.setArchived(status.equalsIgnoreCase("Archived")); // not used: url final JSONObject owner = response.getJSONObject(CACHE_OWNER); cache.setOwner(parseUser(owner)); cache.getLogCounts().put(LogType.LOG_FOUND_IT, response.getInt(CACHE_FOUNDS)); cache.getLogCounts().put(LogType.LOG_DIDNT_FIND_IT, response.getInt(CACHE_NOTFOUNDS)); cache.setSize(getCacheSize(response)); cache.setDifficulty((float) response.getDouble(CACHE_DIFFICULTY)); cache.setTerrain((float) response.getDouble(CACHE_TERRAIN)); if (!response.isNull(CACHE_RATING)) { cache.setRating((float) response.getDouble(CACHE_RATING)); } cache.setVotes(response.getInt(CACHE_VOTES)); cache.setFavoritePoints(response.getInt(CACHE_RECOMMENDATIONS)); // not used: req_password cache.setDescription(response.getString(CACHE_DESCRIPTION)); cache.setHint(Html.fromHtml(response.getString(CACHE_HINT)).toString()); // not used: hints final JSONArray images = response.getJSONArray(CACHE_IMAGES); if (images != null) { JSONObject imageResponse; for (int i = 0; i < images.length(); i++) { imageResponse = images.getJSONObject(i); if (imageResponse.getBoolean(CACHE_IMAGE_IS_SPOILER)) { final String title = imageResponse.getString(CACHE_IMAGE_CAPTION); final String url = absoluteUrl(imageResponse.getString(CACHE_IMAGE_URL), cache.getGeocode()); final cgImage image = new cgImage(url, title); if (cache.getSpoilers() == null) { cache.setSpoilers(new ArrayList<cgImage>()); } cache.getSpoilers().add(image); } } } // not used: attrnames cache.setLogs(parseLogs(response.getJSONArray(CACHE_LATEST_LOGS))); cache.setHidden(parseDate(response.getString(CACHE_HIDDEN))); } catch (JSONException e) { Log.e(Settings.tag, "OkapiClient.parseCache", e); } return cache; }
@Override protected boolean canCompare(cgCache cache1, cgCache cache2) { return cache1.getLogCounts() != null && cache2.getLogCounts() != null; }
private static void setLocation(final cgCache cache, final String location) { final String latitude = StringUtils.substringBefore(location, "|"); final String longitude = StringUtils.substringAfter(location, "|"); cache.setCoords(new Geopoint(latitude, longitude)); }
private static void setLocation(final cgCache cache, final String location) { final String latitude = StringUtils.substringBefore(location, "|"); final String longitude = StringUtils.substringAfter(location, "|"); // FIXME: the next line should be a setter at cgCache cache.setCoords(GeopointParser.parse(latitude, longitude)); }
private static void assertCacheProperties(cgCache cache) { assertNotNull(cache); assertFalse(cache.getLocation().startsWith(",")); assertTrue(cache.isReliableLatLon()); }