Coverage details for net.sf.tourviewer.lib.ciclo.CicloTurReader

LineHitsSource
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  
168public 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 {
240        byte[] buffer = new byte[FILE_SIGNATURE.length()];
250        in.mark(buffer.length);
260        in.read(buffer);
27         
28         try {
290            return FILE_SIGNATURE.equalsIgnoreCase(new String(buffer));
30         }
31         finally {
320            in.reset();
330        }
34     }
35     
36     public TourSet read(InputStream inStream) throws IOException {
370        in = new BufferedReader(new InputStreamReader(inStream));
38         
390        TourSet tourSet = new TourSet();
400        Tour tour = new Tour();
410        tour.setAutoStatistics(false);
42         try {
430            String signature = readLine();
440            int version1 = Integer.parseInt(readLine());
450            int version2 = Integer.parseInt(readLine());
460            int version3 = Integer.parseInt(readLine());
470            tour.setTitle(readLine()); // 5
480            tour.setStartLocation(readLine());
490            tour.setEndLocation(readLine());
500            String date = readLine();
510            String time = readLine();
520            tour.setStartTime(parseTime(date, time)); // 10
530            StringBuffer sb = new StringBuffer();
540            int noteCount = Integer.parseInt(readLine());
550            for (int i = 0; i < noteCount; i++) {
560                sb.append(readLine());
57             }
580            tour.setNote(sb.toString());
590            tour.setDistance(Integer.parseInt(readLine()));
60             // TODO: need to calculate tour.TravelTime();
610            int tourDurance = Integer.parseInt(readLine());
620            readLine();
630            tour.setMaxAltitude(Integer.parseInt(readLine()));
640            int diffAltitude = Integer.parseInt(readLine()); // TODO // 15
650            tour.setAvgAltitudeInc(Double.parseDouble(readLine()));
660            tour.setAvgSlope(Double.parseDouble(readLine()));
670            tour.setAvgSpeed(Double.parseDouble(readLine()));
680            tour.setAvgTemperature(Double.parseDouble(readLine()));
690            tour.setAvgPulse(Double.parseDouble(readLine())); // 20
700            readLine(); // -1
710            tour.setType(parseType(Integer.parseInt(readLine()), version2));
720            readLine(); // Reserved
730            readLine(); // Reserved
740            readLine(); // Reserved // 25
750            readLine(); // Reserved
760            readLine(); // Reserved
770            readLine(); // Reserved
780            readLine(); // Reserved
790            readLine(); // Reserved // 30
800            tourSet.setPersonName(parseName(readLine(), readLine()));
810            tourSet.setDateOfBirth(parseDate(readLine().trim()));
820            String team = readLine();
830            tourSet.setPersonWeight(Integer.parseInt(readLine())); // 35
840            int pulseLow = Integer.parseInt(readLine()); // TODO min
850            int pulseHigh = Integer.parseInt(readLine()); // TODO max
860            String material = readLine(); // TODO
870            int materialWeight = Integer.parseInt(readLine()); // TODO
880            tourSet.setTotalDistance(Integer.parseInt(readLine())); // 40
890            tourSet.setTotalTravelTime(Integer.parseInt(readLine()));
900            tourSet.setTotalAltitudeUp(Integer.parseInt(readLine()));
910            tourSet.setTotalAltitudeDown(Integer.parseInt(readLine()));
920            readLine();
930            readLine(); // 45
940            readLine();
950            readLine();
960            readLine();
970            readLine();
980            readLine(); // 50
990            readLine();
1000            readLine();
1010            readLine();
1020            int recordCount = Integer.parseInt(readLine());
1030            TourRecord lastRecord = readRecords(tour, recordCount); // 55
1040            if (recordCount > 0) {
1050                readLine(); // eat line break
1060                tour.setEndTime(lastRecord.getTime());
1070                tour.setDistance(lastRecord.getDistance());
108             }
1090            int markerCount = Integer.parseInt(readLine()); currentLine++;
1100            readMarkers(tour, markerCount);
1110            if (markerCount > 0) {
1120                readLine(); // eat line break
113             }
114         }
1150        catch (NumberFormatException e) {
1160            e.printStackTrace();
1170            throw new IOException("Invalid data in line " + currentLine + ", expected number");
118         }
1190        catch (ParseException e) {
1200            e.printStackTrace();
1210            throw new IOException("Invalid data format in line " + currentLine);
1220        }
123         
1240        tourSet.addTour(tour);
1250        return tourSet;
126     }
127  
128     private Type parseType(int mode, int version)
129     {
130         // TODO add other types
1310        if (version == 1) {
1320            switch (mode) {
133             case 0:
1340                return Type.JOGGING;
135             case 2:
1360                return Type.BIKE;
137             case 3:
1380                return Type.SKI_BIKE;
139             }
1400        }
1410        else if (version == 2) {
1420            switch (mode) {
143             case 2:
1440                return Type.BIKE; // bike2
145             case 3:
1460                return Type.BIKE; // bike1
147             }
148         }
1490        return Type.OTHER;
150     }
151  
152     private String parseName(String surname, String firstName)
153     {
1540        return (firstName.length() == 0) ? surname : (surname.length() == 0) ? firstName
155                 : surname + ", " + firstName;
156     }
157  
158     private Date parseDate(String date) throws ParseException
159     {
1600        if (date.length() == 0 || date.startsWith(".")) {
1610            return null;
162         }
1630        SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy");
1640        return format.parse(date);
165     }
166  
167     private Date parseTime(String date, String time) throws ParseException
168     {
1690        SimpleDateFormat format = new SimpleDateFormat("dd.MM.yyyy HH:mm");
1700        return format.parse(date + " " + time);
171     }
172  
173     private TourRecord readRecords(Tour tour, int recordCount) throws IOException
174     {
1750        if (recordCount <= 0) {
1760            throw new IllegalArgumentException("recordCount must be greater than 0");
177         }
178         
1790        TourRecord record = null;
1800        for (int i = 0; i < recordCount; i++) {
1810            record = new TourRecord();
1820            Calendar cal = Calendar.getInstance();
1830            long date = (readUnsignedInt() - 86400) * 1000;
1840            cal.setTimeInMillis(date - cal.getTimeZone().getOffset(date));
1850            record.setTime(cal.getTime());
1860            readInt();
1870            record.setDistance(readInt() * 10);
1880            record.setAltitude(readSignedShort());
1890            record.setPulse(readUnsignedByte());
1900            record.setCadence(readSignedByte());
1910            record.setTemperature(readSignedByte());
1920            readSignedByte();
1930            readSignedByte();
1940            readSignedByte();
1950            tour.addRecord(record);
196         }
1970        return record;
198     }
199  
200     int readUnsignedByte() throws IOException
201     {
2028        char[] buffer = new char[1];
2038        if (in.read(buffer, 0, 1) == 1) {
2048            return (int)buffer[0];
205         }
206         else {
2070            throw new IOException("Unexpected end of file");
208         }
209     }
210  
211     private void readMarkers(Tour tour, int markerCount)
212     {
213         // TODO: add marker
2140    }
215  
216     int readSignedByte() throws IOException
217     {
2188        char[] buffer = new char[1];
2198        if (in.read(buffer, 0, 1) == 1) {
2208            return (byte)buffer[0];
221         }
222         else {
2230            throw new IOException("Unexpected end of file");
224         }
225     }
226  
227     int readSignedShort() throws IOException
228     {
2296        char[] buffer = new char[2];
2306        if (in.read(buffer, 0, 2) == 2) {
2316            return (short)(buffer[1] << 8 | buffer[0]);
232         }
233         else {
2340            throw new IOException("Unexpected end of file");
235         }
236     }
237  
238     long readUnsignedInt() throws IOException
239     {
2400        char[] buffer = new char[4];
2410        if (in.read(buffer, 0, 4) == 4) {
2420            return buffer[3] << 24 | buffer[2] << 16 | buffer[1] << 8 | buffer[0];
243         }
244         else {
2450            throw new IOException("Unexpected end of file");
246         }
247     }
248  
249     int readInt() throws IOException
250     {
2516        char[] buffer = new char[4];
2526        if (in.read(buffer, 0, 4) == 4) {
2536            return buffer[3] << 24 | buffer[2] << 16 | buffer[1] << 8 | buffer[0];
254         }
255         else {
2560            throw new IOException("Unexpected end of file");
257         }
258     }
259  
260     String readLine() throws IOException
261     {
2620        currentLine++;
2630        String line = in.readLine();
2640        if (line == null) {
2650            throw new IOException("Unexpected end of file");
266         }
2670        return line;
268     }
269     
270 }

this report was generated by version 1.0.5 of jcoverage.
visit www.jcoverage.com for updates.

copyright © 2003, jcoverage ltd. all rights reserved.
Java is a trademark of Sun Microsystems, Inc. in the United States and other countries.