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

Last change on this file since 3719 was 3719, checked in by bastiK, 13 years ago

added missing license information

  • 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.OsmApi;
18import org.openstreetmap.josm.io.OsmApiInitializationException;
19import org.openstreetmap.josm.io.OsmTransferCancelledException;
20
21public class ApiPreconditionCheckerHook implements UploadHook {
22
23 public boolean checkUpload(APIDataSet apiData) {
24 OsmApi api = OsmApi.getOsmApi();
25 try {
26 // FIXME: this should run asynchronously and a progress monitor
27 // should be displayed.
28 api.initialize(NullProgressMonitor.INSTANCE);
29 long maxNodes = 0;
30 if (api.getCapabilities().isDefined("waynodes", "maximum")) {
31 maxNodes = api.getCapabilities().getLong("waynodes","maximum");
32 }
33 if (maxNodes > 0) {
34 if( !checkMaxNodes(apiData.getPrimitivesToAdd(), maxNodes))
35 return false;
36 if( !checkMaxNodes(apiData.getPrimitivesToUpdate(), maxNodes))
37 return false;
38 if( !checkMaxNodes(apiData.getPrimitivesToDelete(), maxNodes))
39 return false;
40 }
41 } catch(OsmTransferCancelledException e){
42 return false;
43 } catch (OsmApiInitializationException e) {
44 ExceptionDialogUtil.explainOsmTransferException(e);
45 return false;
46 }
47 return true;
48 }
49
50 private boolean checkMaxNodes(Collection<OsmPrimitive> primitives, long maxNodes) {
51 for (OsmPrimitive osmPrimitive : primitives) {
52 for (String key: osmPrimitive.keySet()) {
53 String value = osmPrimitive.get(key);
54 if(key.length() > 255) {
55 if (osmPrimitive.isDeleted()) {
56 // if OsmPrimitive is going to be deleted we automatically shorten the
57 // value
58 System.out.println(
59 tr("Warning: automatically truncating value of tag ''{0}'' on deleted object {1}",
60 key,
61 Long.toString(osmPrimitive.getId())
62 )
63 );
64 osmPrimitive.put(key, value.substring(0, 255));
65 continue;
66 }
67 JOptionPane.showMessageDialog(Main.parent,
68 tr("Length of value for tag ''{0}'' on object {1} exceeds the max. allowed length {2}. Values length is {3}.",
69 key, Long.toString(osmPrimitive.getId()), 255, value.length()
70 ),
71 tr("Precondition Violation"),
72 JOptionPane.ERROR_MESSAGE
73 );
74 Main.main.getCurrentDataSet().setSelected(Collections.singleton(osmPrimitive));
75 return false;
76 }
77 }
78
79 if (osmPrimitive instanceof Way &&
80 ((Way)osmPrimitive).getNodesCount() > maxNodes) {
81 JOptionPane.showMessageDialog(
82 Main.parent,
83 tr("{0} nodes in way {1} exceed the max. allowed number of nodes {2}",
84 ((Way)osmPrimitive).getNodesCount(),
85 Long.toString(osmPrimitive.getId()),
86 maxNodes
87 ),
88 tr("API Capabilities Violation"),
89 JOptionPane.ERROR_MESSAGE
90 );
91 Main.main.getCurrentDataSet().setSelected(Collections.singleton(osmPrimitive));
92 return false;
93 }
94 }
95 return true;
96 }
97}
Note: See TracBrowser for help on using the repository browser.