MimeTypeMap.java revision 0f11696164e1dadd2d33dddeca418f719f26bc10
1/* 2 * Copyright (C) 2007 The Android Open Source Project 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 17package android.webkit; 18 19import android.text.TextUtils; 20import java.util.HashMap; 21import java.util.regex.Pattern; 22import libcore.net.MimeUtils; 23 24/** 25 * Two-way map that maps MIME-types to file extensions and vice versa. 26 * 27 * <p>See also {@link java.net.URLConnection#guessContentTypeFromName} 28 * and {@link java.net.URLConnection#guessContentTypeFromStream}. This 29 * class and {@code URLConnection} share the same MIME-type database. 30 */ 31public class MimeTypeMap { 32 private static final MimeTypeMap sMimeTypeMap = new MimeTypeMap(); 33 34 private MimeTypeMap() { 35 } 36 37 /** 38 * Returns the file extension or an empty string iff there is no 39 * extension. This method is a convenience method for obtaining the 40 * extension of a url and has undefined results for other Strings. 41 * @param url 42 * @return The file extension of the given url. 43 */ 44 public static String getFileExtensionFromUrl(String url) { 45 if (!TextUtils.isEmpty(url)) { 46 int query = url.lastIndexOf('?'); 47 if (query > 0) { 48 url = url.substring(0, query); 49 } 50 int filenamePos = url.lastIndexOf('/'); 51 String filename = 52 0 <= filenamePos ? url.substring(filenamePos + 1) : url; 53 54 // if the filename contains special characters, we don't 55 // consider it valid for our matching purposes: 56 if (!filename.isEmpty() && 57 Pattern.matches("[a-zA-Z_0-9\\.\\-\\(\\)\\%]+", filename)) { 58 int dotPos = filename.lastIndexOf('.'); 59 if (0 <= dotPos) { 60 return filename.substring(dotPos + 1); 61 } 62 } 63 } 64 65 return ""; 66 } 67 68 /** 69 * Return true if the given MIME type has an entry in the map. 70 * @param mimeType A MIME type (i.e. text/plain) 71 * @return True iff there is a mimeType entry in the map. 72 */ 73 public boolean hasMimeType(String mimeType) { 74 return MimeUtils.hasMimeType(mimeType); 75 } 76 77 /** 78 * Return the MIME type for the given extension. 79 * @param extension A file extension without the leading '.' 80 * @return The MIME type for the given extension or null iff there is none. 81 */ 82 public String getMimeTypeFromExtension(String extension) { 83 return MimeUtils.guessMimeTypeFromExtension(extension); 84 } 85 86 // Static method called by jni. 87 private static String mimeTypeFromExtension(String extension) { 88 return MimeUtils.guessMimeTypeFromExtension(extension); 89 } 90 91 /** 92 * Return true if the given extension has a registered MIME type. 93 * @param extension A file extension without the leading '.' 94 * @return True iff there is an extension entry in the map. 95 */ 96 public boolean hasExtension(String extension) { 97 return MimeUtils.hasExtension(extension); 98 } 99 100 /** 101 * Return the registered extension for the given MIME type. Note that some 102 * MIME types map to multiple extensions. This call will return the most 103 * common extension for the given MIME type. 104 * @param mimeType A MIME type (i.e. text/plain) 105 * @return The extension for the given MIME type or null iff there is none. 106 */ 107 public String getExtensionFromMimeType(String mimeType) { 108 return MimeUtils.guessExtensionFromMimeType(mimeType); 109 } 110 111 /** 112 * Get the singleton instance of MimeTypeMap. 113 * @return The singleton instance of the MIME-type map. 114 */ 115 public static MimeTypeMap getSingleton() { 116 return sMimeTypeMap; 117 } 118} 119