2
* This program is free software; you can redistribute it and/or modify
3
* it under the terms of the GNU General Public License as published by
4
* the Free Software Foundation; either version 2 of the License, or
5
* (at your option) any later version.
7
* This program is distributed in the hope that it will be useful,
8
* but WITHOUT ANY WARRANTY; without even the implied warranty of
9
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10
* GNU General Public License for more details.
12
* You should have received a copy of the GNU General Public License
13
* along with this program; if not, write to the Free Software
14
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18
* OptionHandlerJavadoc.java
19
* Copyright (C) 2006 University of Waikato, Hamilton, New Zealand
24
import java.util.Enumeration;
25
import java.util.Vector;
28
* Generates Javadoc comments from the OptionHandler's options. Can
29
* automatically update the option comments if they're surrounded by
30
* the OPTIONS_STARTTAG and OPTIONS_ENDTAG (the indention is determined via
31
* the OPTIONS_STARTTAG). <p/>
33
<!-- options-start -->
34
* Valid options are: <p/>
36
* <pre> -W <classname>
37
* The class to load.</pre>
40
* Suppresses the '*' in the Javadoc.</pre>
42
* <pre> -dir <dir>
43
* The directory above the package hierarchy of the class.</pre>
46
* Suppresses printing in the console.</pre>
49
* Suppresses the 'Valid options are...' prolog in the Javadoc.</pre>
53
* @author fracpete (fracpete at waikato dot ac dot nz)
54
* @version $Revision: 1.7 $
55
* @see #OPTIONS_STARTTAG
56
* @see #OPTIONS_ENDTAG
58
public class OptionHandlerJavadoc
61
/** the start comment tag for inserting the generated Javadoc */
62
public final static String OPTIONS_STARTTAG = "<!-- options-start -->";
64
/** the end comment tag for inserting the generated Javadoc */
65
public final static String OPTIONS_ENDTAG = "<!-- options-end -->";
67
/** whether to include the "Valid options..." prolog in the Javadoc */
68
protected boolean m_Prolog = true;
73
public OptionHandlerJavadoc() {
76
m_StartTag = new String[1];
77
m_EndTag = new String[1];
78
m_StartTag[0] = OPTIONS_STARTTAG;
79
m_EndTag[0] = OPTIONS_ENDTAG;
83
* Returns an enumeration describing the available options.
85
* @return an enumeration of all the available options.
87
public Enumeration listOptions() {
91
result = new Vector();
93
en = super.listOptions();
94
while (en.hasMoreElements())
95
result.addElement(en.nextElement());
97
result.addElement(new Option(
98
"\tSuppresses the 'Valid options are...' prolog in the Javadoc.",
99
"noprolog", 0, "-noprolog"));
101
return result.elements();
105
* Parses a given list of options.
107
* @param options the list of options as an array of strings
108
* @throws Exception if an option is not supported
110
public void setOptions(String[] options) throws Exception {
111
super.setOptions(options);
113
setProlog(!Utils.getFlag("noprolog", options));
117
* Gets the current settings of this object.
119
* @return an array of strings suitable for passing to setOptions
121
public String[] getOptions() {
126
result = new Vector();
128
options = super.getOptions();
129
for (i = 0; i < options.length; i++)
130
result.add(options[i]);
133
result.add("-noprolog");
135
return (String[]) result.toArray(new String[result.size()]);
139
* sets whether to add the "Valid options are..." prolog
141
* @param value true if the prolog is to be added
143
public void setProlog(boolean value) {
148
* whether "Valid options are..." prolog is included in the Javadoc
150
* @return true if the prolog is printed
152
public boolean getProlog() {
157
* generates and returns the Javadoc for the specified start/end tag pair.
159
* @param index the index in the start/end tag array
160
* @return the generated Javadoc
161
* @throws Exception in case the generation fails
163
protected String generateJavadoc(int index) throws Exception {
165
OptionHandler handler;
171
if (!canInstantiateClass())
174
if (!ClassDiscovery.hasInterface(OptionHandler.class, getInstance().getClass()))
175
throw new Exception("Class '" + getClassname() + "' is not an OptionHandler!");
177
// any options at all?
178
handler = (OptionHandler) getInstance();
179
Enumeration enm = handler.listOptions();
180
if (!enm.hasMoreElements())
185
result = "Valid options are: <p/>\n\n";
188
enm = handler.listOptions();
189
while (enm.hasMoreElements()) {
190
Option option = (Option) enm.nextElement();
191
optionStr = toHTML(option.synopsis())
193
+ toHTML(option.description().replaceAll("\\t", " "));
194
result += "<pre> " + optionStr.replaceAll("<br/>", "") + "</pre>\n\n";
199
result = indent(result, 1, "* ");
206
* Parses the given commandline parameters and generates the Javadoc.
208
* @param args the commandline parameters for the object
210
public static void main(String[] args) {
211
runJavadoc(new OptionHandlerJavadoc(), args);