Changeset 16047 in josm
- Timestamp:
- 2020-03-06T09:02:24+01:00 (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/validation/tests/MapCSSTagChecker.java
r16039 r16047 6 6 import java.awt.geom.Area; 7 7 import java.io.BufferedReader; 8 import java.io.File; 8 9 import java.io.IOException; 9 10 import java.io.InputStream; … … 18 19 import java.util.List; 19 20 import java.util.Map; 21 import java.util.Map.Entry; 20 22 import java.util.Objects; 21 23 import java.util.Optional; … … 67 69 import org.openstreetmap.josm.tools.Logging; 68 70 import org.openstreetmap.josm.tools.MultiMap; 71 import org.openstreetmap.josm.tools.Stopwatch; 69 72 import org.openstreetmap.josm.tools.Utils; 70 73 … … 75 78 public class MapCSSTagChecker extends Test.TagTest { 76 79 private MapCSSStyleIndex indexData; 77 final Map<MapCSSRule, MapCSSTagCheckerAndRule> ruleToCheckMap = new HashMap<>(); 78 private final Set<OsmPrimitive> tested = new HashSet<>(); 80 private final Map<MapCSSRule, MapCSSTagCheckerAndRule> ruleToCheckMap = new HashMap<>(); 79 81 private static final Map<IPrimitive, Area> mpAreaCache = new HashMap<>(); 80 82 static final boolean ALL_TESTS = true; … … 203 205 204 206 final MultiMap<String, TagCheck> checks = new MultiMap<>(); 207 208 /** maps the source URL for a test to the title shown in the dialog where known */ 209 private final Map<String, String> urlTitles = new HashMap<>(); 205 210 206 211 /** … … 547 552 final String description1 = group == null ? description : group; 548 553 final String description2 = group == null ? null : description; 554 final String selector = matchingSelector.toString(); 549 555 TestError.Builder errorBuilder = TestError.builder(tester, getSeverity(), 3000) 550 .messageWithManuallyTranslatedDescription(description1, description2, matchingSelector.toString());556 .messageWithManuallyTranslatedDescription(description1, description2, selector); 551 557 if (fix != null) { 552 558 errorBuilder.fix(() -> fix); … … 558 564 if (c instanceof OsmPrimitive) { 559 565 errorBuilder = TestError.builder(tester, getSeverity(), 3000) 560 .messageWithManuallyTranslatedDescription(description1, description2, 561 matchingSelector.toString()); 566 .messageWithManuallyTranslatedDescription(description1, description2, selector); 562 567 if (fix != null) { 563 568 errorBuilder.fix(() -> fix); … … 618 623 @Override 619 624 public String getSource() { 620 return tr("URL / File: {0}", source);625 return source; 621 626 } 622 627 } … … 662 667 .filter(c -> c.rule.declaration == rule.declaration) 663 668 .findFirst() 664 .map(c -> new MapCSSTagCheckerAndRule(c, e.getKey())) 669 .map(c -> new MapCSSTagCheckerAndRule(c, getTitle(e.getKey()))) 665 670 .orElse(null)) 666 671 .filter(Objects::nonNull) … … 681 686 } 682 687 688 private String getTitle(String url) { 689 return urlTitles.getOrDefault(url, tr("unknown")); 690 } 691 683 692 /** 684 693 * See #12627 … … 750 759 addIfNotSimilar(e, errors); 751 760 } 752 if (partialSelection) {753 tested.add(p);754 }755 761 } 756 762 … … 788 794 checks.remove(url); 789 795 checks.putAll(url, result.parseChecks); 796 urlTitles.put(url, findURLTitle(url)); 790 797 indexData = null; 791 798 } 792 799 return result; 800 } 801 802 /** Find a user friendly string for the url. 803 * 804 * @param url the source for the set of rules 805 * @return a value that can be used in tool tip or progress bar. 806 */ 807 private static String findURLTitle(String url) { 808 for (SourceEntry source : new ValidatorPrefHelper().get()) { 809 if (url.equals(source.url) && source.title != null && !source.title.isEmpty()) { 810 return source.title; 811 } 812 } 813 if (url.endsWith(".mapcss")) // do we have others? 814 url = new File(url).getName(); 815 if (url.length() > 33) { 816 url = "..." + url.substring(url.length() - 30); 817 } 818 return url; 793 819 } 794 820 … … 796 822 public synchronized void initialize() throws Exception { 797 823 checks.clear(); 824 urlTitles.clear(); 798 825 indexData = null; 799 826 for (SourceEntry source : new ValidatorPrefHelper().get()) { … … 843 870 super.startTest(progressMonitor); 844 871 super.setShowElements(true); 845 if (indexData == null) {846 indexData = createMapCSSTagCheckerIndex(checks, includeOtherSeverityChecks(), ALL_TESTS);847 }848 tested.clear();849 mpAreaCache.clear();850 872 } 851 873 852 874 @Override 853 875 public synchronized void endTest() { 876 // no need to keep the index, it is quickly build and doubles the memory needs 877 indexData = null; 878 // always clear the cache to make sure that we catch changes in geometry 879 mpAreaCache.clear(); 880 ruleToCheckMap.clear(); 881 super.endTest(); 882 } 883 884 @Override 885 public void visit(Collection<OsmPrimitive> selection) { 886 if (progressMonitor != null) { 887 progressMonitor.setTicksCount(selection.size() * checks.size()); 888 } 889 890 mpAreaCache.clear(); 891 892 Set<OsmPrimitive> surrounding = new HashSet<>(); 893 for (Entry<String, Set<TagCheck>> entry : checks.entrySet()) { 894 if (isCanceled()) { 895 break; 896 } 897 visit(entry.getKey(), entry.getValue(), selection, surrounding); 898 } 899 } 900 901 /** 902 * Perform the checks for one check url 903 * @param url the url for the checks 904 * @param checksForUrl the checks to perform 905 * @param selection collection primitives 906 * @param surrounding surrounding primitives, evtl. filled by this routine 907 */ 908 private void visit(String url, Set<TagCheck> checksForUrl, Collection<OsmPrimitive> selection, 909 Set<OsmPrimitive> surrounding) { 910 MultiMap<String, TagCheck> currentCheck = new MultiMap<>(); 911 currentCheck.putAll(url, checksForUrl); 912 indexData = createMapCSSTagCheckerIndex(currentCheck, includeOtherSeverityChecks(), ALL_TESTS); 913 Set<OsmPrimitive> tested = new HashSet<>(); 914 915 916 String title = getTitle(url); 917 if (progressMonitor != null) { 918 progressMonitor.setExtraText(tr(" {0}", title)); 919 } 920 long cnt = 0; 921 Stopwatch stopwatch = Stopwatch.createStarted(); 922 for (OsmPrimitive p : selection) { 923 if (isCanceled()) { 924 break; 925 } 926 if (isPrimitiveUsable(p)) { 927 check(p); 928 if (partialSelection) { 929 tested.add(p); 930 } 931 } 932 if (progressMonitor != null) { 933 progressMonitor.worked(1); 934 cnt++; 935 // add frequently changing info to progress monitor so that it 936 // doesn't seem to hang when test takes longer than 0.5 seconds 937 if (cnt % 10000 == 0 && stopwatch.elapsed() >= 500) { 938 progressMonitor.setExtraText(tr(" {0}: {1} of {2} elements done", title, cnt, selection.size())); 939 } 940 } 941 } 942 854 943 if (partialSelection && !tested.isEmpty()) { 855 944 // #14287: see https://josm.openstreetmap.de/ticket/14287#comment:15 … … 858 947 // rebuild index with a reduced set of rules (those that use ChildOrParentSelector) and thus may have left selectors 859 948 // matching the previously tested elements 860 indexData = createMapCSSTagCheckerIndex(checks, includeOtherSeverityChecks(), ONLY_SELECTED_TESTS); 861 862 Set<OsmPrimitive> surrounding = new HashSet<>(); 863 for (OsmPrimitive p : tested) { 864 if (p.getDataSet() != null) { 865 surrounding.addAll(p.getDataSet().searchWays(p.getBBox())); 866 surrounding.addAll(p.getDataSet().searchRelations(p.getBBox())); 867 } 868 } 949 indexData = createMapCSSTagCheckerIndex(currentCheck, includeOtherSeverityChecks(), ONLY_SELECTED_TESTS); 950 951 if (surrounding.isEmpty()) { 952 for (OsmPrimitive p : tested) { 953 if (p.getDataSet() != null) { 954 surrounding.addAll(p.getDataSet().searchWays(p.getBBox())); 955 surrounding.addAll(p.getDataSet().searchRelations(p.getBBox())); 956 } 957 } 958 } 959 869 960 final boolean includeOtherSeverity = includeOtherSeverityChecks(); 870 961 for (OsmPrimitive p : surrounding) { … … 877 968 } 878 969 } 879 tested.clear(); 880 } 881 // no need to keep the index, it is quickly build and doubles the memory needs 882 indexData = null; 883 // always clear the cache to make sure that we catch changes in geometry 884 mpAreaCache.clear(); 885 ruleToCheckMap.clear(); 886 super.endTest(); 970 } 887 971 } 888 972 }
Note:
See TracChangeset
for help on using the changeset viewer.