1 package tim.prune.function;
3 import java.io.InputStream;
4 import java.io.UnsupportedEncodingException;
6 import java.net.URLEncoder;
7 import java.util.ArrayList;
9 import javax.swing.JOptionPane;
10 import javax.xml.parsers.SAXParser;
11 import javax.xml.parsers.SAXParserFactory;
14 import tim.prune.I18nManager;
15 import tim.prune.data.DataPoint;
16 import tim.prune.data.Field;
17 import tim.prune.data.Latitude;
18 import tim.prune.data.Longitude;
19 import tim.prune.function.gpsies.GenericDownloaderFunction;
20 import tim.prune.function.gpsies.GpsiesTrack;
23 * Function to search Wikipedia for place names
25 public class SearchWikipediaNames extends GenericDownloaderFunction
28 private String _searchTerm = null;
29 /** Maximum number of results to get */
30 private static final int MAX_RESULTS = 20;
31 /** Username to use for geonames queries */
32 private static final String GEONAMES_USERNAME = "gpsprune";
36 * @param inApp App object
38 public SearchWikipediaNames(App inApp) {
45 public String getNameKey() {
46 return "function.searchwikipedianames";
50 * @param inColNum index of column, 0 or 1
51 * @return key for this column
53 protected String getColumnKey(int inColNum)
55 if (inColNum == 0) return "dialog.wikipedia.column.name";
60 * Before dialog is shown, need to get search term
64 Object search = JOptionPane.showInputDialog(_app.getFrame(),
65 I18nManager.getText("dialog.searchwikipedianames.search"),
66 I18nManager.getText(getNameKey()),
67 JOptionPane.QUESTION_MESSAGE, null, null, "");
70 _searchTerm = search.toString();
71 if (!_searchTerm.equals("")) {
78 * Run method to call geonames in separate thread
82 _statusLabel.setText(I18nManager.getText("confirm.running"));
84 String descMessage = "";
85 InputStream inStream = null;
87 // language (only de and en available)
88 String lang = I18nManager.getText("wikipedia.lang");
89 if (lang.equals("de") || lang.equals("als")) {
95 // Replace awkward characters with character equivalents
98 searchTerm = URLEncoder.encode(_searchTerm, "UTF-8");
99 } catch (UnsupportedEncodingException e1) {
100 searchTerm = _searchTerm;
102 // Example http://ws.geonames.org/wikipediaSearch?q=london&maxRows=10
103 String urlString = "http://api.geonames.org/wikipediaSearch?title=" + searchTerm
104 + "&maxRows=" + MAX_RESULTS + "&lang=" + lang + "&username=" + GEONAMES_USERNAME;
105 // Parse the returned XML with a special handler
106 GetWikipediaXmlHandler xmlHandler = new GetWikipediaXmlHandler();
109 URL url = new URL(urlString);
110 SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
111 inStream = url.openStream();
112 saxParser.parse(inStream, xmlHandler);
114 catch (Exception e) {
115 descMessage = e.getClass().getName() + " - " + e.getMessage();
117 // Close stream and ignore errors
120 } catch (Exception e) {}
121 // Add track list to model
122 ArrayList<GpsiesTrack> trackList = xmlHandler.getTrackList();
123 // TODO: Do a better job of sorting replies by relevance - use three different lists
124 _trackListModel.addTracks(trackList);
126 // Set status label according to error or "none found", leave blank if ok
127 if (descMessage.equals("") && (trackList == null || trackList.size() == 0)) {
128 descMessage = I18nManager.getText("dialog.gpsies.nonefound");
130 _statusLabel.setText(descMessage);
134 * Load the selected track or point
136 protected void loadSelected()
138 // Find the row selected in the table and get the corresponding track
139 int rowNum = _trackTable.getSelectedRow();
140 if (rowNum >= 0 && rowNum < _trackListModel.getRowCount())
142 String coords = _trackListModel.getTrack(rowNum).getDownloadLink();
143 String[] latlon = coords.split(",");
144 if (latlon.length == 2)
146 DataPoint point = new DataPoint(new Latitude(latlon[0]), new Longitude(latlon[1]), null);
147 point.setFieldValue(Field.WAYPT_NAME, _trackListModel.getTrack(rowNum).getTrackName(), false);
148 _app.createPoint(point);