Skip to content

Commit 66b9baa

Browse files
committed
Fix doclet options parsing and some tests
== CustomOption - Add hyphen prefix to the option name only when creating the Doclet option. The Apache Commons CLI Option doesn't allow that on the name (may the hyphen is added automatically after giving the name) - Changes the CustomOption construtor to replaces a null `parameters` value by an empty string, since a split is performed to extract the individual option arguments (parameters) and the value cannot be null - Provide a basic and general implementation of the process method. That was the stopping the options to be processed. The processing (validation) of each option may be different and an implementation should be provided when creating each option. But for now, a default implementation works as before. == SupportedOptions - If an option has no arguments, when creating it using the Apache Commons CLI, the argName method from the Option.builder() must not be called. Removes the method call from newNoArgOption() method and explicitly sets the number of arguments to 0 (otherwise, it was being set as unlimited). = AbstractTest - Changes the way to get an instance of the javadoc command line tool, using the javax.tools.ToolProvider instead of java.util.spi.ToolProvider. The new API also provide the more specific DocumentationTool interface to execute the javadoc tool, which enables passing the doclet class more easily. Signed-off-by: Manoel Campos <manoelcampos@gmail.com>
1 parent 9ec0a57 commit 66b9baa

File tree

4 files changed

+29
-9
lines changed

4 files changed

+29
-9
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ out/
66
.project
77
.gradle
88
CHANGELOG.md
9+
javadoc.xml
910

1011
# IntelliJ IDEA
1112
.idea/

src/main/java/com/github/markusbernhardt/xmldoclet/CustomOption.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.ArrayList;
77
import java.util.Collections;
88
import java.util.List;
9+
import java.util.Objects;
910

1011
/**
1112
* @author Manoel Campos
@@ -40,7 +41,7 @@ public static CustomOption of(final Option cliOption) {
4041
return new CustomOption(
4142
cliOption.getArgs(),
4243
cliOption.getDescription(),
43-
List.of(cliOption.getOpt()),
44+
List.of('-' + cliOption.getOpt()),
4445
cliOption.getArgName());
4546
}
4647

@@ -50,7 +51,7 @@ public CustomOption(
5051
this.argumentCount = argumentCount;
5152
this.description = description;
5253
this.names = new ArrayList<>(names);
53-
this.parameters = parameters;
54+
this.parameters = Objects.requireNonNullElse(parameters, "");
5455
}
5556

5657
@Override
@@ -96,6 +97,6 @@ public String[] getParameterArray() {
9697
*/
9798
@Override
9899
public boolean process(final String option, final List<String> arguments) {
99-
throw new UnsupportedOperationException("Not supported yet.");
100+
return arguments.size() == argumentCount;
100101
}
101102
}

src/main/java/com/github/markusbernhardt/xmldoclet/SupportedOptions.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,8 @@ private void newArgOption(final String optionName, final String argName, final S
8787

8888
private void newNoArgOption(final String optionName, final String description) {
8989
final var option = Option.builder(optionName)
90-
.argName(optionName)
9190
.required(false)
92-
.hasArg(false)
91+
.numberOfArgs(0)
9392
.desc(description)
9493
.build();
9594

src/test/java/com/github/markusbernhardt/xmldoclet/AbstractTest.java

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import com.github.markusbernhardt.xmldoclet.xjc.Root;
66
import org.junit.Test;
77

8+
import javax.tools.DocumentationTool;
9+
import javax.tools.ToolProvider;
810
import java.io.File;
911
import java.io.PrintWriter;
1012
import java.nio.charset.Charset;
@@ -13,7 +15,6 @@
1315
import java.util.List;
1416
import java.util.logging.Level;
1517
import java.util.logging.Logger;
16-
import java.util.spi.ToolProvider;
1718

1819
import static java.util.Arrays.stream;
1920

@@ -128,9 +129,27 @@ public Root executeJavadoc(
128129

129130
LOGGER.info("Executing doclet with arguments: " + join(" ", argumentList));
130131

131-
final var javadoc = ToolProvider.findFirst("javadoc").orElseThrow();
132-
final String[] arguments = argumentList.toArray(new String[] {});
133-
javadoc.run(infoWriter, errorWriter, arguments);
132+
final DocumentationTool javadoc = ToolProvider.getSystemDocumentationTool();
133+
if (javadoc == null) {
134+
throw new IllegalStateException("No javadoc command available on the system");
135+
}
136+
137+
// Create a task to run the doclet
138+
final var task = javadoc.getTask(
139+
new PrintWriter(System.out, true, Charset.defaultCharset()), // output writer
140+
null, // file manager (use default)
141+
null, // diagnostic listener (use default)
142+
XmlDoclet.class,
143+
argumentList, // arguments
144+
null // compilation units (use default)
145+
);
146+
147+
// Run the task
148+
if (task.call()) {
149+
System.out.println("Doclet ran successfully");
150+
} else {
151+
System.err.println("Doclet execution failed");
152+
}
134153

135154
LOGGER.info("done with doclet processing");
136155
} catch (Exception e) {

0 commit comments

Comments
 (0)