Changeset 1071 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2008-11-09T20:30:39+01:00 (16 years ago)
- Location:
- trunk/src/org/openstreetmap/josm
- Files:
-
- 2 added
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/io/OsmReader.java
r769 r1071 87 87 osm.version = version; 88 88 osm.checkTagged(); 89 89 osm.checkDirectionTagged(); 90 90 } 91 91 } -
trunk/src/org/openstreetmap/josm/io/OsmServerObjectReader.java
r655 r1071 1 // 1 //License: GPL. For details, see LICENSE file. 2 2 package org.openstreetmap.josm.io; 3 3 … … 12 12 13 13 public class OsmServerObjectReader extends OsmServerReader { 14 15 public final static String TYPE_WAY = "way"; 16 public final static String TYPE_REL = "relation"; 17 public final static String TYPE_NODE = "node"; 18 19 /** 20 * Method to download single objects from OSM server. ways, relations, nodes 21 * @param id Object ID 22 * @param type way node relation 23 * @param full download with or without child objects 24 * @return the data requested 25 * @throws SAXException 26 * @throws IOException 27 */ 28 public DataSet parseOsm(long id, String type, boolean full) throws SAXException, IOException { 29 try { 30 31 Main.pleaseWaitDlg.progress.setValue(0); 32 Main.pleaseWaitDlg.currentAction.setText(tr("Contacting OSM Server...")); 33 StringBuffer sb = new StringBuffer(); 34 sb.append(type); 35 sb.append("/"); 36 sb.append(id); 37 if (full) 38 { 39 sb.append("/full"); 40 } 41 42 final InputStream in = getInputStream(sb.toString(), Main.pleaseWaitDlg); 43 if (in == null) 44 return null; 45 Main.pleaseWaitDlg.currentAction.setText(tr("Downloading OSM data...")); 46 final DataSet data = OsmReader.parseDataSet(in, null, Main.pleaseWaitDlg); 47 // String origin = Main.pref.get("osm-server.url")+"/"+Main.pref.get("osm-server.version", "0.5"); 48 // Bounds bounds = new Bounds(new LatLon(lat1, lon1), new LatLon(lat2, lon2)); 49 // DataSource src = new DataSource(bounds, origin); 50 // data.dataSources.add(src); 51 in.close(); 52 activeConnection = null; 53 return data; 54 } catch (IOException e) { 55 if (cancel) 56 return null; 57 throw e; 58 } catch (SAXException e) { 59 throw e; 60 } catch (Exception e) { 61 if (cancel) 62 return null; 63 if (e instanceof RuntimeException) 64 throw (RuntimeException)e; 65 throw new RuntimeException(e); 66 } 14 15 public final static String TYPE_WAY = "way"; 16 public final static String TYPE_REL = "relation"; 17 public final static String TYPE_NODE = "node"; 18 19 /** 20 * Method to download single objects from OSM server. ways, relations, nodes 21 * @param id Object ID 22 * @param type way node relation 23 * @param full download with or without child objects 24 * @return the data requested 25 * @throws SAXException 26 * @throws IOException 27 */ 28 public DataSet parseOsm(long id, String type, boolean full) throws SAXException, IOException { 29 try { 30 31 Main.pleaseWaitDlg.progress.setValue(0); 32 Main.pleaseWaitDlg.currentAction.setText(tr("Contacting OSM Server...")); 33 StringBuffer sb = new StringBuffer(); 34 sb.append(type); 35 sb.append("/"); 36 sb.append(id); 37 if (full) 38 sb.append("/full"); 39 40 final InputStream in = getInputStream(sb.toString(), Main.pleaseWaitDlg); 41 if (in == null) 42 return null; 43 Main.pleaseWaitDlg.currentAction.setText(tr("Downloading OSM data...")); 44 final DataSet data = OsmReader.parseDataSet(in, null, Main.pleaseWaitDlg); 45 // String origin = Main.pref.get("osm-server.url")+"/"+Main.pref.get("osm-server.version", "0.5"); 46 // Bounds bounds = new Bounds(new LatLon(lat1, lon1), new LatLon(lat2, lon2)); 47 // DataSource src = new DataSource(bounds, origin); 48 // data.dataSources.add(src); 49 in.close(); 50 activeConnection = null; 51 return data; 52 } catch (IOException e) { 53 if (cancel) 54 return null; 55 throw e; 56 } catch (SAXException e) { 57 throw e; 58 } catch (Exception e) { 59 if (cancel) 60 return null; 61 if (e instanceof RuntimeException) 62 throw (RuntimeException)e; 63 throw new RuntimeException(e); 67 64 } 65 } 68 66 69 67 } -
trunk/src/org/openstreetmap/josm/io/OsmServerReader.java
r916 r1071 35 35 String version = Main.pref.get("osm-server.version", "0.5"); 36 36 urlStr = Main.pref.get("osm-server.url")+"/"+version+"/" + urlStr; 37 //System.out.println("download: "+urlStr);37 System.out.println("download: "+urlStr); 38 38 initAuthentication(); 39 39 URL url = new URL(urlStr); -
trunk/src/org/openstreetmap/josm/io/OsmServerWriter.java
r977 r1071 4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 5 6 import java.io.BufferedOutputStream; 6 7 import java.io.BufferedReader; 7 8 import java.io.ByteArrayOutputStream; … … 10 11 import java.io.InputStreamReader; 11 12 import java.io.OutputStream; 13 import java.io.OutputStreamWriter; 14 import java.io.PrintWriter; 15 import java.io.StringWriter; 16 import java.io.UnsupportedEncodingException; 12 17 import java.net.ConnectException; 13 18 import java.net.HttpURLConnection; … … 25 30 import org.openstreetmap.josm.data.osm.Way; 26 31 import org.openstreetmap.josm.data.osm.Changeset; 32 import org.openstreetmap.josm.data.osm.visitor.CreateOsmChangeVisitor; 27 33 import org.openstreetmap.josm.data.osm.visitor.NameVisitor; 28 34 import org.openstreetmap.josm.data.osm.visitor.Visitor; … … 73 79 74 80 long uploadStartTime; 81 75 82 public String timeLeft(int progress, int list_size) { 76 83 long now = System.currentTimeMillis(); … … 88 95 time_left_str += Integer.toString(seconds_left); 89 96 return time_left_str; 90 } 97 } 98 91 99 public void uploadOsm(Collection<OsmPrimitive> list) throws SAXException { 92 100 processed = new LinkedList<OsmPrimitive>(); … … 96 104 Main.pleaseWaitDlg.progress.setValue(0); 97 105 106 // controls whether or not we open and close a changeset. API 0.6 requires changesets. 98 107 boolean useChangesets = Main.pref.get("osm-server.version", "0.5").equals("0.6"); 99 108 109 // controls whether or not we try and uplaod the whole bunch in one go 110 boolean useDiffUploads = Main.pref.getBoolean("osm-server.atomic-upload", 111 Main.pref.get("osm-server.version", "0.5").equals("0.6")); 112 100 113 String comment = null; 101 while( useChangesets && comment == null) 102 { 114 while (useChangesets && comment == null) { 103 115 comment = JOptionPane.showInputDialog(Main.parent, tr("Provide a brief comment as to the changes to you are uploading:"), 104 116 tr("Commit comment"), JOptionPane.QUESTION_MESSAGE); 105 117 if( comment == null ) 106 118 return; … … 111 123 } 112 124 try { 113 if ( useChangesets && !startChangeset(10, comment))125 if (useChangesets && !startChangeset(10, comment)) 114 126 return; 115 127 } 116 128 catch (OsmTransferException ex) { 117 dealWithTransferException 129 dealWithTransferException(ex); 118 130 return; 119 131 } 120 121 NameVisitor v = new NameVisitor(); 122 try { 123 uploadStartTime = System.currentTimeMillis(); 124 for (OsmPrimitive osm : list) { 125 if (cancel) 126 return; 127 osm.visit(v); 128 int progress = Main.pleaseWaitDlg.progress.getValue(); 129 String time_left_str = timeLeft(progress, list.size()); 130 Main.pleaseWaitDlg.currentAction.setText(tr("Upload {0} {1} (id: {2}) {3}% {4}/{5} ({6} left)...", 131 tr(v.className), v.name, osm.id, 100.0*progress/list.size(), progress, list.size(), time_left_str)); 132 osm.visit(this); 133 Main.pleaseWaitDlg.progress.setValue(Main.pleaseWaitDlg.progress.getValue()+1); 134 Main.pleaseWaitDlg.progress.setValue(progress+1); 135 } 136 if( useChangesets ) 137 stopChangeset(10); 132 133 try { 134 if (useDiffUploads) { 135 uploadDiff(10, list); 136 } else { 137 NameVisitor v = new NameVisitor(); 138 uploadStartTime = System.currentTimeMillis(); 139 for (OsmPrimitive osm : list) { 140 if (cancel) 141 return; 142 osm.visit(v); 143 int progress = Main.pleaseWaitDlg.progress.getValue(); 144 String time_left_str = timeLeft(progress, list.size()); 145 Main.pleaseWaitDlg.currentAction.setText(tr("Upload {0} {1} (id: {2}) {3}% {4}/{5} ({6} left)...", 146 tr(v.className), v.name, osm.id, 100.0*progress/list.size(), progress, list.size(), time_left_str)); 147 osm.visit(this); 148 Main.pleaseWaitDlg.progress.setValue(progress+1); 149 } 150 } 151 if (useChangesets) 152 stopChangeset(10); 138 153 } catch (RuntimeException e) { 139 154 try { 140 if( useChangesets ) stopChangeset(10); 155 if (useChangesets) 156 stopChangeset(10); 141 157 } 142 158 catch (OsmTransferException ex) { … … 211 227 } 212 228 if (retCode != 200 && retCode != 412) { 229 213 230 if (retries >= 0) { 214 231 retries--; … … 217 234 System.out.println("retrying ("+retries+" left)"); 218 235 return startChangeset(retries, comment); 219 } else {220 // Look for a detailed error message from the server221 if (activeConnection.getHeaderField("Error") != null)222 retMsg += "\n" + activeConnection.getHeaderField("Error");223 224 // Report our error225 ByteArrayOutputStream o = new ByteArrayOutputStream();226 OsmWriter.output(o, changeset);227 System.out.println(new String(o.toByteArray(), "UTF-8").toString());228 //throw new RuntimeException(retCode+" "+retMsg);229 throw new OsmTransferException (retCode + " " + retMsg);230 236 } 237 238 // Look for a detailed error message from the server 239 if (activeConnection.getHeaderField("Error") != null) 240 retMsg += "\n" + activeConnection.getHeaderField("Error"); 241 242 // Report our error 243 ByteArrayOutputStream o = new ByteArrayOutputStream(); 244 OsmWriter.output(o, changeset); 245 System.out.println(new String(o.toByteArray(), "UTF-8").toString()); 246 //throw new RuntimeException(retCode+" "+retMsg); 247 throw new OsmTransferException (retCode + " " + retMsg); 248 231 249 } 232 250 } catch (UnknownHostException e) { … … 265 283 return true; 266 284 } 267 285 286 private void uploadDiff(int retries, Collection<OsmPrimitive> list) throws OsmTransferException { 287 288 CreateOsmChangeVisitor duv = new CreateOsmChangeVisitor(changeset); 289 290 for (OsmPrimitive osm : list) { 291 int progress = Main.pleaseWaitDlg.progress.getValue(); 292 Main.pleaseWaitDlg.currentAction.setText(tr("Preparing...")); 293 if (cancel) 294 return; 295 osm.visit(duv); 296 Main.pleaseWaitDlg.progress.setValue(progress+1); 297 } 298 System.out.println("the document:\n"); 299 String diff = duv.getDocument(); 300 System.out.println(diff); 301 302 Main.pleaseWaitDlg.currentAction.setText(tr("Uploading...")); 303 try { 304 if (cancel) 305 return; // assume cancel 306 String version = Main.pref.get("osm-server.version", "0.6"); 307 URL url = new URL( 308 Main.pref.get("osm-server.url") + 309 "/" + version + 310 "/" + "changeset" + 311 "/" + changeset.id + 312 "/upload" ); 313 System.out.print("upload to: "+url+ "..." ); 314 activeConnection = (HttpURLConnection)url.openConnection(); 315 activeConnection.setConnectTimeout(15000); 316 activeConnection.setRequestMethod("POST"); 317 addAuth(activeConnection); 318 319 activeConnection.setDoOutput(true); 320 PrintWriter out; 321 try { 322 out = new PrintWriter(new OutputStreamWriter(activeConnection.getOutputStream(), "UTF-8")); 323 } catch (UnsupportedEncodingException e) { 324 throw new RuntimeException(e); 325 } 326 out.print(diff); 327 out.close(); 328 329 activeConnection.connect(); 330 System.out.println("connected"); 331 332 int retCode = activeConnection.getResponseCode(); 333 String retMsg = ""; 334 335 if (retCode == 200) { 336 DiffResultReader.parseDiffResult(activeConnection.getInputStream(), list, processed, duv.getNewIdMap(), Main.pleaseWaitDlg); 337 } else if (retCode != 200 && retCode != 412) { 338 if (retries >= 0) { 339 retries--; 340 System.out.print("backing off for 10 seconds..."); 341 Thread.sleep(10000); 342 System.out.println("retrying ("+retries+" left)"); 343 stopChangeset(retries); 344 } else { 345 // Look for a detailed error message from the server 346 if (activeConnection.getHeaderField("Error") != null) 347 retMsg += "\n" + activeConnection.getHeaderField("Error"); 348 349 // Report our error 350 ByteArrayOutputStream o = new ByteArrayOutputStream(); 351 OsmWriter.output(o, changeset); 352 System.out.println(new String(o.toByteArray(), "UTF-8").toString()); 353 //throw new RuntimeException(retCode+" "+retMsg); 354 throw new OsmTransferException(retCode+" "+retMsg); 355 } 356 } 357 } catch (UnknownHostException e) { 358 //throw new RuntimeException(tr("Unknown host")+": "+e.getMessage(), e); 359 throw new OsmTransferException(tr("Unknown host")+": "+e.getMessage(), e); 360 } catch(SocketTimeoutException e) { 361 System.out.println(" timed out, retries left: " + retries); 362 if (cancel) 363 return; // assume cancel 364 if (retries-- > 0) 365 stopChangeset(retries); 366 else 367 //throw new RuntimeException(e.getMessage()+ " " + e.getClass().getCanonicalName(), e); 368 throw new OsmTransferException(e.getMessage()+ " " + e.getClass().getCanonicalName(), e); 369 } catch(ConnectException e) { 370 System.out.println(" timed out, retries left: " + retries); 371 if (cancel) 372 return; // assume cancel 373 if (retries-- > 0) 374 stopChangeset(retries); 375 else 376 //throw new RuntimeException(e.getMessage()+ " " + e.getClass().getCanonicalName(), e); 377 throw new OsmTransferException(e.getMessage()+ " " + e.getClass().getCanonicalName(), e); 378 } catch (Exception e) { 379 if (cancel) 380 return; // assume cancel 381 if (e instanceof OsmTransferException) 382 throw (OsmTransferException)e; 383 if (e instanceof RuntimeException) 384 throw (RuntimeException)e; 385 throw new RuntimeException(e.getMessage()+ " " + e.getClass().getCanonicalName(), e); 386 } 387 } 388 389 268 390 private void stopChangeset(int retries) throws OsmTransferException { 269 391 Main.pleaseWaitDlg.currentAction.setText(tr("Closing changeset...")); … … 443 565 int retCode = activeConnection.getResponseCode(); 444 566 /* When creating new, the returned value is the new id, otherwise it is the new version */ 445 if (retCode == 200) 446 { 447 if(osm.id == 0) 448 { 567 if (retCode == 200) { 568 if (osm.id == 0) { 449 569 osm.id = readId(activeConnection.getInputStream()); 450 570 osm.version = 1; 451 } 452 else 453 { 571 } else { 454 572 int read_version = (int)readId(activeConnection.getInputStream()); 455 if ( read_version > 0)573 if (read_version > 0) 456 574 osm.version = read_version; 457 575 } 458 } 459 else 460 { 576 } else { 461 577 System.out.println("got return: "+retCode+" with id "+osm.id); 462 578 } … … 464 580 if (retCode == 410 && requestMethod.equals("DELETE")) 465 581 return; // everything fine.. was already deleted. 466 else if (retCode != 200) 467 { 468 if (retries >= 0 && retCode != 412) 469 { 582 else if (retCode != 200) { 583 if (retries >= 0 && retCode != 412) { 470 584 retries--; 471 585 System.out.print("backing off for 10 seconds..."); -
trunk/src/org/openstreetmap/josm/io/OsmWriter.java
r1037 r1071 32 32 * map to allow references to use the correnct primitives. 33 33 */ 34 p rivateHashMap<OsmPrimitive, Long> usedNewIds = new HashMap<OsmPrimitive, Long>();34 public HashMap<OsmPrimitive, Long> usedNewIds = new HashMap<OsmPrimitive, Long>(); 35 35 36 36 private final boolean osmConform; … … 131 131 } 132 132 133 p rivateOsmWriter(PrintWriter out, boolean osmConform, Changeset changeset) {133 public OsmWriter(PrintWriter out, boolean osmConform, Changeset changeset) { 134 134 super(out); 135 135 this.osmConform = osmConform; … … 178 178 return osmConform ? 0 : newIdCounter--; 179 179 } 180 181 180 182 181 private void addTags(OsmPrimitive osm, String tagname, boolean tagOpen) {
Note:
See TracChangeset
for help on using the changeset viewer.