@@ -0,0 +1,3296 @@
+/*%0A * Licensed to the Apache Software Foundation (ASF) under one or more%0A * contributor license agreements.  See the NOTICE file distributed with%0A * this work for additional information regarding copyright ownership.%0A * The ASF licenses this file to You under the Apache License, Version 2.0%0A * (the %22License%22); you may not use this file except in compliance with%0A * the License.  You may obtain a copy of the License at%0A *%0A *     http://www.apache.org/licenses/LICENSE-2.0%0A *%0A * Unless required by applicable law or agreed to in writing, software%0A * distributed under the License is distributed on an %22AS IS%22 BASIS,%0A * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.%0A * See the License for the specific language governing permissions and%0A * limitations under the License.%0A */%0A%0Apackage org.apache.tika.example;%0A%0Aimport java.io.BufferedInputStream;%0Aimport java.io.IOException;%0Aimport java.io.InputStream;%0Aimport java.nio.file.Files;%0Aimport java.nio.file.Path;%0A%0Aimport org.apache.tika.Tika;%0Aimport org.apache.tika.exception.TikaException;%0Aimport org.apache.tika.metadata.Metadata;%0Aimport org.apache.tika.parser.ParseContext;%0Aimport org.apache.tika.parser.Parser;%0Aimport org.xml.sax.SAXException;%0Aimport org.xml.sax.helpers.DefaultHandler;%0A%0A/**%0A * This example demonstrates how to interrupt document parsing if%0A * some condition is met.%0A * %3Cp%3E%0A * %7B@link InterruptingContentHandler%7D throws special exception as soon as%0A * find %7B@code query%7D string in parsed file.%0A *%0A * See also http://stackoverflow.com/questions/31939851%0A */%0Apublic class InterruptableParsingExample %7B%0A    private Tika tika = new Tika(); // for default autodetect parser%0A%0A    public boolean findInFile(String query, Path path) %7B%0A        InterruptingContentHandler handler = new InterruptingContentHandler(query);%0A%0A        Metadata metadata = new Metadata();%0A        ParseContext context = new ParseContext();%0A        context.set(Parser.class, tika.getParser());%0A%0A        try (InputStream is = new BufferedInputStream(Files.newInputStream(path))) %7B%0A            tika.getParser().parse(is, handler, metadata, context);%0A        %7D catch (QueryMatchedException e) %7B%0A            return true;%0A        %7D catch (SAXException %7C TikaException %7C IOException e) %7B%0A            // something went wrong with parsing...%0A            e.printStackTrace();%0A        %7D%0A        return false;%0A    %7D%0A%0A    class QueryMatchedException extends SAXException %7B%0A    %7D%0A%0A    /**%0A     * Trivial content handler that searched for %7B@code query%7D in characters send to it.%0A     * %3Cp%3E%0A     * Throws %7B@link QueryMatchedException%7D when query string is found.%0A     */%0A    class InterruptingContentHandler extends DefaultHandler %7B%0A        private String query;%0A        private StringBuilder sb = new StringBuilder();%0A%0A        InterruptingContentHandler(String query) %7B%0A            this.query = query;%0A        %7D%0A%0A        @Override%0A        public void characters(char%5B%5D ch, int start, int length) throws SAXException %7B%0A            sb.append(new String(ch, start, length).toLowerCase());%0A%0A            if (sb.toString().contains(query))%0A                throw new QueryMatchedException();%0A%0A            if (sb.length() %3E 2 * query.length())%0A                sb.delete(0, sb.length() - query.length()); // keep tail with query.length() chars%0A        %7D%0A    %7D%0A%7D%0A

Add interruptable parsing example