KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > io > DefaultMimeTypeTable


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.core.io;
17
18 import java.io.File JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.util.Hashtable JavaDoc;
21 import java.util.Map JavaDoc;
22 import java.util.regex.Matcher JavaDoc;
23 import java.util.regex.Pattern JavaDoc;
24
25 public class DefaultMimeTypeTable {
26
27     private static Map JavaDoc mimeTable = loadTable();
28
29     private static Map JavaDoc<String JavaDoc, String JavaDoc> loadTable() {
30         Map JavaDoc<String JavaDoc, String JavaDoc> result = new Hashtable JavaDoc<String JavaDoc, String JavaDoc>();
31
32         try {
33             String JavaDoc table = DiskIO
34                     .readStringFromResource("org/columba/core/config/mime_table");
35             Pattern JavaDoc listPattern = Pattern.compile("(\\w+) (\\w+/\\w+)");
36             Matcher JavaDoc matcher = listPattern.matcher(table);
37             while (matcher.find()) {
38                 result.put(matcher.group(1), matcher.group(2));
39             }
40         } catch (IOException JavaDoc e) {
41             // do nothing here
42
}
43
44         return result;
45     }
46
47     public static String JavaDoc lookup(File JavaDoc file) {
48         int dotPos = file.getName().lastIndexOf('.');
49         String JavaDoc _return = lookup(file.getName().substring(dotPos + 1));
50         if (dotPos == -1 || dotPos == file.getName().length() - 1) {
51             _return = "application/octet-stream";
52         }
53         return _return;
54
55     }
56
57     public static String JavaDoc lookup(String JavaDoc string) {
58         String JavaDoc lookupResult = (String JavaDoc) mimeTable.get(string);
59
60         if (lookupResult == null) {
61             lookupResult = "application/octet-stream";
62         }
63
64         return lookupResult;
65     }
66
67 }
68
Popular Tags