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

Last change on this file since 3083 was 3083, checked in by bastiK, 14 years ago

added svn:eol-style=native to source files

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