KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > meshcms > core > FileTypes


1 /*
2  * MeshCMS - A simple CMS based on SiteMesh
3  * Copyright (C) 2004-2007 Luciano Vernaschi
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License
7  * as published by the Free Software Foundation; either version 2
8  * of the License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  *
19  * You can contact the author at http://www.cromoteca.com
20  * and at info@cromoteca.com
21  */

22
23 package org.meshcms.core;
24
25 import java.util.*;
26 import org.meshcms.util.*;
27
28 /**
29  * Contains data about file types and extension.
30  */

31 public final class FileTypes {
32   public static final SortedMap EXT_MAP = new TreeMap();
33
34   /**
35    * Default icon for unknown file types.
36    */

37   public static final String JavaDoc DEFAULT_ICON = "icon_file.gif";
38
39   /**
40    * Default icon for folders.
41    */

42   public static final String JavaDoc DIR_ICON = "icon_folder.gif";
43
44   /**
45    * Denotes an unknown file type.
46    */

47   public static final TypeInfo UNKNOWN;
48
49   /**
50    * Denotes a directory.
51    */

52   public static final TypeInfo DIRECTORY;
53
54   /**
55    * Id of static HTML files.
56    */

57   public static final int HTML_ID = 1;
58
59   /**
60    * Id of server-side HTML files (e.g. JSPs).
61    */

62   public static final int SERVERSIDE_ID = 2;
63
64   static {
65     UNKNOWN = new TypeInfo();
66     UNKNOWN.id = -1;
67     UNKNOWN.description = "Unknown";
68     UNKNOWN.compressible = false;
69     UNKNOWN.iconFile = DEFAULT_ICON;
70     UNKNOWN.preventHotlinking = false;
71
72     DIRECTORY = new TypeInfo();
73     DIRECTORY.id = 0;
74     DIRECTORY.description = "Folder";
75     DIRECTORY.compressible = false;
76     DIRECTORY.iconFile = DIR_ICON;
77     DIRECTORY.preventHotlinking = false;
78
79     TypeInfo info = new TypeInfo();
80     info.id = HTML_ID;
81     info.description = "Static Page";
82     info.compressible = true;
83     info.preventHotlinking = false;
84     info.iconFile = "icon_html.gif";
85     EXT_MAP.put("htm", info);
86     EXT_MAP.put("html", info);
87     EXT_MAP.put("xhtml", info);
88
89     info = new TypeInfo();
90     info.id = SERVERSIDE_ID;
91     info.description = "Server-Side Page";
92     info.compressible = true;
93     info.preventHotlinking = false;
94     info.iconFile = "icon_html.gif";
95     EXT_MAP.put("asp", info);
96     EXT_MAP.put("cgi", info);
97     EXT_MAP.put("jsp", info);
98     EXT_MAP.put("php", info);
99     EXT_MAP.put("pl", info);
100     EXT_MAP.put("shtml", info);
101
102     info = new TypeInfo();
103     info.id = 3;
104     info.description = "Web Image";
105     info.compressible = false;
106     info.preventHotlinking = true;
107     info.iconFile = "icon_image.gif";
108     EXT_MAP.put("gif", info);
109     EXT_MAP.put("jpeg", info);
110     EXT_MAP.put("jpg", info);
111     EXT_MAP.put("mng", info);
112     EXT_MAP.put("png", info);
113
114     info = new TypeInfo();
115     info.id = 4;
116     info.description = "Java File";
117     info.compressible = true;
118     info.preventHotlinking = false;
119     info.iconFile = "icon_java.gif";
120     EXT_MAP.put("class", info);
121     EXT_MAP.put("java", info);
122
123     info = new TypeInfo();
124     info.id = 5;
125     info.description = "Java Archive";
126     info.compressible = false;
127     info.preventHotlinking = false;
128     info.iconFile = "icon_java.gif";
129     EXT_MAP.put("ear", info);
130     EXT_MAP.put("jar", info);
131     EXT_MAP.put("war", info);
132
133     info = new TypeInfo();
134     info.id = 6;
135     info.description = "XML File";
136     info.compressible = true;
137     info.preventHotlinking = false;
138     info.iconFile = "icon_xml.gif";
139     EXT_MAP.put("dtd", info);
140     EXT_MAP.put("tld", info);
141     EXT_MAP.put("xml", info);
142     EXT_MAP.put("xsl", info);
143
144     info = new TypeInfo();
145     info.id = 7;
146     info.description = "Audio File";
147     info.compressible = false;
148     info.preventHotlinking = true;
149     info.iconFile = "icon_audio.gif";
150     EXT_MAP.put("au", info);
151     EXT_MAP.put("mp3", info);
152     EXT_MAP.put("ogg", info);
153     EXT_MAP.put("wav", info);
154     EXT_MAP.put("wma", info);
155
156     info = new TypeInfo();
157     info.id = 8;
158     info.description = "Video File";
159     info.compressible = false;
160     info.preventHotlinking = true;
161     info.iconFile = "icon_video.gif";
162     EXT_MAP.put("avi", info);
163     EXT_MAP.put("flv", info);
164     EXT_MAP.put("mov", info);
165     EXT_MAP.put("mpeg", info);
166     EXT_MAP.put("mpg", info);
167     EXT_MAP.put("wmv", info);
168
169     info = new TypeInfo();
170     info.id = 9;
171     info.description = "Archive";
172     info.compressible = false;
173     info.preventHotlinking = true;
174     info.iconFile = "icon_archive.gif";
175     EXT_MAP.put("7z", info);
176     EXT_MAP.put("bz2", info);
177     EXT_MAP.put("gz", info);
178     EXT_MAP.put("rar", info);
179     EXT_MAP.put("rpm", info);
180     EXT_MAP.put("tar", info);
181     EXT_MAP.put("tgz", info);
182     EXT_MAP.put("z", info);
183     EXT_MAP.put("zip", info);
184
185     info = new TypeInfo();
186     info.id = 10;
187     info.description = "Plain Text File";
188     info.compressible = true;
189     info.preventHotlinking = false;
190     info.iconFile = "icon_text.gif";
191     EXT_MAP.put("log", info);
192     EXT_MAP.put("txt", info);
193
194     info = new TypeInfo();
195     info.id = 11;
196     info.description = "Style Sheet File";
197     info.compressible = true;
198     info.preventHotlinking = false;
199     info.iconFile = "icon_style.gif";
200     EXT_MAP.put("css", info);
201
202     info = new TypeInfo();
203     info.id = 12;
204     info.description = "Script File";
205     info.compressible = true;
206     info.preventHotlinking = false;
207     info.iconFile = "icon_script.gif";
208     EXT_MAP.put("js", info);
209
210     info = new TypeInfo();
211     info.id = 13;
212     info.description = "Executable File";
213     info.compressible = false;
214     info.preventHotlinking = true;
215     info.iconFile = "icon_program.gif";
216     EXT_MAP.put("bin", info);
217     EXT_MAP.put("exe", info);
218
219     info = new TypeInfo();
220     info.id = 14;
221     info.description = "Word Document";
222     info.compressible = true;
223     info.preventHotlinking = true;
224     info.iconFile = "icon_word.gif";
225     EXT_MAP.put("doc", info);
226     EXT_MAP.put("rtf", info);
227
228     info = new TypeInfo();
229     info.id = 15;
230     info.description = "Excel Document";
231     info.compressible = true;
232     info.preventHotlinking = true;
233     info.iconFile = "icon_excel.gif";
234     EXT_MAP.put("xls", info);
235
236     info = new TypeInfo();
237     info.id = 16;
238     info.description = "PowerPoint Document";
239     info.compressible = true;
240     info.preventHotlinking = true;
241     info.iconFile = "icon_powerpoint.gif";
242     EXT_MAP.put("pps", info);
243     EXT_MAP.put("ppt", info);
244
245     info = new TypeInfo();
246     info.id = 17;
247     info.description = "PDF Document";
248     info.compressible = false;
249     info.preventHotlinking = true;
250     info.iconFile = "icon_pdf.gif";
251     EXT_MAP.put("pdf", info);
252
253     info = new TypeInfo();
254     info.id = 18;
255     info.description = "Image";
256     info.compressible = false;
257     info.preventHotlinking = true;
258     info.iconFile = "icon_image.gif";
259     EXT_MAP.put("bmp", info);
260     EXT_MAP.put("psd", info);
261     EXT_MAP.put("tga", info);
262     EXT_MAP.put("tif", info);
263     EXT_MAP.put("tiff", info);
264
265     info = new TypeInfo();
266     info.id = 19;
267     info.description = "Icon";
268     info.compressible = true;
269     info.preventHotlinking = false;
270     info.iconFile = "icon_image.gif";
271     EXT_MAP.put("ico", info);
272
273     info = new TypeInfo();
274     info.id = 20;
275     info.description = "Flash File";
276     info.compressible = false;
277     info.preventHotlinking = true;
278     info.iconFile = "icon_flash.gif";
279     EXT_MAP.put("swf", info);
280
281     info = new TypeInfo();
282     info.id = 21;
283     info.description = "Isaac File";
284     info.compressible = false;
285     info.preventHotlinking = true;
286     info.iconFile = "icon_isaac.gif";
287     EXT_MAP.put("isc", info);
288   }
289
290   static TypeInfo getInfo(String JavaDoc extension) {
291     TypeInfo info = (TypeInfo) EXT_MAP.get(extension);
292     return (info == null) ? UNKNOWN : info;
293   }
294
295   /**
296    * @return true if the type of the file is the same as the given extension.
297    * For example, isLike("button.gif", "jpg") returns true
298    * since gif and jpg are both images.
299    */

300   public static boolean isLike(String JavaDoc fileName, String JavaDoc extension) {
301     return getInfo(Utils.getExtension(fileName, false)).id == getInfo(extension).id;
302   }
303
304   /**
305    * @return the description of the type of the file.
306    */

307   public static String JavaDoc getDescription(String JavaDoc fileName) {
308     return getInfo(Utils.getExtension(fileName, false)).description;
309   }
310
311   /**
312    * @return the name of the icon file for the type of the given file.
313    */

314   public static String JavaDoc getIconFile(String JavaDoc fileName) {
315     return getInfo(Utils.getExtension(fileName, false)).iconFile;
316   }
317
318   /**
319    * @return true if the file is supposed to be compressible. For example, text
320    * files are compressible, while ZIP files are not.
321    */

322   public static boolean isCompressible(String JavaDoc fileName) {
323     return getInfo(Utils.getExtension(fileName, false)).compressible;
324   }
325
326   /**
327    * @return true if the file should be referred from a page to be accessed.
328    */

329   public static boolean isPreventHotlinking(String JavaDoc fileName) {
330     return getInfo(Utils.getExtension(fileName, false)).preventHotlinking;
331   }
332
333   /**
334    * @return true if the file is a page (static or server-side).
335    */

336   public static boolean isPage(String JavaDoc fileName) {
337     int id = getInfo(Utils.getExtension(fileName, false)).id;
338     return id == HTML_ID || id == SERVERSIDE_ID;
339   }
340
341   static class TypeInfo {
342     int id;
343     String JavaDoc description;
344     String JavaDoc iconFile;
345     boolean compressible;
346     boolean preventHotlinking;
347   }
348 }
349
Popular Tags