View Javadoc

1   /*
2    * Copyright (c) 2001 - 2005 ivata limited.
3    * All rights reserved.
4    * -----------------------------------------------------------------------------
5    * ivata masks may be redistributed under the GNU General Public
6    * License as published by the Free Software Foundation;
7    * version 2 of the License.
8    *
9    * These programs are free software; you can redistribute them and/or
10   * modify them under the terms of the GNU General Public License
11   * as published by the Free Software Foundation; version 2 of the License.
12   *
13   * These programs are distributed in the hope that they will be useful,
14   * but WITHOUT ANY WARRANTY; without even the implied warranty of
15   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16   *
17   * See the GNU General Public License in the file LICENSE.txt for more
18   * details.
19   *
20   * If you would like a copy of the GNU General Public License write to
21   *
22   * Free Software Foundation, Inc.
23   * 59 Temple Place - Suite 330
24   * Boston, MA 02111-1307, USA.
25   *
26   *
27   * To arrange commercial support and licensing, contact ivata at
28   *                  http://www.ivata.com/contact.jsp
29   * -----------------------------------------------------------------------------
30   * $Log: MimeTypesHandling.java,v $
31   * Revision 1.3  2005/04/09 18:04:17  colinmacleod
32   * Changed copyright text to GPL v2 explicitly.
33   *
34   * Revision 1.2  2005/01/06 22:21:45  colinmacleod
35   * Moved up a version number.
36   * Changed copyright notices to 2005.
37   * Updated the documentation:
38   *   - started working on multiproject:site docu.
39   *   - changed the logo.
40   * Added checkstyle and fixed LOADS of style issues.
41   * Added separate thirdparty subproject.
42   * Added struts (in web), util and webgui (in webtheme) from ivata op.
43   *
44   * Revision 1.4  2004/03/21 21:16:36  colinmacleod
45   * Shortened name to ivata op.
46   *
47   * Revision 1.3  2004/02/10 19:57:25  colinmacleod
48   * Changed email address.
49   *
50   * Revision 1.2  2004/02/01 22:07:32  colinmacleod
51   * Added full names to author tags
52   *
53   * Revision 1.1.1.1  2004/01/27 20:59:46  colinmacleod
54   * Moved ivata op to SourceForge.
55   *
56   * Revision 1.2  2003/10/15 14:13:53  colin
57   * fixing for XDoclet
58   *
59   * Revision 1.9  2003/06/12 09:06:44  peter
60   * added logic for presentations, oo writer and more spreadsheets
61   *
62   * Revision 1.8  2003/06/03 10:42:30  peter
63   * added rtf type and removed application for unknown starting with application/
64   *
65   * Revision 1.7  2003/02/28 17:24:58  peter
66   * fixed MimeType2Icon, substring check implemented ...
67   *
68   * Revision 1.6  2003/02/27 17:25:34  peter
69   * fixed a bug in MimeType2Image - image mime types
70   *
71   * Revision 1.5  2003/02/24 19:27:31  colin
72   * restructured file paths
73   * Revision 1.4  2003/02/04 17:43:52  colin
74   * copyright notice
75   * Revision 1.3  2002/08/27 15:24:32  peter
76   * mimeType2Icon Method added
77   * Revision 1.2  2002/08/15 08:27:04  peter
78   * modified
79   * Revision 1.1  2002/08/08 15:03:06  peter
80   * created
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         //if still not set, it's unknown...
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 }