opennlp.tools.cmdline.GenerateManualTool Maven / Gradle / Ivy
/*
* 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 opennlp.tools.cmdline;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.StringReader;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.StringTokenizer;
import opennlp.tools.cmdline.ArgumentParser.Argument;
public class GenerateManualTool {
private static final int MAX_LINE_LENGTH = 110; // optimized for printing
public static void main(String[] args) throws FileNotFoundException {
if (args.length != 1) {
System.out.print(getUsage());
System.exit(0);
}
StringBuilder sb = new StringBuilder();
appendHeader(sb);
// organize by package name
LinkedHashMap> packageNameToolMap = new LinkedHashMap<>();
for (Entry entry : CLI.getToolLookupMap().entrySet()) {
final String toolName = entry.getKey();
final CmdLineTool tool = entry.getValue();
String packageName = tool.getClass().getPackage().getName();
packageName = packageName.substring(packageName.lastIndexOf(".") + 1);
if (!packageNameToolMap.containsKey(packageName)) {
packageNameToolMap.put(packageName,
new LinkedHashMap());
}
packageNameToolMap.get(packageName).put(toolName, tool);
}
// add tools grouped by package
for (Entry> entry : packageNameToolMap.entrySet()) {
appendToolGroup(entry.getKey(), entry.getValue(), sb);
}
// footer
appendFooter(sb);
// output to file
try (PrintWriter out = new PrintWriter(args[0])) {
out.println(sb);
}
}
/**
* @return this tool usage
*/
private static String getUsage() {
return "Requires one argument: \n" +
" Path to the output XML file \n";
}
/**
* Appends a group of tools, based on the tool package name
*
* @param groupName
* @param toolsMap
* @param sb
*/
private static void appendToolGroup(String groupName,
Map toolsMap, StringBuilder sb) {
sb.append("\n\n");
sb.append("").append(firstCaps(groupName)).append(" \n\n");
for (Entry entry : toolsMap.entrySet()) {
appendTool(groupName, entry.getKey(), entry.getValue(), sb);
}
sb.append(" \n\n");
}
/**
* Appends a tool
*
* @param groupName
* @param toolName
* @param tool
* @param sb
*/
private static void appendTool(String groupName, String toolName,
CmdLineTool tool, StringBuilder sb) {
sb.append("\n\n");
sb.append("").append(toolName).append(" \n\n");
sb.append("").append(firstCaps(tool.getShortDescription()))
.append(" \n\n");
appendCode(tool.getHelp(), sb);
if (TypedCmdLineTool.class.isAssignableFrom(tool.getClass())) {
appendHelpForTool((TypedCmdLineTool>) tool, sb);
}
sb.append(" \n\n");
}
private static void appendHelpForTool(TypedCmdLineTool> tool,
StringBuilder sb) {
Class> type = tool.type;
Set formats = StreamFactoryRegistry.getFactories(type).keySet();
sb.append("The supported formats and arguments are: \n\n");
Map> formatArguments = new LinkedHashMap<>();
for (String formatName : formats) {
if (!StreamFactoryRegistry.DEFAULT_FORMAT.equals(formatName)) {
ObjectStreamFactory> format = tool.getStreamFactory(formatName);
formatArguments.put(formatName,
ArgumentParser.createArguments(format.getParameters()));
}
}
appendArgumentTable(formatArguments, sb);
}
private static void appendArgumentTable(
Map> formatArguments, StringBuilder sb) {
sb.append(
"\n");
sb.append(
"Format Argument Value " +
"Optional Description
\n");
sb.append("\n");
for (Entry> entry : formatArguments.entrySet()) {
final String format = entry.getKey();
final List arguments = entry.getValue();
int i = 0;
for (Argument argument : arguments) {
sb.append("\n");
if (i == 0) {
sb.append("").append(format).append(" \n");
}
sb.append("").append(argument.getArgument())
.append(" \n");
sb.append("").append(argument.getValue()).append(" \n");
sb.append("").append(yes(argument.getOptional()))
.append(" \n");
sb.append("").append(firstCaps(argument.getDescription()))
.append(" \n");
sb.append("
\n");
i++;
}
}
sb.append(" \n");
sb.append(" \n\n");
}
private static void appendHeader(StringBuilder sb) {
sb.append("\n"
+ "\n" + "\n" + "\n\n"
+ "\n\n" + "\n\n"
+ "The Command Line Interface \n\n" + ""
+ "This section details the available tools and parameters of the Command Line Interface. "
+ "For a introduction in its usage please refer to . "
+ " \n\n");
}
private static void appendFooter(StringBuilder sb) {
sb.append("\n\n ");
}
private static String firstCaps(String str) {
if (str.length() > 1) {
return str.substring(0, 1).toUpperCase() + str.substring(1);
} else {
return str;
}
}
private static String yes(boolean optional) {
if (optional) {
return "Yes";
}
return "No";
}
private static void appendCode(String help, StringBuilder sb) {
sb.append("\n" + "\n").append(" \n");
}
/**
* Prevents long lines. Lines are optimized for printing.
*
* @param stringBlock
* @return
*/
private static String splitLongLines(String stringBlock) {
StringBuilder sb = new StringBuilder();
String line;
try {
BufferedReader reader = new BufferedReader(new StringReader(stringBlock));
while ((line = reader.readLine()) != null) {
if (line.length() <= MAX_LINE_LENGTH) {
sb.append(line).append("\n");
} else {
StringTokenizer tok = new StringTokenizer(line, " ");
int lineLen = 0;
while (tok.hasMoreTokens()) {
String word = tok.nextToken() + " ";
if (lineLen + word.length() > MAX_LINE_LENGTH) {
sb.append("\n ");
lineLen = 8;
}
sb.append(word);
lineLen += word.length();
}
}
}
} catch (Exception e) {
// nothing to do
}
return sb.toString();
}
}