Changeset 1670 in josm for trunk/src/org/openstreetmap/josm/io/OsmApi.java
- Timestamp:
- 15.06.2009 20:22:46 (3 years ago)
- File:
-
- 1 edited
-
trunk/src/org/openstreetmap/josm/io/OsmApi.java (modified) (14 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/io/OsmApi.java
r1667 r1670 29 29 import org.openstreetmap.josm.Main; 30 30 import org.openstreetmap.josm.data.osm.Changeset; 31 import org.openstreetmap.josm.data.osm.Node;32 31 import org.openstreetmap.josm.data.osm.OsmPrimitive; 33 import org.openstreetmap.josm.data.osm.Relation; 34 import org.openstreetmap.josm.data.osm.Way; 32 import org.openstreetmap.josm.data.osm.OsmPrimitiveType; 35 33 import org.openstreetmap.josm.data.osm.visitor.CreateOsmChangeVisitor; 36 34 import org.xml.sax.Attributes; … … 67 65 if (api == null) { 68 66 api = new OsmApi(serverUrl); 67 instances.put(serverUrl,api); 69 68 } 70 69 return api; … … 80 79 String serverUrl = Main.pref.get("osm-server.url"); 81 80 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")); 83 82 return getOsmApi(serverUrl); 84 83 } … … 96 95 */ 97 96 private String version = null; 98 99 /**100 * Maximum downloadable area from server (degrees squared), from capabilities response101 * FIXME: make download dialog use this, instead of hard-coded default.102 */103 private String maxArea = null;104 97 105 98 /** the api capabilities */ … … 143 136 144 137 /** 145 * creates an instance of the OSM API. Initializes the server URL with the146 * value of the preference <code>osm-server.url</code>147 *148 * @exception IllegalStateException thrown, if the preference <code>osm-server.url</code> is not set149 */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 OsmPrimitive158 * @param o the primitive159 * @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 /**170 138 * Returns the OSM protocol version we use to talk to the server. 171 139 * @return protocol version, or null if not yet negotiated. … … 185 153 /** 186 154 * 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 192 157 */ 193 158 public void initialize() throws OsmApiInitializationException { … … 238 203 239 204 /** 240 * Helper that makes an int from the first whitespace separated token in a string.241 * @param s the string242 * @return the integer represenation of the first token in the string243 * @throws OsmTransferException if the string is empty or does not represent a number244 */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 string257 * @return the long represenation of the first token in the string258 * @throws OsmTransferException if the string is empty or does not represent a number259 */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 /**270 205 * Returns the base URL for API requests, including the negotiated version number. 271 206 * @return base URL string … … 293 228 public void createPrimitive(OsmPrimitive osm) throws OsmTransferException { 294 229 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 } 297 238 } 298 239 … … 309 250 if (version.equals("0.5")) { 310 251 // 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)); 312 253 } else { 254 String ret = null; 313 255 // 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 } 315 262 } 316 263 } … … 324 271 initialize(); 325 272 // 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)); 327 274 } 328 275 … … 334 281 public void createChangeset(String comment) throws OsmTransferException { 335 282 changeset = new Changeset(); 336 Main.pleaseWaitDlg.currentAction.setText(tr("Opening changeset..."));283 notifyStatusMessage(tr("Opening changeset...")); 337 284 Properties sysProp = System.getProperties(); 338 285 Object ua = sysProp.get("http.agent"); … … 349 296 public void stopChangeset() throws OsmTransferException { 350 297 initialize(); 351 Main.pleaseWaitDlg.currentAction.setText(tr("Closing changeset..."));298 notifyStatusMessage(tr("Closing changeset...")); 352 299 sendRequest("PUT", "changeset" + "/" + changeset.id + "/close", null); 353 300 changeset = null; … … 372 319 CreateOsmChangeVisitor duv = new CreateOsmChangeVisitor(changeset, OsmApi.this); 373 320 321 notifyStatusMessage(tr("Preparing...")); 374 322 for (OsmPrimitive osm : list) { 375 int progress = Main.pleaseWaitDlg.progress.getValue();376 Main.pleaseWaitDlg.currentAction.setText(tr("Preparing..."));377 323 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...")); 382 327 383 328 String diff = duv.getDocument(); … … 517 462 } 518 463 } 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 } 519 485 }
Note: See TracChangeset
for help on using the changeset viewer.
