use of org.apache.tika.mime.MimeTypes in project tika by apache.
the class TikaDetectorsTest method testGetHTML.
@Test
public void testGetHTML() throws Exception {
Response response = WebClient.create(endPoint + DETECTORS_PATH).type("text/html").accept("text/html").get();
String text = getStringFromInputStream((InputStream) response.getEntity());
assertContains("<h2>DefaultDetector</h2>", text);
assertContains("Composite", text);
assertContains("<h3>OggDetector", text);
assertContains("<h3>POIFSContainerDetector", text);
assertContains("<h3>MimeTypes", text);
assertContains(OggDetector.class.getName(), text);
assertContains(POIFSContainerDetector.class.getName(), text);
assertContains(ZipContainerDetector.class.getName(), text);
assertContains(MimeTypes.class.getName(), text);
}
use of org.apache.tika.mime.MimeTypes in project tika by apache.
the class MyFirstTika method parseUsingComponents.
public static String parseUsingComponents(String filename, TikaConfig tikaConfig, Metadata metadata) throws Exception {
MimeTypes mimeRegistry = tikaConfig.getMimeRepository();
System.out.println("Examining: [" + filename + "]");
metadata.set(Metadata.RESOURCE_NAME_KEY, filename);
System.out.println("The MIME type (based on filename) is: [" + mimeRegistry.detect(null, metadata) + "]");
InputStream stream = TikaInputStream.get(new File(filename));
System.out.println("The MIME type (based on MAGIC) is: [" + mimeRegistry.detect(stream, metadata) + "]");
stream = TikaInputStream.get(new File(filename));
Detector detector = tikaConfig.getDetector();
System.out.println("The MIME type (based on the Detector interface) is: [" + detector.detect(stream, metadata) + "]");
LanguageDetector langDetector = new OptimaizeLangDetector().loadModels();
LanguageResult lang = langDetector.detect(FileUtils.readFileToString(new File(filename), UTF_8));
System.out.println("The language of this content is: [" + lang.getLanguage() + "]");
// Get a non-detecting parser that handles all the types it can
Parser parser = tikaConfig.getParser();
// Tell it what we think the content is
MediaType type = detector.detect(stream, metadata);
metadata.set(Metadata.CONTENT_TYPE, type.toString());
// Have the file parsed to get the content and metadata
ContentHandler handler = new BodyContentHandler();
parser.parse(stream, handler, metadata, new ParseContext());
return handler.toString();
}
use of org.apache.tika.mime.MimeTypes in project tika by apache.
the class EmbeddedDocumentUtil method getExtension.
public String getExtension(TikaInputStream is, Metadata metadata) {
String mimeString = metadata.get(Metadata.CONTENT_TYPE);
TikaConfig config = getConfig();
MimeType mimeType = null;
MimeTypes types = config.getMimeRepository();
boolean detected = false;
if (mimeString != null) {
try {
mimeType = types.forName(mimeString);
} catch (MimeTypeException e) {
//swallow
}
}
if (mimeType == null) {
Detector detector = config.getDetector();
try {
MediaType mediaType = detector.detect(is, metadata);
mimeType = types.forName(mediaType.toString());
detected = true;
is.reset();
} catch (IOException e) {
//swallow
} catch (MimeTypeException e) {
//swallow
}
}
if (mimeType != null) {
if (detected) {
//set or correct the mime type
metadata.set(Metadata.CONTENT_TYPE, mimeType.toString());
}
return mimeType.getExtension();
}
return ".bin";
}
use of org.apache.tika.mime.MimeTypes in project tika by apache.
the class CustomMimeInfo method customMimeInfo.
public static String customMimeInfo() throws Exception {
String path = "file:///path/to/prescription-type.xml";
MimeTypes typeDatabase = MimeTypesFactory.create(new URL(path));
Tika tika = new Tika(typeDatabase);
String type = tika.detect("/path/to/prescription.xpd");
return type;
}
use of org.apache.tika.mime.MimeTypes in project tika by apache.
the class CustomMimeInfo method customCompositeDetector.
public static String customCompositeDetector() throws Exception {
String path = "file:///path/to/prescription-type.xml";
MimeTypes typeDatabase = MimeTypesFactory.create(new URL(path));
Tika tika = new Tika(new CompositeDetector(typeDatabase, new EncryptedPrescriptionDetector()));
String type = tika.detect("/path/to/tmp/prescription.xpd");
return type;
}
Aggregations