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