View Javadoc

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      /**
33       * Creates an empty instance; same as calling new MimeTypes().
34       *
35       * @return an empty instance
36       */
37      public static MimeTypes create() {
38          return new MimeTypes();
39      }
40  
41      /**
42       * Creates and returns a MimeTypes instance from the specified document.
43       */
44      public static MimeTypes create(Document document) {
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       */
54      public static MimeTypes create(InputStream inputStream) {
55          MimeTypes mimeTypes = new MimeTypes();
56          new MimeTypesReader(mimeTypes).read(inputStream);
57          return mimeTypes;
58      }
59  
60      /**
61       * Creates and returns a MimeTypes instance from the resource
62       * at the location specified by the URL.  Opens and closes the
63       * InputStream from the URL.
64       */
65      public static MimeTypes create(URL url) throws IOException {
66          InputStream stream = url.openStream();
67          try {
68              return create(stream);
69          } finally {
70              stream.close();
71          }
72      }
73  
74      /**
75       * Creates and returns a MimeTypes instance from the specified file path,
76       * as interpreted by the class loader in getResource().
77       */
78      public static MimeTypes create(String filePath) throws IOException {
79          return create(MimeTypesReader.class.getResource(filePath));
80      }
81  }