Home | History | Annotate | Download | only in internal
      1 /*******************************************************************************
      2  * Copyright (c) 2009, 2018 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.XMLDocument;
     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 
     29 	private XmlDocumentation() {
     30 	}
     31 
     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 
     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 	 */
     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 XMLDocument("documentation", null, null,
     69 				"UTF-8", true, new FileOutputStream(file));
     70 
     71 		for (final Command c : AllCommands.get()) {
     72 			writeCommand(c, root);
     73 		}
     74 
     75 		root.close();
     76 	}
     77 
     78 }
     79