Line | Hits | Source |
---|---|---|
1 | package net.sf.tourviewer.lib.ciclo; | |
2 | ||
3 | import java.io.BufferedReader; | |
4 | import java.io.IOException; | |
5 | import java.io.InputStream; | |
6 | import java.io.InputStreamReader; | |
7 | import java.text.ParseException; | |
8 | import java.text.SimpleDateFormat; | |
9 | import java.util.Calendar; | |
10 | import java.util.Date; | |
11 | import net.sf.tourviewer.lib.Tour; | |
12 | import net.sf.tourviewer.lib.TourRecord; | |
13 | import net.sf.tourviewer.lib.TourSet; | |
14 | import net.sf.tourviewer.lib.Tour.Type; | |
15 | ||
16 | 8 | public class CicloTurReader |
17 | { | |
18 | ||
19 | private static final String FILE_SIGNATURE = "HACtronic"; | |
20 | private int currentLine; | |
21 | BufferedReader in; | |
22 | ||
23 | public boolean accept(InputStream in) throws IOException { | |
24 | 0 | byte[] buffer = new byte[FILE_SIGNATURE.length()]; |
25 | 0 | in.mark(buffer.length); |
26 | 0 | in.read(buffer); |
27 | ||
28 | try { | |
29 | 0 | return FILE_SIGNATURE.equalsIgnoreCase(new String(buffer)); |
30 | } | |
31 | finally { | |
32 | 0 | in.reset(); |
33 | 0 | } |
34 | } | |
35 | ||
36 | public TourSet read(InputStream inStream) throws IOException { | |
37 | 0 | in = new BufferedReader(new InputStreamReader(inStream)); |
38 | ||
39 | 0 | TourSet tourSet = new TourSet(); |
40 | 0 | Tour tour = new Tour(); |
41 | 0 | tour.setAutoStatistics(false); |
42 | try { | |
43 | 0 | String signature = readLine(); |
44 | 0 | int version1 = Integer.parseInt(readLine()); |
45 | 0 | int version2 = Integer.parseInt(readLine()); |
46 | 0 | int version3 = Integer.parseInt(readLine()); |
47 | 0 | tour.setTitle(readLine()); // 5 |
48 | 0 | tour.setStartLocation(readLine()); |
49 | 0 | tour.setEndLocation(readLine()); |
50 | 0 | String date = readLine(); |
51 | 0 | String time = readLine(); |
52 | 0 | tour.setStartTime(parseTime(date, time)); // 10 |
53 | 0 | StringBuffer sb = new StringBuffer(); |
54 | 0 | int noteCount = Integer.parseInt(readLine()); |
55 | 0 | for (int i = 0; i < noteCount; i++) { |
56 | 0 | sb.append(readLine()); |
57 | } | |
58 | 0 | tour.setNote(sb.toString()); |
59 | 0 | tour.setDistance(Integer.parseInt(readLine())); |
60 | // TODO: need to calculate tour.TravelTime(); | |
61 | 0 | int tourDurance = Integer.parseInt(readLine()); |
62 | 0 | readLine(); |
63 | 0 | tour.setMaxAltitude(Integer.parseInt(readLine())); |
64 | 0 | int diffAltitude = Integer.parseInt(readLine()); // TODO // 15 |
65 | 0 | tour.setAvgAltitudeInc(Double.parseDouble(readLine())); |
66 | 0 | tour.setAvgSlope(Double.parseDouble(readLine())); |
67 | 0 | tour.setAvgSpeed(Double.parseDouble(readLine())); |
68 | 0 | tour.setAvgTemperature(Double.parseDouble(readLine())); |
69 | 0 | tour.setAvgPulse(Double.parseDouble(readLine())); // 20 |
70 | 0 | readLine(); // -1 |
71 | 0 | tour.setType(parseType(Integer.parseInt(readLine()), version2)); |
72 | 0 | readLine(); // Reserved |
73 | 0 | readLine(); // Reserved |
74 | 0 | readLine(); // Reserved // 25 |
75 | 0 | readLine(); // Reserved |
76 | 0 | readLine(); // Reserved |
77 | 0 | readLine(); // Reserved |
78 | 0 | readLine(); // Reserved |
79 | 0 | readLine(); // Reserved // 30 |
80 | 0 | tourSet.setPersonName(parseName(readLine(), readLine())); |
81 | 0 | tourSet.setDateOfBirth(parseDate(readLine().trim())); |
82 | 0 | String team = readLine(); |
83 | 0 | tourSet.setPersonWeight(Integer.parseInt(readLine())); // 35 |
84 | 0 | int pulseLow = Integer.parseInt(readLine()); // TODO min |
85 | 0 | int pulseHigh = Integer.parseInt(readLine()); // TODO max |
86 | 0 | String material = readLine(); // TODO |
87 | 0 | int materialWeight = Integer.parseInt(readLine()); // TODO |
88 | 0 | tourSet.setTotalDistance(Integer.parseInt(readLine())); // 40 |
89 | 0 | tourSet.setTotalTravelTime(Integer.parseInt(readLine())); |
90 | 0 | tourSet.setTotalAltitudeUp(Integer.parseInt(readLine())); |
91 | 0 | tourSet.setTotalAltitudeDown(Integer.parseInt(readLine())); |
92 | 0 | readLine(); |
93 | 0 | readLine(); // 45 |
94 | 0 | readLine(); |
95 | 0 | readLine(); |
96 | 0 | readLine(); |
97 | 0 | readLine(); |
98 | 0 | readLine(); // 50 |
99 | 0 | readLine(); |
100 | 0 | readLine(); |
101 | 0 | readLine(); |
102 | 0 | int recordCount = Integer.parseInt(readLine()); |
103 | 0 | TourRecord lastRecord = readRecords(tour, recordCount); // 55 |
104 | 0 | if (recordCount > 0) { |
105 | 0 | readLine(); // eat line break |
106 | 0 | tour.setEndTime(lastRecord.getTime()); |
107 | 0 | tour.setDistance(lastRecord.getDistance()); |
108 | } | |
109 | 0 | int markerCount = Integer.parseInt(readLine()); currentLine++; |
110 | 0 | readMarkers(tour, markerCount); |
111 | 0 | if (markerCount > 0) { |
112 | 0 | readLine(); // eat line break |
113 | } | |
114 | } | |
115 | 0 | catch (NumberFormatException e) { |
116 | 0 | e.printStackTrace(); |
117 | 0 | throw new IOException("Invalid data in line " + currentLine + ", expected number"); |
118 | } | |
119 | 0 | catch (ParseException e) { |
120 | 0 | e.printStackTrace(); |
121 | 0 | throw new IOException("Invalid data format in line " + currentLine); |
122 | 0 | } |
123 | ||
124 | 0 | tourSet.addTour(tour); |
125 | 0 | return tourSet; |
126 | } | |
127 | ||
128 | private Type parseType(int mode, int version) | |
129 | { | |
130 | // TODO add other types | |
131 | 0 | if (version == 1) { |
132 | 0 | switch (mode) { |
133 | case 0: | |
134 | 0 | return Type.JOGGING; |
135 | case 2: | |
136 | 0 | return Type.BIKE; |
137 | case 3: | |
138 | 0 | return Type.SKI_BIKE; |
139 | } | |
140 | 0 | } |
141 | 0 | else if (version == 2) { |
142 | 0 | switch (mode) { |
143 | case 2: | |
144 | 0 | return Type.BIKE; // bike2 |
145 | case 3: | |
146 | 0 | return Type.BIKE; // bike1 |
147 | } | |
148 | } | |
149 | 0 | return Type.OTHER; |
150 | } | |
151 | ||
152 | private String parseName(String surname, String firstName) | |
153 | { | |
154 | 0 | return (firstName.length() == 0) ? surname : (surname.length() == 0) ? firstName |
155 | : surname + ", " + firstName; | |
156 | } | |
157 | ||
158 | private Date parseDate(String date) throws ParseException | |
159 | { | |
160 | 0 | if (date.length() == 0 || date.startsWith(".")) { |
161 | 0 | return null; |
162 | } | |
163 | 0 | SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy"); |
164 | 0 | return format.parse(date); |
165 | } | |
166 | ||
167 | private Date parseTime(String date, String time) throws ParseException | |
168 | { | |
169 | 0 | SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy HH:mm"); |
170 | 0 | return format.parse(date + " " + time); |
171 | } | |
172 | ||
173 | private TourRecord readRecords(Tour tour, int recordCount) throws IOException | |
174 | { | |
175 | 0 | if (recordCount <= 0) { |
176 | 0 | throw new IllegalArgumentException("recordCount must be greater than 0"); |
177 | } | |
178 | ||
179 | 0 | TourRecord record = null; |
180 | 0 | for (int i = 0; i < recordCount; i++) { |
181 | 0 | record = new TourRecord(); |
182 | 0 | Calendar cal = Calendar.getInstance(); |
183 | 0 | long date = (readUnsignedInt() - 86400) * 1000; |
184 | 0 | cal.setTimeInMillis(date - cal.getTimeZone().getOffset(date)); |
185 | 0 | record.setTime(cal.getTime()); |
186 | 0 | readInt(); |
187 | 0 | record.setDistance(readInt() * 10); |
188 | 0 | record.setAltitude(readSignedShort()); |
189 | 0 | record.setPulse(readUnsignedByte()); |
190 | 0 | record.setCadence(readSignedByte()); |
191 | 0 | record.setTemperature(readSignedByte()); |
192 | 0 | readSignedByte(); |
193 | 0 | readSignedByte(); |
194 | 0 | readSignedByte(); |
195 | 0 | tour.addRecord(record); |
196 | } | |
197 | 0 | return record; |
198 | } | |
199 | ||
200 | int readUnsignedByte() throws IOException | |
201 | { | |
202 | 8 | char[] buffer = new char[1]; |
203 | 8 | if (in.read(buffer, 0, 1) == 1) { |
204 | 8 | return (int)buffer[0]; |
205 | } | |
206 | else { | |
207 | 0 | throw new IOException("Unexpected end of file"); |
208 | } | |
209 | } | |
210 | ||
211 | private void readMarkers(Tour tour, int markerCount) | |
212 | { | |
213 | // TODO: add marker | |
214 | 0 | } |
215 | ||
216 | int readSignedByte() throws IOException | |
217 | { | |
218 | 8 | char[] buffer = new char[1]; |
219 | 8 | if (in.read(buffer, 0, 1) == 1) { |
220 | 8 | return (byte)buffer[0]; |
221 | } | |
222 | else { | |
223 | 0 | throw new IOException("Unexpected end of file"); |
224 | } | |
225 | } | |
226 | ||
227 | int readSignedShort() throws IOException | |
228 | { | |
229 | 6 | char[] buffer = new char[2]; |
230 | 6 | if (in.read(buffer, 0, 2) == 2) { |
231 | 6 | return (short)(buffer[1] << 8 | buffer[0]); |
232 | } | |
233 | else { | |
234 | 0 | throw new IOException("Unexpected end of file"); |
235 | } | |
236 | } | |
237 | ||
238 | long readUnsignedInt() throws IOException | |
239 | { | |
240 | 0 | char[] buffer = new char[4]; |
241 | 0 | if (in.read(buffer, 0, 4) == 4) { |
242 | 0 | return buffer[3] << 24 | buffer[2] << 16 | buffer[1] << 8 | buffer[0]; |
243 | } | |
244 | else { | |
245 | 0 | throw new IOException("Unexpected end of file"); |
246 | } | |
247 | } | |
248 | ||
249 | int readInt() throws IOException | |
250 | { | |
251 | 6 | char[] buffer = new char[4]; |
252 | 6 | if (in.read(buffer, 0, 4) == 4) { |
253 | 6 | return buffer[3] << 24 | buffer[2] << 16 | buffer[1] << 8 | buffer[0]; |
254 | } | |
255 | else { | |
256 | 0 | throw new IOException("Unexpected end of file"); |
257 | } | |
258 | } | |
259 | ||
260 | String readLine() throws IOException | |
261 | { | |
262 | 0 | currentLine++; |
263 | 0 | String line = in.readLine(); |
264 | 0 | if (line == null) { |
265 | 0 | throw new IOException("Unexpected end of file"); |
266 | } | |
267 | 0 | return line; |
268 | } | |
269 | ||
270 | } |
this report was generated by version 1.0.5 of jcoverage. |
copyright © 2003, jcoverage ltd. all rights reserved. |