- Timestamp:
- 2019-05-21T20:24:59+02:00 (6 years ago)
- Location:
- trunk/src/org/openstreetmap/josm/data/validation/tests
- Files:
-
- 1 added
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/validation/tests/MapCSSTagChecker.java
r15103 r15104 38 38 import org.openstreetmap.josm.data.coor.LatLon; 39 39 import org.openstreetmap.josm.data.osm.DataSet; 40 import org.openstreetmap.josm.data.osm.INode;41 40 import org.openstreetmap.josm.data.osm.IPrimitive; 42 import org.openstreetmap.josm.data.osm.IRelation;43 import org.openstreetmap.josm.data.osm.IWay;44 41 import org.openstreetmap.josm.data.osm.OsmPrimitive; 45 42 import org.openstreetmap.josm.data.osm.OsmUtils; … … 86 83 import org.openstreetmap.josm.tools.GeoPropertyIndex; 87 84 import org.openstreetmap.josm.tools.I18n; 88 import org.openstreetmap.josm.tools.JosmRuntimeException;89 85 import org.openstreetmap.josm.tools.Logging; 90 86 import org.openstreetmap.josm.tools.MultiMap; … … 97 93 */ 98 94 public class MapCSSTagChecker extends Test.TagTest { 99 IndexDataindexData;95 MapCSSTagCheckerIndex indexData; 100 96 final Set<OsmPrimitive> tested = new HashSet<>(); 101 97 102 private static final boolean ALL_TESTS = true;103 private static final boolean ONLY_SELECTED_TESTS = false;104 105 /**106 * Helper class to store indexes of rules.107 * @author Gerd108 *109 */110 private static final class IndexData {111 final Map<MapCSSRule, TagCheck> ruleToCheckMap = new HashMap<>();112 113 /**114 * Rules for nodes115 */116 final MapCSSRuleIndex nodeRules = new MapCSSRuleIndex();117 /**118 * Rules for ways without tag area=no119 */120 final MapCSSRuleIndex wayRules = new MapCSSRuleIndex();121 /**122 * Rules for ways with tag area=no123 */124 final MapCSSRuleIndex wayNoAreaRules = new MapCSSRuleIndex();125 /**126 * Rules for relations that are not multipolygon relations127 */128 final MapCSSRuleIndex relationRules = new MapCSSRuleIndex();129 /**130 * Rules for multipolygon relations131 */132 final MapCSSRuleIndex multipolygonRules = new MapCSSRuleIndex();133 134 private IndexData(MultiMap<String, TagCheck> checks, boolean includeOtherSeverity, boolean allTests) {135 buildIndex(checks, includeOtherSeverity, allTests);136 }137 138 private void buildIndex(MultiMap<String, TagCheck> checks, boolean includeOtherSeverity, boolean allTests) {139 List<TagCheck> allChecks = new ArrayList<>();140 for (Set<TagCheck> cs : checks.values()) {141 allChecks.addAll(cs);142 }143 144 ruleToCheckMap.clear();145 nodeRules.clear();146 wayRules.clear();147 wayNoAreaRules.clear();148 relationRules.clear();149 multipolygonRules.clear();150 151 // optimization: filter rules for different primitive types152 for (TagCheck c : allChecks) {153 if (!includeOtherSeverity && Severity.OTHER == c.getSeverity()154 && c.setClassExpressions.isEmpty()) {155 // Ignore "information" level checks if not wanted, unless they also set a MapCSS class156 continue;157 }158 159 for (Selector s : c.rule.selectors) {160 // find the rightmost selector, this must be a GeneralSelector161 boolean hasLeftRightSel = false;162 Selector selRightmost = s;163 while (selRightmost instanceof Selector.ChildOrParentSelector) {164 hasLeftRightSel = true;165 selRightmost = ((Selector.ChildOrParentSelector) selRightmost).right;166 }167 if (!allTests && !hasLeftRightSel) {168 continue;169 }170 171 MapCSSRule optRule = new MapCSSRule(s.optimizedBaseCheck(), c.rule.declaration);172 173 ruleToCheckMap.put(optRule, c);174 final String base = ((GeneralSelector) selRightmost).getBase();175 switch (base) {176 case Selector.BASE_NODE:177 nodeRules.add(optRule);178 break;179 case Selector.BASE_WAY:180 wayNoAreaRules.add(optRule);181 wayRules.add(optRule);182 break;183 case Selector.BASE_AREA:184 wayRules.add(optRule);185 multipolygonRules.add(optRule);186 break;187 case Selector.BASE_RELATION:188 relationRules.add(optRule);189 multipolygonRules.add(optRule);190 break;191 case Selector.BASE_ANY:192 nodeRules.add(optRule);193 wayRules.add(optRule);194 wayNoAreaRules.add(optRule);195 relationRules.add(optRule);196 multipolygonRules.add(optRule);197 break;198 case Selector.BASE_CANVAS:199 case Selector.BASE_META:200 case Selector.BASE_SETTING:201 break;202 default:203 final RuntimeException e = new JosmRuntimeException(MessageFormat.format("Unknown MapCSS base selector {0}", base));204 Logging.warn(tr("Failed to index validator rules. Error was: {0}", e.getMessage()));205 Logging.error(e);206 }207 }208 }209 nodeRules.initIndex();210 wayRules.initIndex();211 wayNoAreaRules.initIndex();212 relationRules.initIndex();213 multipolygonRules.initIndex();214 }215 216 /**217 * Get the index of rules for the given primitive.218 * @param p the primitve219 * @return index of rules for the given primitive220 */221 public MapCSSRuleIndex get(OsmPrimitive p) {222 if (p instanceof INode) {223 return nodeRules;224 } else if (p instanceof IWay) {225 if (OsmUtils.isFalse(p.get("area"))) {226 return wayNoAreaRules;227 } else {228 return wayRules;229 }230 } else if (p instanceof IRelation) {231 if (((IRelation<?>) p).isMultipolygon()) {232 return multipolygonRules;233 } else {234 return relationRules;235 }236 } else {237 throw new IllegalArgumentException("Unsupported type: " + p);238 }239 }240 241 /**242 * return the TagCheck for which the given indexed rule was created.243 * @param rule an indexed rule244 * @return the original TagCheck245 */246 public TagCheck getCheck(MapCSSRule rule) {247 return ruleToCheckMap.get(rule);248 }249 }250 98 251 99 /** … … 860 708 public synchronized Collection<TestError> getErrorsForPrimitive(OsmPrimitive p, boolean includeOtherSeverity) { 861 709 final List<TestError> res = new ArrayList<>(); 862 if (indexData == null) 863 indexData = new IndexData(checks, includeOtherSeverity, ALL_TESTS); 710 if (indexData == null) { 711 indexData = new MapCSSTagCheckerIndex(checks, includeOtherSeverity, MapCSSTagCheckerIndex.ALL_TESTS); 712 } 864 713 865 714 MapCSSRuleIndex matchingRuleIndex = indexData.get(p); … … 1140 989 super.setShowElements(true); 1141 990 if (indexData == null) { 1142 indexData = new IndexData(checks, includeOtherSeverityChecks(),ALL_TESTS);991 indexData = new MapCSSTagCheckerIndex(checks, includeOtherSeverityChecks(), MapCSSTagCheckerIndex.ALL_TESTS); 1143 992 } 1144 993 tested.clear(); … … 1153 1002 // rebuild index with a reduced set of rules (those that use ChildOrParentSelector) and thus may have left selectors 1154 1003 // matching the previously tested elements 1155 indexData = new IndexData(checks, includeOtherSeverityChecks(),ONLY_SELECTED_TESTS);1004 indexData = new MapCSSTagCheckerIndex(checks, includeOtherSeverityChecks(), MapCSSTagCheckerIndex.ONLY_SELECTED_TESTS); 1156 1005 1157 1006 Set<OsmPrimitive> surrounding = new HashSet<>(); … … 1164 1013 final boolean includeOtherSeverity = includeOtherSeverityChecks(); 1165 1014 for (OsmPrimitive p : surrounding) { 1166 if (tested.contains(p) 1015 if (tested.contains(p)) 1167 1016 continue; 1168 1017 Collection<TestError> additionalErrors = getErrorsForPrimitive(p, includeOtherSeverity);
Note:
See TracChangeset
for help on using the changeset viewer.