1 /*
2  * Copyright (C) 2010 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.google.clearsilver.jsilver.resourceloader;
18 
19 import com.google.clearsilver.jsilver.exceptions.JSilverTemplateNotFoundException;
20 
21 import java.io.File;
22 import java.io.FileInputStream;
23 import java.io.InputStreamReader;
24 import java.io.IOException;
25 import java.io.Reader;
26 
27 /**
28  * Loads resources from a directory.
29  *
30  * @see ResourceLoader
31  */
32 public class FileSystemResourceLoader extends BufferedResourceLoader {
33 
34   private final File rootDir;
35 
FileSystemResourceLoader(File rootDir)36   public FileSystemResourceLoader(File rootDir) {
37     this.rootDir = rootDir;
38   }
39 
FileSystemResourceLoader(String rootDir)40   public FileSystemResourceLoader(String rootDir) {
41     this(new File(rootDir));
42   }
43 
44   @Override
open(String name)45   public Reader open(String name) throws IOException {
46     File file = new File(rootDir, name);
47     // Check for non-directory rather than is-file so that reads from
48     // e.g. pipes work.
49     if (file.exists() && !file.isDirectory() && file.canRead()) {
50       return buffer(new InputStreamReader(new FileInputStream(file), getCharacterSet()));
51     } else {
52       return null;
53     }
54   }
55 
56   @Override
openOrFail(String name)57   public Reader openOrFail(String name) throws JSilverTemplateNotFoundException, IOException {
58     Reader reader = open(name);
59     if (reader == null) {
60       throw new JSilverTemplateNotFoundException("No file '" + name + "' inside directory '"
61           + rootDir + "'");
62     } else {
63       return reader;
64     }
65   }
66 
67   /**
68    * Some applications, e.g. online help, need to know when a file has changed due to a symlink
69    * modification hence the use of {@link File#getCanonicalFile()}, if possible.
70    */
71   @Override
getResourceVersionId(String filename)72   public Object getResourceVersionId(String filename) {
73     File file = new File(rootDir, filename);
74     // Check for non-directory rather than is-file so that reads from
75     // e.g. pipes work.
76     if (file.exists() && !file.isDirectory() && file.canRead()) {
77       String fullPath;
78       try {
79         fullPath = file.getCanonicalPath();
80       } catch (IOException e) {
81         fullPath = file.getAbsolutePath();
82       }
83       return String.format("%s@%s", fullPath, file.lastModified());
84     } else {
85       return null;
86     }
87   }
88 }
89