1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package no.sintef.file;
22
23 import java.io.InvalidObjectException;
24 import java.io.ObjectStreamException;
25 import java.io.Serializable;
26 import java.util.ArrayList;
27 import java.util.Arrays;
28 import java.util.Collections;
29 import java.util.HashMap;
30 import java.util.Iterator;
31 import java.util.List;
32
33
34 /***
35 * Mime type enumeration and helper methods.
36 *
37 * @author Fredrik Vraalsen
38 */
39 public final class MimeTypeEnum implements Serializable {
40
41 public static final MimeTypeEnum TEXT_PLAIN = new MimeTypeEnum("text/plain");
42 public static final MimeTypeEnum TEXT_RTF = new MimeTypeEnum("text/rtf");
43 public static final MimeTypeEnum TEXT_XML = new MimeTypeEnum("text/xml");
44 public static final MimeTypeEnum TEXT_HTML = new MimeTypeEnum("text/html");
45 public static final MimeTypeEnum IMAGE_GIF = new MimeTypeEnum("image/gif");
46 public static final MimeTypeEnum IMAGE_PNG = new MimeTypeEnum("image/png");
47 public static final MimeTypeEnum IMAGE_JPEG = new MimeTypeEnum("image/jpeg");
48 public static final MimeTypeEnum IMAGE_SVG = new MimeTypeEnum("image/svg+xml");
49 public static final MimeTypeEnum APPLICATION_MSWORD = new MimeTypeEnum("application/msword");
50 public static final MimeTypeEnum APPLICATION_PDF = new MimeTypeEnum("application/pdf");
51 public static final MimeTypeEnum APPLICATION_ZIP = new MimeTypeEnum("application/zip");
52 public static final MimeTypeEnum APPLICATION_BINARY = new MimeTypeEnum("application/binary");
53 private static final MimeTypeEnum DEFAULT_MIMETYPE = APPLICATION_BINARY;
54
55 private static final MimeTypeEnum[] VALS = {
56 TEXT_PLAIN,
57 TEXT_RTF,
58 TEXT_XML,
59 TEXT_HTML,
60 IMAGE_GIF,
61 IMAGE_PNG,
62 IMAGE_JPEG,
63 IMAGE_SVG,
64 APPLICATION_MSWORD,
65 APPLICATION_PDF,
66 APPLICATION_ZIP,
67 APPLICATION_BINARY
68 };
69
70 public static final List VALUES = Collections.unmodifiableList(Arrays.asList(VALS));
71 private static final List NAMES = new ArrayList();
72 private static final HashMap MIME_TYPES = new HashMap();
73
74 static {
75 for (Iterator i = VALUES.iterator(); i.hasNext();) {
76 NAMES.add(i.next().toString());
77 }
78 MIME_TYPES.put("dgm", TEXT_XML);
79 MIME_TYPES.put("xml", TEXT_XML);
80 MIME_TYPES.put("xmi", TEXT_XML);
81 MIME_TYPES.put("xsd", TEXT_XML);
82 MIME_TYPES.put("txt", TEXT_PLAIN);
83 MIME_TYPES.put("rtf", TEXT_RTF);
84 MIME_TYPES.put("html", TEXT_HTML);
85 MIME_TYPES.put("htm", TEXT_HTML);
86 MIME_TYPES.put("gif", IMAGE_GIF);
87 MIME_TYPES.put("png", IMAGE_PNG);
88 MIME_TYPES.put("jpg", IMAGE_JPEG);
89 MIME_TYPES.put("jpeg", IMAGE_JPEG);
90 MIME_TYPES.put("svg", IMAGE_SVG);
91 MIME_TYPES.put("doc", APPLICATION_MSWORD);
92 MIME_TYPES.put("pdf", APPLICATION_PDF);
93 MIME_TYPES.put("zip", APPLICATION_ZIP);
94 }
95
96 private final String mimeType;
97
98 /***
99 * Private constructor.
100 *
101 * @param mimeType mime type string
102 */
103 private MimeTypeEnum(String mimeType) {
104 this.mimeType = mimeType;
105 }
106
107 /***
108 * Get MimeTypeEnum corresponding to mime type string.
109 *
110 * @param mimeType
111 * mime type string
112 * @return MimeTypeEnum corresponding to mimeType, or null if not found
113 */
114 public static MimeTypeEnum forName(String mimeType) {
115 int index = NAMES.indexOf(mimeType);
116 if (index < 0 || index >= VALS.length) {
117 return null;
118 } else {
119 return VALS[index];
120 }
121 }
122
123 /***
124 * @return mime type string for this MimeTypeEnum
125 */
126 public String toString() {
127 return mimeType;
128 }
129
130 /***
131 * Determine whether MimeTypeEnum represents an image type.
132 *
133 * @param type
134 * the MimeTypeEnum to check
135 * @return true if MimeTypeEnum represents an image type, false otherwise
136 */
137 public static boolean isImage(MimeTypeEnum type) {
138 return (type == MimeTypeEnum.IMAGE_GIF
139 || type == MimeTypeEnum.IMAGE_JPEG
140 || type == MimeTypeEnum.IMAGE_PNG
141 || type == MimeTypeEnum.IMAGE_SVG);
142 }
143
144 /***
145 * Determine whether MimeTypeEnum represents a text type.
146 *
147 * @param type
148 * the MimeTypeEnum to check
149 * @return true if MimeTypeEnum represents a text type, false otherwise
150 */
151 public static boolean isText(MimeTypeEnum type) {
152 return (type == MimeTypeEnum.TEXT_PLAIN
153 || type == MimeTypeEnum.TEXT_HTML
154 || type == MimeTypeEnum.TEXT_XML);
155 }
156
157 /***
158 * Get MimeTypeEnum corresponding to filename.
159 *
160 * @param filename filename to determine mime type for
161 * @return MimeTypeEnum corresponding to filename, or DEFAULT_MIMETYPE if
162 * not found
163 */
164 public static MimeTypeEnum getMimeType(String filename) {
165 String suffix = FileSuffixFilter.getSuffix(filename);
166 if (suffix == null) {
167 return DEFAULT_MIMETYPE;
168 }
169 MimeTypeEnum result = (MimeTypeEnum) MIME_TYPES.get(suffix.toLowerCase());
170 return (result != null ? result : DEFAULT_MIMETYPE);
171 }
172
173 /***
174 * Enumeration pattern: Ensure that a.equals(b) <=> (a == b) for all
175 * MimeTypeEnums a and b when deserializing.
176 *
177 * @return the MimeTypeEnum corresponding to the mime type string
178 * @throws ObjectStreamException
179 * if no MimeTypeEnum can be found for that mime type string
180 */
181 private Object readResolve() throws ObjectStreamException {
182 MimeTypeEnum result = forName(mimeType);
183 if (result == null) {
184 throw new InvalidObjectException("Illegal MimeTypeEnum instance: " + mimeType);
185 }
186 return result;
187 }
188
189 }