SupportedFormatsAdoc.java
/*
* 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.cli;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import org.apache.tika.mime.MediaType;
import org.apache.tika.parser.CompositeParser;
import org.apache.tika.parser.ParseContext;
import org.apache.tika.parser.Parser;
import org.apache.tika.parser.ParserDecorator;
import org.apache.tika.parser.ocr.TesseractOCRConfig;
/**
* Renders the leaf parsers of a composite and their media types as an AsciiDoc
* list for the docs' Supported Formats page. Output is fully sorted so a
* checked-in copy can be diffed against a fresh render.
*/
final class SupportedFormatsAdoc {
static final String HEADER = "//\n" +
"// Licensed to the Apache Software Foundation (ASF) under one or more\n" +
"// contributor license agreements. See the NOTICE file distributed with\n" +
"// this work for additional information regarding copyright ownership.\n" +
"// The ASF licenses this file to You under the Apache License, Version 2.0\n" +
"// (the \"License\"); you may not use this file except in compliance with\n" +
"// the License. You may obtain a copy of the License at\n" +
"//\n" +
"// http://www.apache.org/licenses/LICENSE-2.0\n" +
"//\n" +
"// Unless required by applicable law or agreed to in writing, software\n" +
"// distributed under the License is distributed on an \"AS IS\" BASIS,\n" +
"// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n" +
"// See the License for the specific language governing permissions and\n" +
"// limitations under the License.\n" +
"//\n" +
"\n" +
"// Generated by `tika-app --list-parser-details-adoc`. Do not edit by hand;\n" +
"// regenerate and commit. TikaCLITest fails when this file is stale.\n";
private SupportedFormatsAdoc() {
}
static String render(Parser root) {
// Tesseract only advertises types when the binary is installed; hide it so
// the output does not depend on the generating machine.
ParseContext context = new ParseContext();
TesseractOCRConfig noOcr = new TesseractOCRConfig();
noOcr.setSkipOcr(true);
context.set(TesseractOCRConfig.class, noOcr);
Map<String, Set<String>> byClass = new TreeMap<>();
collect(root, context, byClass);
StringBuilder sb = new StringBuilder(HEADER).append('\n');
for (Map.Entry<String, Set<String>> e : byClass.entrySet()) {
String fqn = e.getKey();
int dot = fqn.lastIndexOf('.');
sb.append("* `").append(fqn, 0, dot + 1).append("` link:{tika-javadoc-url}/")
.append(fqn.replace('.', '/')).append(".html[").append(fqn.substring(dot + 1))
.append("]\n");
for (String type : e.getValue()) {
sb.append("** `").append(type).append("`\n");
}
}
return sb.toString();
}
private static void collect(Parser p, ParseContext context, Map<String, Set<String>> byClass) {
while (p instanceof ParserDecorator) {
p = ((ParserDecorator) p).getWrappedParser();
}
if (p instanceof CompositeParser) {
for (Parser child : ((CompositeParser) p).getAllComponentParsers()) {
collect(child, context, byClass);
}
return;
}
Set<MediaType> supported = p.getSupportedTypes(context);
if (supported.isEmpty()) {
return;
}
Set<String> types = byClass.computeIfAbsent(p.getClass().getName(), k -> new TreeSet<>());
for (MediaType mt : supported) {
types.add(mt.toString());
}
}
}