Changeset 30234 in osm


Ignore:
Timestamp:
2014-01-27T00:58:04+01:00 (11 years ago)
Author:
donvip
Message:

[josm_geochat, josm_livegps] update to JOSM 6756

Location:
applications/editors/josm/plugins
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • applications/editors/josm/plugins/geochat/build.xml

    r30131 r30234  
    55    <property name="commit.message" value="[josm_geochat] copypaste from keyboard, font size advanced parameters"/>
    66    <!-- enter the *lowest* JOSM version this plugin is currently compatible with -->
    7     <property name="plugin.main.version" value="6484"/>
     7    <property name="plugin.main.version" value="6756"/>
    88
    99    <property name="plugin.author" value="Ilya Zverev"/>
  • applications/editors/josm/plugins/geochat/src/geochat/ChatServerConnection.java

    r29854 r30234  
    1515import java.util.Set;
    1616
    17 import org.json.JSONArray;
    18 import org.json.JSONException;
    19 import org.json.JSONObject;
     17import javax.json.JsonArray;
     18import javax.json.JsonException;
     19import javax.json.JsonObject;
     20
    2021import org.openstreetmap.josm.Main;
    2122import org.openstreetmap.josm.data.coor.CoordinateFormat;
     
    9293            String query = "whoami&uid=" + uid;
    9394            JsonQueryUtil.queryAsync(query, new JsonQueryCallback() {
    94                 public void processJson( JSONObject json ) {
    95                     if( json != null && json.has("name") )
     95                public void processJson( JsonObject json ) {
     96                    if( json != null && json.get("name") != null )
    9697                        login(uid, json.getString("name"));
    9798                    else if( userName != null && userName.length() > 1 )
     
    143144                    + nameAttr;
    144145            JsonQueryUtil.queryAsync(query, new JsonQueryCallback() {
    145                 public void processJson( JSONObject json ) {
     146                public void processJson( JsonObject json ) {
    146147                    if( json == null )
    147148                        fireLoginFailed(tr("Could not get server response, check logs"));
    148                     else if( json.has("error") )
     149                    else if( json.get("error") != null )
    149150                        fireLoginFailed(tr("Failed to login as {0}:", userName) + "\n" + json.getString("error"));
    150                     else if( !json.has("uid") )
     151                    else if( json.get("uid") == null)
    151152                        fireLoginFailed(tr("The server did not return user ID"));
    152153                    else {
    153                         String name = json.has("name") ? json.getString("name") : userName;
     154                        String name = json.get("name") != null ? json.getString("name") : userName;
    154155                        login(json.getInt("uid"), name);
    155156                    }
     
    190191        String query = "logout&uid=" + userId;
    191192        JsonQueryUtil.queryAsync(query, new JsonQueryCallback() {
    192             public void processJson( JSONObject json ) {
    193                 if( json != null && json.has("message") ) {
     193            public void processJson( JsonObject json ) {
     194                if( json != null && json.get("message") != null) {
    194195                    logoutIntl();
    195196                }
     
    245246                    query += "&to=" + URLEncoder.encode(targetUser, "UTF8");
    246247            JsonQueryUtil.queryAsync(query, new JsonQueryCallback() {
    247                 public void processJson( JSONObject json ) {
     248                public void processJson( JsonObject json ) {
    248249                    if( json == null )
    249250                        fireMessageFailed(tr("Could not get server response, check logs"));
    250                     else if( json.has("error") )
     251                    else if( json.get("error") != null )
    251252                        fireMessageFailed(tr("Failed to send message:") + "\n" + json.getString("error"));
    252253                }
     
    356357                    + "&lon=" + pos.lonToString(CoordinateFormat.DECIMAL_DEGREES)
    357358                    + "&uid=" + userId + "&last=" + lastId;
    358             JSONObject json;
     359            JsonObject json;
    359360            try {
    360361                json = JsonQueryUtil.query(query);
     
    366367//              fireLoginFailed(tr("Could not get server response, check logs"));
    367368//              logoutIntl(); // todo: uncomment?
    368             } else if( json.has("error") ) {
     369            } else if( json.get("error") != null) {
    369370                fireLoginFailed(tr("Failed to get messages as {0}:", userName) + "\n" + json.getString("error"));
    370371                logoutIntl();
    371372            } else {
    372                 if( json.has("users") ) {
    373                     Map<String, LatLon> users = parseUsers(json.getJSONArray("users"));
     373                if( json.get("users") != null) {
     374                    Map<String, LatLon> users = parseUsers(json.getJsonArray("users"));
    374375                    for( ChatServerConnectionListener listener : listeners )
    375376                        listener.updateUsers(users);
    376377                }
    377                 if( json.has("messages") ) {
    378                     List<ChatMessage> messages = parseMessages(json.getJSONArray("messages"), false);
     378                if( json.get("messages") != null) {
     379                    List<ChatMessage> messages = parseMessages(json.getJsonArray("messages"), false);
    379380                    for( ChatMessage m : messages )
    380381                        if( m.getId() > lastId )
     
    383384                        listener.receivedMessages(needReset, messages);
    384385                }
    385                 if( json.has("private") ) {
    386                     List<ChatMessage> messages = parseMessages(json.getJSONArray("private"), true);
     386                if( json.get("private") != null) {
     387                    List<ChatMessage> messages = parseMessages(json.getJsonArray("private"), true);
    387388                    for( ChatMessage m : messages )
    388389                        if( m.getId() > lastId )
     
    396397        }
    397398
    398         private List<ChatMessage> parseMessages( JSONArray messages, boolean priv ) {
     399        private List<ChatMessage> parseMessages( JsonArray messages, boolean priv ) {
    399400            List<ChatMessage> result = new ArrayList<ChatMessage>();
    400             for( int i = 0; i < messages.length(); i++ ) {
     401            for( int i = 0; i < messages.size(); i++ ) {
    401402                try {
    402                     JSONObject msg = messages.getJSONObject(i);
    403                     long id = msg.getLong("id");
    404                     double lat = msg.getDouble("lat");
    405                     double lon = msg.getDouble("lon");
    406                     long timeStamp = msg.getLong("timestamp");
     403                        JsonObject msg = messages.getJsonObject(i);
     404                    long id = msg.getJsonNumber("id").longValue();
     405                    double lat = msg.getJsonNumber("lat").doubleValue();
     406                    double lon = msg.getJsonNumber("lon").doubleValue();
     407                    long timeStamp = msg.getJsonNumber("timestamp").longValue();
    407408                    String author = msg.getString("author");
    408409                    String message = msg.getString("message");
     
    411412                            incoming, message, new Date(timeStamp * 1000));
    412413                    cm.setPrivate(priv);
    413                     if( msg.has("recipient") && !incoming )
     414                    if( msg.get("recipient") != null && !incoming )
    414415                        cm.setRecipient(msg.getString("recipient"));
    415416                    result.add(cm);
    416                 } catch( JSONException e ) {
     417                } catch( JsonException e ) {
    417418                    // do nothing, just skip this message
    418419                }
     
    421422        }
    422423
    423         private Map<String, LatLon> parseUsers( JSONArray users ) {
     424        private Map<String, LatLon> parseUsers( JsonArray users ) {
    424425            Map<String, LatLon> result = new HashMap<String, LatLon>();
    425             for( int i = 0; i < users.length(); i++ ) {
     426            for( int i = 0; i < users.size(); i++ ) {
    426427                try {
    427                     JSONObject user = users.getJSONObject(i);
     428                        JsonObject user = users.getJsonObject(i);
    428429                    String name = user.getString("user");
    429                     double lat = user.getDouble("lat");
    430                     double lon = user.getDouble("lon");
     430                    double lat = user.getJsonNumber("lat").doubleValue();
     431                    double lon = user.getJsonNumber("lon").doubleValue();
    431432                    result.put(name, new LatLon(lat, lon));
    432                 } catch( JSONException e ) {
     433                } catch( JsonException e ) {
    433434                    // do nothing, just skip this user
    434435                }
  • applications/editors/josm/plugins/geochat/src/geochat/JsonQueryCallback.java

    r29584 r30234  
    22package geochat;
    33
    4 import org.json.JSONObject;
     4import javax.json.JsonObject;
    55
    66/**
     
    1717     * @param json JSON parsed response or null if the query was unsuccessful.
    1818     */
    19     void processJson( JSONObject json );
     19    void processJson( JsonObject json );
    2020}
  • applications/editors/josm/plugins/geochat/src/geochat/JsonQueryUtil.java

    r29584 r30234  
    88import java.net.MalformedURLException;
    99import java.net.URL;
    10 import org.json.JSONException;
    11 import org.json.JSONObject;
    12 import org.json.JSONTokener;
     10
     11import javax.json.Json;
     12import javax.json.JsonException;
     13import javax.json.JsonObject;
     14
    1315import org.openstreetmap.josm.Main;
    1416
     
    2325     * Query the server synchronously.
    2426     * @param query Query string, starting with action. Example: <tt>get&lat=1.0&lon=-2.0&uid=12345</tt>
    25      * @return Parsed JSONObject if the query was successful, <tt>null</tt> otherwise.
     27     * @return Parsed JsonObject if the query was successful, <tt>null</tt> otherwise.
    2628     * @throws IOException There was a problem connecting to the server or parsing JSON.
    2729     */
    28     public static JSONObject query( String query ) throws IOException {
     30    public static JsonObject query( String query ) throws IOException {
    2931        try {
    3032            String serverURL = Main.pref.get("geochat.server", "http://zverik.dev.openstreetmap.org/osmochat.php?action=");
     
    4042                throw new IOException("Empty response");
    4143            try {
    42                 JSONTokener tokener = new JSONTokener(inp);
    43                 JSONObject result = new JSONObject(tokener);
    44                 return result;
    45             } catch( JSONException e ) {
     44                return Json.createReader(inp).readObject();
     45            } catch( JsonException e ) {
    4646                throw new IOException("Failed to parse JSON: " + e.getMessage());
    4747            } finally {
     
    7575
    7676    private void doRealRun() {
    77         JSONObject obj;
     77        JsonObject obj;
    7878        try {
    7979            obj = query(query);
  • applications/editors/josm/plugins/livegps/build.xml

    r30131 r30234  
    22<project name="livegps" default="dist" basedir=".">
    33    <property name="commit.message" value="Changed the constructor signature of the plugin main class"/>
    4     <property name="plugin.main.version" value="6484"/>
     4    <property name="plugin.main.version" value="6756"/>
    55       
    66    <!-- Configure these properties (replace "..." accordingly).
  • applications/editors/josm/plugins/livegps/src/livegps/LiveGpsAcquirer.java

    r29769 r30234  
    22
    33import static org.openstreetmap.josm.tools.I18n.tr;
    4 
    54
    65import java.beans.PropertyChangeEvent;
     
    98import java.io.IOException;
    109import java.io.InputStreamReader;
     10import java.io.StringReader;
    1111import java.net.InetAddress;
    1212import java.net.Socket;
     
    1414import java.util.List;
    1515
     16import javax.json.Json;
     17import javax.json.JsonException;
     18import javax.json.JsonNumber;
     19import javax.json.JsonObject;
     20
    1621import org.openstreetmap.josm.Main;
    17 
    18 import org.json.JSONObject;
    19 import org.json.JSONException;
    2022
    2123public class LiveGpsAcquirer implements Runnable {
     
    4143     */
    4244    public LiveGpsAcquirer() {
    43         super();
    4445
    4546        gpsdHost = Main.pref.get(C_HOST, DEFAULT_HOST);
     
    7778     * @param statusMessage the status message.
    7879     */
    79     public void fireGpsStatusChangeEvent(LiveGpsStatus.GpsStatus status,
    80             String statusMessage) {
     80    public void fireGpsStatusChangeEvent(LiveGpsStatus.GpsStatus status, String statusMessage) {
    8181        PropertyChangeEvent event = new PropertyChangeEvent(this, "gpsstatus",
    8282                null, new LiveGpsStatus(status, statusMessage));
     
    9797     */
    9898    public void fireGpsDataChangeEvent(LiveGpsData oldData, LiveGpsData newData) {
    99         PropertyChangeEvent event = new PropertyChangeEvent(this, "gpsdata",
    100                 oldData, newData);
     99        PropertyChangeEvent event = new PropertyChangeEvent(this, "gpsdata", oldData, newData);
    101100
    102101        if (!event.equals(lastDataEvent)) {
     
    124123        while (!shutdownFlag) {
    125124
    126             while (!connected) {
    127                 try {
     125            while (!connected) {
     126                try {
    128127                    connect();
    129                 } catch (IOException iox) {
    130                     fireGpsStatusChangeEvent( LiveGpsStatus.GpsStatus.CONNECTION_FAILED, tr("Connection Failed"));
    131                     try {
    132                         Thread.sleep(1000);
    133                     } catch (InterruptedException ignore) {}
    134                 }
    135             }
    136 
    137             assert (connected);
    138 
    139             try {
    140                     String line;
    141 
    142                     // <FIXXME date="23.06.2007" author="cdaller">
    143                     // TODO this read is blocking if gps is connected but has no
    144                     // fix, so gpsd does not send positions
    145                     line = gpsdReader.readLine();
    146                     // </FIXXME>
    147                     if (line == null)
    148                         throw new IOException();
    149 
    150                     if (JSONProtocol == true)
    151                         gpsData = ParseJSON(line);
    152                     else
    153                         gpsData = ParseOld(line);
    154 
    155                     if (gpsData == null)
    156                         continue;
    157 
    158                     fireGpsDataChangeEvent(oldGpsData, gpsData);
    159                     oldGpsData = gpsData;
     128                } catch (IOException iox) {
     129                    fireGpsStatusChangeEvent( LiveGpsStatus.GpsStatus.CONNECTION_FAILED, tr("Connection Failed"));
     130                        try {
     131                            Thread.sleep(1000);
     132                        } catch (InterruptedException ignore) {
     133                               
     134                        }
     135                    }
     136            }
     137
     138            assert (connected);
     139
     140            try {
     141                String line;
     142
     143                // <FIXXME date="23.06.2007" author="cdaller">
     144                // TODO this read is blocking if gps is connected but has no
     145                // fix, so gpsd does not send positions
     146                line = gpsdReader.readLine();
     147                // </FIXXME>
     148                if (line == null)
     149                    throw new IOException();
     150
     151                if (JSONProtocol == true)
     152                    gpsData = ParseJSON(line);
     153                else
     154                    gpsData = ParseOld(line);
     155
     156                if (gpsData == null)
     157                    continue;
     158
     159                fireGpsDataChangeEvent(oldGpsData, gpsData);
     160                oldGpsData = gpsData;
    160161            } catch (IOException iox) {
    161                 System.out.println("LiveGps: lost connection to gpsd");
     162                Main.warn("LiveGps: lost connection to gpsd");
    162163                fireGpsStatusChangeEvent(
    163164                        LiveGpsStatus.GpsStatus.CONNECTION_FAILED,
    164165                        tr("Connection Failed"));
    165                 disconnect();
     166                disconnect();
    166167                try {
    167168                    Thread.sleep(1000);
     
    171172        }
    172173
    173         System.out.println("LiveGps: Disconnected from gpsd");
     174        Main.info("LiveGps: Disconnected from gpsd");
    174175        fireGpsStatusChangeEvent(LiveGpsStatus.GpsStatus.DISCONNECTED,
    175176                tr("Not connected"));
    176         disconnect();
     177        disconnect();
    177178    }
    178179
     
    182183
    183184    private void connect() throws IOException {
    184         JSONObject greeting;
     185        JsonObject greeting;
    185186        String line, type, release;
    186187
    187         System.out.println("LiveGps: trying to connect to gpsd at " + gpsdHost + ":" + gpsdPort);
     188        Main.info("LiveGps: trying to connect to gpsd at " + gpsdHost + ":" + gpsdPort);
    188189        fireGpsStatusChangeEvent( LiveGpsStatus.GpsStatus.CONNECTING, tr("Connecting"));
    189190
     
    194195                break;
    195196            } catch (IOException e) {
    196                 System.out.println("LiveGps: Could not open connection to gpsd: " + e);
     197                Main.warn("LiveGps: Could not open connection to gpsd: " + e);
    197198                gpsdSocket = null;
    198199            }
     
    213214
    214215        try {
    215             greeting = new JSONObject(line);
     216            greeting = Json.createReader(new StringReader(line)).readObject();
    216217            type = greeting.getString("class");
    217218            if (type.equals("VERSION")) {
    218219                release = greeting.getString("release");
    219                 System.out.println("LiveGps: Connected to gpsd " + release);
     220                Main.info("LiveGps: Connected to gpsd " + release);
    220221            } else
    221                 System.out.println("LiveGps: Unexpected JSON in gpsd greeting: " + line);
    222         } catch (JSONException jex) {
     222                Main.info("LiveGps: Unexpected JSON in gpsd greeting: " + line);
     223        } catch (JsonException jex) {
    223224            if (line.startsWith("GPSD,")) {
    224225                connected = true;
    225226                JSONProtocol = false;
    226                 System.out.println("LiveGps: Connected to old gpsd protocol version.");
     227                Main.info("LiveGps: Connected to old gpsd protocol version.");
    227228                fireGpsStatusChangeEvent(LiveGpsStatus.GpsStatus.CONNECTED, tr("Connected"));
    228229            }
     
    230231
    231232        if (JSONProtocol == true) {
    232             JSONObject Watch = new JSONObject();
    233             try {
    234                 Watch.put("enable", true);
    235                 Watch.put("json", true);
    236             } catch (JSONException je) {}
     233                JsonObject Watch = Json.createObjectBuilder()
     234                                .add("enable", true)
     235                                .add("json", true)
     236                                .build();
    237237
    238238            String Request = "?WATCH=" + Watch.toString() + ";\n";
     
    245245
    246246    private void disconnect() {
    247         assert(gpsdSocket != null);
    248 
    249         connected = false;
     247        assert(gpsdSocket != null);
     248
     249        connected = false;
    250250
    251251        try {
    252                 gpsdSocket.close();
    253                 gpsdSocket = null;
    254         } catch (Exception e) {
    255                 System.out.println("LiveGps: Unable to close socket; reconnection may not be possible");
    256         }
     252            gpsdSocket.close();
     253            gpsdSocket = null;
     254        } catch (Exception e) {
     255                Main.warn("LiveGps: Unable to close socket; reconnection may not be possible");
     256        }
    257257    }
    258258
    259259    private LiveGpsData ParseJSON(String line) {
    260         JSONObject report;
     260        JsonObject report;
    261261        String type;
    262262        double lat = 0;
     
    268268
    269269        try {
    270             report = new JSONObject(line);
     270            report = Json.createReader(new StringReader(line)).readObject();
    271271            type = report.getString("class");
    272         } catch (JSONException jex) {
    273             System.out.println("LiveGps: line read from gpsd is not a JSON object:" + line);
     272        } catch (JsonException jex) {
     273                Main.warn("LiveGps: line read from gpsd is not a JSON object:" + line);
    274274            return null;
    275275        }
     
    278278
    279279        try {
    280             lat = report.getDouble("lat");
    281             lon = report.getDouble("lon");
    282             speed = (new Float(report.getDouble("speed"))).floatValue();
    283             course = (new Float(report.getDouble("track"))).floatValue();
    284             if (report.has("epx"))
    285                 epx = (new Float(report.getDouble("epx"))).floatValue();
    286             if (report.has("epy"))
    287                 epy = (new Float(report.getDouble("epy"))).floatValue();
     280            lat = report.getJsonNumber("lat").doubleValue();
     281            lon = report.getJsonNumber("lon").doubleValue();
     282            speed = (new Float(report.getJsonNumber("speed").doubleValue())).floatValue();
     283            course = (new Float(report.getJsonNumber("track").doubleValue())).floatValue();
     284            JsonNumber epxJson = report.getJsonNumber("epx");
     285            if (epxJson != null)
     286                epx = (new Float(epxJson.doubleValue())).floatValue();
     287            JsonNumber epyJson = report.getJsonNumber("epy");
     288            if (epyJson != null)
     289                epy = (new Float(epyJson.doubleValue())).floatValue();
    288290
    289291            return new LiveGpsData(lat, lon, course, speed, epx, epy);
    290         } catch (JSONException je) {}
     292        } catch (JsonException je) {
     293                Main.debug(je.getMessage());
     294        }
    291295
    292296        return null;
Note: See TracChangeset for help on using the changeset viewer.