OsmAnd/OsmAnd-java/src/net/osmand/render/RenderingRule.java

230 lines
5.7 KiB
Java
Raw Normal View History

package net.osmand.render;
import java.util.ArrayList;
import java.util.Collections;
2014-09-13 12:26:42 +02:00
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
2014-06-08 21:01:46 +02:00
import net.osmand.util.Algorithms;
public class RenderingRule {
private RenderingRuleProperty[] properties;
private int[] intProperties;
private RenderingRule[] attributesRef;
private float[] floatProperties;
private List<RenderingRule> ifElseChildren;
private List<RenderingRule> ifChildren;
2014-09-13 12:26:42 +02:00
private boolean isGroup;
private final RenderingRulesStorage storage;
2014-09-13 12:26:42 +02:00
private Map<String, String> attributes;
2014-09-13 12:26:42 +02:00
public RenderingRule(Map<String, String> attributes, boolean isGroup, RenderingRulesStorage storage){
this.isGroup = isGroup;
this.storage = storage;
2014-09-13 12:26:42 +02:00
init(attributes);
}
public void storeAttributes(Map<String, String> attributes){
this.attributes = new HashMap<String, String>(attributes);
}
public Map<String, String> getAttributes() {
return attributes == null ? Collections.EMPTY_MAP : attributes;
}
2014-09-13 12:26:42 +02:00
public void init(Map<String, String> attributes) {
ArrayList<RenderingRuleProperty> props = new ArrayList<RenderingRuleProperty>(attributes.size());
intProperties = new int[attributes.size()];
2014-09-13 12:26:42 +02:00
floatProperties = null;
attributesRef = null;
int i = 0;
Iterator<Entry<String, String>> it = attributes.entrySet().iterator();
while (it.hasNext()) {
Entry<String, String> e = it.next();
RenderingRuleProperty property = storage.PROPS.get(e.getKey());
if (property != null) {
props.add(property);
String vl = e.getValue();
if(vl.startsWith("$")){
if (attributesRef == null) {
attributesRef = new RenderingRule[attributes.size()];
}
attributesRef[i] = storage.getRenderingAttributeRule(vl.substring(1));
} else if (property.isString()) {
intProperties[i] = storage.getDictionaryValue(vl);
} else if (property.isFloat()) {
if (floatProperties == null) {
// lazy creates
floatProperties = new float[attributes.size()];
}
floatProperties[i] = property.parseFloatValue(vl);
intProperties[i] = property.parseIntValue(vl);
} else {
intProperties[i] = property.parseIntValue(vl);
}
i++;
}
}
properties = props.toArray(new RenderingRuleProperty[props.size()]);
}
private int getPropertyIndex(String property){
for (int i = 0; i < properties.length; i++) {
RenderingRuleProperty prop = properties[i];
if (prop.getAttrName().equals(property)) {
return i;
}
}
return -1;
}
public String getStringPropertyValue(String property) {
int i = getPropertyIndex(property);
if(i >= 0){
return storage.getStringValue(intProperties[i]);
}
return null;
}
public float getFloatPropertyValue(String property) {
int i = getPropertyIndex(property);
if(i >= 0){
return floatProperties[i];
}
return 0;
}
public String getColorPropertyValue(String property) {
int i = getPropertyIndex(property);
if(i >= 0){
2014-06-08 21:01:46 +02:00
return Algorithms.colorToString(intProperties[i]);
}
return null;
}
public int getIntPropertyValue(String property) {
int i = getPropertyIndex(property);
if(i >= 0){
return intProperties[i];
}
return -1;
}
protected int getIntProp(int ind){
return intProperties[ind];
}
protected RenderingRule getAttrProp(int ind) {
if(attributesRef == null) {
return null;
}
return attributesRef[ind];
}
protected float getFloatProp(int ind){
return floatProperties[ind];
}
public RenderingRuleProperty[] getProperties() {
return properties;
}
@SuppressWarnings("unchecked")
public List<RenderingRule> getIfChildren() {
return ifChildren != null ? ifChildren : Collections.EMPTY_LIST ;
}
@SuppressWarnings("unchecked")
public List<RenderingRule> getIfElseChildren() {
return ifElseChildren != null ? ifElseChildren : Collections.EMPTY_LIST ;
}
public void addIfChildren(RenderingRule rr){
if(ifChildren == null){
ifChildren = new ArrayList<RenderingRule>();
}
ifChildren.add(rr);
}
public void addIfElseChildren(RenderingRule rr){
if(ifElseChildren == null){
ifElseChildren = new ArrayList<RenderingRule>();
}
ifElseChildren.add(rr);
}
2015-01-08 22:53:25 +01:00
public void addToBeginIfElseChildren(RenderingRule rr){
if(ifElseChildren == null){
ifElseChildren = new ArrayList<RenderingRule>();
}
ifElseChildren.add(0, rr);
}
2014-09-13 12:26:42 +02:00
public boolean isGroup() {
return isGroup;
}
@Override
public String toString() {
StringBuilder bls = new StringBuilder();
toString("", bls);
return bls.toString();
}
public StringBuilder toString(String indent, StringBuilder bls ) {
2014-09-13 12:26:42 +02:00
if(isGroup){
bls.append("switch test [");
} else {
bls.append(" test [");
}
2014-08-21 22:39:36 +02:00
printAttrs(bls, true);
bls.append("]");
bls.append(" set [");
printAttrs(bls, false);
bls.append("]");
for(RenderingRule rc : getIfElseChildren()){
2014-09-13 12:26:42 +02:00
String cindent = indent + "* case ";
2014-08-21 22:39:36 +02:00
bls.append("\n").append(cindent);
rc.toString(indent + "* ", bls);
}
for(RenderingRule rc : getIfChildren()){
2014-09-13 12:26:42 +02:00
String cindent = indent + "* apply " ;
2014-08-21 22:39:36 +02:00
bls.append("\n").append(cindent);
rc.toString(indent + "* ", bls);
}
return bls;
}
2014-09-13 12:26:42 +02:00
2014-08-21 22:39:36 +02:00
protected void printAttrs(StringBuilder bls, boolean in) {
for(RenderingRuleProperty p : getProperties()){
2014-08-21 22:39:36 +02:00
if(p.isInputProperty() != in) {
continue;
}
bls.append(" ").append(p.getAttrName()).append("= ");
if(p.isString()){
bls.append("\"").append(getStringPropertyValue(p.getAttrName())).append("\"");
} else if(p.isFloat()){
bls.append(getFloatPropertyValue(p.getAttrName()));
} else if(p.isColor()){
bls.append(getColorPropertyValue(p.getAttrName()));
} else if(p.isIntParse()){
bls.append(getIntPropertyValue(p.getAttrName()));
}
}
}
}