1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.tools.template_engine;
|
---|
3 |
|
---|
4 | /**
|
---|
5 | * {@link TemplateEntry} that concatenates several templates.
|
---|
6 | */
|
---|
7 | public final class CompoundTemplateEntry implements TemplateEntry {
|
---|
8 |
|
---|
9 | /**
|
---|
10 | * Factory method to concatenate several {@code TemplateEntry}s.
|
---|
11 | *
|
---|
12 | * If the number of entries is 0 or 1, the result may not be a {@code CompoundTemplateEntry},
|
---|
13 | * but optimized to a static text or the single entry itself.
|
---|
14 | * @param entries the {@code TemplateEntry}s to concatenate
|
---|
15 | * @return a {@link TemplateEntry} that concatenates all the entries
|
---|
16 | */
|
---|
17 | public static TemplateEntry fromArray(TemplateEntry... entries) {
|
---|
18 | if (entries.length == 0)
|
---|
19 | return new StaticText("");
|
---|
20 | else if (entries.length == 1)
|
---|
21 | return entries[0];
|
---|
22 | else
|
---|
23 | return new CompoundTemplateEntry(entries);
|
---|
24 | }
|
---|
25 |
|
---|
26 | private CompoundTemplateEntry(TemplateEntry... entries) {
|
---|
27 | this.entries = entries;
|
---|
28 | }
|
---|
29 |
|
---|
30 | private final TemplateEntry[] entries;
|
---|
31 |
|
---|
32 | @Override
|
---|
33 | public void appendText(StringBuilder result, TemplateEngineDataProvider dataProvider) {
|
---|
34 | for (TemplateEntry te: entries) {
|
---|
35 | te.appendText(result, dataProvider);
|
---|
36 | }
|
---|
37 | }
|
---|
38 |
|
---|
39 | @Override
|
---|
40 | public boolean isValid(TemplateEngineDataProvider dataProvider) {
|
---|
41 | for (TemplateEntry te: entries) {
|
---|
42 | if (!te.isValid(dataProvider))
|
---|
43 | return false;
|
---|
44 | }
|
---|
45 | return true;
|
---|
46 | }
|
---|
47 |
|
---|
48 | @Override
|
---|
49 | public String toString() {
|
---|
50 | StringBuilder result = new StringBuilder();
|
---|
51 | for (TemplateEntry te: entries) {
|
---|
52 | result.append(te);
|
---|
53 | }
|
---|
54 | return result.toString();
|
---|
55 | }
|
---|
56 | }
|
---|