1 52 53 package freemarker.cache; 54 55 import java.io.Reader ; 56 import java.io.StringReader ; 57 import java.util.HashMap ; 58 import java.util.Map ; 59 60 98 public class StringTemplateLoader implements TemplateLoader { 99 private final Map templates = new HashMap (); 100 101 108 public void putTemplate(String name, String templateSource) { 109 putTemplate(name, templateSource, System.currentTimeMillis()); 110 } 111 112 127 public void putTemplate(String name, String templateSource, long lastModified) { 128 templates.put(name, new StringTemplateSource(name, templateSource, lastModified)); 129 } 130 131 public void closeTemplateSource(Object templateSource) { 132 } 133 134 public Object findTemplateSource(String name) { 135 return templates.get(name); 136 } 137 138 public long getLastModified(Object templateSource) { 139 return ((StringTemplateSource)templateSource).lastModified; 140 } 141 142 public Reader getReader(Object templateSource, String encoding) { 143 return new StringReader (((StringTemplateSource)templateSource).source); 144 } 145 146 private static class StringTemplateSource { 147 private final String name; 148 private final String source; 149 private final long lastModified; 150 151 StringTemplateSource(String name, String source, long lastModified) { 152 if(name == null) { 153 throw new IllegalArgumentException ("name == null"); 154 } 155 if(source == null) { 156 throw new IllegalArgumentException ("source == null"); 157 } 158 if(lastModified < -1L) { 159 throw new IllegalArgumentException ("lastModified < -1L"); 160 } 161 this.name = name; 162 this.source = source; 163 this.lastModified = lastModified; 164 } 165 166 public boolean equals(Object obj) { 167 if(obj instanceof StringTemplateSource) { 168 return name.equals(((StringTemplateSource)obj).name); 169 } 170 return false; 171 } 172 173 public int hashCode() { 174 return name.hashCode(); 175 } 176 } 177 } 178 | Popular Tags |