Changeset 34 in josm for src/org/openstreetmap/josm/io/OsmServerWriter.java
- Timestamp:
- 2005-12-22T23:54:50+01:00 (20 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/org/openstreetmap/josm/io/OsmServerWriter.java
r33 r34 1 1 package org.openstreetmap.josm.io; 2 2 3 import java.io.BufferedReader; 3 4 import java.io.IOException; 5 import java.io.InputStream; 6 import java.io.InputStreamReader; 7 import java.io.OutputStream; 8 import java.net.HttpURLConnection; 9 import java.net.URL; 10 import java.util.Collection; 4 11 12 import org.jdom.Document; 13 import org.jdom.Element; 5 14 import org.jdom.JDOMException; 6 import org.openstreetmap.josm.data.osm.DataSet; 15 import org.jdom.output.Format; 16 import org.jdom.output.XMLOutputter; 17 import org.openstreetmap.josm.Main; 18 import org.openstreetmap.josm.data.osm.Key; 19 import org.openstreetmap.josm.data.osm.LineSegment; 20 import org.openstreetmap.josm.data.osm.Node; 21 import org.openstreetmap.josm.data.osm.OsmPrimitive; 22 import org.openstreetmap.josm.data.osm.Track; 23 import org.openstreetmap.josm.data.osm.visitor.OsmXmlVisitor; 24 import org.openstreetmap.josm.data.osm.visitor.Visitor; 7 25 8 26 /** … … 15 33 * - All remaining objects with modified flag set are updated. 16 34 * 35 * This class implements visitor and will perform the correct upload action 36 * on the visited element. 37 * 17 38 * @author imi 18 39 */ 19 public class OsmServerWriter extends OsmConnection { 40 public class OsmServerWriter extends OsmConnection implements Visitor { 20 41 21 22 42 /** 23 43 * Send the dataset to the server. Ask the user first and does nothing if 24 44 * he does not want to send the data. 25 45 */ 26 public void uploadOsm( DataSet dataSet) throwsIOException,JDOMException {46 public void uploadOsm(Collection<OsmPrimitive> list) throws JDOMException { 27 47 initAuthentication(); 48 49 try { 50 // for (OsmPrimitive osm : list) 51 // osm.visit(this); 52 } catch (RuntimeException e) { 53 throw new JDOMException("An error occoured: ", e); 54 } 55 } 56 57 /** 58 * Upload a single node. 59 */ 60 @SuppressWarnings("unchecked") 61 public void visit(Node n) { 62 if (n.id == 0) { 63 sendRequest("PUT", "newnode", n); 64 } else if (Main.main.ds.deleted.contains(n)) { 65 sendRequest("DELETE", "node/"+n.id, n); 66 } else { 67 sendRequest("PUT", "node/"+n.id, n); 68 } 69 } 70 71 public void visit(LineSegment ls) { 72 } 73 74 public void visit(Track t) { 75 } 76 77 public void visit(Key k) { 78 } 79 80 /** 81 * Read an long from the input stream and return it. 82 */ 83 private long readId(InputStream inputStream) throws IOException { 84 BufferedReader in = new BufferedReader(new InputStreamReader(inputStream)); 85 String s = in.readLine(); 86 if (s == null) 87 return 0; 88 try { 89 return Long.parseLong(s); 90 } catch (NumberFormatException e) { 91 return 0; 92 } 93 } 94 95 @SuppressWarnings("unchecked") 96 private void sendRequest(String requestMethod, String urlSuffix, OsmPrimitive osm) { 97 try { 98 URL url = new URL(Main.pref.osmDataServer + "/" + urlSuffix); 99 HttpURLConnection con = (HttpURLConnection)url.openConnection(); 100 con.setConnectTimeout(20000); 101 con.setRequestMethod(requestMethod); 102 con.setDoOutput(true); 103 con.connect(); 104 105 OsmXmlVisitor visitor = new OsmXmlVisitor(false); 106 osm.visit(visitor); 107 Element root = new Element("osm"); 108 root.setAttribute("version", "0.2"); 109 root.getChildren().add(visitor.element); 110 XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat()); 111 OutputStream out = con.getOutputStream(); 112 Document doc = new Document(root); 113 xmlOut.output(doc, out); 114 xmlOut.output(doc, System.out); 115 out.close(); 116 117 int retCode = con.getResponseCode(); 118 System.out.println(retCode+" "+con.getResponseMessage()); 119 if (retCode == 200 && osm.id == 0) 120 osm.id = readId(con.getInputStream()); 121 con.disconnect(); 122 } catch (Exception e) { 123 throw new RuntimeException(e.getMessage(), e); 124 } 28 125 } 29 126 }
Note:
See TracChangeset
for help on using the changeset viewer.
