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

Last change on this file since 8373 was 8373, checked in by Don-vip, 9 years ago

code style - Method makes literal string comparisons passing the literal as an argument

  • Property svn:eol-style set to native
File size: 3.5 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
13public interface Instruction extends StyleKeys {
14
15 void execute(Environment env);
16
17 public static 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 public static 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 = null;
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}
Note: See TracBrowser for help on using the repository browser.