Skip to content

Commit

Permalink
Use jakarta.json instead of javax.json
Browse files Browse the repository at this point in the history
Signed-off-by: Taylor Smock <[email protected]>
  • Loading branch information
tsmock committed Aug 22, 2023
1 parent 80cf0d1 commit aa84663
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 57 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ant-release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ jobs:
call-workflow:
uses: JOSM/JOSMPluginAction/.github/workflows/ant.yml@v1
with:
josm-revision: "r14153"
josm-revision: "r18723"

2 changes: 1 addition & 1 deletion .github/workflows/ant.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
call-workflow:
strategy:
matrix:
josm-revision: ["", "r14153"]
josm-revision: ["", "r18723"]
uses: JOSM/JOSMPluginAction/.github/workflows/ant.yml@v1
with:
josm-revision: ${{ matrix.josm-revision }}
3 changes: 2 additions & 1 deletion build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<!-- enter the SVN commit message -->
<property name="commit.message" value="Commit message"/>
<!-- enter the *lowest* JOSM version this plugin is currently compatible with -->
<property name="plugin.main.version" value="14153"/>
<property name="plugin.main.version" value="18723"/>

<!-- Configure these properties (replace "..." accordingly).
See https://josm.openstreetmap.de/wiki/DevelopersGuide/DevelopingPlugins
Expand All @@ -23,6 +23,7 @@
<target name="additional-manifest">
<manifest file="MANIFEST" mode="update">
<attribute name="12735_Plugin-Url" value="v0.6.0;https://github.com/JOSM/austriaaddresshelper/releases/download/v0.6.0/austriaaddresshelper.jar" />
<attribute name="14153_Plugin-Url" value="0.8.1;https://github.com/JOSM/austriaaddresshelper/releases/download/v0.8.1/austriaaddresshelper.jar" />
</manifest>
</target>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
import java.awt.event.KeyEvent;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URL;
import java.net.URLEncoder;
import java.text.DecimalFormat;
Expand All @@ -21,11 +20,11 @@
import java.util.Map;
import java.util.TreeMap;

import javax.json.Json;
import javax.json.JsonArray;
import javax.json.JsonObject;
import javax.json.JsonReader;
import javax.json.JsonValue;
import jakarta.json.Json;
import jakarta.json.JsonArray;
import jakarta.json.JsonObject;
import jakarta.json.JsonReader;
import jakarta.json.JsonValue;
import javax.swing.JOptionPane;

import org.openstreetmap.josm.actions.JosmAction;
Expand All @@ -43,7 +42,9 @@
import org.openstreetmap.josm.io.OverpassDownloadReader;
import org.openstreetmap.josm.tools.HttpClient;
import org.openstreetmap.josm.tools.ImageProvider;
import org.openstreetmap.josm.tools.Logging;
import org.openstreetmap.josm.tools.Shortcut;
import org.openstreetmap.josm.tools.Utils;

