View Javadoc

1   /*
2    * <cvs:source>$Source: /devel/astrogrid/slinger/src/java/org/astrogrid/slinger/mime/MimeNames.java,v $</cvs:source>
3    * <cvs:author>$Author: mch $</cvs:author>
4    * <cvs:date>$Date: 2005/01/26 17:31:56 $</cvs:date>
5    * <cvs:version>$Revision: 1.3 $</cvs:version>
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; } //must have initialised on another thread
32        
33        humanLookup = new Hashtable();
34        mimeLookup = new Hashtable();
35        
36        //populate lookup tables
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        //if mime is null, it might be because the humanstring is actually a mime
63        //type, so return that
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           //give up finding a nice one and just return the mime type...
89           friendly = mimeType;
90        }
91        
92        return friendly;
93     }
94  }
95  /*
96   *   $Log: MimeNames.java,v $
97   *   Revision 1.3  2005/01/26 17:31:56  mch
98   *   Split slinger out to scapi, swib, etc.
99   *
100  *   Revision 1.1.2.3  2004/12/08 18:37:11  mch
101  *   Introduced SPI and SPL
102  *
103  *   Revision 1.1.2.2  2004/11/25 18:28:21  mch
104  *   More tarting up
105  *
106  *   Revision 1.1.2.1  2004/11/25 07:18:13  mch
107  *   added mime names
108  *
109  */