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