OsmAnd/OsmAnd-java/src/main/java/net/osmand/render/RenderingRuleProperty.java

349 lines
9.1 KiB
Java
Raw Normal View History

package net.osmand.render;
import net.osmand.PlatformUtil;
import net.osmand.binary.BinaryMapDataObject;
2015-01-08 01:15:47 +01:00
import net.osmand.binary.BinaryMapIndexReader.TagValuePair;
import org.apache.commons.logging.Log;
public class RenderingRuleProperty {
private final static Log log = PlatformUtil.getLog(RenderingRuleProperty.class);
private final static int INT_TYPE = 1;
private final static int FLOAT_TYPE = 2;
private final static int STRING_TYPE = 3;
private final static int COLOR_TYPE = 4;
private final static int BOOLEAN_TYPE = 5;
public static final int TRUE_VALUE = 1;
public static final int FALSE_VALUE = 0;
// Fields C++
protected final int type;
protected final boolean input;
protected final String attrName;
protected int id = -1;
// use for custom rendering rule properties
protected String name;
protected String description;
2015-06-06 13:01:49 +02:00
protected String defaultValueDescription;
protected String[] possibleValues;
2014-10-26 12:14:25 +01:00
protected String category;
2015-05-01 15:18:56 +02:00
protected RenderingRuleProperty(String attrName, int type, boolean input){
this.attrName = attrName;
this.type = type;
this.input = input;
}
public boolean isInputProperty() {
return input;
}
public boolean isOutputProperty() {
return !input;
}
public void setId(int id) {
if (this.id != -1) {
throw new IllegalArgumentException();
}
this.id = id;
}
public int getId() {
return id;
}
public String getAttrName() {
return attrName;
}
public String getName() {
return name;
}
2014-10-26 12:14:25 +01:00
public String getCategory() {
return category;
}
public String getDescription() {
return description;
}
2015-06-06 13:01:49 +02:00
public String getDefaultValueDescription() {
return defaultValueDescription;
}
protected void setName(String name) {
this.name = name;
}
protected void setDescription(String description) {
this.description = description;
}
2015-06-06 13:01:49 +02:00
public void setDefaultValueDescription(String defaultValueDescription) {
this.defaultValueDescription = defaultValueDescription;
}
2014-10-26 12:14:25 +01:00
public void setCategory(String category) {
this.category = category;
}
protected void setPossibleValues(String[] possibleValues) {
this.possibleValues = possibleValues;
}
public String[] getPossibleValues() {
if (isBoolean()) {
return new String[] { "true", "false" };
}
return possibleValues;
}
public boolean isBoolean() {
return type == BOOLEAN_TYPE;
}
public boolean isFloat() {
return type == FLOAT_TYPE;
}
public boolean isInt() {
return type == INT_TYPE;
}
public boolean isColor() {
return type == COLOR_TYPE;
}
public boolean isString() {
return type == STRING_TYPE;
}
public boolean isIntParse(){
return type == INT_TYPE || type == STRING_TYPE || type == COLOR_TYPE || type == BOOLEAN_TYPE;
}
public boolean accept(int ruleValue, int renderingProperty, RenderingRuleSearchRequest req){
if(!isIntParse() || !input){
return false;
}
return ruleValue == renderingProperty;
}
public boolean accept(float ruleValue, float renderingProperty, RenderingRuleSearchRequest req){
if(type != FLOAT_TYPE || !input){
return false;
}
return ruleValue == renderingProperty;
}
@Override
public String toString() {
return "#RenderingRuleProperty " + getAttrName();
}
public int parseIntValue(String value){
if(type == INT_TYPE){
try {
int colon = value.indexOf(':');
if(colon != -1) {
int c = 0;
2013-09-14 00:37:19 +02:00
if(colon > 0) {
2013-10-16 17:39:58 +02:00
c += (int) Float.parseFloat(value.substring(0, colon));
}
2013-10-16 17:39:58 +02:00
c += (int) Float.parseFloat(value.substring(colon + 1));
return c;
}
2013-10-16 17:39:58 +02:00
return (int) Float.parseFloat(value);
} catch (NumberFormatException e) {
2014-12-07 20:21:42 +01:00
log.error("Rendering parse " + value + " in " + attrName);
}
2014-12-07 20:21:42 +01:00
return -1;
} else if(type == BOOLEAN_TYPE){
return Boolean.parseBoolean(value) ? TRUE_VALUE : FALSE_VALUE;
} else if(type == STRING_TYPE){
// requires dictionary to parse
return -1;
} else if(type == COLOR_TYPE){
try {
return parseColor(value);
} catch (RuntimeException e) {
2014-12-18 18:39:50 +01:00
log.error("Rendering parse " + e.getMessage() + " in " + attrName);
}
return -1;
2013-10-07 23:41:02 +02:00
} else if(type == FLOAT_TYPE){
// parse as complex value
try {
int colon = value.indexOf(':');
if(colon != -1) {
2013-10-16 17:39:58 +02:00
return (int) Float.parseFloat(value.substring(colon + 1));
2013-10-07 23:41:02 +02:00
}
return 0;
} catch (NumberFormatException e) {
2014-12-18 18:39:50 +01:00
log.error("Rendering parse " + value + " in " + attrName);
2013-10-07 23:41:02 +02:00
}
return -1;
} else {
return -1;
}
}
public float parseFloatValue(String value){
if(type == FLOAT_TYPE){
try {
int colon = value.indexOf(':');
if(colon != -1) {
2013-09-14 00:37:19 +02:00
if(colon > 0) {
2013-10-07 23:41:02 +02:00
return Float.parseFloat(value.substring(0, colon));
}
return 0;
}
return Float.parseFloat(value);
} catch (NumberFormatException e) {
2014-12-18 18:39:50 +01:00
log.error("Rendering parse " + value + " in " + attrName);
}
return -1;
} else {
return -1;
}
}
public static RenderingRuleProperty createOutputIntProperty(String name){
return new RenderingRuleProperty(name, INT_TYPE, false);
}
public static RenderingRuleProperty createOutputBooleanProperty(String name){
return new RenderingRuleProperty(name, BOOLEAN_TYPE, false);
}
public static RenderingRuleProperty createInputBooleanProperty(String name){
return new RenderingRuleProperty(name, BOOLEAN_TYPE, true);
}
public static RenderingRuleProperty createOutputFloatProperty(String name){
return new RenderingRuleProperty(name, FLOAT_TYPE, false);
}
public static RenderingRuleProperty createOutputStringProperty(String name){
return new RenderingRuleProperty(name, STRING_TYPE, false);
}
public static RenderingRuleProperty createInputIntProperty(String name){
return new RenderingRuleProperty(name, INT_TYPE, true);
}
public static RenderingRuleProperty createInputColorProperty(String name){
return new RenderingRuleProperty(name, COLOR_TYPE, true);
}
public static RenderingRuleProperty createOutputColorProperty(String name){
return new RenderingRuleProperty(name, COLOR_TYPE, false);
}
public static RenderingRuleProperty createInputStringProperty(String name){
return new RenderingRuleProperty(name, STRING_TYPE, true);
}
public static RenderingRuleProperty createInputLessIntProperty(String name){
return new RenderingRuleProperty(name, INT_TYPE, true) {
@Override
public boolean accept(int ruleValue, int renderingProperty, RenderingRuleSearchRequest req) {
if(!isIntParse() || !input){
return false;
}
return ruleValue >= renderingProperty;
}
};
}
public static RenderingRuleProperty createInputGreaterIntProperty(String name){
return new RenderingRuleProperty(name, INT_TYPE, true) {
@Override
public boolean accept(int ruleValue, int renderingProperty, RenderingRuleSearchRequest req) {
if(!isIntParse() || !input){
return false;
}
return ruleValue <= renderingProperty;
}
};
}
public static RenderingRuleProperty createAdditionalStringProperty(String name) {
return new RenderingRuleProperty(name, STRING_TYPE, true) {
@Override
public boolean accept(int ruleValue, int renderingProperty, RenderingRuleSearchRequest req) {
BinaryMapDataObject obj = req.getObject();
2014-09-05 12:23:57 +02:00
String val = req.getStorage().getStringValue(ruleValue);
if (obj == null) {
2014-09-05 12:23:57 +02:00
int vl = req.getIntPropertyValue(this);
if(vl == -1) {
return false;
}
String val2 = req.getStorage().getStringValue(vl);
2019-07-08 01:30:33 +02:00
return val != null && (val.equals(val2) ||
(val2.indexOf(';') != -1 && val2.contains(val+';')));
}
2014-09-05 12:23:57 +02:00
2015-01-08 01:15:47 +01:00
int k = val.indexOf('=');
if (k != -1) {
String ts = val.substring(0, k);
String vs = val.substring(k + 1);
Integer ruleInd = req.getObject().getMapIndex().getRule(ts, vs);
if (ruleInd != null) {
if (req.getObject().containsAdditionalType(ruleInd)) {
return true;
}
}
2015-01-08 01:15:47 +01:00
} else {
2015-05-14 11:25:58 +02:00
String ts = val;
int[] additionalTypes = obj.getAdditionalTypes();
for (int i = 0; i < additionalTypes.length; i++) {
TagValuePair vp = obj.getMapIndex().decodeType(additionalTypes[i]);
if (vp != null && ts.equals(vp.tag)) {
return true;
2015-01-08 01:15:47 +01:00
}
}
}
return false;
}
};
}
/**
* Parse the color string, and return the corresponding color-int.
* If the string cannot be parsed, throws an IllegalArgumentException
* exception. Supported formats are:
* #RRGGBB
* #AARRGGBB
* 'red', 'blue', 'green', 'black', 'white', 'gray', 'cyan', 'magenta',
* 'yellow', 'lightgray', 'darkgray'
*/
public static int parseColor(String colorString) {
if (colorString.charAt(0) == '#') {
// Use a long to avoid rollovers on #ffXXXXXX
long color = Long.parseLong(colorString.substring(1), 16);
if (colorString.length() == 7) {
// Set the alpha value
color |= 0x00000000ff000000;
} else if (colorString.length() != 9) {
throw new IllegalArgumentException("Unknown color " + colorString); //$NON-NLS-1$
}
return (int)color;
}
throw new IllegalArgumentException("Unknown color " + colorString); //$NON-NLS-1$
}
2014-06-08 21:01:46 +02:00
}