Ignore:
Timestamp:
15.06.2009 20:22:46 (3 years ago)
Author:
Gubaer
Message:

fixed: bug in OsmApi.getOsmApi()
cleanup: exception handling in interfacing with OSM API
new: new action for updating individual elements with the their current state on the server (including new menu item in the file menu)
new: improved user feedback in case of conflicts
new: handles 410 Gone conflicts when uploading a changeset
new: undoable command for "purging" a primitive from the current dataset (necessary if the primitive is already deleted on the server and the user wants to remove it from its local dataset)
new: undoable command for "undeleting" an already deleted primitive on the server (kind of "cloning")
new: after a full upload, checks whether there are primitives in the local dataset which might be deleted on the server.
new: data structures for history data
new: history download support in io package

File:
1 edited

Legend:

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

    r1667 r1670  
    2929import org.openstreetmap.josm.Main; 
    3030import org.openstreetmap.josm.data.osm.Changeset; 
    31 import org.openstreetmap.josm.data.osm.Node; 
    3231import org.openstreetmap.josm.data.osm.OsmPrimitive; 
    33 import org.openstreetmap.josm.data.osm.Relation; 
    34 import org.openstreetmap.josm.data.osm.Way; 
     32import org.openstreetmap.josm.data.osm.OsmPrimitiveType; 
    3533import org.openstreetmap.josm.data.osm.visitor.CreateOsmChangeVisitor; 
    3634import org.xml.sax.Attributes; 
     
    6765        if (api == null) { 
    6866            api = new OsmApi(serverUrl); 
     67            instances.put(serverUrl,api); 
    6968        } 
    7069        return api; 
     
    8079        String serverUrl = Main.pref.get("osm-server.url"); 
    8180        if (serverUrl == null) 
    82             throw new IllegalStateException(tr("preference {0} missing. Can't initialize OsmApi", "osm-server.url")); 
     81            throw new IllegalStateException(tr("preference ''{0}'' missing. Can't initialize OsmApi", "osm-server.url")); 
    8382        return getOsmApi(serverUrl); 
    8483    } 
     
    9695     */ 
    9796    private String version = null; 
    98  
    99     /** 
    100      * Maximum downloadable area from server (degrees squared), from capabilities response 
    101      * FIXME: make download dialog use this, instead of hard-coded default. 
    102      */ 
    103     private String maxArea = null; 
    10497 
    10598    /** the api capabilities */ 
     
    143136 
    144137    /** 
    145      * creates an instance of the OSM API. Initializes the server URL with the 
    146      * value of the preference <code>osm-server.url</code> 
    147      *  
    148      * @exception IllegalStateException thrown, if the preference <code>osm-server.url</code> is not set 
    149      */ 
    150     protected OsmApi() { 
    151         this.serverUrl = Main.pref.get("osm-server.url"); 
    152         if (serverUrl == null) 
    153             throw new IllegalStateException(tr("preference {0} missing. Can't initialize OsmApi", "osm-server.url")); 
    154     } 
    155  
    156     /** 
    157      * Helper that returns the lower-case type name of an OsmPrimitive 
    158      * @param o the primitive 
    159      * @return "node", "way", "relation", or "changeset" 
    160      */ 
    161     public static String which(OsmPrimitive o) { 
    162         if (o instanceof Node) return "node"; 
    163         if (o instanceof Way) return "way"; 
    164         if (o instanceof Relation) return "relation"; 
    165         if (o instanceof Changeset) return "changeset"; 
    166         return ""; 
    167     } 
    168  
    169     /** 
    170138     * Returns the OSM protocol version we use to talk to the server. 
    171139     * @return protocol version, or null if not yet negotiated. 
     
    185153    /** 
    186154     * Initializes this component by negotiating a protocol version with the server. 
    187      *  
    188      * @exception UnknownHostException thrown, if the API host is unknown 
    189      * @exception SocketTimeoutException thrown, if the connection to the API host  times out 
    190      * @exception ConnectException throw, if the connection to the API host fails 
    191      * @exception Exception any other exception 
     155     * 
     156     * @exception OsmApiInitializationException thrown, if an exception occurs 
    192157     */ 
    193158    public void initialize() throws OsmApiInitializationException { 
     
    238203 
    239204    /** 
    240      * Helper that makes an int from the first whitespace separated token in a string. 
    241      * @param s the string 
    242      * @return the integer represenation of the first token in the string 
    243      * @throws OsmTransferException if the string is empty or does not represent a number 
    244      */ 
    245     public static int parseInt(String s) throws OsmTransferException { 
    246         StringTokenizer t = new StringTokenizer(s); 
    247         try { 
    248             return Integer.parseInt(t.nextToken()); 
    249         } catch (Exception x) { 
    250             throw new OsmTransferException(tr("Cannot read numeric value from response")); 
    251         } 
    252     } 
    253  
    254     /** 
    255      * Helper that makes a long from the first whitespace separated token in a string. 
    256      * @param s the string 
    257      * @return the long represenation of the first token in the string 
    258      * @throws OsmTransferException if the string is empty or does not represent a number 
    259      */ 
    260     public static long parseLong(String s) throws OsmTransferException { 
    261         StringTokenizer t = new StringTokenizer(s); 
    262         try { 
    263             return Long.parseLong(t.nextToken()); 
    264         } catch (Exception x) { 
    265             throw new OsmTransferException(tr("Cannot read numeric value from response")); 
    266         } 
    267     } 
    268  
    269     /** 
    270205     * Returns the base URL for API requests, including the negotiated version number. 
    271206     * @return base URL string 
     
    293228    public void createPrimitive(OsmPrimitive osm) throws OsmTransferException { 
    294229        initialize(); 
    295         osm.id = parseLong(sendRequest("PUT", which(osm)+"/create", toXml(osm, true))); 
    296         osm.version = 1; 
     230        String ret = ""; 
     231        try { 
     232            ret = sendRequest("PUT", OsmPrimitiveType.from(osm).getAPIName()+"/create", toXml(osm, true)); 
     233            osm.id = Long.parseLong(ret.trim()); 
     234            osm.version = 1; 
     235        } catch(NumberFormatException e){ 
     236            throw new OsmTransferException(tr("unexpected format of id replied by the server, got ''{0}''", ret)); 
     237        } 
    297238    } 
    298239 
     
    309250        if (version.equals("0.5")) { 
    310251            // legacy mode does not return the new object version. 
    311             sendRequest("PUT", which(osm)+"/" + osm.id, toXml(osm, true)); 
     252            sendRequest("PUT", OsmPrimitiveType.from(osm).getAPIName()+"/" + osm.id, toXml(osm, true)); 
    312253        } else { 
     254            String ret = null; 
    313255            // normal mode (0.6 and up) returns new object version. 
    314             osm.version = parseInt(sendRequest("PUT", which(osm)+"/" + osm.id, toXml(osm, true))); 
     256            try { 
     257                ret = sendRequest("PUT", OsmPrimitiveType.from(osm).getAPIName()+"/" + osm.id, toXml(osm, true)); 
     258                osm.version = Integer.parseInt(ret.trim()); 
     259            } catch(NumberFormatException e) { 
     260                throw new OsmTransferException(tr("unexpected format of new version of modified primitive ''{0}'', got ''{1}''", osm.id, ret)); 
     261            } 
    315262        } 
    316263    } 
     
    324271        initialize(); 
    325272        // legacy mode does not require payload. normal mode (0.6 and up) requires payload for version matching. 
    326         sendRequest("DELETE", which(osm)+"/" + osm.id, version.equals("0.5") ? null : toXml(osm, false)); 
     273        sendRequest("DELETE", OsmPrimitiveType.from(osm).getAPIName()+"/" + osm.id, version.equals("0.5") ? null : toXml(osm, false)); 
    327274    } 
    328275 
     
    334281    public void createChangeset(String comment) throws OsmTransferException { 
    335282        changeset = new Changeset(); 
    336         Main.pleaseWaitDlg.currentAction.setText(tr("Opening changeset...")); 
     283        notifyStatusMessage(tr("Opening changeset...")); 
    337284        Properties sysProp = System.getProperties(); 
    338285        Object ua = sysProp.get("http.agent"); 
     
    349296    public void stopChangeset() throws OsmTransferException { 
    350297        initialize(); 
    351         Main.pleaseWaitDlg.currentAction.setText(tr("Closing changeset...")); 
     298        notifyStatusMessage(tr("Closing changeset...")); 
    352299        sendRequest("PUT", "changeset" + "/" + changeset.id + "/close", null); 
    353300        changeset = null; 
     
    372319        CreateOsmChangeVisitor duv = new CreateOsmChangeVisitor(changeset, OsmApi.this); 
    373320 
     321        notifyStatusMessage(tr("Preparing...")); 
    374322        for (OsmPrimitive osm : list) { 
    375             int progress = Main.pleaseWaitDlg.progress.getValue(); 
    376             Main.pleaseWaitDlg.currentAction.setText(tr("Preparing...")); 
    377323            osm.visit(duv); 
    378             Main.pleaseWaitDlg.progress.setValue(progress+1); 
    379         } 
    380  
    381         Main.pleaseWaitDlg.currentAction.setText(tr("Uploading...")); 
     324            notifyRelativeProgress(1); 
     325        } 
     326        notifyStatusMessage(tr("Uploading...")); 
    382327 
    383328        String diff = duv.getDocument(); 
     
    517462        } 
    518463    } 
     464 
     465    /** 
     466     * notifies any listeners about the current state of this API. Currently just 
     467     * displays the message in the global progress dialog, see {@see Main#pleaseWaitDlg} 
     468     *  
     469     * @param message a status message. 
     470     */ 
     471    protected void notifyStatusMessage(String message) { 
     472        Main.pleaseWaitDlg.currentAction.setText(message); 
     473    } 
     474 
     475    /** 
     476     * notifies any listeners about the current about a relative progress. Currently just 
     477     * increments the progress monitor in the in the global progress dialog, see {@see Main#pleaseWaitDlg} 
     478     *  
     479     * @param int the delta 
     480     */ 
     481    protected void notifyRelativeProgress(int delta) { 
     482        int current= Main.pleaseWaitDlg.progress.getValue(); 
     483        Main.pleaseWaitDlg.progress.setValue(current + delta); 
     484    } 
    519485} 
Note: See TracChangeset for help on using the changeset viewer.