add select="when-matched" rule option

This commit is contained in:
Hannes Janetzek 2014-03-10 01:15:49 +01:00
parent ca1a7b90d7
commit 61ca022d5b
2 changed files with 45 additions and 17 deletions

View File

@ -23,12 +23,13 @@ import org.oscim.theme.styles.RenderStyle;
public abstract class Rule { public abstract class Rule {
private final Rule[] subRules; private final Rule[] subRules;
private final RenderStyle[] styles; private final RenderStyle[] styles;
private final int zoom; private final int zoom;
private final int element; private final int element;
private final boolean selectFirstMatch;
private final boolean selectWhenMatched;
Rule(int element, int zoom, int selector, Rule[] subRules, RenderStyle[] styles) { Rule(int element, int zoom, int selector, Rule[] subRules, RenderStyle[] styles) {
this.element = element; this.element = element;
@ -36,36 +37,55 @@ public abstract class Rule {
this.subRules = subRules; this.subRules = subRules;
this.styles = styles; this.styles = styles;
selectFirstMatch = (selector & Selector.FIRST) != 0;
selectWhenMatched = (selector & Selector.WHEN_MATCHED) != 0;
} }
abstract boolean matchesTags(Tag[] tags); abstract boolean matchesTags(Tag[] tags);
public boolean matchElement(int type, Tag[] tags, int zoomLevel, public boolean matchElement(int type, Tag[] tags, int zoomLevel, List<RenderStyle> result) {
List<RenderStyle> matchingList) {
if (((mElement & type) != 0) && ((mZoom & zoomLevel) != 0) && (matchesTags(tags))) {
if (((element & type) != 0) && ((zoom & zoomLevel) != 0) && (matchesTags(tags))) {
boolean matched = false; boolean matched = false;
// check subrules if (subRules != null) {
for (Rule subRule : mSubRules) { if (selectFirstMatch) {
if (subRule.matchElement(type, tags, zoomLevel, matchingList) && mMatchFirst) { /* only add first matching rule and when-matched rules iff a
* previous rule matched */
for (Rule r : subRules) {
/* continue if matched xor selectWhenMatch */
if (matched ^ r.selectWhenMatched)
continue;
if (r.matchElement(type, tags, zoomLevel, result))
matched = true; matched = true;
break; }
} else {
/* add all rules and when-matched rules iff a previous rule
* matched */
for (Rule r : subRules) {
if (r.selectWhenMatched && !matched)
continue;
if (r.matchElement(type, tags, zoomLevel, result))
matched = true;
}
} }
} }
if (!mMatchFirst || matched) { if (styles == null)
// add instructions for this rule /* matched if styles where added */
for (RenderStyle ri : mRenderInstructions) return matched;
matchingList.add(ri);
}
// this rule did match /* add instructions for this rule */
for (RenderStyle ri : styles)
result.add(ri);
/* this rule did not match */
return true; return true;
} }
// this rule did not match /* this rule did not match */
return false; return false;
} }

View File

@ -0,0 +1,8 @@
package org.oscim.theme.rule;
public class Selector {
public static final int ANY = 0;
public static final int FIRST = 1 << 0;
public static final int WHEN_MATCHED = 1 << 1;
}