3536d5f12efccf7da066ce4b1d140553c633e631
1 package be
.nikiroo
.fanfix
.library
;
3 import java
.io
.IOException
;
4 import java
.io
.InputStream
;
5 import java
.util
.HashMap
;
9 import be
.nikiroo
.utils
.IOUtils
;
10 import be
.nikiroo
.utils
.streams
.ReplaceInputStream
;
12 public class Template
{
13 private Class
<?
> location
;
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
>>();
20 public Template(Class
<?
> location
, String name
) {
21 this.location
= location
;
25 public synchronized InputStream
read() throws IOException
{
27 String from
[] = new String
[values
.size() + valuesTemplate
.size()
28 + valuesTemplateList
.size()];
29 String to
[] = new String
[from
.length
];
33 for (String key
: values
.keySet()) {
34 from
[i
] = "${" + key
+ "}";
35 to
[i
] = values
.get(key
);
39 for (String key
: valuesTemplate
.keySet()) {
40 InputStream value
= valuesTemplate
.get(key
).read();
42 from
[i
] = "${" + key
+ "}";
43 to
[i
] = IOUtils
.readSmallStream(value
);
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();
56 value
.append(IOUtils
.readSmallStream(valueOne
));
62 from
[i
] = "${" + key
+ "}";
63 to
[i
] = value
.toString();
68 InputStream in
= IOUtils
.openResource(location
, name
);
69 return new ReplaceInputStream(in
, from
, to
);
72 public synchronized Template
set(String key
, String value
) {
73 values
.put(key
, value
);
74 valuesTemplate
.remove(key
);
75 valuesTemplateList
.remove(key
);
79 public synchronized Template
set(String key
, Template value
) {
81 valuesTemplate
.put(key
, value
);
82 valuesTemplateList
.remove(key
);
86 public synchronized Template
set(String key
, List
<Template
> value
) {
88 valuesTemplate
.remove(key
);
89 valuesTemplateList
.put(key
, value
);
94 public String
toString() {
96 "[Template for %s with (%d,%d,%d) value(s) to replace]", name
,
97 values
.size(), valuesTemplate
.size(),
98 valuesTemplateList
.size());