X-Git-Url: https://gitweb.fperrin.net/?a=blobdiff_plain;f=tim%2Fprune%2Ffunction%2FSearchWikipediaNames.java;h=5712983394e24882d7b1098a1eeead49d92f41e8;hb=7f5ed2be62905bd37717376dc22d09e5ea7edb4d;hp=4377df81d5a39bbdd65bc897d0bf575104b2011e;hpb=f35b6d628f68e3b5ef19965ad8988d0dd1eb8efa;p=GpsPrune.git diff --git a/tim/prune/function/SearchWikipediaNames.java b/tim/prune/function/SearchWikipediaNames.java index 4377df8..5712983 100644 --- a/tim/prune/function/SearchWikipediaNames.java +++ b/tim/prune/function/SearchWikipediaNames.java @@ -1,7 +1,9 @@ package tim.prune.function; import java.io.InputStream; +import java.io.UnsupportedEncodingException; import java.net.URL; +import java.net.URLEncoder; import java.util.ArrayList; import javax.swing.JOptionPane; @@ -26,6 +28,8 @@ public class SearchWikipediaNames extends GenericDownloaderFunction private String _searchTerm = null; /** Maximum number of results to get */ private static final int MAX_RESULTS = 20; + /** Username to use for geonames queries */ + private static final String GEONAMES_USERNAME = "gpsprune"; /** * Constructor @@ -63,7 +67,7 @@ public class SearchWikipediaNames extends GenericDownloaderFunction JOptionPane.QUESTION_MESSAGE, null, null, ""); if (search != null) { - _searchTerm = search.toString(); + _searchTerm = search.toString().toLowerCase(); if (!_searchTerm.equals("")) { super.begin(); } @@ -77,22 +81,43 @@ public class SearchWikipediaNames extends GenericDownloaderFunction { _statusLabel.setText(I18nManager.getText("confirm.running")); - String descMessage = ""; - InputStream inStream = null; + // Replace awkward characters with character equivalents + final String searchTerm = encodeSearchTerm(_searchTerm); - // language (only de and en available) + // Firstly try the local language String lang = I18nManager.getText("wikipedia.lang"); - if (lang.equals("de") || lang.equals("als")) { - lang = "de"; + submitSearch(searchTerm, lang); + // If we didn't get anything, try a secondary language + if (_trackListModel.isEmpty() && _errorMessage == null && lang.equals("als")) { + submitSearch(searchTerm, "de"); } - else { - lang = "en"; + // If still nothing then try english + if (_trackListModel.isEmpty() && _errorMessage == null && !lang.equals("en")) { + submitSearch(searchTerm, "en"); } - // Example http://ws.geonames.org/wikipediaSearch?q=london&maxRows=10 - String urlString = "http://ws.geonames.org/wikipediaSearch?title=" + _searchTerm + "&maxRows=" + MAX_RESULTS - + "&lang=" + lang; + + // Set status label according to error or "none found", leave blank if ok + if (_errorMessage == null && _trackListModel.isEmpty()) { + _errorMessage = I18nManager.getText("dialog.gpsies.nonefound"); + } + _statusLabel.setText(_errorMessage == null ? "" : _errorMessage); + } + + /** + * Submit the given search to the server + * @param inSearchTerm search term + * @param inLang language code such as en, de + */ + private void submitSearch(String inSearchTerm, String inLang) + { + // System.out.println("Searching for '" + inSearchTerm + "' with lang: " + inLang); + + String urlString = "http://api.geonames.org/wikipediaSearch?title=" + inSearchTerm + + "&maxRows=" + MAX_RESULTS + "&lang=" + inLang + "&username=" + GEONAMES_USERNAME; // Parse the returned XML with a special handler GetWikipediaXmlHandler xmlHandler = new GetWikipediaXmlHandler(); + InputStream inStream = null; + try { URL url = new URL(urlString); @@ -101,7 +126,7 @@ public class SearchWikipediaNames extends GenericDownloaderFunction saxParser.parse(inStream, xmlHandler); } catch (Exception e) { - descMessage = e.getClass().getName() + " - " + e.getMessage(); + _errorMessage = e.getClass().getName() + " - " + e.getMessage(); } // Close stream and ignore errors try { @@ -111,30 +136,81 @@ public class SearchWikipediaNames extends GenericDownloaderFunction ArrayList trackList = xmlHandler.getTrackList(); // TODO: Do a better job of sorting replies by relevance - use three different lists _trackListModel.addTracks(trackList); + } - // Set status label according to error or "none found", leave blank if ok - if (descMessage.equals("") && (trackList == null || trackList.size() == 0)) { - descMessage = I18nManager.getText("dialog.gpsies.nonefound"); + /** + * Replace tricky characters in the search term and encode the others + * @param inSearchTerm entered search term + * @return modified search term to give to geonames + */ + private static final String encodeSearchTerm(String inSearchTerm) + { + if (inSearchTerm != null && inSearchTerm.length() > 0) + { + // Replace umlauts oe, ue, ae, OE, UE, AE, szlig + StringBuilder sb = new StringBuilder(); + final int numChars = inSearchTerm.length(); + for (int i=0; i= 0 && rowNum < _trackListModel.getRowCount()) + // Find the rows selected in the table and get the corresponding coords + int numSelected = _trackTable.getSelectedRowCount(); + if (numSelected < 1) return; + int[] rowNums = _trackTable.getSelectedRows(); + for (int i=0; i= 0 && rowNum < _trackListModel.getRowCount()) { - DataPoint point = new DataPoint(new Latitude(latlon[0]), new Longitude(latlon[1]), null); - point.setFieldValue(Field.WAYPT_NAME, _trackListModel.getTrack(rowNum).getTrackName(), false); - _app.createPoint(point); + String coords = _trackListModel.getTrack(rowNum).getDownloadLink(); + String[] latlon = coords.split(","); + if (latlon.length == 2) + { + DataPoint point = new DataPoint(new Latitude(latlon[0]), new Longitude(latlon[1]), null); + point.setFieldValue(Field.WAYPT_NAME, _trackListModel.getTrack(rowNum).getTrackName(), false); + _app.createPoint(point); + } } } // Close the dialog