Ignore:
Timestamp:
2005-12-22T23:54:50+01:00 (20 years ago)
Author:
imi
Message:
  • fixed bug in movement that lat/lon was not updated
  • upload of nodes (disabled since untested)
File:
1 edited

Legend:

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

    r33 r34  
    11package org.openstreetmap.josm.io;
    22
     3import java.io.BufferedReader;
    34import java.io.IOException;
     5import java.io.InputStream;
     6import java.io.InputStreamReader;
     7import java.io.OutputStream;
     8import java.net.HttpURLConnection;
     9import java.net.URL;
     10import java.util.Collection;
    411
     12import org.jdom.Document;
     13import org.jdom.Element;
    514import org.jdom.JDOMException;
    6 import org.openstreetmap.josm.data.osm.DataSet;
     15import org.jdom.output.Format;
     16import org.jdom.output.XMLOutputter;
     17import org.openstreetmap.josm.Main;
     18import org.openstreetmap.josm.data.osm.Key;
     19import org.openstreetmap.josm.data.osm.LineSegment;
     20import org.openstreetmap.josm.data.osm.Node;
     21import org.openstreetmap.josm.data.osm.OsmPrimitive;
     22import org.openstreetmap.josm.data.osm.Track;
     23import org.openstreetmap.josm.data.osm.visitor.OsmXmlVisitor;
     24import org.openstreetmap.josm.data.osm.visitor.Visitor;
    725
    826/**
     
    1533 * - All remaining objects with modified flag set are updated.
    1634 *
     35 * This class implements visitor and will perform the correct upload action
     36 * on the visited element.
     37 *
    1738 * @author imi
    1839 */
    19 public class OsmServerWriter extends OsmConnection {
     40public class OsmServerWriter extends OsmConnection implements Visitor {
    2041
    21        
    2242        /**
    2343         * Send the dataset to the server. Ask the user first and does nothing if
    2444         * he does not want to send the data.
    2545         */
    26         public void uploadOsm(DataSet dataSet) throws IOException, JDOMException {
     46        public void uploadOsm(Collection<OsmPrimitive> list) throws JDOMException {
    2747                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                }
    28125        }
    29126}
Note: See TracChangeset for help on using the changeset viewer.