1 48 49 package org.jpublish.template; 50 51 import java.io.*; 52 53 import org.jpublish.util.encoding.CharacterEncodingManager; 54 import org.jpublish.util.encoding.CharacterEncodingMap; 55 import org.jpublish.view.ContentSource; 56 57 62 63 public class TemplateContentSource implements ContentSource { 64 65 private TemplateManager templateManager = null; 66 private String path = null; 67 68 74 75 public TemplateContentSource(TemplateManager templateManager, String path) { 76 this.templateManager = templateManager; 77 this.path = path; 78 } 79 80 86 87 public long getLastModified() throws IOException { 88 return templateManager.getLastModified(path); 89 } 90 91 97 98 public InputStream getInputStream() throws IOException { 99 try { 100 Template template = templateManager.getTemplate(path); 101 CharacterEncodingManager characterEncodingManager = 102 templateManager.getSiteContext().getCharacterEncodingManager(); 103 CharacterEncodingMap characterEncodingMap = 104 characterEncodingManager.getMap(path); 105 return new ByteArrayInputStream(template.getText().getBytes(characterEncodingMap.getTemplateEncoding())); 106 } catch (Exception e) { 108 throw new IOException("Error getting stream: " + e); 109 } 110 } 111 112 118 119 public Reader getReader() throws IOException { 120 try { 121 Template template = templateManager.getTemplate(path); 122 return new StringReader(template.getText()); 123 } catch (Exception e) { 124 throw new IOException("Error getting reader: " + e); 125 } 126 } 127 128 136 137 public Reader getReader(String encoding) throws IOException { 138 try { 139 Template template = templateManager.getTemplate(path); 140 byte[] data = template.getText().getBytes(); 141 String encodedString = new String (data, encoding); 142 return new StringReader(encodedString); 143 } catch (Exception e) { 144 throw new IOException("Error getting reader: " + e); 145 } 146 } 147 148 } 149 | Popular Tags |