Ignore:
Timestamp:
03.02.2010 22:53:24 (2 years ago)
Author:
mjulius
Message:

bring PrimitiveData.getId() in line with OsmPrimitive.getId()
remove OsmReader.OsmPrimitiveData and use PrimitiveData instead

File:
1 edited

Legend:

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

    r2919 r2932  
    88import java.util.ArrayList; 
    99import java.util.Collection; 
    10 import java.util.Date; 
    1110import java.util.HashMap; 
    1211import java.util.LinkedList; 
     
    2423import org.openstreetmap.josm.data.osm.DataSource; 
    2524import org.openstreetmap.josm.data.osm.Node; 
     25import org.openstreetmap.josm.data.osm.NodeData; 
    2626import org.openstreetmap.josm.data.osm.OsmPrimitive; 
    2727import org.openstreetmap.josm.data.osm.OsmPrimitiveType; 
     28import org.openstreetmap.josm.data.osm.PrimitiveData; 
    2829import org.openstreetmap.josm.data.osm.PrimitiveId; 
    2930import org.openstreetmap.josm.data.osm.Relation; 
     31import org.openstreetmap.josm.data.osm.RelationData; 
    3032import org.openstreetmap.josm.data.osm.RelationMember; 
    3133import org.openstreetmap.josm.data.osm.SimplePrimitiveId; 
    3234import org.openstreetmap.josm.data.osm.User; 
    3335import org.openstreetmap.josm.data.osm.Way; 
     36import org.openstreetmap.josm.data.osm.WayData; 
    3437import org.openstreetmap.josm.gui.progress.NullProgressMonitor; 
    3538import org.openstreetmap.josm.gui.progress.ProgressMonitor; 
     
    6669     * longs too, but in contrast to internal ids negative values are used 
    6770     * to identify primitives unknown to the OSM server 
    68      * 
    69      * The keys are strings composed as follows 
    70      * <ul> 
    71      *   <li>"n" + id  for nodes</li> 
    72      *   <li>"w" + id  for nodes</li> 
    73      *   <li>"r" + id  for nodes</li> 
    74      * </ul> 
    7571     */ 
    7672    private Map<PrimitiveId, OsmPrimitive> externalIdMap = new HashMap<PrimitiveId, OsmPrimitive>(); 
     
    8682    } 
    8783 
    88     private static class OsmPrimitiveData { 
    89         public long id = 0; 
    90         public boolean modified = false; 
    91         public boolean deleted = false; 
    92         public Date timestamp = new Date(); 
    93         public User user = null; 
    94         public boolean visible = true; 
    95         public int version = 0; 
    96         public LatLon latlon = new LatLon(0,0); 
    97         private OsmPrimitive primitive; 
    98         private int changesetId; 
    99  
    100         public void copyTo(OsmPrimitive osm) { 
    101             // It's not necessary to call clearOsmId for id < 0. The same thing is done by parameterless constructor 
    102             if (id > 0) { 
    103                 osm.setOsmId(id, version); 
    104             } 
    105             osm.setDeleted(deleted); 
    106             osm.setModified(modified | deleted); 
    107             osm.setTimestamp(timestamp); 
    108             osm.setUser(user); 
    109             if (!osm.isNew() && changesetId > 0) { 
    110                 osm.setChangesetId(changesetId); 
    111             } else if (osm.isNew() && changesetId > 0) { 
    112                 System.out.println(tr("Warning: ignoring changeset id {0} for new object with external id {1} and internal id {2}", 
    113                         changesetId, 
    114                         id, 
    115                         osm.getUniqueId() 
    116                 )); 
    117             } 
    118             if (! osm.isNew()) { 
    119                 // ignore visible attribute for objects not yet known to the server 
    120                 // 
    121                 osm.setVisible(visible); 
    122             } 
    123             osm.mappaintStyle = null; 
    124         } 
    125  
    126         public Node createNode() { 
    127             Node node = new Node(); 
    128             node.setCoor(latlon); 
    129             copyTo(node); 
    130             primitive = node; 
    131             return node; 
    132         } 
    133  
    134         public Way createWay() { 
    135             Way way = new Way(); 
    136             copyTo(way); 
    137             primitive = way; 
    138             return way; 
    139         } 
    140         public Relation createRelation() { 
    141             Relation relation= new Relation(); 
    142             copyTo(relation); 
    143             primitive = relation; 
    144             return relation; 
    145         } 
    146  
    147         public void rememberTag(String key, String value) { 
    148             primitive.put(key, value); 
    149         } 
    150     } 
    151  
    15284    /** 
    15385     * Used as a temporary storage for relation members, before they 
     
    15587     */ 
    15688    private static class RelationMemberData { 
    157         public String type; 
     89        public OsmPrimitiveType type; 
    15890        public long id; 
    15991        public String role; 
     
    184116         * The current osm primitive to be read. 
    185117         */ 
    186         private OsmPrimitiveData current; 
     118        private OsmPrimitive currentPrimitive; 
     119        private long currentExternalId; 
    187120        private String generator; 
    188121 
     
    229162 
    230163            } else if (qName.equals("node")) { 
    231                 current = new OsmPrimitiveData(); 
    232                 current.latlon = new LatLon(getDouble(atts, "lat"), getDouble(atts, "lon")); 
    233                 readCommon(atts, current); 
    234                 Node n = current.createNode(); 
    235                 externalIdMap.put(new SimplePrimitiveId(current.id, OsmPrimitiveType.NODE), n); 
     164                NodeData nd = new NodeData(); 
     165                nd.setCoor(new LatLon(getDouble(atts, "lat"), getDouble(atts, "lon"))); 
     166                readCommon(atts, nd); 
     167                Node n = new Node(nd.getId(), nd.getVersion()); 
     168                n.load(nd); 
     169                externalIdMap.put(nd.getPrimitiveId(), n); 
     170                currentPrimitive = n; 
     171                currentExternalId = nd.getUniqueId(); 
    236172            } else if (qName.equals("way")) { 
    237                 current = new OsmPrimitiveData(); 
    238                 readCommon(atts, current); 
    239                 Way w = current.createWay(); 
    240                 externalIdMap.put(new SimplePrimitiveId(current.id, OsmPrimitiveType.WAY), w); 
    241                 ways.put(current.id, new ArrayList<Long>()); 
     173                WayData wd = new WayData(); 
     174                readCommon(atts, wd); 
     175                Way w = new Way(wd.getId(), wd.getVersion()); 
     176                w.load(wd); 
     177                externalIdMap.put(wd.getPrimitiveId(), w); 
     178                ways.put(wd.getUniqueId(), new ArrayList<Long>()); 
     179                currentPrimitive = w; 
     180                currentExternalId = wd.getUniqueId(); 
    242181            } else if (qName.equals("nd")) { 
    243                 Collection<Long> list = ways.get(current.id); 
     182                Collection<Long> list = ways.get(currentExternalId); 
    244183                if (list == null) { 
    245184                    throwException( 
     
    249188                if (atts.getValue("ref") == null) { 
    250189                    throwException( 
    251                             tr("Missing mandatory attribute ''{0}'' on <nd> of way {1}.", "ref", current.id) 
     190                            tr("Missing mandatory attribute ''{0}'' on <nd> of way {1}.", "ref", currentPrimitive.getUniqueId()) 
    252191                    ); 
    253192                } 
     
    258197                    ); 
    259198                } 
    260                 if (current.deleted) { 
    261                     logger.info(tr("Deleted way {0} contains nodes", current.id)); 
     199                if (currentPrimitive.isDeleted()) { 
     200                    logger.info(tr("Deleted way {0} contains nodes", currentPrimitive.getUniqueId())); 
    262201                } else { 
    263202                    list.add(id); 
     
    267206 
    268207            } else if (qName.equals("relation")) { 
    269                 current = new OsmPrimitiveData(); 
    270                 readCommon(atts, current); 
    271                 Relation r = current.createRelation(); 
    272                 externalIdMap.put(new SimplePrimitiveId(current.id, OsmPrimitiveType.RELATION), r); 
    273                 relations.put(current.id, new LinkedList<RelationMemberData>()); 
     208                RelationData rd = new RelationData(); 
     209                readCommon(atts, rd); 
     210                Relation r = new Relation(rd.getId(), rd.getVersion()); 
     211                r.load(rd); 
     212                externalIdMap.put(rd.getPrimitiveId(), r); 
     213                relations.put(r.getUniqueId(), new LinkedList<RelationMemberData>()); 
     214                currentPrimitive = r; 
     215                currentExternalId = rd.getUniqueId(); 
    274216            } else if (qName.equals("member")) { 
    275                 Collection<RelationMemberData> list = relations.get(current.id); 
     217                Collection<RelationMemberData> list = relations.get(currentExternalId); 
    276218                if (list == null) { 
    277219                    throwException( 
     
    282224                String value = atts.getValue("ref"); 
    283225                if (value == null) { 
    284                     throwException(tr("Missing attribute ''ref'' on member in relation {0}.",current.id)); 
     226                    throwException(tr("Missing attribute ''ref'' on member in relation {0}.",currentPrimitive.getUniqueId())); 
    285227                } 
    286228                try { 
    287229                    emd.id = Long.parseLong(value); 
    288230                } catch(NumberFormatException e) { 
    289                     throwException(tr("Illegal value for attribute ''ref'' on member in relation {0}. Got {1}", Long.toString(current.id),value)); 
     231                    throwException(tr("Illegal value for attribute ''ref'' on member in relation {0}. Got {1}", Long.toString(currentPrimitive.getUniqueId()),value)); 
    290232                } 
    291233                value = atts.getValue("type"); 
    292234                if (value == null) { 
    293                     throwException(tr("Missing attribute ''type'' on member {0} in relation {1}.", Long.toString(emd.id), Long.toString(current.id))); 
    294                 } 
    295                 if (! (value.equals("way") || value.equals("node") || value.equals("relation"))) { 
    296                     throwException(tr("Illegal value for attribute ''type'' on member {0} in relation {1}. Got {2}.", Long.toString(emd.id), Long.toString(current.id), value)); 
    297                 } 
    298                 emd.type= value; 
     235                    throwException(tr("Missing attribute ''type'' on member {0} in relation {1}.", Long.toString(emd.id), Long.toString(currentPrimitive.getUniqueId()))); 
     236                } 
     237                try { 
     238                    emd.type = OsmPrimitiveType.fromApiTypeName(value); 
     239                } catch(IllegalArgumentException e) { 
     240                    throwException(tr("Illegal value for attribute ''type'' on member {0} in relation {1}. Got {2}.", Long.toString(emd.id), Long.toString(currentPrimitive.getUniqueId()), value)); 
     241                } 
    299242                value = atts.getValue("role"); 
    300243                emd.role = value; 
     
    304247                } 
    305248 
    306                 if (current.deleted) { 
    307                     logger.info(tr("Deleted relation {0} contains members", current.id)); 
     249                if (currentPrimitive.isDeleted()) { 
     250                    logger.info(tr("Deleted relation {0} contains members", currentPrimitive.getUniqueId())); 
    308251                } else { 
    309252                    list.add(emd); 
     
    315258                String key = atts.getValue("k"); 
    316259                String value = atts.getValue("v"); 
    317                 current.rememberTag(key, value); 
     260                currentPrimitive.put(key, value); 
    318261            } else { 
    319262                System.out.println(tr("Undefined element ''{0}'' found in input stream. Skipping.", qName)); 
     
    342285         * Read out the common attributes from atts and put them into this.current. 
    343286         */ 
    344         void readCommon(Attributes atts, OsmPrimitiveData current) throws SAXException { 
    345             current.id = getLong(atts, "id"); 
    346             if (current.id == 0) { 
     287        void readCommon(Attributes atts, PrimitiveData current) throws SAXException { 
     288            current.setId(getLong(atts, "id")); 
     289            if (current.getUniqueId() == 0) { 
    347290                throwException(tr("Illegal object with ID=0.")); 
    348291            } 
     
    350293            String time = atts.getValue("timestamp"); 
    351294            if (time != null && time.length() != 0) { 
    352                 current.timestamp =  DateUtils.fromString(time); 
     295                current.setTimestamp(DateUtils.fromString(time)); 
    353296            } 
    354297 
     
    357300            // uid attribute added in 0.6 API 
    358301            String uid = atts.getValue("uid"); 
    359             current.user = createUser(uid, user); 
     302            current.setUser(createUser(uid, user)); 
    360303 
    361304            // visible attribute added in 0.4 API 
    362305            String visible = atts.getValue("visible"); 
    363306            if (visible != null) { 
    364                 current.visible = Boolean.parseBoolean(visible); 
    365             } 
    366  
    367             String version = atts.getValue("version"); 
    368             current.version = 0; 
    369             if (version != null) { 
     307                current.setVisible(Boolean.parseBoolean(visible)); 
     308            } 
     309 
     310            String versionString = atts.getValue("version"); 
     311            int version = 0; 
     312            if (versionString != null) { 
    370313                try { 
    371                     current.version = Integer.parseInt(version); 
     314                    version = Integer.parseInt(versionString); 
    372315                } catch(NumberFormatException e) { 
    373                     throwException(tr("Illegal value for attribute ''version'' on OSM primitive with ID {0}. Got {1}.", Long.toString(current.id), version)); 
     316                    throwException(tr("Illegal value for attribute ''version'' on OSM primitive with ID {0}. Got {1}.", Long.toString(current.getUniqueId()), versionString)); 
    374317                } 
    375318                if (ds.getVersion().equals("0.6")){ 
    376                     if (current.version <= 0 && current.id > 0) { 
    377                         throwException(tr("Illegal value for attribute ''version'' on OSM primitive with ID {0}. Got {1}.", Long.toString(current.id), version)); 
    378                     } else if (current.version < 0 && current.id  <=0) { 
    379                         System.out.println(tr("WARNING: Normalizing value of attribute ''version'' of element {0} to {2}, API version is ''{3}''. Got {1}.", current.id, current.version, 0, "0.6")); 
    380                         current.version = 0; 
     319                    if (version <= 0 && current.getUniqueId() > 0) { 
     320                        throwException(tr("Illegal value for attribute ''version'' on OSM primitive with ID {0}. Got {1}.", Long.toString(current.getUniqueId()), versionString)); 
     321                    } else if (version < 0 && current.getUniqueId() <= 0) { 
     322                        System.out.println(tr("WARNING: Normalizing value of attribute ''version'' of element {0} to {2}, API version is ''{3}''. Got {1}.", current.getUniqueId(), version, 0, "0.6")); 
     323                        version = 0; 
    381324                    } 
    382325                } else if (ds.getVersion().equals("0.5")) { 
    383                     if (current.version <= 0 && current.id > 0) { 
    384                         System.out.println(tr("WARNING: Normalizing value of attribute ''version'' of element {0} to {2}, API version is ''{3}''. Got {1}.", current.id, current.version, 1, "0.5")); 
    385                         current.version = 1; 
    386                     } else if (current.version < 0 && current.id  <=0) { 
    387                         System.out.println(tr("WARNING: Normalizing value of attribute ''version'' of element {0} to {2}, API version is ''{3}''. Got {1}.", current.id, current.version, 0, "0.5")); 
    388                         current.version = 0; 
     326                    if (version <= 0 && current.getUniqueId() > 0) { 
     327                        System.out.println(tr("WARNING: Normalizing value of attribute ''version'' of element {0} to {2}, API version is ''{3}''. Got {1}.", current.getUniqueId(), version, 1, "0.5")); 
     328                        version = 1; 
     329                    } else if (version < 0 && current.getUniqueId() <= 0) { 
     330                        System.out.println(tr("WARNING: Normalizing value of attribute ''version'' of element {0} to {2}, API version is ''{3}''. Got {1}.", current.getUniqueId(), version, 0, "0.5")); 
     331                        version = 0; 
    389332                    } 
    390333                } else { 
     
    395338                // version expected for OSM primitives with an id assigned by the server (id > 0), since API 0.6 
    396339                // 
    397                 if (current.id > 0 && ds.getVersion() != null && ds.getVersion().equals("0.6")) { 
    398                     throwException(tr("Missing attribute ''version'' on OSM primitive with ID {0}.", Long.toString(current.id))); 
    399                 } else if (current.id > 0 && ds.getVersion() != null && ds.getVersion().equals("0.5")) { 
     340                if (current.getUniqueId() > 0 && ds.getVersion() != null && ds.getVersion().equals("0.6")) { 
     341                    throwException(tr("Missing attribute ''version'' on OSM primitive with ID {0}.", Long.toString(current.getUniqueId()))); 
     342                } else if (current.getUniqueId() > 0 && ds.getVersion() != null && ds.getVersion().equals("0.5")) { 
    400343                    // default version in 0.5 files for existing primitives 
    401                     System.out.println(tr("WARNING: Normalizing value of attribute ''version'' of element {0} to {2}, API version is ''{3}''. Got {1}.", current.id, current.version, 1, "0.5")); 
    402                     current.version= 1; 
    403                 } else if (current.id <= 0 && ds.getVersion() != null && ds.getVersion().equals("0.5")) { 
     344                    System.out.println(tr("WARNING: Normalizing value of attribute ''version'' of element {0} to {2}, API version is ''{3}''. Got {1}.", current.getUniqueId(), version, 1, "0.5")); 
     345                    version= 1; 
     346                } else if (current.getUniqueId() <= 0 && ds.getVersion() != null && ds.getVersion().equals("0.5")) { 
    404347                    // default version in 0.5 files for new primitives, no warning necessary. This is 
    405348                    // (was) legal in API 0.5 
    406                     current.version= 0; 
    407                 } 
    408             } 
     349                    version= 0; 
     350                } 
     351            } 
     352            current.setVersion(version); 
    409353 
    410354            String action = atts.getValue("action"); 
     
    412356                // do nothing 
    413357            } else if (action.equals("delete")) { 
    414                 current.deleted = true; 
     358                current.setDeleted(true); 
    415359            } else if (action.startsWith("modify")) { //FIXME: why startsWith()? why not equals()? 
    416                 current.modified = true; 
     360                current.setModified(true); 
    417361            } 
    418362 
    419363            String v = atts.getValue("changeset"); 
    420364            if (v == null) { 
    421                 current.changesetId = 0; 
     365                current.setChangesetId(0); 
    422366            } else { 
    423367                try { 
    424                     current.changesetId = Integer.parseInt(v); 
     368                    current.setChangesetId(Integer.parseInt(v)); 
    425369                } catch(NumberFormatException e) { 
    426                     if (current.id <= 0) { 
     370                    if (current.getUniqueId() <= 0) { 
    427371                        // for a new primitive we just log a warning 
    428                         System.out.println(tr("Illegal value for attribute ''changeset'' on new object {1}. Got {0}. Resetting to 0.", v, current.id)); 
    429                         current.changesetId = 0; 
     372                        System.out.println(tr("Illegal value for attribute ''changeset'' on new object {1}. Got {0}. Resetting to 0.", v, current.getUniqueId())); 
     373                        current.setChangesetId(0); 
    430374                    } else { 
    431375                        // for an existing primitive this is a problem 
     
    433377                    } 
    434378                } 
    435                 if (current.changesetId <=0) { 
    436                     if (current.id <= 0) { 
     379                if (current.getChangesetId() <=0) { 
     380                    if (current.getUniqueId() <= 0) { 
    437381                        // for a new primitive we just log a warning 
    438                         System.out.println(tr("Illegal value for attribute ''changeset'' on new object {1}. Got {0}. Resetting to 0.", v, current.id)); 
    439                         current.changesetId = 0; 
     382                        System.out.println(tr("Illegal value for attribute ''changeset'' on new object {1}. Got {0}. Resetting to 0.", v, current.getUniqueId())); 
     383                        current.setChangesetId(0); 
    440384                    } else { 
    441385                        // for an existing primitive this is a problem 
     
    475419                    if (id <= 0) 
    476420                        throw new IllegalDataException ( 
    477                                 tr( 
    478                                         "Way with external ID ''{0}'' includes missing node with external ID ''{1}''.", 
     421                                tr("Way with external ID ''{0}'' includes missing node with external ID ''{1}''.", 
    479422                                        externalWayId, 
    480                                         id 
    481                                 ) 
    482                         ); 
     423                                        id)); 
    483424                    // create an incomplete node if necessary 
    484425                    // 
     
    490431                } 
    491432                if (n.isDeleted()) { 
    492                     logger.warning(tr("Deleted node {0} was removed from way {1}", id, w.getId())); 
    493                 } else { 
    494                     wayNodes.add(n); 
    495                 } 
     433                    logger.warning(tr("Deleted node {0} is part of way {1}", id, w.getId())); 
     434                } 
     435                wayNodes.add(n); 
    496436            } 
    497437            w.setNodes(wayNodes); 
    498438            if (w.hasIncompleteNodes()) { 
    499439                if (logger.isLoggable(Level.FINE)) { 
    500                     logger.fine(tr("Marked way {0} with {1} nodes incomplete because at least one node was missing in the " + 
    501                             "loaded data and is therefore incomplete too.", externalWayId, w.getNodesCount())); 
    502                 } 
    503                 ds.addPrimitive(w); 
    504             } else { 
    505                 ds.addPrimitive(w); 
    506             } 
     440                    logger.fine(tr("Way {0} with {1} nodes has incomplete nodes because at least one node was missing in the loaded data.", 
     441                            externalWayId, w.getNodesCount())); 
     442                } 
     443            } 
     444            ds.addPrimitive(w); 
    507445        } 
    508446    } 
     
    538476 
    539477                // lookup the member from the map of already created primitives 
    540                 // 
    541                 try { 
    542                     OsmPrimitiveType type = OsmPrimitiveType.fromApiTypeName(rm.type); 
    543                     primitive = externalIdMap.get(new SimplePrimitiveId(rm.id, type)); 
    544                 } catch(IllegalArgumentException e) { 
    545                     throw new IllegalDataException( 
    546                             tr("Unknown relation member type ''{0}'' in relation with external id ''{1}''.", rm.type,externalRelationId) 
    547                     ); 
    548                 } 
     478                primitive = externalIdMap.get(new SimplePrimitiveId(rm.id, rm.type)); 
    549479 
    550480                if (primitive == null) { 
     
    555485                        // 
    556486                        throw new IllegalDataException( 
    557                                 tr( 
    558                                         "Relation with external id ''{0}'' refers to a missing primitive with external id ''{1}''.", 
     487                                tr("Relation with external id ''{0}'' refers to a missing primitive with external id ''{1}''.", 
    559488                                        externalRelationId, 
    560                                         rm.id 
    561                                 ) 
    562                         ); 
     489                                        rm.id)); 
    563490 
    564491                    // member refers to OSM primitive which was not present in the parsed data 
    565492                    // -> create a new incomplete primitive and add it to the dataset 
    566493                    // 
    567                     if (rm.type.equals("node")) { 
    568                         primitive = new Node(rm.id); 
    569                     } else if (rm.type.equals("way")) { 
    570                         primitive = new Way(rm.id); 
    571                     } else if (rm.type.equals("relation")) { 
    572                         primitive = new Relation(rm.id); 
    573                     } else 
    574                         // can't happen, we've been testing for valid member types 
    575                         // at the beginning of this method 
    576                         // 
    577                         throw new AssertionError(); 
    578                     ds.addPrimitive(primitive); 
    579                     externalIdMap.put(new SimplePrimitiveId(rm.id, OsmPrimitiveType.fromApiTypeName(rm.type)), primitive); 
     494                    primitive = ds.getPrimitiveById(rm.id, rm.type); 
     495                    if (primitive == null) { 
     496                        switch (rm.type) { 
     497                        case NODE: 
     498                            primitive = new Node(rm.id); break; 
     499                        case WAY: 
     500                            primitive = new Way(rm.id); break; 
     501                        case RELATION: 
     502                            primitive = new Relation(rm.id); break; 
     503                        default: throw new AssertionError(); // can't happen 
     504                        } 
     505 
     506                        ds.addPrimitive(primitive); 
     507                        externalIdMap.put(new SimplePrimitiveId(rm.id, rm.type), primitive); 
     508                    } 
    580509                } 
    581510                if (primitive.isDeleted()) { 
    582                     logger.warning(tr("Deleted member {0} was removed from relation {1}", primitive.getId(), relation.getId())); 
    583                 } else { 
    584                     relationMembers.add(new RelationMember(rm.role, primitive)); 
    585                 } 
     511                    logger.warning(tr("Deleted member {0} is used by relation {1}", primitive.getId(), relation.getId())); 
     512                } 
     513                relationMembers.add(new RelationMember(rm.role, primitive)); 
    586514            } 
    587515            relation.setMembers(relationMembers); 
Note: See TracChangeset for help on using the changeset viewer.