| 1 | package be.nikiroo.fanfix.library; |
| 2 | |
| 3 | import java.io.IOException; |
| 4 | import java.io.InputStream; |
| 5 | import java.util.HashMap; |
| 6 | import java.util.List; |
| 7 | import java.util.Map; |
| 8 | |
| 9 | import be.nikiroo.utils.IOUtils; |
| 10 | import be.nikiroo.utils.streams.ReplaceInputStream; |
| 11 | |
| 12 | public class Template { |
| 13 | private Class<?> location; |
| 14 | private String name; |
| 15 | |
| 16 | private Map<String, String> values = new HashMap<String, String>(); |
| 17 | private Map<String, Template> valuesTemplate = new HashMap<String, Template>(); |
| 18 | private Map<String, List<Template>> valuesTemplateList = new HashMap<String, List<Template>>(); |
| 19 | |
| 20 | public Template(Class<?> location, String name) { |
| 21 | this.location = location; |
| 22 | this.name = name; |
| 23 | } |
| 24 | |
| 25 | public synchronized InputStream read() throws IOException { |
| 26 | |
| 27 | String from[] = new String[values.size() + valuesTemplate.size() |
| 28 | + valuesTemplateList.size()]; |
| 29 | String to[] = new String[from.length]; |
| 30 | |
| 31 | int i = 0; |
| 32 | |
| 33 | for (String key : values.keySet()) { |
| 34 | from[i] = "${" + key + "}"; |
| 35 | to[i] = values.get(key); |
| 36 | |
| 37 | i++; |
| 38 | } |
| 39 | for (String key : valuesTemplate.keySet()) { |
| 40 | InputStream value = valuesTemplate.get(key).read(); |
| 41 | try { |
| 42 | from[i] = "${" + key + "}"; |
| 43 | to[i] = IOUtils.readSmallStream(value); |
| 44 | } finally { |
| 45 | value.close(); |
| 46 | } |
| 47 | |
| 48 | i++; |
| 49 | } |
| 50 | for (String key : valuesTemplateList.keySet()) { |
| 51 | List<Template> templates = valuesTemplateList.get(key); |
| 52 | StringBuilder value = new StringBuilder(); |
| 53 | for (Template template : templates) { |
| 54 | InputStream valueOne = template.read(); |
| 55 | try { |
| 56 | value.append(IOUtils.readSmallStream(valueOne)); |
| 57 | } finally { |
| 58 | valueOne.close(); |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | from[i] = "${" + key + "}"; |
| 63 | to[i] = value.toString(); |
| 64 | |
| 65 | i++; |
| 66 | } |
| 67 | |
| 68 | InputStream in = IOUtils.openResource(location, name); |
| 69 | return new ReplaceInputStream(in, from, to); |
| 70 | } |
| 71 | |
| 72 | public synchronized Template set(String key, String value) { |
| 73 | values.put(key, value == null ? "" : value); |
| 74 | valuesTemplate.remove(key); |
| 75 | valuesTemplateList.remove(key); |
| 76 | return this; |
| 77 | } |
| 78 | |
| 79 | public synchronized Template set(String key, Template value) { |
| 80 | if (value == null) { |
| 81 | return set(key, ""); |
| 82 | } |
| 83 | |
| 84 | values.remove(key); |
| 85 | valuesTemplate.put(key, value); |
| 86 | valuesTemplateList.remove(key); |
| 87 | |
| 88 | return this; |
| 89 | } |
| 90 | |
| 91 | public synchronized Template set(String key, List<Template> value) { |
| 92 | values.remove(key); |
| 93 | valuesTemplate.remove(key); |
| 94 | valuesTemplateList.put(key, value); |
| 95 | return this; |
| 96 | } |
| 97 | |
| 98 | @Override |
| 99 | public String toString() { |
| 100 | return String.format( |
| 101 | "[Template for %s with (%d,%d,%d) value(s) to replace]", name, |
| 102 | values.size(), valuesTemplate.size(), |
| 103 | valuesTemplateList.size()); |
| 104 | } |
| 105 | } |