1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.gui.mappaint.mapcss;
|
---|
3 |
|
---|
4 | import java.util.Arrays;
|
---|
5 |
|
---|
6 | import org.openstreetmap.josm.gui.mappaint.Cascade;
|
---|
7 | import org.openstreetmap.josm.gui.mappaint.Environment;
|
---|
8 | import org.openstreetmap.josm.gui.mappaint.Keyword;
|
---|
9 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
|
---|
10 | import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
|
---|
11 | import org.openstreetmap.josm.gui.mappaint.StyleKeys;
|
---|
12 |
|
---|
13 | /**
|
---|
14 | * A MapCSS Instruction.
|
---|
15 | *
|
---|
16 | * For example a simple assignment like <code>width: 3;</code>, but may also
|
---|
17 | * be a set instruction (<code>set highway;</code>).
|
---|
18 | * A MapCSS {@link Declaration} is a list of instructions.
|
---|
19 | */
|
---|
20 | @FunctionalInterface
|
---|
21 | public interface Instruction extends StyleKeys {
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Execute the instruction in the given environment.
|
---|
25 | * @param env the environment
|
---|
26 | */
|
---|
27 | void execute(Environment env);
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * A float value that will be added to the current float value. Specified as +5 or -3 in MapCSS
|
---|
31 | */
|
---|
32 | class RelativeFloat {
|
---|
33 | public final float val;
|
---|
34 |
|
---|
35 | public RelativeFloat(float val) {
|
---|
36 | this.val = val;
|
---|
37 | }
|
---|
38 |
|
---|
39 | @Override
|
---|
40 | public String toString() {
|
---|
41 | return "RelativeFloat{" + "val=" + val + '}';
|
---|
42 | }
|
---|
43 | }
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * An instruction that assigns a given value to a variable on evaluation
|
---|
47 | */
|
---|
48 | class AssignmentInstruction implements Instruction {
|
---|
49 | public final String key;
|
---|
50 | public final Object val;
|
---|
51 | public final boolean isSetInstruction;
|
---|
52 |
|
---|
53 | public AssignmentInstruction(String key, Object val, boolean isSetInstruction) {
|
---|
54 | this.key = key.intern();
|
---|
55 | this.isSetInstruction = isSetInstruction;
|
---|
56 | if (val instanceof LiteralExpression) {
|
---|
57 | Object litValue = ((LiteralExpression) val).evaluate(null);
|
---|
58 | if (litValue instanceof Keyword && "none".equals(((Keyword) litValue).val)) {
|
---|
59 | this.val = null;
|
---|
60 | } else if (TEXT.equals(key)) {
|
---|
61 | /* Special case for declaration 'text: ...'
|
---|
62 | *
|
---|
63 | * - Treat the value 'auto' as keyword.
|
---|
64 | * - Treat any other literal value 'litval' as as reference to tag with key 'litval'
|
---|
65 | *
|
---|
66 | * - Accept function expressions as is. This allows for
|
---|
67 | * tag(a_tag_name) value of a tag
|
---|
68 | * eval("a static text") a static text
|
---|
69 | * parent_tag(a_tag_name) value of a tag of a parent relation
|
---|
70 | */
|
---|
71 | if (litValue.equals(Keyword.AUTO)) {
|
---|
72 | this.val = Keyword.AUTO;
|
---|
73 | } else {
|
---|
74 | String s = Cascade.convertTo(litValue, String.class);
|
---|
75 | if (s != null) {
|
---|
76 | this.val = new MapPaintStyles.TagKeyReference(s);
|
---|
77 | } else {
|
---|
78 | this.val = litValue;
|
---|
79 | }
|
---|
80 | }
|
---|
81 | } else {
|
---|
82 | this.val = litValue;
|
---|
83 | }
|
---|
84 | } else {
|
---|
85 | this.val = val;
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | @Override
|
---|
90 | public void execute(Environment env) {
|
---|
91 | Object value;
|
---|
92 | if (val instanceof Expression) {
|
---|
93 | value = ((Expression) val).evaluate(env);
|
---|
94 | } else {
|
---|
95 | value = val;
|
---|
96 | }
|
---|
97 | if (ICON_IMAGE.equals(key) || FILL_IMAGE.equals(key) || REPEAT_IMAGE.equals(key)) {
|
---|
98 | if (value instanceof String) {
|
---|
99 | value = new IconReference((String) value, env.source);
|
---|
100 | }
|
---|
101 | }
|
---|
102 | env.mc.getOrCreateCascade(env.layer).putOrClear(key, value);
|
---|
103 | }
|
---|
104 |
|
---|
105 | @Override
|
---|
106 | public String toString() {
|
---|
107 | return key + ": " + (val instanceof float[] ? Arrays.toString((float[]) val) :
|
---|
108 | (val instanceof String ? ("String<"+val+'>') : val)) + ';';
|
---|
109 | }
|
---|
110 | }
|
---|
111 | }
|
---|