Changeset 2591 in osm for applications/editors/josm/plugins/validator/src
- Timestamp:
- 2007-04-18T23:56:19+02:00 (18 years ago)
- Location:
- applications/editors/josm/plugins/validator/src/org/openstreetmap/josm/plugins/validator
- Files:
-
- 1 added
- 12 edited
Legend:
- Unmodified
- Added
- Removed
-
applications/editors/josm/plugins/validator/src/org/openstreetmap/josm/plugins/validator/Test.java
r2453 r2591 1 1 package org.openstreetmap.josm.plugins.validator; 2 2 3 import java.util.*; 3 import java.util.ArrayList; 4 import java.util.Collection; 5 import java.util.List; 4 6 5 7 import javax.swing.JPanel; 6 8 9 import org.openstreetmap.josm.command.Command; 7 10 import org.openstreetmap.josm.data.osm.*; 8 11 import org.openstreetmap.josm.data.osm.visitor.Visitor; … … 30 33 /** The list of errors */ 31 34 protected List<TestError> errors = new ArrayList<TestError>(30); 35 36 /** Whether the test is run on a partial selection data */ 37 protected boolean partialSelection; 32 38 33 39 /** … … 66 72 errors = new ArrayList<TestError>(30); 67 73 } 74 75 /** 76 * Flag notifying that this test is run over a partial data selection 77 * @param partialSelection Whether the test is on a partial selection data 78 */ 79 public void setPartialSelection(boolean partialSelection) 80 { 81 this.partialSelection = partialSelection; 82 } 68 83 69 84 /** … … 92 107 for (OsmPrimitive p : selection) 93 108 { 94 p.visit(this); 109 if( !p.deleted ) 110 p.visit(this); 95 111 } 96 112 } … … 116 132 { 117 133 } 134 135 /** 136 * Fixes the error with the appropiate command 137 * 138 * @param testError 139 * @return The command to fix the error 140 */ 141 public Command fixError(TestError testError) 142 { 143 return null; 144 } 145 146 /** 147 * Returns true if the given error can be fixed automatically 148 * 149 * @param testError The error to check if can be fixed 150 * @return true if the error can be fixed 151 */ 152 public boolean isFixable(TestError testError) 153 { 154 return false; 155 } 118 156 } -
applications/editors/josm/plugins/validator/src/org/openstreetmap/josm/plugins/validator/TestError.java
r2453 r2591 4 4 import java.util.List; 5 5 6 import org.openstreetmap.josm.command.Command; 6 7 import org.openstreetmap.josm.data.osm.OsmPrimitive; 7 8 … … 17 18 private String message; 18 19 /** The affected primitives */ 19 private List<? extends OsmPrimitive> primitives; 20 private List<OsmPrimitive> primitives; 21 /** The tester that raised this error */ 22 private Test tester; 23 /** Internal code used by testers to classify errors */ 24 private int internalCode; 20 25 21 26 /** … … 28 33 /** 29 34 * Constructor 35 * @param tester The tester 30 36 * @param severity The severity of this error 31 37 * @param message The error message 32 38 * @param primitives The affected primitives 33 39 */ 34 public TestError( Severity severity, String message, List<? extendsOsmPrimitive> primitives)40 public TestError(Test tester, Severity severity, String message, List<OsmPrimitive> primitives) 35 41 { 42 this.tester = tester; 36 43 this.severity = severity; 37 44 this.message = message; … … 41 48 /** 42 49 * Constructor 50 * @param tester The tester 43 51 * @param severity The severity of this error 44 52 * @param message The error message 45 53 * @param primitive The affected primitive 46 54 */ 47 public TestError( Severity severity, String message, OsmPrimitive primitive)55 public TestError(Test tester, Severity severity, String message, OsmPrimitive primitive) 48 56 { 57 this.tester = tester; 49 58 this.severity = severity; 50 59 this.message = message; … … 54 63 55 64 this.primitives = primitives; 65 } 66 67 /** 68 * Constructor 69 * @param tester The tester 70 * @param severity The severity of this error 71 * @param message The error message 72 * @param primitive The affected primitive 73 * @param internalCode The internal code 74 */ 75 public TestError(Test tester, Severity severity, String message, OsmPrimitive primitive, int internalCode) 76 { 77 this(tester, severity, message, primitive); 78 this.internalCode = internalCode; 56 79 } 57 80 … … 78 101 * @return the list of primitives affected by this error 79 102 */ 80 public List< ? extendsOsmPrimitive> getPrimitives()103 public List<OsmPrimitive> getPrimitives() 81 104 { 82 105 return primitives; … … 110 133 this.severity = severity; 111 134 } 135 136 /** 137 * Gets the tester that raised this error 138 * @return the tester that raised this error 139 */ 140 public Test getTester() 141 { 142 return tester; 143 } 144 145 /** 146 * Gets the internal code 147 * @return the internal code 148 */ 149 public int getInternalCode() 150 { 151 return internalCode; 152 } 153 154 155 /** 156 * Sets the internal code 157 * @param internalCode The internal code 158 */ 159 public void setInternalCode(int internalCode) 160 { 161 this.internalCode = internalCode; 162 } 163 164 /** 165 * Returns true if the error can be fixed automatically 166 * 167 * @return true if the error can be fixed 168 */ 169 public boolean isFixable() 170 { 171 return tester != null && tester.isFixable(this); 172 } 173 174 /** 175 * Fixes the error with the appropiate command 176 * 177 * @return The command to fix the error 178 */ 179 public Command getFix() 180 { 181 if( tester == null ) 182 return null; 183 184 return tester.fixError(this); 185 } 112 186 } -
applications/editors/josm/plugins/validator/src/org/openstreetmap/josm/plugins/validator/ValidateAction.java
r2453 r2591 24 24 private static final long serialVersionUID = -2304521273582574603L; 25 25 26 /** Last selection used to validate */ 27 private Collection<OsmPrimitive> lastSelection; 28 26 29 /** 27 30 * Constructor … … 34 37 public void actionPerformed(ActionEvent ev) 35 38 { 39 doValidate(ev, true); 40 } 41 42 /** 43 * Does the validation. 44 * <p> 45 * If getSelectedItems is true, the selected items (or all items, if no one 46 * is selected) are validated. If it is false, last selected items are 47 * revalidated 48 * 49 * @param ev The event 50 * @param getSelectedItems If selected or last selected items must be validated 51 */ 52 public void doValidate(ActionEvent ev, boolean getSelectedItems) 53 { 36 54 OSMValidatorPlugin plugin = OSMValidatorPlugin.getPlugin(); 37 55 plugin.errors = new ArrayList<TestError>(); … … 42 60 return; 43 61 44 Collection<OsmPrimitive> selection = Main.ds.getSelected(); 45 if( selection.isEmpty() ) 46 selection = Main.ds.allNonDeletedPrimitives(); 62 Collection<OsmPrimitive> selection; 63 if( getSelectedItems ) 64 { 65 selection = Main.ds.getSelected(); 66 if( selection.isEmpty() ) 67 { 68 selection = Main.ds.allNonDeletedPrimitives(); 69 lastSelection = null; 70 } 71 else 72 { 73 AgregatePrimitivesVisitor v = new AgregatePrimitivesVisitor(); 74 selection = v.visit(selection); 75 lastSelection = selection; 76 } 77 } 47 78 else 48 79 { 49 AgregatePrimitivesVisitor v = new AgregatePrimitivesVisitor(); 50 selection = v.visit(selection); 80 if( lastSelection == null ) 81 selection = Main.ds.allNonDeletedPrimitives(); 82 else 83 selection = lastSelection; 51 84 } 52 85 53 86 for(Test test : tests) 54 87 { 88 test.setPartialSelection(lastSelection != null); 55 89 test.startTest(); 56 90 test.visit(selection); -
applications/editors/josm/plugins/validator/src/org/openstreetmap/josm/plugins/validator/ValidatorDialog.java
r2453 r2591 9 9 import java.util.Map.Entry; 10 10 11 import javax.swing. JPanel;12 import javax.swing. JScrollPane;13 import javax.swing. JTree;11 import javax.swing.*; 12 import javax.swing.event.TreeSelectionEvent; 13 import javax.swing.event.TreeSelectionListener; 14 14 import javax.swing.tree.*; 15 15 16 16 import org.openstreetmap.josm.Main; 17 import org.openstreetmap.josm.command.Command; 18 import org.openstreetmap.josm.command.SequenceCommand; 17 19 import org.openstreetmap.josm.data.osm.OsmPrimitive; 18 20 import org.openstreetmap.josm.gui.dialogs.ToggleDialog; … … 41 43 */ 42 44 protected JTree tree = new JTree(treeModel); 45 46 /** 47 * The fix button 48 */ 49 private JButton fixButton; 43 50 44 51 … … 54 61 tree.expandRow(0); 55 62 tree.setVisibleRowCount(8); 56 tree.addMouseListener(new DblClickWatch()); 63 tree.addMouseListener(new ClickWatch()); 64 tree.addTreeSelectionListener(new SelectionWatch()); 57 65 tree.setCellRenderer(new ErrorTreeRenderer()); 58 66 tree.getSelectionModel().setSelectionMode(TreeSelectionModel.DISCONTIGUOUS_TREE_SELECTION); … … 65 73 add(buttonPanel, BorderLayout.SOUTH); 66 74 buttonPanel.add(Util.createButton("Validate", "dialogs/refresh", "Validate the data.", this)); 75 add(buttonPanel, BorderLayout.SOUTH); 76 fixButton = Util.createButton("Fix", "dialogs/fix", "Fix the selected errors.", this); 77 fixButton.setEnabled(false); 78 buttonPanel.add(fixButton); 67 79 add(buttonPanel, BorderLayout.SOUTH); 68 80 } … … 136 148 } 137 149 150 /** 151 * Fix selected errors 152 * @param e 153 */ 154 @SuppressWarnings("unchecked") 155 private void fixErrors(ActionEvent e) 156 { 157 DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent(); 158 if( node == null ) 159 return; 160 161 Bag<String, Command> commands = new Bag<String, Command>(); 162 163 Enumeration<DefaultMutableTreeNode> children = node.breadthFirstEnumeration(); 164 while( children.hasMoreElements() ) 165 { 166 DefaultMutableTreeNode childNode = children.nextElement(); 167 Object nodeInfo = childNode.getUserObject(); 168 if( nodeInfo instanceof TestError) 169 { 170 TestError error = (TestError)nodeInfo; 171 Command fixCommand = error.getFix(); 172 if( fixCommand != null ) 173 { 174 commands.add(error.getMessage(), fixCommand); 175 } 176 } 177 } 178 179 Command fixCommand = null; 180 if( commands.size() == 0 ) 181 return; 182 else if( commands.size() > 1 ) 183 { 184 List<Command> allComands = new ArrayList<Command>(50); 185 for( Entry<String, List<Command>> errorType : commands.entrySet()) 186 { 187 String description = errorType.getKey(); 188 List<Command> errorCommands = errorType.getValue(); 189 allComands.add( new SequenceCommand("Fix " + description, errorCommands) ); 190 } 191 192 fixCommand = new SequenceCommand("Fix errors", allComands); 193 } 194 else 195 { 196 for( Entry<String, List<Command>> errorType : commands.entrySet()) 197 { 198 String description = errorType.getKey(); 199 List<Command> errorCommands = errorType.getValue(); 200 fixCommand = new SequenceCommand("Fix " + description, errorCommands); 201 } 202 } 203 204 Main.main.editLayer().add( fixCommand ); 205 Main.map.repaint(); 206 Main.ds.fireSelectionChanged(Main.ds.getSelected()); 207 208 OSMValidatorPlugin.getPlugin().validateAction.doValidate(e, false); 209 } 210 138 211 /** 139 212 * Sets the selection of the map to the current selected items. 140 213 */ 141 214 @SuppressWarnings("unchecked") 142 p ublicvoid setSelectedItems()215 private void setSelectedItems() 143 216 { 144 217 Collection<OsmPrimitive> sel = new HashSet<OsmPrimitive>(40); … … 160 233 } 161 234 } 235 162 236 Main.ds.setSelected(sel); 163 237 } … … 165 239 public void actionPerformed(ActionEvent e) 166 240 { 167 if( e.getActionCommand().equals("Select")) 241 String actionCommand = e.getActionCommand(); 242 if( actionCommand.equals("Select")) 168 243 setSelectedItems(); 169 else if( e.getActionCommand().equals("Validate"))244 else if( actionCommand.equals("Validate")) 170 245 OSMValidatorPlugin.getPlugin().validateAction.actionPerformed(e); 246 else if( actionCommand.equals("Fix")) 247 fixErrors(e); 248 } 249 250 /** 251 * Refresh the error messages display 252 */ 253 public void refresh() 254 { 255 buildTree(); 171 256 } 172 257 173 /** 174 * Refresh the error messages display 175 */ 176 public void refresh() 177 { 178 buildTree(); 179 } 180 181 /** 182 * Watches for double clicks and from editing or new property, depending on the 183 * location, the click was. 184 * @author imi 185 */ 186 public class DblClickWatch extends MouseAdapter 187 { 188 @SuppressWarnings("unchecked") 258 /** 259 * Checks for fixes in selected element and, if needed, adds to the sel parameter all selected elements 260 * @param sel The collection where to add all selected elements 261 * @param addSelected if true, add all selected elements to collection 262 * @return whether the selected elements has any fix 263 */ 264 @SuppressWarnings("unchecked") 265 private boolean setSelection(Collection<OsmPrimitive> sel, boolean addSelected) 266 { 267 boolean hasFixes = false; 268 269 DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent(); 270 if( node == null ) 271 return hasFixes; 272 273 Enumeration<DefaultMutableTreeNode> children = node.breadthFirstEnumeration(); 274 while( children.hasMoreElements() ) 275 { 276 DefaultMutableTreeNode childNode = children.nextElement(); 277 Object nodeInfo = childNode.getUserObject(); 278 if( nodeInfo instanceof TestError) 279 { 280 TestError error = (TestError)nodeInfo; 281 hasFixes = hasFixes || error.isFixable(); 282 if( addSelected ) 283 { 284 sel.addAll( error.getPrimitives() ); 285 } 286 } 287 } 288 289 return hasFixes; 290 } 291 292 /** 293 * Watches for clicks. 294 */ 295 public class ClickWatch extends MouseAdapter 296 { 189 297 @Override 190 298 public void mouseClicked(MouseEvent e) 191 299 { 192 if (e.getClickCount() < 2 || e.getSource() instanceof JScrollPane) 300 fixButton.setEnabled(false); 301 302 if(e.getSource() instanceof JScrollPane) 193 303 return; 194 else 304 305 DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent(); 306 if( node == null ) 307 return; 308 309 Collection<OsmPrimitive> sel = new HashSet<OsmPrimitive>(40); 310 boolean isDblClick = e.getClickCount() > 1; 311 312 boolean hasFixes = setSelection(sel, isDblClick); 313 fixButton.setEnabled(hasFixes); 314 315 if( isDblClick) 195 316 { 196 Collection<OsmPrimitive> sel = new HashSet<OsmPrimitive>(40);197 198 DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent();199 Enumeration<DefaultMutableTreeNode> children = node.breadthFirstEnumeration();200 while( children.hasMoreElements() )201 {202 DefaultMutableTreeNode childNode = children.nextElement();203 Object nodeInfo = childNode.getUserObject();204 if( nodeInfo instanceof TestError)205 {206 TestError error = (TestError)nodeInfo;207 sel.addAll( error.getPrimitives() );208 }209 }210 317 Main.ds.setSelected(sel); 211 318 } 212 319 } 213 320 } 321 322 /** 323 * Watches for tree selection. 324 */ 325 public class SelectionWatch implements TreeSelectionListener 326 { 327 @SuppressWarnings("unchecked") 328 public void valueChanged(TreeSelectionEvent e) 329 { 330 fixButton.setEnabled(false); 331 332 if(e.getSource() instanceof JScrollPane) 333 return; 334 335 DefaultMutableTreeNode node = (DefaultMutableTreeNode)tree.getLastSelectedPathComponent(); 336 if( node == null ) 337 return; 338 339 boolean hasFixes = setSelection(null, false); 340 fixButton.setEnabled(hasFixes); 341 } 342 } 214 343 } -
applications/editors/josm/plugins/validator/src/org/openstreetmap/josm/plugins/validator/tests/DuplicateNode.java
r2453 r2591 5 5 import java.util.List; 6 6 7 import org.openstreetmap.josm.command.Command; 8 import org.openstreetmap.josm.command.DeleteCommand; 7 9 import org.openstreetmap.josm.data.coor.LatLon; 8 10 import org.openstreetmap.josm.data.osm.Node; 11 import org.openstreetmap.josm.data.osm.OsmPrimitive; 9 12 import org.openstreetmap.josm.plugins.validator.Severity; 10 13 import org.openstreetmap.josm.plugins.validator.Test; … … 19 22 { 20 23 /** Bag of all nodes */ 21 Bag<LatLon, Node> nodes;24 Bag<LatLon, OsmPrimitive> nodes; 22 25 23 26 /** … … 34 37 public void startTest() 35 38 { 36 nodes = new Bag<LatLon, Node>(1000);39 nodes = new Bag<LatLon, OsmPrimitive>(1000); 37 40 } 38 41 … … 40 43 public void endTest() 41 44 { 42 for(List< Node> duplicated : nodes.values() )45 for(List<OsmPrimitive> duplicated : nodes.values() ) 43 46 { 44 47 if( duplicated.size() > 1) 45 48 { 46 errors.add( new TestError(Severity.ERROR, tr("Duplicated nodes"), duplicated) ); 49 TestError testError = new TestError(this, Severity.ERROR, tr("Duplicated nodes"), duplicated); 50 errors.add( testError ); 47 51 } 48 52 } … … 55 59 nodes.add(n.coor, n); 56 60 } 61 62 @Override 63 public Command fixError(TestError testError) 64 { 65 //TODO Which should be the fix? 66 return new DeleteCommand(testError.getPrimitives()); 67 } 68 69 @Override 70 public boolean isFixable(TestError testError) 71 { 72 return false; //(testError.getTester() instanceof DuplicateNode); 73 } 57 74 } -
applications/editors/josm/plugins/validator/src/org/openstreetmap/josm/plugins/validator/tests/DuplicateSegment.java
r2453 r2591 6 6 7 7 import org.openstreetmap.josm.data.coor.LatLon; 8 import org.openstreetmap.josm.data.osm.OsmPrimitive; 8 9 import org.openstreetmap.josm.data.osm.Segment; 9 10 import org.openstreetmap.josm.plugins.validator.Severity; … … 19 20 { 20 21 /** Bag of all segments */ 21 Bag<CompoundLatLon, Segment> segments;22 Bag<CompoundLatLon, OsmPrimitive> segments; 22 23 23 24 /** … … 35 36 public void startTest() 36 37 { 37 segments = new Bag<CompoundLatLon, Segment>(1000);38 segments = new Bag<CompoundLatLon, OsmPrimitive>(1000); 38 39 } 39 40 … … 41 42 public void endTest() 42 43 { 43 for(List< Segment> duplicated : segments.values() )44 for(List<OsmPrimitive> duplicated : segments.values() ) 44 45 { 45 46 if( duplicated.size() > 1) 46 47 { 47 errors.add( new TestError( Severity.ERROR, tr("Duplicated segments"), duplicated) );48 errors.add( new TestError(this, Severity.ERROR, tr("Duplicated segments"), duplicated) ); 48 49 } 49 50 } -
applications/editors/josm/plugins/validator/src/org/openstreetmap/josm/plugins/validator/tests/OrphanSegment.java
r2453 r2591 43 43 for(Segment segment : segments ) 44 44 { 45 errors.add( new TestError( Severity.OTHER, tr("Segments not in a way"), segment) );45 errors.add( new TestError(this, Severity.OTHER, tr("Segments not in a way"), segment) ); 46 46 } 47 47 segments = null; -
applications/editors/josm/plugins/validator/src/org/openstreetmap/josm/plugins/validator/tests/SingleNodeSegment.java
r2453 r2591 3 3 import static org.openstreetmap.josm.tools.I18n.tr; 4 4 5 import org.openstreetmap.josm.command.Command; 6 import org.openstreetmap.josm.command.DeleteCommand; 5 7 import org.openstreetmap.josm.data.osm.Segment; 6 8 import org.openstreetmap.josm.plugins.validator.Severity; … … 31 33 if( !s.incomplete && s.from.equals(s.to) ) 32 34 { 33 errors.add( new TestError( Severity.ERROR, tr("Single node segments"), s) );35 errors.add( new TestError(this, Severity.ERROR, tr("Single node segments"), s) ); 34 36 } 35 37 } 38 39 @Override 40 public Command fixError(TestError testError) 41 { 42 return new DeleteCommand(testError.getPrimitives()); 43 } 44 45 @Override 46 public boolean isFixable(TestError testError) 47 { 48 return (testError.getTester() instanceof SingleNodeSegment); 49 } 36 50 } -
applications/editors/josm/plugins/validator/src/org/openstreetmap/josm/plugins/validator/tests/SpellCheck.java
r2453 r2591 13 13 14 14 import org.openstreetmap.josm.Main; 15 import org.openstreetmap.josm.command.ChangePropertyCommand; 16 import org.openstreetmap.josm.command.Command; 17 import org.openstreetmap.josm.command.SequenceCommand; 15 18 import org.openstreetmap.josm.data.osm.*; 16 19 import org.openstreetmap.josm.gui.annotation.AnnotationPreset; … … 45 48 protected JCheckBox prefCheckValues; 46 49 50 /** Empty values error */ 51 protected static int EMPTY_VALUES = 0; 52 /** Invalid key error */ 53 protected static int INVALID_KEY = 1; 54 /** Invalid value error */ 55 protected static int INVALID_VALUE = 2; 56 47 57 /** 48 58 * Constructor … … 161 171 if( (value==null || value.trim().length() == 0) && !withErrors.contains(p, "EV")) 162 172 { 163 errors.add( new TestError( Severity.WARNING, tr("Tags with empty value"), p) );173 errors.add( new TestError(this, Severity.WARNING, tr("Tags with empty values"), p, EMPTY_VALUES) ); 164 174 withErrors.add(p, "EV"); 165 175 } 166 176 if( spellCheckKeyData.containsKey(key) && !withErrors.contains(p, "IPK")) 167 177 { 168 errors.add( new TestError( Severity.WARNING, tr("Invalid property key"), p) );178 errors.add( new TestError(this, Severity.WARNING, tr("Invalid property keys"), p, INVALID_KEY) ); 169 179 withErrors.add(p, "IPK"); 170 180 } … … 174 184 if( values != null && !values.contains(prop.getValue()) && !withErrors.contains(p, "UPV")) 175 185 { 176 errors.add( new TestError( Severity.OTHER, tr("Unknown property value"), p) );186 errors.add( new TestError(this, Severity.OTHER, tr("Unknown property values"), p, INVALID_VALUE) ); 177 187 withErrors.add(p, "UPV"); 178 188 } … … 277 287 Main.pref.put(PREF_CHECK_VALUES, prefCheckValues.isSelected()); 278 288 } 289 290 @Override 291 public Command fixError(TestError testError) 292 { 293 List<Command> commands = new ArrayList<Command>(50); 294 295 int i = -1; 296 List<OsmPrimitive> primitives = testError.getPrimitives(); 297 for(OsmPrimitive p : primitives ) 298 { 299 i++; 300 Map<String, String> tags = p.keys; 301 if( tags == null || tags.size() == 0 ) 302 continue; 303 304 for(Entry<String, String> prop: tags.entrySet() ) 305 { 306 String key = prop.getKey(); 307 String value = prop.getValue(); 308 if( value == null || value.trim().length() == 0 ) 309 commands.add( new ChangePropertyCommand(primitives.subList(i, i+1), key, null) ); 310 else 311 { 312 String replacementKey = spellCheckKeyData.get(key); 313 if( replacementKey != null ) 314 commands.add( new ChangePropertyKeyCommand(primitives.subList(i, i+1), key, replacementKey) ); 315 } 316 } 317 } 318 319 return commands.size() > 1 ? new SequenceCommand("Fix properties", commands) : commands.get(0); 320 } 321 322 @Override 323 public boolean isFixable(TestError testError) 324 { 325 if( testError.getTester() instanceof SpellCheck) 326 { 327 int code = testError.getInternalCode(); 328 return code == INVALID_KEY || code == EMPTY_VALUES; 329 } 330 331 return false; 332 } 279 333 } 280 334 -
applications/editors/josm/plugins/validator/src/org/openstreetmap/josm/plugins/validator/tests/TaggedSegment.java
r2453 r2591 3 3 import static org.openstreetmap.josm.tools.I18n.tr; 4 4 5 import java.util. Map;5 import java.util.*; 6 6 7 import org.openstreetmap.josm.command.*; 8 import org.openstreetmap.josm.data.osm.OsmPrimitive; 7 9 import org.openstreetmap.josm.data.osm.Segment; 8 10 import org.openstreetmap.josm.plugins.validator.Severity; … … 35 37 if( tags == null ) 36 38 return; 39 tags = new HashMap<String, String>(tags); 40 for( String tag : allowedTags) 41 tags.remove(tag); 37 42 38 int numAllowedTags = 0; 39 for( String tag : allowedTags) 40 if( tags.containsKey(tag) ) numAllowedTags++; 41 42 if( tags.size() - numAllowedTags > 0 ) 43 if( tags.size() > 0 ) 43 44 { 44 errors.add( new TestError( Severity.WARNING, tr("Segments with tags"), s) );45 errors.add( new TestError(this, Severity.WARNING, tr("Segments with tags"), s) ); 45 46 } 46 47 } 48 49 @Override 50 public Command fixError(TestError testError) 51 { 52 List<Command> commands = new ArrayList<Command>(50); 53 54 int i = -1; 55 List<OsmPrimitive> primitives = testError.getPrimitives(); 56 for(OsmPrimitive p : primitives ) 57 { 58 i++; 59 Map<String, String> tags = p.keys; 60 if( tags == null ) 61 continue; 62 63 tags = new HashMap<String, String>(tags); 64 for( String tag : allowedTags) 65 tags.remove(tag); 66 67 if( tags.size() == 0 ) 68 continue; 69 70 for(String key : tags.keySet() ) 71 { 72 commands.add( new ChangePropertyCommand(primitives.subList(i, i+1), key, null) ); 73 } 74 } 75 76 return commands.size() > 1 ? new SequenceCommand("Remove keys", commands) : commands.get(0); 77 } 78 79 @Override 80 public boolean isFixable(TestError testError) 81 { 82 return (testError.getTester() instanceof TaggedSegment); 83 } 47 84 } -
applications/editors/josm/plugins/validator/src/org/openstreetmap/josm/plugins/validator/tests/UnorderedWay.java
r2453 r2591 3 3 import static org.openstreetmap.josm.tools.I18n.tr; 4 4 5 import java.util.*; 6 7 import org.openstreetmap.josm.actions.ReorderAction; 8 import org.openstreetmap.josm.command.*; 9 import org.openstreetmap.josm.data.osm.OsmPrimitive; 5 10 import org.openstreetmap.josm.data.osm.Segment; 6 11 import org.openstreetmap.josm.data.osm.Way; … … 33 38 if( last != null && !last.incomplete && !s.incomplete && !last.to.equals(s.from) ) 34 39 { 35 errors.add( new TestError( Severity.WARNING, tr("Unordered ways"), w) );40 errors.add( new TestError(this, Severity.WARNING, tr("Unordered ways"), w) ); 36 41 break; 37 42 } … … 39 44 } 40 45 } 46 47 @Override 48 public Command fixError(TestError testError) 49 { 50 List<Command> commands = new ArrayList<Command>(50); 51 52 for(OsmPrimitive p : testError.getPrimitives() ) 53 { 54 Way w = (Way)p; 55 Way newWay = new Way(w); 56 newWay.segments.clear(); 57 newWay.segments.addAll(ReorderAction.sortSegments(new HashSet<Segment>(w.segments))); 58 return new ChangeCommand(p, newWay); 59 } 60 61 return commands.size() > 1 ? new SequenceCommand("Remove keys", commands) : commands.get(0); 62 } 63 64 @Override 65 public boolean isFixable(TestError testError) 66 { 67 return (testError.getTester() instanceof UnorderedWay); 68 } 41 69 } -
applications/editors/josm/plugins/validator/src/org/openstreetmap/josm/plugins/validator/tests/UntaggedNode.java
r2453 r2591 3 3 import static org.openstreetmap.josm.tools.I18n.tr; 4 4 5 import java.util.HashSet; 6 import java.util.Map; 7 import java.util.Set; 5 import java.util.*; 8 6 7 import org.openstreetmap.josm.Main; 8 import org.openstreetmap.josm.command.Command; 9 import org.openstreetmap.josm.command.DeleteCommand; 9 10 import org.openstreetmap.josm.data.osm.Node; 11 import org.openstreetmap.josm.data.osm.OsmPrimitive; 10 12 import org.openstreetmap.josm.data.osm.Segment; 11 13 import org.openstreetmap.josm.plugins.validator.Severity; … … 19 21 public class UntaggedNode extends Test 20 22 { 21 /** Tags allowed in a segment*/23 /** Tags allowed in a node */ 22 24 public static String[] allowedTags = new String[] { "created_by" }; 23 25 24 26 /** Bag of all nodes */ 25 27 Set<Node> emptyNodes; 26 28 27 29 /** 28 30 * Constructor … … 40 42 } 41 43 44 @Override 45 public void visit(Collection<OsmPrimitive> selection) 46 { 47 // If there is a partial selection, it may be false positives if a 48 // node is selected, but not the container segment. So, in this 49 // case, we must visit all segments, selected or not. 50 51 for (OsmPrimitive p : selection) 52 { 53 if( !p.deleted ) 54 { 55 if( !partialSelection || p instanceof Node ) 56 p.visit(this); 57 } 58 } 59 60 if( partialSelection ) 61 { 62 for( Segment s : Main.ds.segments) 63 visit(s); 64 } 65 } 66 42 67 @Override 43 68 public void visit(Node n) … … 70 95 for(Node node : emptyNodes) 71 96 { 72 errors.add( new TestError( Severity.OTHER, tr("Untagged and unconnected nodes"), node) );97 errors.add( new TestError(this, Severity.OTHER, tr("Untagged and unconnected nodes"), node) ); 73 98 } 74 99 emptyNodes = null; 75 100 } 101 102 @Override 103 public Command fixError(TestError testError) 104 { 105 return new DeleteCommand(testError.getPrimitives()); 106 } 107 108 @Override 109 public boolean isFixable(TestError testError) 110 { 111 return (testError.getTester() instanceof UntaggedNode); 112 } 76 113 }
Note:
See TracChangeset
for help on using the changeset viewer.