1 package net.sourceforge.blogentis.storage;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25 import java.io.File;
26 import java.io.FileInputStream;
27 import java.io.FileNotFoundException;
28 import java.io.FileReader;
29 import java.io.IOException;
30 import java.io.InputStream;
31 import java.util.Date;
32 import java.util.HashSet;
33 import java.util.Set;
34
35 import org.apache.commons.io.IOUtils;
36 import org.apache.commons.logging.Log;
37 import org.apache.commons.logging.LogFactory;
38 import org.apache.turbine.Turbine;
39 import org.apache.turbine.services.mimetype.TurbineMimeTypes;
40
41 /***
42 * Implementation of an AbstractFileResource using local files.
43 *
44 * @author abas
45 */
46 public class LocalFileResource
47 implements AbstractFileResource {
48 private static final Log log = LogFactory.getLog(LocalFileResource.class);
49
50 protected File localFile;
51
52 protected String originalPath;
53
54 /* package */LocalFileResource(String path) {/package-summary/html">class="comment"> package */LocalFileResource(String path) {/package-summary.html">LocalFileResource(String path) {
55 if (!path.startsWith("/"))
56 path = "/" + path;
57 localFile = new File(Turbine.getRealPath("/templates/" + path));
58 originalPath = path;
59 }
60
61 public static Set getAllFiles(String path, FileResourceFilter filter) {
62 Set s = new HashSet();
63 File file = new File(Turbine.getRealPath("/templates/" + path));
64 if (!file.isDirectory())
65 return s;
66 String[] paths = file.list();
67 if(path.equals("/"))
68 path="";
69 for(int i = 0; i < paths.length; i++) {
70 String f = path + "/" + paths[i];
71 if (filter != null && filter.ignored(f))
72 continue;
73 s.add(f);
74 if (filter == null || filter.descendInto(f))
75 s.addAll(getAllFiles(f, filter));
76 }
77 return s;
78 }
79
80 public String getPath() {
81 return originalPath;
82 }
83
84 public String getName() {
85 return localFile.getName();
86 }
87
88 public int getSize() {
89
90 return (int)localFile.length();
91 }
92
93 public Date getLastModified() {
94 return new Date(localFile.lastModified());
95 }
96
97 public String getMimeType() {
98 return TurbineMimeTypes.getContentType(localFile);
99 }
100
101 public InputStream getFileAsInputStream()
102 throws FileNotFoundException {
103 return new FileInputStream(localFile);
104 }
105
106 public String getFileAsString()
107 throws FileNotFoundException {
108 FileReader r = new FileReader(localFile);
109 try {
110 return IOUtils.toString(r);
111 } catch (IOException e) {
112 String s = "Could not open for reading "
113 + localFile.getAbsolutePath();
114 log.error(s, e);
115 return s;
116 }
117 }
118
119 public boolean exists() {
120 return localFile.exists();
121 }
122
123 public boolean isOriginal() {
124 return true;
125 }
126
127 public String getProperty(String propertyName) {
128 return null;
129 }
130
131 public void setProperty(String propertyName, String propertyContents) {
132
133 }
134
135 public boolean isDirectory() {
136 return localFile.isDirectory();
137 }
138 }