source: josm/src/org/openstreetmap/josm/io/RawCsvReader.java@ 312

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