All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.tika.parser.external.ExternalParsersConfigReader Maven / Gradle / Ivy

There is a newer version: 6.5.21
Show newest version
/*
 * 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.parser.external;

import javax.xml.parsers.DocumentBuilder;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.StringTokenizer;
import java.util.regex.Pattern;

import org.apache.tika.exception.TikaException;
import org.apache.tika.mime.MediaType;
import org.apache.tika.mime.MimeTypeException;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.utils.XMLReaderUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 * Builds up ExternalParser instances based on XML file(s)
 *  which define what to run, for what, and how to process
 *  any output metadata.
 * Typically used to configure up a series of external programs 
 *  (like catdoc or pdf2txt) to extract text content from documents.
 *  
 * 
 *  TODO XML DTD Here
 * 
*/ public final class ExternalParsersConfigReader implements ExternalParsersConfigReaderMetKeys { public static List read(InputStream stream) throws TikaException, IOException { try { DocumentBuilder builder = XMLReaderUtils.getDocumentBuilder(); Document document = builder.parse(new InputSource(stream)); return read(document); } catch (SAXException e) { throw new TikaException("Invalid parser configuration", e); } } public static List read(Document document) throws TikaException, IOException { return read(document.getDocumentElement()); } public static List read(Element element) throws TikaException, IOException { List parsers = new ArrayList(); if (element != null && element.getTagName().equals(EXTERNAL_PARSERS_TAG)) { NodeList nodes = element.getChildNodes(); for (int i = 0; i < nodes.getLength(); i++) { Node node = nodes.item(i); if (node.getNodeType() == Node.ELEMENT_NODE) { Element child = (Element) node; if (child.getTagName().equals(PARSER_TAG)) { ExternalParser p = readParser(child); if(p != null) { parsers.add( p ); } } } } } else { throw new MimeTypeException( "Not a <" + EXTERNAL_PARSERS_TAG + "/> configuration document: " + element.getTagName()); } return parsers; } /** * Builds and Returns an ExternalParser, or null if a check * command was given that didn't match. */ private static ExternalParser readParser(Element parserDef) throws TikaException { ExternalParser parser = new ExternalParser(); NodeList children = parserDef.getChildNodes(); for(int i=0; i readMimeTypes(Element mimeTypes) { Set types = new HashSet(); NodeList children = mimeTypes.getChildNodes(); for(int i=0; i readMetadataPatterns(Element metadataDef) { Map metadata = new HashMap(); NodeList children = metadataDef.getChildNodes(); for(int i=0; i errorVals = new ArrayList(); NodeList children = checkDef.getChildNodes(); for(int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy