1
2
3
4
5
6
7
8
9
10 package org.astrogrid.slinger.mime ;
11
12 /***
13 * Converters between 'human' names and official mime types
14 *
15 */
16 import java.util.Hashtable;
17
18 public class MimeNames implements MimeTypes {
19
20 private static Hashtable humanLookup= null;
21 private static Hashtable mimeLookup= null;
22
23
24 private static void addLookup(String mime, String human) {
25 humanLookup.put(mime.trim().toLowerCase(), human);
26 mimeLookup.put(human.trim().toLowerCase(), mime);
27 }
28
29 public synchronized static void initialise() {
30
31 if (humanLookup != null) { return; }
32
33 humanLookup = new Hashtable();
34 mimeLookup = new Hashtable();
35
36
37 addLookup(PLAINTEXT, "Text");
38 addLookup(VOTABLE, "VOTable");
39 addLookup(HTML, "HTML");
40 addLookup(CSV, "Comma-Separated");
41 addLookup(TSV, "Tab-Separated");
42 addLookup(FITS, "FITS");
43 }
44
45 /***
46 * Guess the mime type from a given 'human' string (eg 'VOTable')
47 * @return The mime type if the filename has a recognised .extension, otherwise null.
48 *
49 */
50 public static String getMimeType(String humanString) {
51
52 if (humanLookup == null) {
53 initialise();
54 }
55
56 if (null == humanString) {
57 return null;
58 }
59
60 String mime = (String) mimeLookup.get(humanString.toLowerCase());
61
62
63
64 if (mime == null) {
65 return humanString;
66 }
67 return mime;
68
69 }
70
71 /***
72 * Get a human-friendly string corresponding to the given mime type
73 *
74 */
75 public static String humanFriendly(String mimeType) {
76
77 if (humanLookup == null) {
78 initialise();
79 }
80
81 if (null == mimeType) {
82 return null;
83 }
84
85 String friendly = (String) humanLookup.get(mimeType.toLowerCase());
86
87 if (friendly == null) {
88
89 friendly = mimeType;
90 }
91
92 return friendly;
93 }
94 }
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109