Changeset 2211 in josm
- Timestamp:
- 2009-09-29T07:09:33+02:00 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/io/GpxReader.java
r2181 r2211 74 74 private LatLon parseLatLon(Attributes atts) { 75 75 return new LatLon( 76 parseCoord(atts.getValue("lat")),77 parseCoord(atts.getValue("lon")));76 parseCoord(atts.getValue("lat")), 77 parseCoord(atts.getValue("lon"))); 78 78 } 79 79 80 80 @Override public void startElement(String namespaceURI, String qName, String rqName, Attributes atts) throws SAXException { 81 81 switch(currentState) { 82 case init:83 if (qName.equals("metadata")) {84 states.push(currentState);85 currentState = state.metadata;86 } else if (qName.equals("wpt")) {87 states.push(currentState);88 currentState = state.wpt;89 currentWayPoint = new WayPoint(parseLatLon(atts));90 } else if (qName.equals("rte")) {91 states.push(currentState);92 currentState = state.rte;93 currentRoute = new GpxRoute();94 } else if (qName.equals("trk")) {95 states.push(currentState);96 currentState = state.trk;97 currentTrack = new GpxTrack();98 } else if (qName.equals("extensions")) {99 states.push(currentState);100 currentState = state.ext;101 }102 break;103 case author:104 if (qName.equals("link")) {105 states.push(currentState);106 currentState = state.link;107 currentLink = new GpxLink(atts.getValue("href"));108 } else if (qName.equals("email")) {109 currentData.attr.put(GpxData.META_AUTHOR_EMAIL, atts.getValue("id") + "@" + atts.getValue("domain"));110 }111 break;112 case trk:113 if (qName.equals("trkseg")) {114 states.push(currentState);115 currentState = state.trkseg;116 currentTrackSeg = new ArrayList<WayPoint>();117 } else if (qName.equals("link")) {118 states.push(currentState);119 currentState = state.link;120 currentLink = new GpxLink(atts.getValue("href"));121 } else if (qName.equals("extensions")) {122 states.push(currentState);123 currentState = state.ext;124 }125 break;126 case metadata:127 if (qName.equals("author")) {128 states.push(currentState);129 currentState = state.author;130 } else if (qName.equals("extensions")) {131 states.push(currentState);132 currentState = state.ext;133 } else if (qName.equals("copyright")) {134 states.push(currentState);135 currentState = state.copyright;136 currentData.attr.put(GpxData.META_COPYRIGHT_AUTHOR, atts.getValue("author"));137 } else if (qName.equals("link")) {138 states.push(currentState);139 currentState = state.link;140 currentLink = new GpxLink(atts.getValue("href"));141 }142 break;143 case trkseg:144 if (qName.equals("trkpt")) {145 states.push(currentState);146 currentState = state.wpt;147 currentWayPoint = new WayPoint(parseLatLon(atts));148 }149 break;150 case wpt:151 if (qName.equals("link")) {152 states.push(currentState);153 currentState = state.link;154 currentLink = new GpxLink(atts.getValue("href"));155 } else if (qName.equals("extensions")) {156 states.push(currentState);157 currentState = state.ext;158 }159 break;160 case rte:161 if (qName.equals("link")) {162 states.push(currentState);163 currentState = state.link;164 currentLink = new GpxLink(atts.getValue("href"));165 } else if (qName.equals("rtept")) {166 states.push(currentState);167 currentState = state.wpt;168 currentWayPoint = new WayPoint(parseLatLon(atts));169 } else if (qName.equals("extensions")) {170 states.push(currentState);171 currentState = state.ext;172 }173 break;174 default:82 case init: 83 if (qName.equals("metadata")) { 84 states.push(currentState); 85 currentState = state.metadata; 86 } else if (qName.equals("wpt")) { 87 states.push(currentState); 88 currentState = state.wpt; 89 currentWayPoint = new WayPoint(parseLatLon(atts)); 90 } else if (qName.equals("rte")) { 91 states.push(currentState); 92 currentState = state.rte; 93 currentRoute = new GpxRoute(); 94 } else if (qName.equals("trk")) { 95 states.push(currentState); 96 currentState = state.trk; 97 currentTrack = new GpxTrack(); 98 } else if (qName.equals("extensions")) { 99 states.push(currentState); 100 currentState = state.ext; 101 } 102 break; 103 case author: 104 if (qName.equals("link")) { 105 states.push(currentState); 106 currentState = state.link; 107 currentLink = new GpxLink(atts.getValue("href")); 108 } else if (qName.equals("email")) { 109 currentData.attr.put(GpxData.META_AUTHOR_EMAIL, atts.getValue("id") + "@" + atts.getValue("domain")); 110 } 111 break; 112 case trk: 113 if (qName.equals("trkseg")) { 114 states.push(currentState); 115 currentState = state.trkseg; 116 currentTrackSeg = new ArrayList<WayPoint>(); 117 } else if (qName.equals("link")) { 118 states.push(currentState); 119 currentState = state.link; 120 currentLink = new GpxLink(atts.getValue("href")); 121 } else if (qName.equals("extensions")) { 122 states.push(currentState); 123 currentState = state.ext; 124 } 125 break; 126 case metadata: 127 if (qName.equals("author")) { 128 states.push(currentState); 129 currentState = state.author; 130 } else if (qName.equals("extensions")) { 131 states.push(currentState); 132 currentState = state.ext; 133 } else if (qName.equals("copyright")) { 134 states.push(currentState); 135 currentState = state.copyright; 136 currentData.attr.put(GpxData.META_COPYRIGHT_AUTHOR, atts.getValue("author")); 137 } else if (qName.equals("link")) { 138 states.push(currentState); 139 currentState = state.link; 140 currentLink = new GpxLink(atts.getValue("href")); 141 } 142 break; 143 case trkseg: 144 if (qName.equals("trkpt")) { 145 states.push(currentState); 146 currentState = state.wpt; 147 currentWayPoint = new WayPoint(parseLatLon(atts)); 148 } 149 break; 150 case wpt: 151 if (qName.equals("link")) { 152 states.push(currentState); 153 currentState = state.link; 154 currentLink = new GpxLink(atts.getValue("href")); 155 } else if (qName.equals("extensions")) { 156 states.push(currentState); 157 currentState = state.ext; 158 } 159 break; 160 case rte: 161 if (qName.equals("link")) { 162 states.push(currentState); 163 currentState = state.link; 164 currentLink = new GpxLink(atts.getValue("href")); 165 } else if (qName.equals("rtept")) { 166 states.push(currentState); 167 currentState = state.wpt; 168 currentWayPoint = new WayPoint(parseLatLon(atts)); 169 } else if (qName.equals("extensions")) { 170 states.push(currentState); 171 currentState = state.ext; 172 } 173 break; 174 default: 175 175 } 176 176 accumulator.setLength(0); … … 193 193 @Override public void endElement(String namespaceURI, String qName, String rqName) { 194 194 switch (currentState) { 195 case metadata: 196 if (qName.equals("name")) { 197 currentData.attr.put(GpxData.META_NAME, accumulator.toString()); 198 } else if (qName.equals("desc")) { 199 currentData.attr.put(GpxData.META_DESC, accumulator.toString()); 200 } else if (qName.equals("time")) { 201 currentData.attr.put(GpxData.META_TIME, accumulator.toString()); 202 } else if (qName.equals("keywords")) { 203 currentData.attr.put(GpxData.META_KEYWORDS, accumulator.toString()); 204 } else if (qName.equals("metadata")) { 205 currentState = states.pop(); 206 } 207 //TODO: parse bounds, extensions 208 break; 209 case author: 210 if (qName.equals("author")) { 211 currentState = states.pop(); 212 } else if (qName.equals("name")) { 213 currentData.attr.put(GpxData.META_AUTHOR_NAME, accumulator.toString()); 214 } else if (qName.equals("email")) { 215 // do nothing, has been parsed on startElement 216 } else if (qName.equals("link")) { 217 currentData.attr.put(GpxData.META_AUTHOR_LINK, currentLink); 218 } 219 break; 220 case copyright: 221 if (qName.equals("copyright")) { 222 currentState = states.pop(); 223 } else if (qName.equals("year")) { 224 currentData.attr.put(GpxData.META_COPYRIGHT_YEAR, accumulator.toString()); 225 } else if (qName.equals("license")) { 226 currentData.attr.put(GpxData.META_COPYRIGHT_LICENSE, accumulator.toString()); 227 } 228 break; 229 case link: 230 if (qName.equals("text")) { 231 currentLink.text = accumulator.toString(); 232 } else if (qName.equals("type")) { 233 currentLink.type = accumulator.toString(); 234 } else if (qName.equals("link")) { 235 currentState = states.pop(); 236 } 237 if (currentState == state.author) { 238 currentData.attr.put(GpxData.META_AUTHOR_LINK, currentLink); 239 } else if (currentState != state.link) { 240 Map<String, Object> attr = getAttr(); 241 if (!attr.containsKey(GpxData.META_LINKS)) { 242 attr.put(GpxData.META_LINKS, new LinkedList<GpxLink>()); 243 } 244 ((Collection<GpxLink>) attr.get(GpxData.META_LINKS)).add(currentLink); 245 } 246 break; 247 case wpt: 248 if ( qName.equals("ele") || qName.equals("magvar") 249 || qName.equals("name") || qName.equals("geoidheight") 250 || qName.equals("type") || qName.equals("sym")) { 251 currentWayPoint.attr.put(qName, accumulator.toString()); 252 } else if(qName.equals("hdop") /*|| qName.equals("vdop") || 195 case metadata: 196 if (qName.equals("name")) { 197 currentData.attr.put(GpxData.META_NAME, accumulator.toString()); 198 } else if (qName.equals("desc")) { 199 currentData.attr.put(GpxData.META_DESC, accumulator.toString()); 200 } else if (qName.equals("time")) { 201 currentData.attr.put(GpxData.META_TIME, accumulator.toString()); 202 } else if (qName.equals("keywords")) { 203 currentData.attr.put(GpxData.META_KEYWORDS, accumulator.toString()); 204 } else if (qName.equals("metadata")) { 205 currentState = states.pop(); 206 } 207 //TODO: parse bounds, extensions 208 break; 209 case author: 210 if (qName.equals("author")) { 211 currentState = states.pop(); 212 } else if (qName.equals("name")) { 213 currentData.attr.put(GpxData.META_AUTHOR_NAME, accumulator.toString()); 214 } else if (qName.equals("email")) { 215 // do nothing, has been parsed on startElement 216 } else if (qName.equals("link")) { 217 currentData.attr.put(GpxData.META_AUTHOR_LINK, currentLink); 218 } 219 break; 220 case copyright: 221 if (qName.equals("copyright")) { 222 currentState = states.pop(); 223 } else if (qName.equals("year")) { 224 currentData.attr.put(GpxData.META_COPYRIGHT_YEAR, accumulator.toString()); 225 } else if (qName.equals("license")) { 226 currentData.attr.put(GpxData.META_COPYRIGHT_LICENSE, accumulator.toString()); 227 } 228 break; 229 case link: 230 if (qName.equals("text")) { 231 currentLink.text = accumulator.toString(); 232 } else if (qName.equals("type")) { 233 currentLink.type = accumulator.toString(); 234 } else if (qName.equals("link")) { 235 if (currentLink.uri == null && accumulator != null && accumulator.toString().length() != 0) { 236 currentLink = new GpxLink(accumulator.toString()); 237 } 238 currentState = states.pop(); 239 } 240 if (currentState == state.author) { 241 currentData.attr.put(GpxData.META_AUTHOR_LINK, currentLink); 242 } else if (currentState != state.link) { 243 Map<String, Object> attr = getAttr(); 244 if (!attr.containsKey(GpxData.META_LINKS)) { 245 attr.put(GpxData.META_LINKS, new LinkedList<GpxLink>()); 246 } 247 ((Collection<GpxLink>) attr.get(GpxData.META_LINKS)).add(currentLink); 248 } 249 break; 250 case wpt: 251 if ( qName.equals("ele") || qName.equals("magvar") 252 || qName.equals("name") || qName.equals("geoidheight") 253 || qName.equals("type") || qName.equals("sym")) { 254 currentWayPoint.attr.put(qName, accumulator.toString()); 255 } else if(qName.equals("hdop") /*|| qName.equals("vdop") || 253 256 qName.equals("pdop")*/) { 254 try {255 currentWayPoint.attr.put(qName, Float.parseFloat(accumulator.toString()));256 } catch(Exception e) {257 currentWayPoint.attr.put(qName, new Float(0));258 }259 } else if (qName.equals("time")) {260 currentWayPoint.attr.put(qName, accumulator.toString());261 currentWayPoint.setTime();262 } else if (qName.equals("cmt") || qName.equals("desc")) {263 currentWayPoint.attr.put(qName, accumulator.toString());264 currentWayPoint.setTime();265 } else if (qName.equals("rtept")) {266 currentState = states.pop();267 currentRoute.routePoints.add(currentWayPoint);268 } else if (qName.equals("trkpt")) {269 currentState = states.pop();270 currentTrackSeg.add(currentWayPoint);271 } else if (qName.equals("wpt")) {272 currentState = states.pop();273 currentData.waypoints.add(currentWayPoint);274 }275 break;276 case trkseg:277 if (qName.equals("trkseg")) {278 currentState = states.pop();279 currentTrack.trackSegs.add(currentTrackSeg);280 }281 break;282 case trk:283 if (qName.equals("trk")) {284 currentState = states.pop();285 currentData.tracks.add(currentTrack);286 } else if (qName.equals("name") || qName.equals("cmt")287 || qName.equals("desc") || qName.equals("src")288 || qName.equals("type") || qName.equals("number")289 290 currentTrack.attr.put(qName, accumulator.toString());291 }292 break;293 case ext:294 if (qName.equals("extensions")) {295 currentState = states.pop();296 }297 break;298 default:299 if (qName.equals("wpt")) {300 currentState = states.pop();301 } else if (qName.equals("rte")) {302 currentState = states.pop();303 currentData.routes.add(currentRoute);304 }257 try { 258 currentWayPoint.attr.put(qName, Float.parseFloat(accumulator.toString())); 259 } catch(Exception e) { 260 currentWayPoint.attr.put(qName, new Float(0)); 261 } 262 } else if (qName.equals("time")) { 263 currentWayPoint.attr.put(qName, accumulator.toString()); 264 currentWayPoint.setTime(); 265 } else if (qName.equals("cmt") || qName.equals("desc")) { 266 currentWayPoint.attr.put(qName, accumulator.toString()); 267 currentWayPoint.setTime(); 268 } else if (qName.equals("rtept")) { 269 currentState = states.pop(); 270 currentRoute.routePoints.add(currentWayPoint); 271 } else if (qName.equals("trkpt")) { 272 currentState = states.pop(); 273 currentTrackSeg.add(currentWayPoint); 274 } else if (qName.equals("wpt")) { 275 currentState = states.pop(); 276 currentData.waypoints.add(currentWayPoint); 277 } 278 break; 279 case trkseg: 280 if (qName.equals("trkseg")) { 281 currentState = states.pop(); 282 currentTrack.trackSegs.add(currentTrackSeg); 283 } 284 break; 285 case trk: 286 if (qName.equals("trk")) { 287 currentState = states.pop(); 288 currentData.tracks.add(currentTrack); 289 } else if (qName.equals("name") || qName.equals("cmt") 290 || qName.equals("desc") || qName.equals("src") 291 || qName.equals("type") || qName.equals("number") 292 || qName.equals("url")) { 293 currentTrack.attr.put(qName, accumulator.toString()); 294 } 295 break; 296 case ext: 297 if (qName.equals("extensions")) { 298 currentState = states.pop(); 299 } 300 break; 301 default: 302 if (qName.equals("wpt")) { 303 currentState = states.pop(); 304 } else if (qName.equals("rte")) { 305 currentState = states.pop(); 306 currentData.routes.add(currentRoute); 307 } 305 308 } 306 309 } 307 310 308 311 @Override public void endDocument() throws SAXException { 309 if (!states.empty()) {312 if (!states.empty()) 310 313 throw new SAXException(tr("Parse error: invalid document structure for GPX document.")); 311 }312 314 data = currentData; 313 315 }
Note:
See TracChangeset
for help on using the changeset viewer.