Changeset 3226 in josm for trunk/src


Ignore:
Timestamp:
2010-05-09T15:27:32+02:00 (14 years ago)
Author:
jttt
Message:

Show line number in error message when loading of osm file fails. See #4487

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/io/OsmReader.java

    r3217 r3226  
    4444import org.xml.sax.Locator;
    4545import org.xml.sax.SAXException;
     46import org.xml.sax.SAXParseException;
    4647import org.xml.sax.helpers.DefaultHandler;
    4748
     
    134135        @Override public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
    135136
    136             if (qName.equals("osm")) {
    137                 if (atts == null) {
    138                     throwException(tr("Missing mandatory attribute ''{0}'' of XML element {1}.", "version", "osm"));
    139                 }
    140                 String v = atts.getValue("version");
    141                 if (v == null) {
    142                     throwException(tr("Missing mandatory attribute ''{0}''.", "version"));
    143                 }
    144                 if (!(v.equals("0.5") || v.equals("0.6"))) {
    145                     throwException(tr("Unsupported version: {0}", v));
    146                 }
    147                 // save generator attribute for later use when creating DataSource objects
    148                 generator = atts.getValue("generator");
    149                 ds.setVersion(v);
    150 
    151             } else if (qName.equals("bounds")) {
    152                 // new style bounds.
    153                 String minlon = atts.getValue("minlon");
    154                 String minlat = atts.getValue("minlat");
    155                 String maxlon = atts.getValue("maxlon");
    156                 String maxlat = atts.getValue("maxlat");
    157                 String origin = atts.getValue("origin");
    158                 if (minlon != null && maxlon != null && minlat != null && maxlat != null) {
    159                     if (origin == null) {
    160                         origin = generator;
    161                     }
    162                     Bounds bounds = new Bounds(
    163                             new LatLon(Double.parseDouble(minlat), Double.parseDouble(minlon)),
    164                             new LatLon(Double.parseDouble(maxlat), Double.parseDouble(maxlon)));
    165                     DataSource src = new DataSource(bounds, origin);
    166                     ds.dataSources.add(src);
     137            try {
     138                if (qName.equals("osm")) {
     139                    if (atts == null) {
     140                        throwException(tr("Missing mandatory attribute ''{0}'' of XML element {1}.", "version", "osm"));
     141                    }
     142                    String v = atts.getValue("version");
     143                    if (v == null) {
     144                        throwException(tr("Missing mandatory attribute ''{0}''.", "version"));
     145                    }
     146                    if (!(v.equals("0.5") || v.equals("0.6"))) {
     147                        throwException(tr("Unsupported version: {0}", v));
     148                    }
     149                    // save generator attribute for later use when creating DataSource objects
     150                    generator = atts.getValue("generator");
     151                    ds.setVersion(v);
     152
     153                } else if (qName.equals("bounds")) {
     154                    // new style bounds.
     155                    String minlon = atts.getValue("minlon");
     156                    String minlat = atts.getValue("minlat");
     157                    String maxlon = atts.getValue("maxlon");
     158                    String maxlat = atts.getValue("maxlat");
     159                    String origin = atts.getValue("origin");
     160                    if (minlon != null && maxlon != null && minlat != null && maxlat != null) {
     161                        if (origin == null) {
     162                            origin = generator;
     163                        }
     164                        Bounds bounds = new Bounds(
     165                                new LatLon(Double.parseDouble(minlat), Double.parseDouble(minlon)),
     166                                new LatLon(Double.parseDouble(maxlat), Double.parseDouble(maxlon)));
     167                        DataSource src = new DataSource(bounds, origin);
     168                        ds.dataSources.add(src);
     169                    } else {
     170                        throwException(tr(
     171                                "Missing manadatory attributes on element ''bounds''. Got minlon=''{0}'',minlat=''{1}'',maxlon=''{3}'',maxlat=''{4}'', origin=''{5}''.",
     172                                minlon, minlat, maxlon, maxlat, origin
     173                        ));
     174                    }
     175
     176                    // ---- PARSING NODES AND WAYS ----
     177
     178                } else if (qName.equals("node")) {
     179                    NodeData nd = new NodeData();
     180                    nd.setCoor(new LatLon(getDouble(atts, "lat"), getDouble(atts, "lon")));
     181                    readCommon(atts, nd);
     182                    Node n = new Node(nd.getId(), nd.getVersion());
     183                    n.load(nd);
     184                    externalIdMap.put(nd.getPrimitiveId(), n);
     185                    currentPrimitive = n;
     186                    currentExternalId = nd.getUniqueId();
     187                } else if (qName.equals("way")) {
     188                    WayData wd = new WayData();
     189                    readCommon(atts, wd);
     190                    Way w = new Way(wd.getId(), wd.getVersion());
     191                    w.load(wd);
     192                    externalIdMap.put(wd.getPrimitiveId(), w);
     193                    ways.put(wd.getUniqueId(), new ArrayList<Long>());
     194                    currentPrimitive = w;
     195                    currentExternalId = wd.getUniqueId();
     196                } else if (qName.equals("nd")) {
     197                    Collection<Long> list = ways.get(currentExternalId);
     198                    if (list == null) {
     199                        throwException(
     200                                tr("Found XML element <nd> not as direct child of element <way>.")
     201                        );
     202                    }
     203                    if (atts.getValue("ref") == null) {
     204                        throwException(
     205                                tr("Missing mandatory attribute ''{0}'' on <nd> of way {1}.", "ref", currentPrimitive.getUniqueId())
     206                        );
     207                    }
     208                    long id = getLong(atts, "ref");
     209                    if (id == 0) {
     210                        throwException(
     211                                tr("Illegal value of attribute ''ref'' of element <nd>. Got {0}.", id)
     212                        );
     213                    }
     214                    if (currentPrimitive.isDeleted()) {
     215                        logger.info(tr("Deleted way {0} contains nodes", currentPrimitive.getUniqueId()));
     216                    } else {
     217                        list.add(id);
     218                    }
     219
     220                    // ---- PARSING RELATIONS ----
     221
     222                } else if (qName.equals("relation")) {
     223                    RelationData rd = new RelationData();
     224                    readCommon(atts, rd);
     225                    Relation r = new Relation(rd.getId(), rd.getVersion());
     226                    r.load(rd);
     227                    externalIdMap.put(rd.getPrimitiveId(), r);
     228                    relations.put(rd.getUniqueId(), new LinkedList<RelationMemberData>());
     229                    currentPrimitive = r;
     230                    currentExternalId = rd.getUniqueId();
     231                } else if (qName.equals("member")) {
     232                    Collection<RelationMemberData> list = relations.get(currentExternalId);
     233                    if (list == null) {
     234                        throwException(
     235                                tr("Found XML element <member> not as direct child of element <relation>.")
     236                        );
     237                    }
     238                    RelationMemberData emd = new RelationMemberData();
     239                    String value = atts.getValue("ref");
     240                    if (value == null) {
     241                        throwException(tr("Missing attribute ''ref'' on member in relation {0}.",currentPrimitive.getUniqueId()));
     242                    }
     243                    try {
     244                        emd.id = Long.parseLong(value);
     245                    } catch(NumberFormatException e) {
     246                        throwException(tr("Illegal value for attribute ''ref'' on member in relation {0}. Got {1}", Long.toString(currentPrimitive.getUniqueId()),value));
     247                    }
     248                    value = atts.getValue("type");
     249                    if (value == null) {
     250                        throwException(tr("Missing attribute ''type'' on member {0} in relation {1}.", Long.toString(emd.id), Long.toString(currentPrimitive.getUniqueId())));
     251                    }
     252                    try {
     253                        emd.type = OsmPrimitiveType.fromApiTypeName(value);
     254                    } catch(IllegalArgumentException e) {
     255                        throwException(tr("Illegal value for attribute ''type'' on member {0} in relation {1}. Got {2}.", Long.toString(emd.id), Long.toString(currentPrimitive.getUniqueId()), value));
     256                    }
     257                    value = atts.getValue("role");
     258                    emd.role = value;
     259
     260                    if (emd.id == 0) {
     261                        throwException(tr("Incomplete <member> specification with ref=0"));
     262                    }
     263
     264                    if (currentPrimitive.isDeleted()) {
     265                        logger.info(tr("Deleted relation {0} contains members", currentPrimitive.getUniqueId()));
     266                    } else {
     267                        list.add(emd);
     268                    }
     269
     270                    // ---- PARSING TAGS (applicable to all objects) ----
     271
     272                } else if (qName.equals("tag")) {
     273                    String key = atts.getValue("k");
     274                    String value = atts.getValue("v");
     275                    if (key == null || value == null) {
     276                        throwException(tr("Missing key or value attribute in tag."));
     277                    }
     278                    currentPrimitive.put(intern(key), intern(value));
     279
    167280                } else {
    168                     throwException(tr(
    169                             "Missing manadatory attributes on element ''bounds''. Got minlon=''{0}'',minlat=''{1}'',maxlon=''{3}'',maxlat=''{4}'', origin=''{5}''.",
    170                             minlon, minlat, maxlon, maxlat, origin
    171                     ));
    172                 }
    173 
    174                 // ---- PARSING NODES AND WAYS ----
    175 
    176             } else if (qName.equals("node")) {
    177                 NodeData nd = new NodeData();
    178                 nd.setCoor(new LatLon(getDouble(atts, "lat"), getDouble(atts, "lon")));
    179                 readCommon(atts, nd);
    180                 Node n = new Node(nd.getId(), nd.getVersion());
    181                 n.load(nd);
    182                 externalIdMap.put(nd.getPrimitiveId(), n);
    183                 currentPrimitive = n;
    184                 currentExternalId = nd.getUniqueId();
    185             } else if (qName.equals("way")) {
    186                 WayData wd = new WayData();
    187                 readCommon(atts, wd);
    188                 Way w = new Way(wd.getId(), wd.getVersion());
    189                 w.load(wd);
    190                 externalIdMap.put(wd.getPrimitiveId(), w);
    191                 ways.put(wd.getUniqueId(), new ArrayList<Long>());
    192                 currentPrimitive = w;
    193                 currentExternalId = wd.getUniqueId();
    194             } else if (qName.equals("nd")) {
    195                 Collection<Long> list = ways.get(currentExternalId);
    196                 if (list == null) {
    197                     throwException(
    198                             tr("Found XML element <nd> not as direct child of element <way>.")
    199                     );
    200                 }
    201                 if (atts.getValue("ref") == null) {
    202                     throwException(
    203                             tr("Missing mandatory attribute ''{0}'' on <nd> of way {1}.", "ref", currentPrimitive.getUniqueId())
    204                     );
    205                 }
    206                 long id = getLong(atts, "ref");
    207                 if (id == 0) {
    208                     throwException(
    209                             tr("Illegal value of attribute ''ref'' of element <nd>. Got {0}.", id)
    210                     );
    211                 }
    212                 if (currentPrimitive.isDeleted()) {
    213                     logger.info(tr("Deleted way {0} contains nodes", currentPrimitive.getUniqueId()));
    214                 } else {
    215                     list.add(id);
    216                 }
    217 
    218                 // ---- PARSING RELATIONS ----
    219 
    220             } else if (qName.equals("relation")) {
    221                 RelationData rd = new RelationData();
    222                 readCommon(atts, rd);
    223                 Relation r = new Relation(rd.getId(), rd.getVersion());
    224                 r.load(rd);
    225                 externalIdMap.put(rd.getPrimitiveId(), r);
    226                 relations.put(rd.getUniqueId(), new LinkedList<RelationMemberData>());
    227                 currentPrimitive = r;
    228                 currentExternalId = rd.getUniqueId();
    229             } else if (qName.equals("member")) {
    230                 Collection<RelationMemberData> list = relations.get(currentExternalId);
    231                 if (list == null) {
    232                     throwException(
    233                             tr("Found XML element <member> not as direct child of element <relation>.")
    234                     );
    235                 }
    236                 RelationMemberData emd = new RelationMemberData();
    237                 String value = atts.getValue("ref");
    238                 if (value == null) {
    239                     throwException(tr("Missing attribute ''ref'' on member in relation {0}.",currentPrimitive.getUniqueId()));
    240                 }
    241                 try {
    242                     emd.id = Long.parseLong(value);
    243                 } catch(NumberFormatException e) {
    244                     throwException(tr("Illegal value for attribute ''ref'' on member in relation {0}. Got {1}", Long.toString(currentPrimitive.getUniqueId()),value));
    245                 }
    246                 value = atts.getValue("type");
    247                 if (value == null) {
    248                     throwException(tr("Missing attribute ''type'' on member {0} in relation {1}.", Long.toString(emd.id), Long.toString(currentPrimitive.getUniqueId())));
    249                 }
    250                 try {
    251                     emd.type = OsmPrimitiveType.fromApiTypeName(value);
    252                 } catch(IllegalArgumentException e) {
    253                     throwException(tr("Illegal value for attribute ''type'' on member {0} in relation {1}. Got {2}.", Long.toString(emd.id), Long.toString(currentPrimitive.getUniqueId()), value));
    254                 }
    255                 value = atts.getValue("role");
    256                 emd.role = value;
    257 
    258                 if (emd.id == 0) {
    259                     throwException(tr("Incomplete <member> specification with ref=0"));
    260                 }
    261 
    262                 if (currentPrimitive.isDeleted()) {
    263                     logger.info(tr("Deleted relation {0} contains members", currentPrimitive.getUniqueId()));
    264                 } else {
    265                     list.add(emd);
    266                 }
    267 
    268                 // ---- PARSING TAGS (applicable to all objects) ----
    269 
    270             } else if (qName.equals("tag")) {
    271                 String key = atts.getValue("k");
    272                 String value = atts.getValue("v");
    273                 if (key == null || value == null) {
    274                     throwException(tr("Missing key or value attribute in tag."));
    275                 }
    276                 currentPrimitive.put(intern(key), intern(value));
    277 
    278             } else {
    279                 System.out.println(tr("Undefined element ''{0}'' found in input stream. Skipping.", qName));
     281                    System.out.println(tr("Undefined element ''{0}'' found in input stream. Skipping.", qName));
     282                }
     283            } catch (Exception e) {
     284                throw new SAXParseException(e.getMessage(), locator, e);
    280285            }
    281286        }
     
    584589        } catch(ParserConfigurationException e) {
    585590            throw new IllegalDataException(e.getMessage(), e);
     591        } catch (SAXParseException e) {
     592            throw new IllegalDataException(tr("Line {0} column {1}: ", e.getLineNumber(), e.getColumnNumber()) + e.getMessage(), e);
    586593        } catch(SAXException e) {
    587594            throw new IllegalDataException(e.getMessage(), e);
Note: See TracChangeset for help on using the changeset viewer.