org.apache.tika.mime.MimeTypesFactory Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of tika-core Show documentation
Show all versions of tika-core Show documentation
This is the core Apache Tika™ toolkit library from which all other modules inherit functionality. It also
includes the core facades for the Tika API.
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.tika.mime;
import java.io.InputStream;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.w3c.dom.Document;
/**
* Creates instances of MimeTypes.
*/
public class MimeTypesFactory {
/**
* Creates an empty instance; same as calling new MimeTypes().
*
* @return an empty instance
*/
public static MimeTypes create() {
return new MimeTypes();
}
/**
* Creates and returns a MimeTypes instance from the specified document.
* @throws MimeTypeException if the type configuration is invalid
*/
public static MimeTypes create(Document document) throws MimeTypeException {
MimeTypes mimeTypes = new MimeTypes();
new MimeTypesReader(mimeTypes).read(document);
mimeTypes.init();
return mimeTypes;
}
/**
* Creates and returns a MimeTypes instance from the specified input stream.
* Does not close the input stream(s).
* @throws IOException if the stream can not be read
* @throws MimeTypeException if the type configuration is invalid
*/
public static MimeTypes create(InputStream... inputStreams)
throws IOException, MimeTypeException {
MimeTypes mimeTypes = new MimeTypes();
MimeTypesReader reader = new MimeTypesReader(mimeTypes);
for(InputStream inputStream : inputStreams) {
reader.read(inputStream);
}
mimeTypes.init();
return mimeTypes;
}
/** @see #create(InputStream...) */
public static MimeTypes create(InputStream stream)
throws IOException, MimeTypeException {
return create(new InputStream[] { stream });
}
/**
* Creates and returns a MimeTypes instance from the resource
* at the location specified by the URL. Opens and closes the
* InputStream from the URL.
* If multiple URLs are supplied, then they are loaded in turn.
*
* @throws IOException if the URL can not be accessed
* @throws MimeTypeException if the type configuration is invalid
*/
public static MimeTypes create(URL... urls)
throws IOException, MimeTypeException {
InputStream[] streams = new InputStream[urls.length];
for(int i=0; i extensionURLs = Collections.list(
classLoader.getResources(classPrefix+extensionFilePath));
// Swap that into an Array, and process
List urls = new ArrayList();
urls.add(coreURL);
urls.addAll(extensionURLs);
return create( urls.toArray(new URL[urls.size()]) );
}
}