View Javadoc

1   /*
2    * Copyright 2004-2006 the Seasar Foundation and the Others.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 
13   * either express or implied. See the License for the specific language
14   * governing permissions and limitations under the License.
15   */
16  package org.seasar.tuigwaa.util;
17  
18  import java.util.Enumeration;
19  import java.util.HashMap;
20  import java.util.Map;
21  import java.util.ResourceBundle;
22  
23  /***
24   * @author someda
25   */
26  public class MimeMappings {
27  
28  	public static final String DEFAULT_CONTENTTYPE = "application/octet-stream";
29  	
30  	private static final String MIME_PATH = "mime";
31  	private static ResourceBundle mimeResource;
32  	
33  	private static final Map EXTENSION_MAP;
34  	
35  	static{
36  		mimeResource = ResourceBundle.getBundle(MIME_PATH);		
37  		EXTENSION_MAP = new HashMap();
38  		
39  		Enumeration keys = mimeResource.getKeys();
40  		while(keys.hasMoreElements()){
41  			String extension = (String)keys.nextElement();
42  			String contentType = mimeResource.getString(extension);
43  			
44  			if(EXTENSION_MAP.get(contentType) == null){ // First Entry is registered
45  				EXTENSION_MAP.put(contentType,extension);
46  			}
47  		}
48  	}
49  	
50  	public static final String EXTENSION_HTML = "html";
51  	public static final String EXTENSION_XWIKI = "wiki";
52  	public static final String EXTENSION_XLS = "xls";
53  	public static final String EXTENSION_CSV = "csv";
54  	public static final String EXTENSION_TGWAR = "tgwar";
55  	public static final String EXTENSION_ZIP = "zip";
56  	public static final String EXTENSION_TXT = "txt";
57  	
58  	public static final String CONTENTTYPE_HTML = getContentType(EXTENSION_HTML);
59  	public static final String CONTENTTYPE_XWIKI = getContentType(EXTENSION_XWIKI);
60  	public static final String CONTENTTYPE_XLS = getContentType(EXTENSION_XLS);
61  	public static final String CONTENTTYPE_CSV = getContentType(EXTENSION_CSV);
62  	public static final String CONTENTTYPE_TXT = getContentType(EXTENSION_TXT);
63  	
64  	// ----- [static methods] -----
65  		
66  	public static String getContentType(String extension){
67  		String contentType = (String) mimeResource.getString(extension);
68  		if(contentType == null || "".equals(contentType)){
69  			contentType = DEFAULT_CONTENTTYPE;
70  		}		
71  		return contentType;
72  	}
73  	
74  	public static String getExtension(String contentType){
75  		return (String)EXTENSION_MAP.get(contentType);
76  	}
77  			
78  }