1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools.template_engine;
|
---|
3 |
|
---|
4 | import java.util.Collection;
|
---|
5 |
|
---|
6 | public class Variable implements TemplateEntry {
|
---|
7 |
|
---|
8 | private static final String SPECIAL_VARIABLE_PREFIX = "special:";
|
---|
9 | private static final String SPECIAL_VALUE_EVERYTHING = "everything";
|
---|
10 |
|
---|
11 |
|
---|
12 | private final String variableName;
|
---|
13 | private final boolean special;
|
---|
14 |
|
---|
15 | public Variable(String variableName) {
|
---|
16 | if (variableName.toLowerCase().startsWith(SPECIAL_VARIABLE_PREFIX)) {
|
---|
17 | this.variableName = variableName.substring(SPECIAL_VARIABLE_PREFIX.length());
|
---|
18 | // special:special:key means that real key named special:key is needed, not special variable
|
---|
19 | this.special = !this.variableName.toLowerCase().startsWith(SPECIAL_VARIABLE_PREFIX);
|
---|
20 | } else {
|
---|
21 | this.variableName = variableName;
|
---|
22 | this.special = false;
|
---|
23 | }
|
---|
24 | }
|
---|
25 |
|
---|
26 | @Override
|
---|
27 | public void appendText(StringBuilder result, TemplateEngineDataProvider dataProvider) {
|
---|
28 | if (special && SPECIAL_VALUE_EVERYTHING.equals(variableName)) {
|
---|
29 | Collection<String> keys = dataProvider.getTemplateKeys();
|
---|
30 | boolean first = true;
|
---|
31 | for (String key: keys) {
|
---|
32 | if (!first) {
|
---|
33 | result.append(", ");
|
---|
34 | } else {
|
---|
35 | first = false;
|
---|
36 | }
|
---|
37 | result.append(key).append("=").append(dataProvider.getTemplateValue(key, false));
|
---|
38 | }
|
---|
39 | } else {
|
---|
40 | Object value = dataProvider.getTemplateValue(variableName, special);
|
---|
41 | if (value != null) {
|
---|
42 | result.append(value);
|
---|
43 | }
|
---|
44 | }
|
---|
45 | }
|
---|
46 |
|
---|
47 | @Override
|
---|
48 | public boolean isValid(TemplateEngineDataProvider dataProvider) {
|
---|
49 | if (special && SPECIAL_VALUE_EVERYTHING.equals(variableName))
|
---|
50 | return true;
|
---|
51 | else
|
---|
52 | return dataProvider.getTemplateValue(variableName, special) != null;
|
---|
53 | }
|
---|
54 |
|
---|
55 | @Override
|
---|
56 | public String toString() {
|
---|
57 | return "{" + variableName + "}";
|
---|
58 | }
|
---|
59 |
|
---|
60 | public boolean isSpecial() {
|
---|
61 | return special;
|
---|
62 | }
|
---|
63 | }
|
---|