KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > media > MimeType


1 /******************************************************************************
2  * MimeType.java
3  * ****************************************************************************/

4
5 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
6 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
7 * Use is subject to license terms. *
8 * J_LZ_COPYRIGHT_END *********************************************************/

9
10 package org.openlaszlo.media;
11
12 import org.openlaszlo.utils.FileUtils;
13
14 /**
15  * A class for defining mimetypes and utilities.
16  *
17  * TODO:[2002-12-3 bloch] Someday this should be refactored for plugins
18  *
19  * @author Eric Bloch
20  * @version 1.0
21  */

22 public class MimeType {
23     public static final String JavaDoc TEXT = "text/html";
24     public static final String JavaDoc SWF = "application/x-shockwave-flash";
25     public static final String JavaDoc JPEG = "image/jpeg";
26     public static final String JavaDoc GIF = "image/gif";
27     public static final String JavaDoc PNG = "image/png";
28     public static final String JavaDoc MP3 = "audio/mpeg";
29     public static final String JavaDoc XMP3 = "audio/x-mpeg";
30     public static final String JavaDoc XML = "text/xml";
31     public static final String JavaDoc HTML = "text/html";
32
33     public static final String JavaDoc UNKNOWN = "unknown";
34
35     /**
36      * @return a mimetype string based on the extension (chars after the '.')
37      * @return <code>UNKNOWN</code> if no guess can be made
38      */

39     public static final String JavaDoc fromExtension(final String JavaDoc name) {
40         String JavaDoc extension = FileUtils.getExtension(name);
41         if (extension == null) {
42             return UNKNOWN;
43         }
44         if (extension.equals("") ) {
45             return UNKNOWN;
46         }
47         if (extension.equalsIgnoreCase("swf")) {
48             return SWF;
49         }
50         if (extension.equalsIgnoreCase("jpg") ||
51             extension.equalsIgnoreCase("jpeg")) {
52             return JPEG;
53         }
54         if (extension.equalsIgnoreCase("gif")) {
55             return GIF;
56         }
57         if (extension.equalsIgnoreCase("png")) {
58             return PNG;
59         }
60         if (extension.equalsIgnoreCase("xml")) {
61             return XML;
62         }
63         if (extension.equalsIgnoreCase("lzx")) {
64             return XML;
65         }
66         if (extension.equalsIgnoreCase("html") ||
67             extension.equalsIgnoreCase("htm")) {
68             return HTML;
69         }
70         if (extension.equalsIgnoreCase("mp3") ||
71             extension.equalsIgnoreCase("mpeg")) {
72             return MP3;
73         }
74         return UNKNOWN;
75     }
76
77     /**
78      * @param mimeType mimetype to give extension of
79      * @return a file type extension string based on the extension (chars after the '.').
80      * If no such mimetype is found, the mimeType string is returned.
81      */

82     public static final String JavaDoc toExtension(final String JavaDoc mimeType) {
83
84         if (mimeType == MimeType.JPEG) {
85             return "jpeg";
86         }
87         if (mimeType == MimeType.SWF) {
88             return "swf";
89         }
90         if (mimeType == MimeType.GIF) {
91             return "gif";
92         }
93         if (mimeType == MimeType.PNG) {
94             return "png";
95         }
96         if (mimeType == MimeType.MP3) {
97             return "mp3";
98         }
99         if (mimeType == MimeType.XMP3) {
100             return "mp3";
101         }
102
103         return mimeType;
104     }
105 }
106
Popular Tags