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 {
private final Rule[] subRules;
private final RenderStyle[] styles;
private final int zoom;
private final int element;
private final boolean selectFirstMatch;
private final boolean selectWhenMatched;
Rule(int element, int zoom, int selector, Rule[] subRules, RenderStyle[] styles) {
this.element = element;
@ -36,36 +37,55 @@ public abstract class Rule {
this.subRules = subRules;
this.styles = styles;
selectFirstMatch = (selector & Selector.FIRST) != 0;
selectWhenMatched = (selector & Selector.WHEN_MATCHED) != 0;
}
abstract boolean matchesTags(Tag[] tags);
public boolean matchElement(int type, Tag[] tags, int zoomLevel,
List<RenderStyle> matchingList) {
if (((mElement & type) != 0) && ((mZoom & zoomLevel) != 0) && (matchesTags(tags))) {
public boolean matchElement(int type, Tag[] tags, int zoomLevel, List<RenderStyle> result) {
if (((element & type) != 0) && ((zoom & zoomLevel) != 0) && (matchesTags(tags))) {
boolean matched = false;
// check subrules
for (Rule subRule : mSubRules) {
if (subRule.matchElement(type, tags, zoomLevel, matchingList) && mMatchFirst) {
matched = true;
break;
if (subRules != null) {
if (selectFirstMatch) {
/* 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;
}
} 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) {
// add instructions for this rule
for (RenderStyle ri : mRenderInstructions)
matchingList.add(ri);
}
if (styles == null)
/* matched if styles where added */
return matched;
// this rule did match
/* add instructions for this rule */
for (RenderStyle ri : styles)
result.add(ri);
/* this rule did not match */
return true;
}
// this rule did not match
/* this rule did not match */
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;
}