weblib: use templates for all html
[fanfix.git] / src / be / nikiroo / fanfix / library / Template.java
index e247e27962e6fe6d94d0a913a3a2afcadaa5c0dd..3ec987332913bc4613c71f58d5e2830964aeba6c 100644 (file)
@@ -1,6 +1,5 @@
 package be.nikiroo.fanfix.library;
 
-import java.io.ByteArrayInputStream;
 import java.io.IOException;
 import java.io.InputStream;
 import java.util.HashMap;
@@ -8,7 +7,6 @@ import java.util.List;
 import java.util.Map;
 
 import be.nikiroo.utils.IOUtils;
-import be.nikiroo.utils.StringUtils;
 import be.nikiroo.utils.streams.ReplaceInputStream;
 
 public class Template {
@@ -60,6 +58,7 @@ public class Template {
                                        valueOne.close();
                                }
                        }
+
                        from[i] = "${" + key + "}";
                        to[i] = value.toString();
 
@@ -67,32 +66,25 @@ public class Template {
                }
 
                InputStream in = IOUtils.openResource(location, name);
-               
-               //TODO: pending fix in replace stream
-               String data = IOUtils.readSmallStream(in);
-               in.close();
-               for(i = 0 ; i < from.length;i++) {
-                       data=data.replace(from[i], to[i]);
-               }
-               
-               //in = new ReplaceInputStream(in, from, to);
-               in = new ByteArrayInputStream(StringUtils.getBytes(data));
-               // END TODO
-               
-               return in;
+               return new ReplaceInputStream(in, from, to);
        }
 
        public synchronized Template set(String key, String value) {
-               values.put(key, value);
+               values.put(key, value == null ? "" : key);
                valuesTemplate.remove(key);
                valuesTemplateList.remove(key);
                return this;
        }
 
        public synchronized Template set(String key, Template value) {
+               if (value == null) {
+                       return set(key, "");
+               }
+
                values.remove(key);
                valuesTemplate.put(key, value);
                valuesTemplateList.remove(key);
+
                return this;
        }