source: josm/trunk/src/org/openstreetmap/josm/actions/upload/ApiPreconditionCheckerHook.java@ 12346

Last change on this file since 12346 was 10463, checked in by Don-vip, 8 years ago

sonar - squid:S1166 - Exception handlers should preserve the original exceptions

  • Property svn:eol-style set to native
File size: 4.2 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions.upload;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.util.Collection;
7import java.util.Collections;
8
9import javax.swing.JOptionPane;
10
11import org.openstreetmap.josm.Main;
12import org.openstreetmap.josm.data.APIDataSet;
13import org.openstreetmap.josm.data.osm.OsmPrimitive;
14import org.openstreetmap.josm.data.osm.Way;
15import org.openstreetmap.josm.gui.ExceptionDialogUtil;
16import org.openstreetmap.josm.gui.progress.NullProgressMonitor;
17import org.openstreetmap.josm.io.OnlineResource;
18import org.openstreetmap.josm.io.OsmApi;
19import org.openstreetmap.josm.io.OsmApiInitializationException;
20import org.openstreetmap.josm.io.OsmTransferCanceledException;
21
22public class ApiPreconditionCheckerHook implements UploadHook {
23
24 @Override
25 public boolean checkUpload(APIDataSet apiData) {
26 OsmApi api = OsmApi.getOsmApi();
27 try {
28 if (Main.isOffline(OnlineResource.OSM_API)) {
29 return false;
30 }
31 // FIXME: this should run asynchronously and a progress monitor
32 // should be displayed.
33 api.initialize(NullProgressMonitor.INSTANCE);
34 long maxNodes = api.getCapabilities().getMaxWayNodes();
35 if (maxNodes > 0) {
36 if (!checkMaxNodes(apiData.getPrimitivesToAdd(), maxNodes))
37 return false;
38 if (!checkMaxNodes(apiData.getPrimitivesToUpdate(), maxNodes))
39 return false;
40 if (!checkMaxNodes(apiData.getPrimitivesToDelete(), maxNodes))
41 return false;
42 }
43 } catch (OsmTransferCanceledException e) {
44 Main.trace(e);
45 return false;
46 } catch (OsmApiInitializationException e) {
47 ExceptionDialogUtil.explainOsmTransferException(e);
48 return false;
49 }
50 return true;
51 }
52
53 private static boolean checkMaxNodes(Collection<OsmPrimitive> primitives, long maxNodes) {
54 for (OsmPrimitive osmPrimitive : primitives) {
55 for (String key: osmPrimitive.keySet()) {
56 String value = osmPrimitive.get(key);
57 if (key.length() > 255) {
58 if (osmPrimitive.isDeleted()) {
59 // if OsmPrimitive is going to be deleted we automatically shorten the value
60 Main.warn(
61 tr("Automatically truncating value of tag ''{0}'' on deleted object {1}",
62 key,
63 Long.toString(osmPrimitive.getId())
64 )
65 );
66 osmPrimitive.put(key, value.substring(0, 255));
67 continue;
68 }
69 JOptionPane.showMessageDialog(Main.parent,
70 tr("Length of value for tag ''{0}'' on object {1} exceeds the max. allowed length {2}. Values length is {3}.",
71 key, Long.toString(osmPrimitive.getId()), 255, value.length()
72 ),
73 tr("Precondition Violation"),
74 JOptionPane.ERROR_MESSAGE
75 );
76 Main.getLayerManager().getEditDataSet().setSelected(Collections.singleton(osmPrimitive));
77 return false;
78 }
79 }
80
81 if (osmPrimitive instanceof Way &&
82 ((Way) osmPrimitive).getNodesCount() > maxNodes) {
83 JOptionPane.showMessageDialog(
84 Main.parent,
85 tr("{0} nodes in way {1} exceed the max. allowed number of nodes {2}",
86 ((Way) osmPrimitive).getNodesCount(),
87 Long.toString(osmPrimitive.getId()),
88 maxNodes
89 ),
90 tr("API Capabilities Violation"),
91 JOptionPane.ERROR_MESSAGE
92 );
93 Main.getLayerManager().getEditDataSet().setSelected(Collections.singleton(osmPrimitive));
94 return false;
95 }
96 }
97 return true;
98 }
99}
Note: See TracBrowser for help on using the repository browser.