• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*******************************************************************************
2  * Copyright (c) 2009, 2021 Mountainminds GmbH & Co. KG and Contributors
3  * This program and the accompanying materials are made available under
4  * the terms of the Eclipse Public License 2.0 which is available at
5  * http://www.eclipse.org/legal/epl-2.0
6  *
7  * SPDX-License-Identifier: EPL-2.0
8  *
9  * Contributors:
10  *    Marc R. Hoffmann - initial API and implementation
11  *
12  *******************************************************************************/
13 package org.jacoco.cli.internal;
14 
15 import java.io.File;
16 import java.io.FileOutputStream;
17 import java.io.IOException;
18 import java.util.List;
19 
20 import org.jacoco.cli.internal.commands.AllCommands;
21 import org.jacoco.report.internal.xml.XMLElement;
22 import org.kohsuke.args4j.spi.OptionHandler;
23 
24 /**
25  * Internal utility to dump all command descriptions as XML.
26  */
27 public final class XmlDocumentation {
28 
XmlDocumentation()29 	private XmlDocumentation() {
30 	}
31 
writeCommand(final Command command, final XMLElement parent)32 	private static void writeCommand(final Command command,
33 			final XMLElement parent) throws IOException {
34 		final CommandParser parser = new CommandParser(command);
35 		final XMLElement element = parent.element("command");
36 		element.attr("name", command.name());
37 		element.element("usage").text(command.usage(parser));
38 		element.element("description").text(command.description());
39 		writeOptions(element, parser.getArguments());
40 		writeOptions(element, parser.getOptions());
41 	}
42 
writeOptions(final XMLElement parent, @SuppressWarnings("rawtypes") final List<OptionHandler> list)43 	private static void writeOptions(final XMLElement parent,
44 			@SuppressWarnings("rawtypes") final List<OptionHandler> list)
45 			throws IOException {
46 		for (final OptionHandler<?> o : list) {
47 			final XMLElement optionNode = parent.element("option");
48 			optionNode.attr("required", String.valueOf(o.option.required()));
49 			optionNode.attr("multiple",
50 					String.valueOf(o.setter.isMultiValued()));
51 			optionNode.element("usage").text(o.getNameAndMeta(null));
52 			optionNode.element("description").text(o.option.usage());
53 		}
54 	}
55 
56 	/**
57 	 * Called during the build process.
58 	 *
59 	 * @param args
60 	 *            exactly one argument expected with the target location
61 	 * @throws IOException
62 	 *             if XML document cannot be written
63 	 */
main(final String... args)64 	public static void main(final String... args) throws IOException {
65 		final File file = new File(args[0]);
66 		file.getParentFile().mkdirs();
67 
68 		final XMLElement root = new XMLElement("documentation", null, null,
69 				true, "UTF-8", new FileOutputStream(file));
70 
71 		for (final Command c : AllCommands.get()) {
72 			writeCommand(c, root);
73 		}
74 
75 		root.close();
76 	}
77 
78 }
79