source: josm/trunk/src/org/openstreetmap/josm/gui/mappaint/mapcss/Instruction.java@ 12259

Last change on this file since 12259 was 12259, checked in by bastiK, 7 years ago

see #14794 - javadoc

  • Property svn:eol-style set to native
File size: 3.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.mappaint.mapcss;
3
4import java.util.Arrays;
5
6import org.openstreetmap.josm.gui.mappaint.Cascade;
7import org.openstreetmap.josm.gui.mappaint.Environment;
8import org.openstreetmap.josm.gui.mappaint.Keyword;
9import org.openstreetmap.josm.gui.mappaint.MapPaintStyles;
10import org.openstreetmap.josm.gui.mappaint.MapPaintStyles.IconReference;
11import 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 MapCSSRule.Declaration} is a list of instructions.
19 */
20@FunctionalInterface
21public 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 class RelativeFloat {
30 public final float val;
31
32 public RelativeFloat(float val) {
33 this.val = val;
34 }
35
36 @Override
37 public String toString() {
38 return "RelativeFloat{" + "val=" + val + '}';
39 }
40 }
41
42 class AssignmentInstruction implements Instruction {
43 public final String key;
44 public final Object val;
45 public final boolean isSetInstruction;
46
47 public AssignmentInstruction(String key, Object val, boolean isSetInstruction) {
48 this.key = key;
49 this.isSetInstruction = isSetInstruction;
50 if (val instanceof LiteralExpression) {
51 Object litValue = ((LiteralExpression) val).evaluate(null);
52 if (litValue instanceof Keyword && "none".equals(((Keyword) litValue).val)) {
53 this.val = null;
54 } else if (TEXT.equals(key)) {
55 /* Special case for declaration 'text: ...'
56 *
57 * - Treat the value 'auto' as keyword.
58 * - Treat any other literal value 'litval' as as reference to tag with key 'litval'
59 *
60 * - Accept function expressions as is. This allows for
61 * tag(a_tag_name) value of a tag
62 * eval("a static text") a static text
63 * parent_tag(a_tag_name) value of a tag of a parent relation
64 */
65 if (litValue.equals(Keyword.AUTO)) {
66 this.val = Keyword.AUTO;
67 } else {
68 String s = Cascade.convertTo(litValue, String.class);
69 if (s != null) {
70 this.val = new MapPaintStyles.TagKeyReference(s);
71 } else {
72 this.val = litValue;
73 }
74 }
75 } else {
76 this.val = litValue;
77 }
78 } else {
79 this.val = val;
80 }
81 }
82
83 @Override
84 public void execute(Environment env) {
85 Object value;
86 if (val instanceof Expression) {
87 value = ((Expression) val).evaluate(env);
88 } else {
89 value = val;
90 }
91 if (ICON_IMAGE.equals(key) || FILL_IMAGE.equals(key) || REPEAT_IMAGE.equals(key)) {
92 if (value instanceof String) {
93 value = new IconReference((String) value, env.source);
94 }
95 }
96 env.mc.getOrCreateCascade(env.layer).putOrClear(key, value);
97 }
98
99 @Override
100 public String toString() {
101 return key + ": " + (val instanceof float[] ? Arrays.toString((float[]) val) :
102 (val instanceof String ? ("String<"+val+'>') : val)) + ';';
103 }
104 }
105}
Note: See TracBrowser for help on using the repository browser.