1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83 package com.ivata.mask.util;
84 /***
85 * <p>
86 * MimeTypeHandling is a class of static methods for handling MimeTypes. It
87 * contains methods for getting MimeType information from filenames etc..
88 * </p>
89 * <p>
90 * Don't create an instance of this class; use the static final methods.
91 * </p>
92 *
93 * @since ivata masks 0.4 (2001-12-27)
94 * @author Peter Illes
95 * @version $Revision: 1.3 $ /
96 */
97 public final class MimeTypesHandling {
98 /***
99 * /**
100 * <p>
101 * This method returns an appropriate mime-icon-image name from a filename
102 * (or rather from the extension of the file).
103 * </p>
104 *
105 * @param filename
106 * a String which is the name of the file.
107 * @return the full path to the image (in the webapp) representing this
108 * file-types
109 */
110 public static String fileName2Icon(final String filename) {
111 String iconDir = "/images/mime/";
112 String extension;
113 try {
114 extension = (filename.substring(filename.lastIndexOf(".") + 1))
115 .toLowerCase();
116 } catch (java.lang.StringIndexOutOfBoundsException e) {
117 return iconDir + "file_types_unknown.gif";
118 }
119 if (extension.equals("")) {
120 return iconDir + "file_types_unknown.gif";
121 } else if (extension.equals("txt")) {
122 return iconDir + "file_types_text.gif";
123 } else if (extension.equals("htm") || extension.equals("html")) {
124 return iconDir + "file_types_html.gif";
125 } else if (extension.equals("jpg") || extension.equals("jpeg")
126 || extension.equals("gif") || extension.equals("bmp")
127 || extension.equals("png") || extension.equals("tiff")) {
128 return iconDir + "file_types_image.gif";
129 } else if (extension.equals("mp3") || extension.equals("wav")
130 || extension.equals("m3u") || extension.equals("ogg")) {
131 return iconDir + "file_types_sound.gif";
132 } else if (extension.equals("mpg") || extension.equals("mpeg")
133 || extension.equals("avi") || extension.equals("mov")) {
134 return iconDir + "file_types_video.gif";
135 } else if (extension.equals("pdf")) {
136 return iconDir + "file_types_pdf.gif";
137 } else if (extension.equals("ps")) {
138 return iconDir + "file_types_printer.gif";
139 } else if (extension.equals("zip") || extension.equals("rar")
140 || extension.equals("gzip") || extension.equals("tar")) {
141 return iconDir + "file_types_zip.gif";
142 } else if (extension.equals("doc")) {
143 return iconDir + "file_types_wdoc.gif";
144 } else if (extension.equals("msg") || extension.equals("eml")) {
145 return iconDir + "file_types_mail.gif";
146 } else {
147 return iconDir + "file_types_unknown.gif";
148 }
149 }
150 /***
151 * <p>
152 * Returns the full path to the appropriate icon for this mime-type that the
153 * webapp will use.
154 * </p>
155 *
156 * @param mimeType
157 * as string
158 * @return the path to the image for use by webapp
159 */
160 public static String mimeType2Icon(final String mimeType) {
161 String iconDir = "/images/mime/";
162 String iconFileName = null;
163 String mimeTypeLower = mimeType.toLowerCase();
164 if (mimeTypeLower.indexOf("text/plain") != -1) {
165 iconFileName = "file_types_text.gif";
166 } else if (mimeTypeLower.indexOf("text/html") != -1) {
167 iconFileName = "file_types_html.gif";
168 } else if (mimeTypeLower.indexOf("text/rtf") != -1
169 || mimeTypeLower.indexOf("application/rtf") != -1) {
170 iconFileName = "file_types_rtf.gif";
171 } else if (mimeTypeLower.indexOf("message/") != -1
172 || mimeTypeLower.indexOf("multipart/") != -1) {
173 iconFileName = "file_types_mail.gif";
174 } else if (mimeTypeLower.indexOf("image/") != -1) {
175 iconFileName = "file_types_image.gif";
176 } else if (mimeTypeLower.indexOf("audio/") != -1) {
177 iconFileName = "file_types_sound.gif";
178 } else if (mimeTypeLower.indexOf("video/") != -1) {
179 iconFileName = "file_types_video.gif";
180 } else if (mimeTypeLower.indexOf("application/pdf") != -1) {
181 iconFileName = "file_types_pdf.gif";
182 } else if (mimeTypeLower.indexOf("application/postscript") != -1) {
183 iconFileName = "file_types_printer.gif";
184 } else if (mimeTypeLower.indexOf("zip") != -1) {
185 iconFileName = "file_types_zip.gif";
186 } else if (mimeTypeLower.indexOf("application/msword") != -1) {
187 iconFileName = "file_types_wdoc.gif";
188 } else if (mimeTypeLower.indexOf("application/vnd.ms-excel") != -1
189 || mimeTypeLower
190 .indexOf("application/vnd.stardivision.calc") != -1
191 || mimeTypeLower
192 .indexOf("application/vnd.sun.xml.calc") != -1) {
193 iconFileName = "file_types_spreadsheet.gif";
194 } else if (mimeTypeLower
195 .indexOf("application/vnd.sun.xml.writer") != -1) {
196 iconFileName = "file_types_oodoc.gif";
197 } else if (mimeTypeLower
198 .indexOf("application/vnd.stardivision.impress") != -1
199 || mimeTypeLower
200 .indexOf("application/vnd.sun.xml.impress") != -1
201 || mimeTypeLower
202 .indexOf("application/vnd.ms-powerpoint") != -1) {
203 iconFileName = "file_types_presentation.gif";
204 }
205
206 if (iconFileName == null) {
207 iconFileName = "file_types_unknown.gif";
208 }
209 return iconDir + iconFileName;
210 }
211 /***
212 * <p>
213 * Private default constructor ensures utility class functionality.
214 * </p>
215 */
216 private MimeTypesHandling() {
217 }
218 }