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 | public interface Instruction extends StyleKeys {
|
---|
14 |
|
---|
15 | void execute(Environment env);
|
---|
16 |
|
---|
17 | class RelativeFloat {
|
---|
18 | public final float val;
|
---|
19 |
|
---|
20 | public RelativeFloat(float val) {
|
---|
21 | this.val = val;
|
---|
22 | }
|
---|
23 |
|
---|
24 | @Override
|
---|
25 | public String toString() {
|
---|
26 | return "RelativeFloat{" + "val=" + val + '}';
|
---|
27 | }
|
---|
28 | }
|
---|
29 |
|
---|
30 | class AssignmentInstruction implements Instruction {
|
---|
31 | public final String key;
|
---|
32 | public final Object val;
|
---|
33 | public final boolean isSetInstruction;
|
---|
34 |
|
---|
35 | public AssignmentInstruction(String key, Object val, boolean isSetInstruction) {
|
---|
36 | this.key = key;
|
---|
37 | this.isSetInstruction = isSetInstruction;
|
---|
38 | if (val instanceof LiteralExpression) {
|
---|
39 | Object litValue = ((LiteralExpression) val).evaluate(null);
|
---|
40 | if (litValue instanceof Keyword && "none".equals(((Keyword) litValue).val)) {
|
---|
41 | this.val = null;
|
---|
42 | } else if (TEXT.equals(key)) {
|
---|
43 | /* Special case for declaration 'text: ...'
|
---|
44 | *
|
---|
45 | * - Treat the value 'auto' as keyword.
|
---|
46 | * - Treat any other literal value 'litval' as as reference to tag with key 'litval'
|
---|
47 | *
|
---|
48 | * - Accept function expressions as is. This allows for
|
---|
49 | * tag(a_tag_name) value of a tag
|
---|
50 | * eval("a static text") a static text
|
---|
51 | * parent_tag(a_tag_name) value of a tag of a parent relation
|
---|
52 | */
|
---|
53 | if (litValue.equals(Keyword.AUTO)) {
|
---|
54 | this.val = Keyword.AUTO;
|
---|
55 | } else {
|
---|
56 | String s = Cascade.convertTo(litValue, String.class);
|
---|
57 | if (s != null) {
|
---|
58 | this.val = new MapPaintStyles.TagKeyReference(s);
|
---|
59 | } else {
|
---|
60 | this.val = litValue;
|
---|
61 | }
|
---|
62 | }
|
---|
63 | } else {
|
---|
64 | this.val = litValue;
|
---|
65 | }
|
---|
66 | } else {
|
---|
67 | this.val = val;
|
---|
68 | }
|
---|
69 | }
|
---|
70 |
|
---|
71 | @Override
|
---|
72 | public void execute(Environment env) {
|
---|
73 | Object value;
|
---|
74 | if (val instanceof Expression) {
|
---|
75 | value = ((Expression) val).evaluate(env);
|
---|
76 | } else {
|
---|
77 | value = val;
|
---|
78 | }
|
---|
79 | if (ICON_IMAGE.equals(key) || FILL_IMAGE.equals(key) || REPEAT_IMAGE.equals(key)) {
|
---|
80 | if (value instanceof String) {
|
---|
81 | value = new IconReference((String) value, env.source);
|
---|
82 | }
|
---|
83 | }
|
---|
84 | env.mc.getOrCreateCascade(env.layer).putOrClear(key, value);
|
---|
85 | }
|
---|
86 |
|
---|
87 | @Override
|
---|
88 | public String toString() {
|
---|
89 | return key + ": " + (val instanceof float[] ? Arrays.toString((float[]) val) :
|
---|
90 | (val instanceof String ? ("String<"+val+'>') : val)) + ';';
|
---|
91 | }
|
---|
92 | }
|
---|
93 | }
|
---|