source: josm/trunk/src/org/openstreetmap/josm/tools/template_engine/Variable.java@ 13003

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

see #14794 - add missing top level javadoc; minor refactoring for Condition

  • Property svn:eol-style set to native
File size: 2.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools.template_engine;
3
4import java.util.Collection;
5import java.util.Locale;
6
7/**
8 * {@link TemplateEntry} that inserts the value of a variable.
9 * <p>
10 * Variables starting with "special:" form a separate namespace and
11 * provide actions other than simple key-value lookup.
12 * <p>
13 * A variable with no mapping for a given data provider will be considered not "valid"
14 * (see {@link TemplateEntry#isValid(TemplateEngineDataProvider)}).
15 */
16public class Variable implements TemplateEntry {
17
18 private static final String SPECIAL_VARIABLE_PREFIX = "special:";
19 private static final String SPECIAL_VALUE_EVERYTHING = "everything";
20
21 private final String variableName;
22 private final boolean special;
23
24 /**
25 * Constructs a new {@code Variable}.
26 * @param variableName the variable name (i.e. the key in the data provider key-value mapping);
27 * will be considered "special" if the variable name starts with {@link SPECIAL_VARIABLE_PREFIX}
28 */
29 public Variable(String variableName) {
30 if (variableName.toLowerCase(Locale.ENGLISH).startsWith(SPECIAL_VARIABLE_PREFIX)) {
31 this.variableName = variableName.substring(SPECIAL_VARIABLE_PREFIX.length());
32 // special:special:key means that real key named special:key is needed, not special variable
33 this.special = !this.variableName.toLowerCase(Locale.ENGLISH).startsWith(SPECIAL_VARIABLE_PREFIX);
34 } else {
35 this.variableName = variableName;
36 this.special = false;
37 }
38 }
39
40 @Override
41 public void appendText(StringBuilder result, TemplateEngineDataProvider dataProvider) {
42 if (special && SPECIAL_VALUE_EVERYTHING.equals(variableName)) {
43 Collection<String> keys = dataProvider.getTemplateKeys();
44 boolean first = true;
45 for (String key: keys) {
46 if (!first) {
47 result.append(", ");
48 } else {
49 first = false;
50 }
51 result.append(key).append('=').append(dataProvider.getTemplateValue(key, false));
52 }
53 } else {
54 Object value = dataProvider.getTemplateValue(variableName, special);
55 if (value != null) {
56 result.append(value);
57 }
58 }
59 }
60
61 @Override
62 public boolean isValid(TemplateEngineDataProvider dataProvider) {
63 if (special && SPECIAL_VALUE_EVERYTHING.equals(variableName))
64 return true;
65 else
66 return dataProvider.getTemplateValue(variableName, special) != null;
67 }
68
69 @Override
70 public String toString() {
71 return '{' + (special ? SPECIAL_VARIABLE_PREFIX : "") + variableName + '}';
72 }
73
74 /**
75 * Check if this variable is special.
76 *
77 * @return true if this variable is special
78 */
79 public boolean isSpecial() {
80 return special;
81 }
82}
Note: See TracBrowser for help on using the repository browser.