View Javadoc

1   package net.sourceforge.blogentis.storage;
2   
3   //-----------------------------------------------------------------------
4   //Blogentis - a blog publishing platform.
5   //Copyright (C) 2004 Tassos Bassoukos <abassouk@gmail.com>
6   //
7   //This library is free software; you can redistribute it and/or
8   //modify it under the terms of the GNU Lesser General Public
9   //License as published by the Free Software Foundation; either
10  //version 2.1 of the License, or (at your option) any later version.
11  //
12  //This library is distributed in the hope that it will be useful,
13  //but WITHOUT ANY WARRANTY; without even the implied warranty of
14  //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  //Lesser General Public License for more details.
16  //
17  //You should have received a copy of the GNU Lesser General Public
18  //License along with this library; if not, write to the Free Software
19  //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  //-----------------------------------------------------------------------
21  //
22  //$Id: LocalFileResource.java,v 1.1 2004/10/22 17:34:11 tassos Exp $
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">/* package */LocalFileResource(String path) {/package-summary.html">class="comment"> package */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          /* yeah, I know ... */
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     // we dont' accept properties...
133     }
134 
135     public boolean isDirectory() {
136         return localFile.isDirectory();
137     }
138 }