##### Coverage Report - org.apache.any23.mime.NaiveMIMETypeDetector

Classes in this File | Line Coverage | Branch Coverage | Complexity
NaiveMIMETypeDetector |
0% | 0/14
0% | 0/8
| 4.5;4.5
NaiveMIMETypeDetector$1 |
0% | 0/14
N/A | N/A
| 4.5;4.5

1 |  |
     /*
2 |  |
      * Licensed to the Apache Software Foundation (ASF) under one or more
3 |  |
      * contributor license agreements.  See the NOTICE file distributed with
4 |  |
      * this work for additional information regarding copyright ownership.
5 |  |
      * The ASF licenses this file to You under the Apache License, Version 2.0
6 |  |
      * (the "License"); you may not use this file except in compliance with
7 |  |
      * the License.  You may obtain a copy of the License at
8 |  |
      *
9 |  |
      *  http://www.apache.org/licenses/LICENSE-2.0
10 |  |
      *
11 |  |
      * Unless required by applicable law or agreed to in writing, software
12 |  |
      * distributed under the License is distributed on an "AS IS" BASIS,
13 |  |
      * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 |  |
      * See the License for the specific language governing permissions and
15 |  |
      * limitations under the License.
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 |  |
      * Basic implementation of {@link MIMETypeDetector} based
28 |  |
      * on file extensions.
29 |  |
      */
30 | 0 |
     public class NaiveMIMETypeDetector implements MIMETypeDetector {
31 |  |
32 | 0 |
         private final static Map<String, String> extensions = new HashMap<String, String>() {
33 |  |
             {
34 |  |
                 // extension -> mime type
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 |  |
                 // Assume index file on web server.
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 |  |
     }