1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
package org.apache.any23.mime; |
19 | |
|
20 | |
import java.io.InputStream; |
21 | |
import java.util.HashMap; |
22 | |
import java.util.Map; |
23 | |
import java.util.regex.Matcher; |
24 | |
import java.util.regex.Pattern; |
25 | |
|
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | 0 | public class NaiveMIMETypeDetector implements MIMETypeDetector { |
31 | |
|
32 | 0 | private final static Map<String, String> extensions = new HashMap<String, String>() { |
33 | |
{ |
34 | |
|
35 | 0 | put("html" , "text/html" ); |
36 | 0 | put("htm" , "text/html" ); |
37 | 0 | put("xhtml", "application/xhtml+xml"); |
38 | 0 | put("xht" , "application/xhtml+xml"); |
39 | 0 | put("rdf" , "application/rdf+xml" ); |
40 | 0 | put("xrdf" , "application/rdf+xml" ); |
41 | 0 | put("rdfx" , "application/rdf+xml" ); |
42 | 0 | put("owl" , "application/rdf+xml" ); |
43 | 0 | put("nt" , "text/plain" ); |
44 | 0 | put("txt" , "text/plain" ); |
45 | 0 | put("ttl" , "application/x-turtle" ); |
46 | 0 | put("n3" , "text/rdf+n3" ); |
47 | 0 | } |
48 | |
}; |
49 | |
|
50 | 0 | private final static Pattern extensionRegex = Pattern.compile(".*\\.([a-z0-9]+)"); |
51 | |
|
52 | |
public MIMEType guessMIMEType( |
53 | |
String fileName, |
54 | |
InputStream input, |
55 | |
|
56 | |
MIMEType mimeTypeFromMetadata |
57 | |
) { |
58 | 0 | if (mimeTypeFromMetadata != null) { |
59 | 0 | return mimeTypeFromMetadata; |
60 | |
} |
61 | 0 | String extension = getExtension(fileName); |
62 | 0 | if (extension == null) { |
63 | |
|
64 | 0 | extension = "html"; |
65 | |
} |
66 | 0 | if (extensions.containsKey(extension)) { |
67 | 0 | return MIMEType.parse(extensions.get(extension)); |
68 | |
} |
69 | 0 | return null; |
70 | |
} |
71 | |
|
72 | |
private String getExtension(String filename) { |
73 | 0 | Matcher m = extensionRegex.matcher(filename); |
74 | 0 | if (!m.matches()) return null; |
75 | 0 | return m.group(1); |
76 | |
} |
77 | |
|
78 | |
} |