[38] | 1 | package org.openstreetmap.josm.io;
|
---|
| 2 |
|
---|
| 3 | import java.io.BufferedReader;
|
---|
| 4 | import java.io.IOException;
|
---|
| 5 | import java.io.Reader;
|
---|
| 6 | import java.util.ArrayList;
|
---|
| 7 | import java.util.Collection;
|
---|
| 8 | import java.util.LinkedList;
|
---|
| 9 | import java.util.StringTokenizer;
|
---|
| 10 |
|
---|
| 11 | import org.jdom.JDOMException;
|
---|
| 12 | import org.openstreetmap.josm.Main;
|
---|
[71] | 13 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
[78] | 14 | import org.openstreetmap.josm.gui.layer.RawGpsDataLayer.GpsPoint;
|
---|
[38] | 15 |
|
---|
| 16 | /**
|
---|
| 17 | * Read raw information from a csv style file (as defined in the preferences).
|
---|
| 18 | * @author imi
|
---|
| 19 | */
|
---|
| 20 | public class RawCsvReader {
|
---|
| 21 |
|
---|
| 22 | /**
|
---|
| 23 | * Reader to read the input from.
|
---|
| 24 | */
|
---|
| 25 | private BufferedReader in;
|
---|
| 26 |
|
---|
| 27 | public RawCsvReader(Reader in) {
|
---|
| 28 | this.in = new BufferedReader(in);
|
---|
| 29 | }
|
---|
| 30 |
|
---|
[78] | 31 | public Collection<GpsPoint> parse() throws JDOMException, IOException {
|
---|
| 32 | Collection<GpsPoint> data = new LinkedList<GpsPoint>();
|
---|
[74] | 33 | String formatStr = Main.pref.get("csvImportString");
|
---|
[38] | 34 | if (formatStr == null)
|
---|
| 35 | formatStr = in.readLine();
|
---|
| 36 | if (formatStr == null)
|
---|
| 37 | throw new JDOMException("Could not detect data format string.");
|
---|
| 38 |
|
---|
| 39 | // get delimiter
|
---|
| 40 | String delim = ",";
|
---|
| 41 | for (int i = 0; i < formatStr.length(); ++i) {
|
---|
| 42 | if (!Character.isLetterOrDigit(formatStr.charAt(i))) {
|
---|
| 43 | delim = ""+formatStr.charAt(i);
|
---|
| 44 | break;
|
---|
| 45 | }
|
---|
| 46 | }
|
---|
| 47 |
|
---|
| 48 | // convert format string
|
---|
| 49 | ArrayList<String> format = new ArrayList<String>();
|
---|
| 50 | for (StringTokenizer st = new StringTokenizer(formatStr, delim); st.hasMoreTokens();)
|
---|
| 51 | format.add(st.nextToken());
|
---|
| 52 |
|
---|
| 53 | // test for completness
|
---|
| 54 | if (!format.contains("lat") || !format.contains("lon")) {
|
---|
[74] | 55 | if (Main.pref.get("csvImportString").equals(""))
|
---|
| 56 | throw new JDOMException("Format string in data is incomplete or not found. Try setting an manual format string in Preferences.");
|
---|
| 57 | throw new JDOMException("Format string is incomplete. Need at least 'lat' and 'lon' specification");
|
---|
[38] | 58 | }
|
---|
| 59 |
|
---|
| 60 | int lineNo = 0;
|
---|
| 61 | try {
|
---|
| 62 | for (String line = in.readLine(); line != null; line = in.readLine()) {
|
---|
| 63 | lineNo++;
|
---|
| 64 | StringTokenizer st = new StringTokenizer(line, delim);
|
---|
[71] | 65 | double lat = 0, lon = 0;
|
---|
[78] | 66 | String time = null;
|
---|
[38] | 67 | for (String token : format) {
|
---|
| 68 | if (token.equals("lat"))
|
---|
[71] | 69 | lat = Double.parseDouble(st.nextToken());
|
---|
[38] | 70 | else if (token.equals("lon"))
|
---|
[71] | 71 | lon = Double.parseDouble(st.nextToken());
|
---|
[78] | 72 | else if (token.equals("time"))
|
---|
| 73 | time = (time == null?"":(time+" ")) + st.nextToken();
|
---|
[38] | 74 | else if (token.equals("ignore"))
|
---|
| 75 | st.nextToken();
|
---|
| 76 | else
|
---|
[74] | 77 | throw new JDOMException("Unknown data type: '"+token+"'."+(Main.pref.get("csvImportString").equals("") ? " Maybe add an format string in preferences." : ""));
|
---|
[38] | 78 | }
|
---|
[78] | 79 | data.add(new GpsPoint(new LatLon(lat, lon), time));
|
---|
[38] | 80 | }
|
---|
| 81 | } catch (RuntimeException e) {
|
---|
| 82 | throw new JDOMException("Parsing error in line "+lineNo, e);
|
---|
| 83 | }
|
---|
| 84 | return data;
|
---|
| 85 | }
|
---|
| 86 | }
|
---|