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

Last change on this file since 7937 was 7937, checked in by bastiK, 9 years ago

add subversion property svn:eol=native

  • Property svn:eol-style set to native
File size: 2.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.tools.template_engine;
3
4import java.util.Collection;
5
6public 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}
Note: See TracBrowser for help on using the repository browser.