KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webdav > ant > Mimetypes


1 // vi: set ts=3 sw=3:
2
/*
3  * $Header: /home/cvs/jakarta-slide/webdavclient/ant/src/java/org/apache/webdav/ant/Mimetypes.java,v 1.3 2004/07/28 09:31:49 ib Exp $
4  * $Revision: 1.3 $
5  * $Date: 2004/07/28 09:31:49 $
6  * ========================================================================
7  * Copyright 2004 The Apache Software Foundation
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ========================================================================
21  */

22 package org.apache.webdav.ant;
23
24 import java.io.File JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Map JavaDoc;
28 import java.util.ResourceBundle JavaDoc;
29
30
31 /**
32  * Helper for mapping file extensions to mime-types.
33  *
34  */

35 public class Mimetypes
36 {
37    private static Map JavaDoc mimetypes = new HashMap JavaDoc();
38    
39    static {
40       ResourceBundle JavaDoc rb =
41          ResourceBundle.getBundle("org.apache.webdav.ant.mimetypes");
42       
43       for(Enumeration JavaDoc e = rb.getKeys(); e.hasMoreElements(); ) {
44          String JavaDoc ext = (String JavaDoc)e.nextElement();
45          mimetypes.put(ext, rb.getString(ext));
46       }
47    }
48    
49    public static String JavaDoc getMimeTypeForExtension(String JavaDoc extension, String JavaDoc defaultType) {
50       String JavaDoc mime = (String JavaDoc)mimetypes.get(extension);
51       if (mime == null) mime = defaultType;
52       return mime;
53    }
54    
55    public static String JavaDoc getMimeType(File JavaDoc file, String JavaDoc defaultType) {
56       String JavaDoc ext = file.getName();
57       int dotPos = ext.lastIndexOf('.');
58       if (dotPos != -1) {
59          return getMimeTypeForExtension(ext.substring(dotPos + 1), defaultType);
60       } else {
61          return defaultType;
62       }
63    }
64    public static String JavaDoc getMimeType(String JavaDoc file, String JavaDoc defaultType) {
65       int dotPos = file.lastIndexOf('.');
66       if (dotPos != -1) {
67          return getMimeTypeForExtension(file.substring(dotPos + 1), defaultType);
68       } else {
69          return defaultType;
70       }
71    }
72    
73    public static String JavaDoc getExtension(String JavaDoc fileName) {
74       int dotPos = fileName.lastIndexOf('.');
75       if (dotPos != -1) {
76          return fileName.substring(dotPos + 1);
77       } else {
78          return null;
79       }
80    }
81 }
82
Popular Tags