001    package com.khubla.pragmatach.plugin.freemarker;
002    
003    import java.io.IOException;
004    import java.io.InputStream;
005    import java.io.InputStreamReader;
006    import java.io.Reader;
007    
008    import javax.servlet.ServletContext;
009    
010    import com.khubla.pragmatach.framework.resourceloader.DefaultResourceLoaderImpl;
011    import com.khubla.pragmatach.framework.resourceloader.ResourceLoader;
012    
013    import freemarker.cache.TemplateLoader;
014    
015    /**
016     * @author tome
017     */
018    public class PragmatachTemplateLoader implements TemplateLoader {
019            /**
020             * ServletContext
021             */
022            private final ServletContext servletContext;
023    
024            public PragmatachTemplateLoader(ServletContext servletContext) {
025                    this.servletContext = servletContext;
026            }
027    
028            @Override
029            public void closeTemplateSource(Object object) throws IOException {
030                    final InputStream inputStream = (InputStream) object;
031                    inputStream.close();
032            }
033    
034            @Override
035            public Object findTemplateSource(String template) throws IOException {
036                    try {
037                            final ResourceLoader resourceLoader = new DefaultResourceLoaderImpl(
038                                            servletContext);
039                            final InputStream is = resourceLoader.getResource(template);
040                            return is;
041                    } catch (final Exception e) {
042                            throw new IOException(e);
043                    }
044            }
045    
046            @Override
047            public long getLastModified(Object object) {
048                    return 0;
049            }
050    
051            @Override
052            public Reader getReader(Object object, String string) throws IOException {
053                    final InputStream inputStream = (InputStream) object;
054                    return new InputStreamReader(inputStream, "UTF-8");
055            }
056    }