/* * 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. */
/** * Demonstrates how to call the different components within Tika: its * {@link Detector} framework (aka MIME identification and repository), its * {@link Parser} interface, its {@link org.apache.tika.language.LanguageIdentifier} and other goodies. * <p> * It also shows the "easy way" via {@link AutoDetectParser} */ publicclassMyFirstTika { publicstaticvoidmain(String[] args)throws Exception { Stringfilename= args[0]; TikaConfigtikaConfig= TikaConfig.getDefaultConfig();
metadata.set(TikaCoreProperties.RESOURCE_NAME_KEY, filename); System.out.println( "The MIME type (based on filename) is: [" + mimeRegistry.detect(null, metadata) + "]");
InputStreamstream= TikaInputStream.get(newFile(filename)); System.out.println( "The MIME type (based on MAGIC) is: [" + mimeRegistry.detect(stream, metadata) + "]");
stream = TikaInputStream.get(newFile(filename)); Detectordetector= tikaConfig.getDetector(); System.out.println("The MIME type (based on the Detector interface) is: [" + detector.detect(stream, metadata) + "]");
System.out.println("The language of this content is: [" + lang.getLanguage() + "]");
// Get a non-detecting parser that handles all the types it can Parserparser= tikaConfig.getParser(); // Tell it what we think the content is MediaTypetype= detector.detect(stream, metadata); metadata.set(Metadata.CONTENT_TYPE, type.toString()); // Have the file parsed to get the content and metadata ContentHandlerhandler=newBodyContentHandler(); parser.parse(stream, handler, metadata, newParseContext());