/**
* Created by tom on 02/08/15.
Expand All @@ -52,10 +53,11 @@ public class AustriaAddressHelperAction extends JosmAction {
static final StringProperty baseUrl = new StringProperty("austriaaddresshelper.url",
"https://bev.kolmann.at/reverse-geocode.php");
static final BooleanProperty checkDuplicates = new BooleanProperty("austriaaddresshelper.check-duplicates", true);
static boolean addressTypeDialogCanceled = false;
static boolean addressTypeDialogCanceled;

protected static HashMap<HashMap<String, String>, String> rememberedAddressTypeChoices = new HashMap<>();
protected static String[] tagsToCheckForDuplicates = {"addr:city", "addr:postcode", "addr:place", "addr:street", "addr:hamlet", "addr:housenumber"};
protected static String[] tagsToCheckForDuplicates = {"addr:city", "addr:postcode", "addr:place", "addr:street",
"addr:hamlet", "addr:housenumber"};
protected static String[] streetTypeTags = {"addr:street", "addr:place", "addr:hamlet", "addr:suburb"};
protected static String[] objectTypesToCheckforDuplicates = {"way", "node", "relation"};
protected static String streetTypeTagPlaceholder = "___street_type_tag___";
Expand All @@ -82,10 +84,10 @@ public void actionPerformed(ActionEvent event) {

final List<Command> commands = new ArrayList<>();
for (OsmPrimitive selectedObject : sel) {
Map<String, String> newObject = loadAddress(selectedObject);
if(newObject != null){
commands.add(new ChangePropertyCommand(Collections.singleton(selectedObject), newObject));
}
Map<String, String> newObject = loadAddress(selectedObject);
if(!Utils.isEmpty(newObject)) {
commands.add(new ChangePropertyCommand(Collections.singleton(selectedObject), newObject));
}
}
if (!commands.isEmpty()) {
UndoRedoHandler.getInstance().add(new SequenceCommand(trn("Add address", "Add addresses", commands.size()), commands));
Expand All @@ -96,13 +98,13 @@ public static Map<String, String> loadAddress(OsmPrimitive selectedObject){
LatLon center = selectedObject.getBBox().getCenter();

try {
URL url = new URL(baseUrl.get()
URL url = URI.create(baseUrl.get()
+ "?lat=" + URLEncoder.encode(DecimalDegreesCoordinateFormat.INSTANCE.latToString(center), "UTF-8")
+ "&lon=" + URLEncoder.encode(DecimalDegreesCoordinateFormat.INSTANCE.lonToString(center), "UTF-8")
+ "&distance=30"
+ "&limit=1"
+ "&epsg=4326"
);
).toURL();

final JsonObject json;
try (BufferedReader in = HttpClient.create(url)
Expand All @@ -115,7 +117,7 @@ public static Map<String, String> loadAddress(OsmPrimitive selectedObject){
}

final JsonArray addressItems = json.getJsonArray("results");
if (addressItems.size() > 0) {
if (!addressItems.isEmpty()) {
final JsonObject firstAddress = addressItems.getJsonObject(0);

String country = "AT";
Expand Down Expand Up @@ -151,9 +153,9 @@ public static Map<String, String> loadAddress(OsmPrimitive selectedObject){
newObject.remove("addr:place");

// Decide whether the address type is 'street' or 'place'.
if (firstAddress.getString("address_type").equals("place")) {
if ("place".equals(firstAddress.getString("address_type"))) {
newObject.put("addr:place", streetOrPlace);
} else if (firstAddress.getString("address_type").equals("street")) {
} else if ("street".equals(firstAddress.getString("address_type"))) {
newObject.put("addr:street", streetOrPlace);
} else {
// Get remembered choice or ask the user.
Expand All @@ -169,7 +171,7 @@ public static Map<String, String> loadAddress(OsmPrimitive selectedObject){
.setDuration(2500)
.show();

return null;
return Collections.emptyMap();
} else {
newObject.put("addr:" + addressType, streetOrPlace);
}
Expand All @@ -178,16 +180,18 @@ public static Map<String, String> loadAddress(OsmPrimitive selectedObject){
newObject.put("addr:housenumber", houseNumber);

// Search for duplicates.
List<String> existingObjectsWithThatAddress = checkDuplicates.get()
List<String> existingObjectsWithThatAddress = Boolean.TRUE.equals(checkDuplicates.get())
? getUrlsOfObjectsWithThatAddress(newObject, center)
: Collections.emptyList();

int dialogAnswer = -2;

if (existingObjectsWithThatAddress == null) {
dialogAnswer = JOptionPane.showOptionDialog(MainApplication.getMainFrame(), tr("Unable to check whether this address already exists in OpenStreetMap: Continue anyway?"), tr("Address Duplicate Check Failed"),
dialogAnswer = JOptionPane.showOptionDialog(MainApplication.getMainFrame(),
tr("Unable to check whether this address already exists in OpenStreetMap: Continue anyway?"),
tr("Address Duplicate Check Failed"),
JOptionPane.YES_NO_OPTION, JOptionPane.WARNING_MESSAGE, null, null, null);
} else if (existingObjectsWithThatAddress.size() > 0) {
} else if (!existingObjectsWithThatAddress.isEmpty()) {
StringBuilder urlList = new StringBuilder();
urlList.append("<ul>");

Expand Down Expand Up @@ -237,7 +241,8 @@ public static Map<String, String> loadAddress(OsmPrimitive selectedObject){
new Notification(
"<strong>" + tr("Austria Address Helper") + "</strong><br />" +
tr("Successfully added address to selected object:") + "<br />" +
encodeHTML(streetOrPlace) + " " + encodeHTML(houseNumber) + ", " + encodeHTML(postcode) + " " + encodeHTML(municipality) + " (" + encodeHTML(country) + ")<br/>" +
encodeHTML(streetOrPlace) + " " + encodeHTML(houseNumber) + ", " + encodeHTML(postcode) +
" " + encodeHTML(municipality) + " (" + encodeHTML(country) + ")<br/>" +
"<strong>" + tr("Distance between building center and address coordinates:") + "</strong> " +
new DecimalFormat("#.##").format(distanceToAddressCoordinates) + " " + tr("meters")
)
Expand All @@ -247,7 +252,7 @@ public static Map<String, String> loadAddress(OsmPrimitive selectedObject){

return newObject;
} else {
return null;
return Collections.emptyMap();
}
} else {
new Notification(
Expand All @@ -258,7 +263,7 @@ public static Map<String, String> loadAddress(OsmPrimitive selectedObject){
.show();
}
} catch (IOException | NullPointerException e) {
e.printStackTrace();
Logging.trace(e);
new Notification(
"<strong>" + tr("Austria Address Helper") + "</strong>" +
tr("An unexpected exception occurred:") + e.toString()
Expand All @@ -267,7 +272,7 @@ public static Map<String, String> loadAddress(OsmPrimitive selectedObject){
.show();
}

return null;
return Collections.emptyMap();
}

protected static ArrayList<String> getUrlsOfObjectsWithThatAddress(Map<String, String> newObject, LatLon position) {
Expand All @@ -285,8 +290,9 @@ protected static ArrayList<String> getUrlsOfObjectsWithThatAddress(Map<String, S
StringBuilder filterLineBuilder = new StringBuilder();

// Iterate over all tags of the new object.
for (String key: newObject.keySet()) {
String value = newObject.get(key);
for (Map.Entry<String, String> entry : newObject.entrySet()) {
String key = entry.getKey();
String value = entry.getValue();

// Only filter for relevant tags.
if (Arrays.asList(tagsToCheckForDuplicates).contains(key)) {
Expand All @@ -303,10 +309,10 @@ protected static ArrayList<String> getUrlsOfObjectsWithThatAddress(Map<String, S
tagName = key;
}

filterLineBuilder.append(tagName.replaceAll("\"", "\\\\\"")); // Escape double quotes
filterLineBuilder.append(tagName.replace("\"", "\\\\\"")); // Escape double quotes

filterLineBuilder.append("\"=\"");
filterLineBuilder.append(value.replaceAll("\"", "\\\\\"")); // Escape double quotes
filterLineBuilder.append(value.replace("\"", "\\\\\"")); // Escape double quotes
filterLineBuilder.append("\"]");
}
}
Expand Down Expand Up @@ -335,9 +341,9 @@ protected static ArrayList<String> getUrlsOfObjectsWithThatAddress(Map<String, S
Exception exception = null;

try {
URL url = new URL(OverpassDownloadReader.OVERPASS_SERVER.get() + "interpreter"
URL url = URI.create(OverpassDownloadReader.OVERPASS_SERVER.get() + "interpreter"
+ "?data=" + URLEncoder.encode(query, "UTF-8")
);
).toURL();

final JsonObject json;

Expand All @@ -352,30 +358,27 @@ protected static ArrayList<String> getUrlsOfObjectsWithThatAddress(Map<String, S

final JsonArray items = json.getJsonArray("elements");

if (items.size() > 0) {
if (!items.isEmpty()) {
for (JsonValue item: items) {
JsonObject itemObject = item.asJsonObject();

try {
String type = itemObject.getString("type");
int osmId = itemObject.getInt("id");

urls.add("https://www.openstreetmap.org/" + URLEncoder.encode(type, "UTF-8") + "/" + URLEncoder.encode(Integer.toString(osmId), "UTF-8"));
} catch (NullPointerException e) {
String type = itemObject.getString("type", null);
int osmId = itemObject.getInt("id", 0);
if (type == null || osmId == 0) {
urls.add("<Could not generate URL>");
} else {
urls.add("https://www.openstreetmap.org/" + URLEncoder.encode(type, "UTF-8") +
"/" + URLEncoder.encode(Integer.toString(osmId), "UTF-8"));
}
}
}

noExceptionThrown = true;
} catch (MalformedURLException | UnsupportedEncodingException | NullPointerException e) {
e.printStackTrace();
exception = e;
} catch (IOException e) {
e.printStackTrace();
Logging.trace(e);
exception = e;
} finally {
if (!noExceptionThrown) {
if (!noExceptionThrown && exception != null) {
new Notification(
"<strong>" + tr("Austria Address Helper") + "</strong>" +
tr("An unexpected exception occurred while checking for address duplicates:") + exception.toString()
Expand Down Expand Up @@ -435,18 +438,13 @@ protected void updateEnabledState(final Collection<? extends OsmPrimitive> selec
setEnabled(selection != null && selection.size() == 1);
}

private static String encodeHTML(String s)
{
StringBuffer out = new StringBuffer();
for(int i=0; i<s.length(); i++)
{
private static String encodeHTML(String s) {
StringBuilder out = new StringBuilder();
for(int i=0; i<s.length(); i++) {
char c = s.charAt(i);
if(c > 127 || c=='"' || c=='<' || c=='>')
{
if(c > 127 || c=='"' || c=='<' || c=='>') {
out.append("&#"+(int)c+";");
}
else
{
} else {
out.append(c);
}
}
Expand Down

0 comments on commit aa84663

Please sign in to comment.