Changeset 6677 in josm


Ignore:
Timestamp:
2014-01-12T23:04:43+01:00 (11 years ago)
Author:
simon04
Message:

see #9414 fix #9566: MapCSS validator: allow error message to access tags of primitive

For instance, throwWarning: tr("{0} is bad", tag("highway")).

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/validation/tests/MapCSSTagChecker.java

    r6675 r6677  
    7474        protected final Map<String, String> keyChange = new LinkedHashMap<String, String>();
    7575        protected final List<String> alternatives = new ArrayList<String>();
    76         protected final Map<String, Severity> errors = new HashMap<String, Severity>();
     76        protected final Map<Instruction.AssignmentInstruction, Severity> errors = new HashMap<Instruction.AssignmentInstruction, Severity>();
    7777        protected final Map<String, Boolean> assertions = new HashMap<String, Boolean>();
    7878
     
    131131                    if (ai.key.startsWith("throw")) {
    132132                        final Severity severity = Severity.valueOf(ai.key.substring("throw".length()).toUpperCase());
    133                         check.errors.put(val, severity);
     133                        check.errors.put(ai, severity);
    134134                    } else if ("fixAdd".equals(ai.key)) {
    135135                        final PrimitiveToTag toTag = PrimitiveToTag.ofMapCSSObject(ai.val, false);
     
    296296                cmds.add(new ChangePropertyKeyCommand(p, oldKey, newKey));
    297297            }
    298             return new SequenceCommand(tr("Fix of {0}", getDescriptionForMatchingSelector(matchingSelector)), cmds);
     298            return new SequenceCommand(tr("Fix of {0}", getDescriptionForMatchingSelector(p, matchingSelector)), cmds);
    299299        }
    300300
     
    304304         * @return a message
    305305         */
    306         String getMessage() {
    307             return errors.isEmpty() ? null : errors.keySet().iterator().next();
     306        String getMessage(OsmPrimitive p) {
     307            if (errors.isEmpty()) {
     308                return null;
     309            } else {
     310                final Object val = errors.keySet().iterator().next().val;
     311                return String.valueOf(
     312                        val instanceof Expression
     313                                ? ((Expression) val).evaluate(new Environment().withPrimitive(p))
     314                                : val
     315                );
     316            }
    308317        }
    309318
     
    312321         *
    313322         * @return a description (possibly with alternative suggestions)
    314          * @see #getDescriptionForMatchingSelector(Selector)
    315          */
    316         String getDescription() {
     323         * @see #getDescriptionForMatchingSelector
     324         */
     325        String getDescription(OsmPrimitive p) {
    317326            if (alternatives.isEmpty()) {
    318                 return getMessage();
     327                return getMessage(p);
    319328            } else {
    320329                /* I18N: {0} is the test error message and {1} is an alternative */
    321                 return tr("{0}, use {1} instead", getMessage(), Utils.join(tr(" or "), alternatives));
     330                return tr("{0}, use {1} instead", getMessage(p), Utils.join(tr(" or "), alternatives));
    322331            }
    323332        }
     
    329338         * @return a description (possibly with alternative suggestions)
    330339         */
    331         String getDescriptionForMatchingSelector(Selector matchingSelector) {
    332             return insertArguments(matchingSelector, getDescription());
     340        String getDescriptionForMatchingSelector(OsmPrimitive p, Selector matchingSelector) {
     341            return insertArguments(matchingSelector, getDescription(p));
    333342        }
    334343
     
    339348        @Override
    340349        public String toString() {
    341             return getDescription();
     350            return getDescription(null);
    342351        }
    343352
     
    355364            if (matchingSelector != null && !errors.isEmpty()) {
    356365                final Command fix = fixPrimitive(p);
    357                 final String description = getDescriptionForMatchingSelector(matchingSelector);
     366                final String description = getDescriptionForMatchingSelector(p, matchingSelector);
    358367                if (fix != null) {
    359368                    return new FixableTestError(null, getSeverity(), description, null, matchingSelector.toString(), 3000, p, fix);
  • trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/ExpressionFactory.java

    r6615 r6677  
    468468         * and the subsequent arguments are parameters for the string indicated by {@code {0}}, {@code {1}}, …
    469469         */
     470        @NullableArguments
    470471        public static String tr(String... args) {
    471472            final String text = args[0];
  • trunk/test/unit/org/openstreetmap/josm/data/validation/tests/MapCSSTagCheckerTest.java

    r6636 r6677  
    3838        final List<MapCSSTagChecker.TagCheck> checks = MapCSSTagChecker.TagCheck.readMapCSS(new StringReader("" +
    3939                "*[natural=marsh] {\n" +
    40                 "   throwWarning: tr(\"{0} is deprecated\", \"{0.tag}\");\n" +
     40                "   throwWarning: tr(\"{0}={1} is deprecated\", \"{0.key}\", tag(\"natural\"));\n" +
    4141                "   fixRemove: \"{0.key}\";\n" +
    4242                "   fixAdd: \"natural=wetland\";\n" +
     
    4646        final MapCSSTagChecker.TagCheck check = checks.get(0);
    4747        assertThat(check, notNullValue());
    48         assertThat(check.getDescription(), is("{0.tag} is deprecated"));
     48        assertThat(check.getDescription(null), is("{0.key}=null is deprecated"));
    4949        assertThat(check.change.get(0).apply(null), is(new Tag("{0.key}")));
    5050        assertThat(check.change.get(1).apply(null), is(new Tag("natural", "wetland")));
     
    8383                if (isError != i.getValue()) {
    8484                    final String error = MessageFormat.format("Expecting test ''{0}'' (i.e., {1}) to {2} {3} (i.e., {4})",
    85                             check.getMessage(), check.rule.selectors, i.getValue() ? "match" : "not match", i.getKey(), p.getKeys());
     85                            check.getMessage(p), check.rule.selectors, i.getValue() ? "match" : "not match", i.getKey(), p.getKeys());
    8686                    System.err.println(error);
    8787                    assertionErrors.add(error);
Note: See TracChangeset for help on using the changeset viewer.