~berthold-daum/zora/trunk

« back to all changes in this revision

Viewing changes to com.bdaum.zoom.gps/src/com/bdaum/zoom/gps/internal/gpx/GpxParser.java

  • Committer: bdaum
  • Date: 2015-12-26 10:21:51 UTC
  • Revision ID: berthold.daum@bdaum.de-20151226102151-44f1j5113167thb9
VersionĀ 2.4.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
24
24
import java.io.InputStream;
25
25
import java.text.ParseException;
26
26
import java.text.SimpleDateFormat;
27
 
import java.util.ArrayList;
28
27
import java.util.Date;
29
28
import java.util.List;
30
29
import java.util.TimeZone;
48
47
        private static final String RTEPT = "rtept"; //$NON-NLS-1$
49
48
        private static final String WPT = "wpt"; //$NON-NLS-1$
50
49
        private static final String TRKPT = "trkpt"; //$NON-NLS-1$
 
50
        private static final String TRKSEG = "trkseg"; //$NON-NLS-1$
51
51
        // private static final String GPXURI = "http://www.topografix.com/GPX/1/0"; //$NON-NLS-1$
52
52
        private static final String LATITUDE = "lat"; //$NON-NLS-1$
53
53
        private static final String LONGITUDE = "lon"; //$NON-NLS-1$
54
54
        private static final SimpleDateFormat DF = new SimpleDateFormat(
55
55
                        "yyyy-MM-dd'T'HH:mm:s"); //$NON-NLS-1$
56
 
        
57
 
        static  {
 
56
 
 
57
        static {
58
58
                DF.setTimeZone(TimeZone.getTimeZone("UTC")); //$NON-NLS-1$
59
59
        }
60
60
 
61
61
        /*
62
62
         * (non-Javadoc)
63
 
         * 
 
63
         *
64
64
         * @see com.bdaum.zoom.gps.gpx.IGpsParser#parse(InputStream in)
65
65
         */
66
66
 
67
 
        public Trackpoint[] parse(InputStream in) throws IOException,
68
 
                        ParseException {
69
 
                final List<Trackpoint> points = new ArrayList<Trackpoint>(250);
 
67
        public void parse(InputStream in, final List<Trackpoint> points)
 
68
                        throws IOException, ParseException {
70
69
                try {
71
70
 
72
71
                        SAXParserFactory factory = SAXParserFactory.newInstance();
74
73
                        DefaultHandler handler = new DefaultHandler() {
75
74
                                private Trackpoint trkpnt;
76
75
                                private StringBuilder text = new StringBuilder();
 
76
                                private int segment = -1;
77
77
 
78
78
                                @Override
79
79
                                public void startElement(String namespaceURI, String localName,
80
80
                                                String qName, Attributes atts) throws SAXException {
81
 
                                        if (TRKPT.equals(qName) || WPT.equals(qName)
82
 
                                                        || RTEPT.equals(qName)) {
 
81
                                        if (TRKSEG.equals(qName))
 
82
                                                segment = 0;
 
83
                                        else if (TRKPT.equals(qName)) {
 
84
                                                if (segment >= 0)
 
85
                                                        ++segment;
 
86
                                                trkpnt = new Trackpoint(
 
87
                                                                parseDouble(atts.getValue("", LATITUDE)), //$NON-NLS-1$
 
88
                                                                parseDouble(atts.getValue("", LONGITUDE)), false); //$NON-NLS-1$
 
89
                                        } else if (WPT.equals(qName) || RTEPT.equals(qName)) {
83
90
                                                trkpnt = new Trackpoint(parseDouble(atts.getValue(
84
91
                                                                "", LATITUDE)), //$NON-NLS-1$
85
 
                                                                parseDouble(atts.getValue("", LONGITUDE))); //$NON-NLS-1$
 
92
                                                                parseDouble(atts.getValue("", LONGITUDE)), true); //$NON-NLS-1$
86
93
                                        }
87
94
                                        text.setLength(0);
88
95
                                }
96
103
                                @Override
97
104
                                public void endElement(String uri, String localName,
98
105
                                                String qName) throws SAXException {
99
 
                                        if (TRKPT.equals(qName) || WPT.equals(qName)
 
106
                                        if (TRKSEG.equals(qName)) {
 
107
                                                if (segment > 0)
 
108
                                                        points.get(points.size() - 1).setSegmentEnd(true);
 
109
                                                segment = -1;
 
110
                                        } else if (TRKPT.equals(qName) || WPT.equals(qName)
100
111
                                                        || RTEPT.equals(qName)) {
101
112
                                                if (trkpnt != null) {
102
113
                                                        if (trkpnt.getTime() >= 0)
103
114
                                                                points.add(trkpnt);
104
115
                                                        trkpnt = null;
105
116
                                                }
106
 
                                        } else if (trkpnt != null
107
 
                                                        && (TIM.equals(qName) || ELE.equals(qName) || SPED
108
 
                                                                        .equals(qName))) {
109
 
                                                String s = text.toString();
110
 
                                                if (TIM.equals(qName))
111
 
                                                        trkpnt.setTime(text2date(s).getTime());
112
 
                                                else if (SPED.equals(qName))
113
 
                                                        trkpnt.setSpeed(parseDouble(s));
114
 
                                                else if (ELE.equals(qName))
115
 
                                                        trkpnt.setAltitude(parseDouble(s));
 
117
                                        } else if (trkpnt != null) {
 
118
                                                int p = qName.indexOf(':');
 
119
                                                String tagName = p < 0 ? qName : qName.substring(p + 1)
 
120
                                                                .trim();
 
121
                                                if (TIM.equals(tagName) || ELE.equals(tagName)
 
122
                                                                || SPED.equals(tagName)) {
 
123
                                                        String s = text.toString();
 
124
                                                        if (TIM.equals(tagName))
 
125
                                                                trkpnt.setTime(text2date(s).getTime());
 
126
                                                        else if (SPED.equals(tagName))
 
127
                                                                trkpnt.setSpeed(parseDouble(s));
 
128
                                                        else if (ELE.equals(tagName))
 
129
                                                                trkpnt.setAltitude(parseDouble(s));
 
130
                                                }
116
131
                                        }
117
132
                                }
118
133
                        };
122
137
                } catch (SAXException e) {
123
138
                        throw new IOException(e.toString());
124
139
                }
125
 
                return points.toArray(new Trackpoint[points.size()]);
126
140
        }
127
141
 
128
142
        private double parseDouble(String text) throws SAXException {