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 package org.apache.tika.mime; 18 19 import java.io.InputStream; 20 import java.io.IOException; 21 import java.net.URL; 22 23 import org.w3c.dom.Document; 24 25 26 /** 27 * Creates instances of MimeTypes. 28 */ 29 public class MimeTypesFactory { 30 31 /** 32 * Creates an empty instance; same as calling new MimeTypes(). 33 * 34 * @return an empty instance 35 */ 36 public static MimeTypes create() { 37 return new MimeTypes(); 38 } 39 40 /** 41 * Creates and returns a MimeTypes instance from the specified document. 42 * @throws MimeTypeException if the type configuration is invalid 43 */ 44 public static MimeTypes create(Document document) throws MimeTypeException { 45 MimeTypes mimeTypes = new MimeTypes(); 46 new MimeTypesReader(mimeTypes).read(document); 47 return mimeTypes; 48 } 49 50 /** 51 * Creates and returns a MimeTypes instance from the specified input stream. 52 * Does not close the input stream. 53 * @throws IOException if the stream can not be read 54 * @throws MimeTypeException if the type configuration is invalid 55 */ 56 public static MimeTypes create(InputStream inputStream) 57 throws IOException, MimeTypeException { 58 MimeTypes mimeTypes = new MimeTypes(); 59 new MimeTypesReader(mimeTypes).read(inputStream); 60 return mimeTypes; 61 } 62 63 /** 64 * Creates and returns a MimeTypes instance from the resource 65 * at the location specified by the URL. Opens and closes the 66 * InputStream from the URL. 67 * 68 * @throws IOException if the URL can not be accessed 69 * @throws MimeTypeException if the type configuration is invalid 70 */ 71 public static MimeTypes create(URL url) 72 throws IOException, MimeTypeException { 73 InputStream stream = url.openStream(); 74 try { 75 return create(stream); 76 } finally { 77 stream.close(); 78 } 79 } 80 81 /** 82 * Creates and returns a MimeTypes instance from the specified file path, 83 * as interpreted by the class loader in getResource(). 84 * 85 * @throws IOException if the file can not be accessed 86 * @throws MimeTypeException if the type configuration is invalid 87 */ 88 public static MimeTypes create(String filePath) 89 throws IOException, MimeTypeException { 90 return create(MimeTypesReader.class.getResource(filePath)); 91 } 92 }