KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > impl > FileTypeMap


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
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, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.vfs.impl;
17
18 import org.apache.commons.vfs.FileContent;
19 import org.apache.commons.vfs.FileObject;
20 import org.apache.commons.vfs.FileSystemException;
21
22 import java.util.HashMap JavaDoc;
23 import java.util.Map JavaDoc;
24
25 /**
26  * A helper class that determines the provider to use for a file.
27  *
28  * @author <a HREF="mailto:adammurdoch@apache.org">Adam Murdoch</a>
29  */

30 class FileTypeMap
31 {
32     private final Map JavaDoc mimeTypeMap = new HashMap JavaDoc();
33     private final Map JavaDoc extensionMap = new HashMap JavaDoc();
34
35     /**
36      * Adds a MIME type mapping.
37      */

38     public void addMimeType(final String JavaDoc mimeType, final String JavaDoc scheme)
39     {
40         mimeTypeMap.put(mimeType, scheme);
41     }
42
43     /**
44      * Adds a filename extension mapping.
45      */

46     public void addExtension(final String JavaDoc extension, final String JavaDoc scheme)
47     {
48         extensionMap.put(extension, scheme);
49     }
50
51     /**
52      * Finds the provider to use to create a filesystem from a given file.
53      */

54     public String JavaDoc getScheme(final FileObject file) throws FileSystemException
55     {
56         // Check the file's mime type for a match
57
final FileContent content = file.getContent();
58         // final String mimeType = (String) content.getAttribute("content-type");
59
final String JavaDoc mimeType = content.getContentInfo().getContentType();
60         if (mimeType != null)
61         {
62             return (String JavaDoc) mimeTypeMap.get(mimeType);
63         }
64
65         // Check the file's extension for a match
66
final String JavaDoc extension = file.getName().getExtension();
67         return (String JavaDoc) extensionMap.get(extension);
68     }
69 }
70
Popular Tags