1515 */
1616package com .manoelcampos .javadoc .coverage ;
1717
18- import java .io .*;
19-
18+ import com .manoelcampos .javadoc .coverage .configuration .Configuration ;
2019import com .manoelcampos .javadoc .coverage .exporter .*;
20+ import com .manoelcampos .javadoc .coverage .stats .JavaDocsStats ;
2121import com .sun .javadoc .*;
22- import com .sun .tools .doclets .standard .Standard ;
2322
2423/**
2524 * A {@link Doclet} that computes coverage of JavaDoc documentation.
3736 * @since 1.0.0
3837 */
3938public class CoverageDoclet {
40- /**
41- * A command line parameter to enable defining the name of the coverage report.
42- * The first value is the long version of the parameter name and the second
43- * is the short one.
44- */
45- public static final String OUTPUT_NAME_OPTION [] = {"-outputName" , "-o" };
46-
47- /**
48- * A command line parameter to enable coverage reports only for certain visibility modifiers. The first value is the long version of the
49- * parameter name and the second is the short one. Values for this option are public (only public stuff is used) and private (everything
50- * is used)
51- */
52- public static final String COVERAGE_ONLY_FOR_PUBLIC_OPTION [] = { "-modifiers" , "-m" };
53-
5439 /**
5540 * The {@link DataExporter} object to export the coverage report to a file
5641 * in a specific format.
5742 */
5843 private final DataExporter exporter ;
5944 private final RootDoc rootDoc ;
60- private final boolean computeOnlyForPublicModifier ;
45+ private final Configuration config ;
6146
6247 /**
63- * Starts the actual parsing or JavaDoc documentation and generation of the coverage report.
64- * This is the entry point for the JavaDoc tool to start the Doclet.
48+ * Starts the actual parsing or JavaDoc documentation and generation of the coverage report. This is the entry point
49+ * for the JavaDoc tool to start the Doclet.
6550 *
6651 * @param rootDoc root element which enables reading JavaDoc documentation
6752 * @return true if the Doclet was started successfully, false otherwise
@@ -78,58 +63,12 @@ public static boolean start(final RootDoc rootDoc) {
7863 */
7964 public CoverageDoclet (final RootDoc rootDoc ) {
8065 this .rootDoc = rootDoc ;
81- computeOnlyForPublicModifier = "public" . equals ( getOptionValue ( COVERAGE_ONLY_FOR_PUBLIC_OPTION ));
66+ this . config = new Configuration ( rootDoc . options ( ));
8267
83- // this needs to be the last part as it already accesses some stuff from the doclet
84- this .exporter = new HtmlExporter (this );
85- }
86-
87- public final boolean computeOnlyForPublicModifier () {
88- return computeOnlyForPublicModifier ;
89- }
68+ JavaDocsStats stats = new JavaDocsStats (rootDoc , config );
9069
91- /**
92- * Checks if a given parameter is a valid custom parameter accepted by this doclet.
93- * @param paramName the name of the parameter to check
94- * @return true if it's a valid custom parameter, false otherwise
95- */
96- private static boolean isCustomParameter (final String paramName ) {
97- return isParameter (paramName , OUTPUT_NAME_OPTION ) || isParameter (paramName , COVERAGE_ONLY_FOR_PUBLIC_OPTION );
98- }
99-
100- /**
101- * Checks if the name of a given parameter corresponds to either its long or short form.
102- *
103- * @param paramName the name of the parameter to check
104- * @param validNames the list of accepted names for that parameter
105- * @return true if the given name corresponds to one of the valid names, false otherwise
106- */
107- private static boolean isParameter (final String paramName , final String [] validNames ) {
108- for (String validName : validNames ) {
109- if (validName .equalsIgnoreCase (paramName )) {
110- return true ;
111- }
112- }
113-
114- return false ;
115- }
116-
117- /**
118- * Validates command line options.
119- *
120- * @param options the array of given options
121- * @param errorReporter an object that allows printing error messages for invalid options
122- * @return true if the options are valid, false otherwise
123- * @see Doclet#validOptions(String[][], DocErrorReporter)
124- */
125- public static boolean validOptions (final String [][] options , final DocErrorReporter errorReporter ) {
126- for (final String [] opt : options ) {
127- if (isCustomParameter (opt [0 ])) {
128- return true ;
129- }
130- }
131-
132- return Standard .validOptions (options , errorReporter );
70+ // this needs to be the last part as it already accesses some stuff from the doclet
71+ this .exporter = new HtmlExporter (config , stats );
13372 }
13473
13574 /**
@@ -140,32 +79,19 @@ public static boolean validOptions(final String[][] options, final DocErrorRepor
14079 * @see Doclet#optionLength(String)
14180 */
14281 public static int optionLength (final String option ) {
143- /*The custom outputName parameter accepts one argument.
144- * The name of the param counts as the one argument.*/
145- if (isCustomParameter (option )) {
146- return 2 ;
147- }
148-
149- return Standard .optionLength (option );
82+ return Configuration .getOptionLength (option );
15083 }
15184
15285 /**
153- * Gets the values associated to a given command line option.
86+ * Checks that all given option are valid
15487 *
155- * @param optionNames an array containing the valid names for the command line option to get its associated values.
156- * This array may include the long and short versions of the option name,
157- * for instance {@code {-outputName, -o}}.
158- * @return the values associated to the option, where the 0th element is the option itself;
159- * or an empty array if the option is invalid.
88+ * @param options the options to be checked on validity
89+ * @param errorReporter
90+ * @return true if the options are valid
91+ * @see Doclet#validOptions(String[][], DocErrorReporter)
16092 */
161- public String getOptionValue (final String [] optionNames ) {
162- for (final String [] optionValues : rootDoc .options ()) {
163- if (isParameter (optionValues [0 ], optionNames )) {
164- return optionValues [1 ];
165- }
166- }
167-
168- return null ;
93+ public static boolean validOptions (final String [][] options , final DocErrorReporter errorReporter ) {
94+ return Configuration .areValidOptions (options , errorReporter );
16995 }
17096
17197 /**
@@ -196,44 +122,4 @@ private boolean render() {
196122 public RootDoc getRootDoc () {
197123 return rootDoc ;
198124 }
199-
200- /**
201- * Gets a {@link PrintWriter} used by the {@link #exporter} to write
202- * the coverage report to.
203- *
204- * @param file the file to which the coverage report will be saved to
205- */
206- public PrintWriter getWriter (final File file ) throws FileNotFoundException {
207- return new PrintWriter (new OutputStreamWriter (new FileOutputStream (file )));
208- }
209-
210- /**
211- * Gets a {@link File} object from a given file name.
212- *
213- * @param fileName the name of the file to get a {@link File} object.
214- * @return the {@link File} object
215- */
216- public File getOutputFile (final String fileName ) {
217- final File dir = new File (getOutputDir ());
218- if (!dir .exists () && !dir .mkdirs ()) {
219- throw new RuntimeException ("The directory '" + getOutputDir () + "' was not created due to unknown reason." );
220- }
221-
222- return new File (dir , fileName );
223- }
224-
225- /**
226- * Gets the output directory passed as a command line argument to javadoc tool.
227- *
228- * @return the output directory to export the JavaDocs
229- */
230- private String getOutputDir () {
231- for (final String [] option : rootDoc .options ()) {
232- if (option .length == 2 && option [0 ].equals ("-d" )) {
233- return Utils .includeTrailingDirSeparator (option [1 ]);
234- }
235- }
236-
237- return "" ;
238- }
239125}
0 commit comments