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