1
2
3
4 package de.powerstat.phplib.templateengine.intern;
5
6
7 import java.util.ArrayList;
8 import java.util.Collections;
9 import java.util.List;
10 import java.util.Map;
11 import java.util.Map.Entry;
12 import java.util.Objects;
13 import java.util.Set;
14 import java.util.TreeSet;
15 import java.util.concurrent.ConcurrentHashMap;
16 import java.util.regex.Pattern;
17
18 import org.apache.logging.log4j.LogManager;
19 import org.apache.logging.log4j.Logger;
20
21
22
23
24
25 public final class VariableManager
26 {
27
28
29
30 private static final Logger LOGGER = LogManager.getLogger(VariableManager.class);
31
32
33
34
35 private static final Pattern TEMPLATE_MATCHER_REGEXP = Pattern.compile("\\{([^{^}\n\r\t :]+)\\}");
36
37
38
39
40 private static final Pattern BLOCK_MATCHER_REGEXP = Pattern.compile("\\{([^ \\t\\r\\n}]+)\\}");
41
42
43
44
45 private final Map<String, String> vars = new ConcurrentHashMap<>();
46
47
48
49
50
51
52
53
54 public VariableManager(final VariableManager vManager)
55 {
56 super();
57 Objects.requireNonNull(vManager, "vManager");
58 for (final Map.Entry<String, String> entry : vManager.vars.entrySet())
59 {
60 this.vars.put(entry.getKey(), entry.getValue());
61 }
62 }
63
64
65
66
67
68 public VariableManager()
69 {
70 super();
71 }
72
73
74
75
76
77
78
79
80 public boolean existsVar(final String varname)
81 {
82 return this.vars.containsKey(varname);
83 }
84
85
86
87
88
89
90
91
92
93
94 public String getVar(final String varname)
95 {
96 final String value = this.vars.get(varname);
97 return (value == null) ? "" : value;
98 }
99
100
101
102
103
104
105
106
107
108
109 public void setVar(final String varname, final String value)
110 {
111
112 this.vars.put(varname, (value == null) ? "" : value);
113 }
114
115
116
117
118
119
120
121
122
123 @SuppressWarnings("java:S1120")
124 public void unsetVar(final String varname)
125 {
126 this.vars.remove(varname);
127 }
128
129
130
131
132
133
134
135
136
137
138 public List<String> getUndefined(final String varname)
139 {
140
141 final var matcher = VariableManager.BLOCK_MATCHER_REGEXP.matcher(getVar(varname));
142 boolean result = matcher.find();
143 final List<String> undefvars = new ArrayList<>();
144 while (result)
145 {
146 final String vname = matcher.group(1);
147 if (!this.vars.containsKey(vname) && !undefvars.contains(vname))
148 {
149 undefvars.add(vname);
150 }
151 result = matcher.find();
152 }
153 return Collections.unmodifiableList(undefvars);
154 }
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188 private String replaceVarsNew(final String block)
189 {
190
191
192
193 final var matcherTemplate = VariableManager.TEMPLATE_MATCHER_REGEXP.matcher(block);
194 final Set<String> varsSetTemplate = new TreeSet<>();
195 while (matcherTemplate.find())
196 {
197 final var varnameTemplate = block.substring(matcherTemplate.start() + 1, matcherTemplate.end() - 1);
198 if (this.vars.containsKey(varnameTemplate))
199 {
200 varsSetTemplate.add(varnameTemplate);
201 }
202 }
203 String resBlock = block;
204 for (final String varName : varsSetTemplate)
205 {
206 resBlock = resBlock.replaceAll("\\{" + varName + "\\}", getVar(varName));
207 }
208 return resBlock;
209 }
210
211
212
213
214
215
216
217
218
219
220 public String subst(final String varname)
221 {
222
223 return replaceVarsNew(getVar(varname));
224 }
225
226
227
228
229
230
231
232
233
234
235
236
237 @SuppressWarnings("java:S2301")
238 public String parse(final String target, final String varname, final boolean append)
239 {
240 LOGGER.debug("varname: {}", varname);
241 final String str = subst(varname);
242 LOGGER.debug("str: {}", str);
243 setVar(target, (append ? getVar(target) : "") + str);
244 return str;
245 }
246
247
248
249
250
251
252
253 public List<String> getVars()
254 {
255 if (this.vars.isEmpty())
256 {
257 return Collections.emptyList();
258 }
259 final List<String> result = new ArrayList<>(this.vars.size());
260 for (final Entry<String, String> entry : this.vars.entrySet())
261 {
262 result.add(entry.getKey());
263 }
264 return Collections.unmodifiableList(result);
265 }
266
267
268
269
270
271
272
273
274
275
276
277
278 @Override
279 public String toString()
280 {
281 return new StringBuilder().append("VariableManager[vars=").append(getVars()).append(']').toString();
282 }
283
284
285
286
287
288
289
290
291 @Override
292 public int hashCode()
293 {
294 return Objects.hash(this.vars);
295 }
296
297
298
299
300
301
302
303
304
305 @Override
306 public boolean equals(final Object obj)
307 {
308 if (this == obj)
309 {
310 return true;
311 }
312 if (!(obj instanceof VariableManager))
313 {
314 return false;
315 }
316 final VariableManager other = (VariableManager)obj;
317 return this.vars.equals(other.vars);
318 }
319
320 }