use more template, use replace input stream
[nikiroo-utils.git] / src / be / nikiroo / fanfix / library / Template.java
CommitLineData
8639c60d
NR
1package be.nikiroo.fanfix.library;
2
8639c60d
NR
3import java.io.IOException;
4import java.io.InputStream;
5import java.util.HashMap;
6import java.util.List;
7import java.util.Map;
8
9import be.nikiroo.utils.IOUtils;
8639c60d
NR
10import be.nikiroo.utils.streams.ReplaceInputStream;
11
12public 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 }
074a8325 61
8639c60d
NR
62 from[i] = "${" + key + "}";
63 to[i] = value.toString();
64
65 i++;
66 }
074a8325 67
8639c60d
NR
68 InputStream in = IOUtils.openResource(location, name);
69
074a8325
NR
70 InputStream stream;
71
72 stream = IOUtils.openResource(location, name);
73 System.out.println("SOURCE = ((" + IOUtils.readSmallStream(stream) + "))");
74 stream=new ReplaceInputStream(IOUtils.openResource(location, name), from, to);
75 System.out.println("RESULT = ((" + IOUtils.readSmallStream(stream) + "))");
8639c60d 76
8639c60d 77
074a8325 78 return new ReplaceInputStream(in, from, to);
8639c60d
NR
79 }
80
81 public synchronized Template set(String key, String value) {
82 values.put(key, value);
83 valuesTemplate.remove(key);
84 valuesTemplateList.remove(key);
85 return this;
86 }
87
88 public synchronized Template set(String key, Template value) {
89 values.remove(key);
90 valuesTemplate.put(key, value);
91 valuesTemplateList.remove(key);
92 return this;
93 }
94
95 public synchronized Template set(String key, List<Template> value) {
96 values.remove(key);
97 valuesTemplate.remove(key);
98 valuesTemplateList.put(key, value);
99 return this;
100 }
101
102 @Override
103 public String toString() {
104 return String.format(
105 "[Template for %s with (%d,%d,%d) value(s) to replace]", name,
106 values.size(), valuesTemplate.size(),
107 valuesTemplateList.size());
108 }
109}