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

org.apache.tika.server.resource.TikaDetectors Maven / Gradle / Ivy

There is a newer version: 3.0.0-BETA
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.server.resource;

import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.apache.tika.detect.CompositeDetector;
import org.apache.tika.detect.Detector;
import org.apache.tika.server.HTMLHelper;

/**
 * 

Provides details of all the {@link Detector}s registered with * Apache Tika, similar to --list-detectors with the Tika CLI. */ @Path("/detectors") public class TikaDetectors { private static final Gson GSON = new GsonBuilder().disableHtmlEscaping().create(); private HTMLHelper html; public TikaDetectors() { this.html = new HTMLHelper(); } @GET @Produces("text/html") public String getDectorsHTML() { StringBuffer h = new StringBuffer(); html.generateHeader(h, "Detectors available to Apache Tika"); detectorAsHTML(TikaResource.getConfig().getDetector(), h, 2); html.generateFooter(h); return h.toString(); } private void detectorAsHTML(Detector d, StringBuffer html, int level) { html.append(""); String name = d.getClass().getName(); html.append(name.substring(name.lastIndexOf('.') + 1)); html.append(""); html.append("

Class: "); html.append(name); html.append("

"); if (d instanceof CompositeDetector) { html.append("

Composite Detector

"); for (Detector cd : ((CompositeDetector) d).getDetectors()) { detectorAsHTML(cd, html, level + 1); } } } @GET @Produces(javax.ws.rs.core.MediaType.APPLICATION_JSON) public String getDetectorsJSON() { Map details = new HashMap(); detectorAsMap(TikaResource.getConfig().getDetector(), details); return GSON.toJson(details); } private void detectorAsMap(Detector d, Map details) { details.put("name", d.getClass().getName()); boolean isComposite = (d instanceof CompositeDetector); details.put("composite", isComposite); if (isComposite) { List> c = new ArrayList>(); for (Detector cd : ((CompositeDetector) d).getDetectors()) { Map cdet = new HashMap(); detectorAsMap(cd, cdet); c.add(cdet); } details.put("children", c); } } @GET @Produces("text/plain") public String getDetectorsPlain() { StringBuffer text = new StringBuffer(); renderDetector(TikaResource.getConfig().getDetector(), text, 0); return text.toString(); } private void renderDetector(Detector d, StringBuffer text, int indent) { boolean isComposite = (d instanceof CompositeDetector); String name = d.getClass().getName(); for (int i = 0; i < indent; i++) { text.append(" "); } text.append(name); if (isComposite) { text.append(" (Composite Detector):\n"); List subDetectors = ((CompositeDetector) d).getDetectors(); for (Detector sd : subDetectors) { renderDetector(sd, text, indent + 1); } } else { text.append("\n"); } } }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy