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