1 package org.apache.velocity.runtime.resource.loader; 2 3 18 19 import java.io.File ; 20 import java.io.InputStream ; 21 import java.io.FileInputStream ; 22 import java.io.BufferedInputStream ; 23 import java.io.FileNotFoundException ; 24 25 import java.util.Hashtable ; 26 import java.util.Vector ; 27 28 import org.apache.velocity.util.StringUtils; 29 30 import org.apache.velocity.runtime.resource.Resource; 31 32 import org.apache.velocity.exception.ResourceNotFoundException; 33 34 import org.apache.commons.collections.ExtendedProperties; 35 36 42 public class FileResourceLoader extends ResourceLoader 43 { 44 47 private Vector paths = null; 48 49 54 private Hashtable templatePaths = new Hashtable (); 55 56 public void init( ExtendedProperties configuration) 57 { 58 rsvc.info("FileResourceLoader : initialization starting."); 59 60 paths = configuration.getVector("path"); 61 62 65 66 int sz = paths.size(); 67 68 for( int i=0; i < sz; i++) 69 { 70 rsvc.info("FileResourceLoader : adding path '" + (String ) paths.get(i) + "'"); 71 } 72 73 rsvc.info("FileResourceLoader : initialization complete."); 74 } 75 76 85 public synchronized InputStream getResourceStream(String templateName) 86 throws ResourceNotFoundException 87 { 88 91 if (templateName == null || templateName.length() == 0) 92 { 93 98 throw new ResourceNotFoundException( 99 "Need to specify a file name or file path!"); 100 } 101 102 String template = StringUtils.normalizePath(templateName); 103 if ( template == null || template.length() == 0 ) 104 { 105 String msg = "File resource error : argument " + template + 106 " contains .. and may be trying to access " + 107 "content outside of template root. Rejected."; 108 109 rsvc.error( "FileResourceLoader : " + msg ); 110 111 throw new ResourceNotFoundException ( msg ); 112 } 113 114 117 if (template.startsWith("/")) 118 { 119 template = template.substring(1); 120 } 121 122 int size = paths.size(); 123 for (int i = 0; i < size; i++) 124 { 125 String path = (String ) paths.get(i); 126 InputStream inputStream = findTemplate(path, template); 127 128 if (inputStream != null) 129 { 130 135 136 templatePaths.put(templateName, path); 137 return inputStream; 138 } 139 } 140 141 146 String msg = "FileResourceLoader Error: cannot find resource " + 147 template; 148 149 throw new ResourceNotFoundException( msg ); 150 } 151 152 159 private InputStream findTemplate(String path, String template) 160 { 161 try 162 { 163 File file = new File ( path, template ); 164 165 if ( file.canRead() ) 166 { 167 return new BufferedInputStream ( 168 new FileInputStream (file.getAbsolutePath())); 169 } 170 else 171 { 172 return null; 173 } 174 } 175 catch( FileNotFoundException fnfe ) 176 { 177 180 return null; 181 } 182 } 183 184 192 public boolean isSourceModified(Resource resource) 193 { 194 199 boolean modified = true; 200 201 String fileName = resource.getName(); 202 String path = (String ) templatePaths.get(fileName); 203 File currentFile = null; 204 205 for (int i = 0; currentFile == null && i < paths.size(); i++) 206 { 207 String testPath = (String ) paths.get(i); 208 File testFile = new File (testPath, fileName); 209 if (testFile.canRead()) 210 { 211 currentFile = testFile; 212 } 213 } 214 File file = new File (path, fileName); 215 if (currentFile == null || !file.exists()) 216 { 217 225 } 226 else if (currentFile.equals(file) && file.canRead()) 227 { 228 234 modified = (file.lastModified() != resource.getLastModified()); 235 } 236 237 240 return modified; 241 } 242 243 public long getLastModified(Resource resource) 244 { 245 String path = (String ) templatePaths.get(resource.getName()); 246 File file = new File (path, resource.getName()); 247 248 if (file.canRead()) 249 { 250 return file.lastModified(); 251 } 252 else 253 { 254 return 0; 255 } 256 } 257 } 258 | Popular Tags |