source: src/main/java/weka/core/OptionHandlerJavadoc.java @ 24

Last change on this file since 24 was 4, checked in by gnappo, 14 years ago

Import di weka.

File size: 6.0 KB
Line 
1/*
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.
6 *
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.
11 *
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.
15 */
16
17/*
18 * OptionHandlerJavadoc.java
19 * Copyright (C) 2006 University of Waikato, Hamilton, New Zealand
20 */
21
22package weka.core;
23
24import java.util.Enumeration;
25import java.util.Vector;
26
27/**
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/>
32 *
33 <!-- options-start -->
34 * Valid options are: <p/>
35 *
36 * <pre> -W &lt;classname&gt;
37 *  The class to load.</pre>
38 *
39 * <pre> -nostars
40 *  Suppresses the '*' in the Javadoc.</pre>
41 *
42 * <pre> -dir &lt;dir&gt;
43 *  The directory above the package hierarchy of the class.</pre>
44 *
45 * <pre> -silent
46 *  Suppresses printing in the console.</pre>
47 *
48 * <pre> -noprolog
49 *  Suppresses the 'Valid options are...' prolog in the Javadoc.</pre>
50 *
51 <!-- options-end -->
52 *
53 * @author  fracpete (fracpete at waikato dot ac dot nz)
54 * @version $Revision: 5953 $
55 * @see #OPTIONS_STARTTAG
56 * @see #OPTIONS_ENDTAG
57 */
58public class OptionHandlerJavadoc 
59  extends Javadoc {
60 
61  /** the start comment tag for inserting the generated Javadoc */
62  public final static String OPTIONS_STARTTAG = "<!-- options-start -->";
63 
64  /** the end comment tag for inserting the generated Javadoc */
65  public final static String OPTIONS_ENDTAG = "<!-- options-end -->";
66 
67  /** whether to include the "Valid options..." prolog in the Javadoc */
68  protected boolean m_Prolog = true;
69 
70  /**
71   * default constructor
72   */
73  public OptionHandlerJavadoc() {
74    super();
75   
76    m_StartTag    = new String[1];
77    m_EndTag      = new String[1];
78    m_StartTag[0] = OPTIONS_STARTTAG;
79    m_EndTag[0]   = OPTIONS_ENDTAG;
80  }
81 
82  /**
83   * Returns an enumeration describing the available options.
84   *
85   * @return an enumeration of all the available options.
86   */
87  public Enumeration listOptions() {
88    Vector<Option>        result;
89    Enumeration   en;
90   
91    result = new Vector<Option>();
92   
93    en = super.listOptions();
94    while (en.hasMoreElements())
95      result.addElement((Option)en.nextElement());
96
97    result.addElement(new Option(
98        "\tSuppresses the 'Valid options are...' prolog in the Javadoc.",
99        "noprolog", 0, "-noprolog"));
100   
101    return result.elements();
102  }
103 
104  /**
105   * Parses a given list of options.
106   *
107   * @param options the list of options as an array of strings
108   * @throws Exception if an option is not supported
109   */
110  public void setOptions(String[] options) throws Exception {
111    super.setOptions(options);
112
113    setProlog(!Utils.getFlag("noprolog", options));
114  }
115 
116  /**
117   * Gets the current settings of this object.
118   *
119   * @return an array of strings suitable for passing to setOptions
120   */
121  public String[] getOptions() {
122    Vector<String>        result;
123    String[]      options;
124    int           i;
125   
126    result  = new Vector<String>();
127   
128    options = super.getOptions();
129    for (i = 0; i < options.length; i++)
130      result.add(options[i]);
131
132    if (!getProlog())
133      result.add("-noprolog");
134   
135    return (String[]) result.toArray(new String[result.size()]);
136  }
137 
138  /**
139   * sets whether to add the "Valid options are..." prolog
140   *
141   * @param value       true if the prolog is to be added
142   */
143  public void setProlog(boolean value) {
144    m_Prolog = value;
145  }
146 
147  /**
148   * whether "Valid options are..." prolog is included in the Javadoc
149   *
150   * @return            true if the prolog is printed
151   */
152  public boolean getProlog() {
153    return m_Prolog;
154  }
155 
156  /**
157   * generates and returns the Javadoc for the specified start/end tag pair.
158   *
159   * @param index       the index in the start/end tag array
160   * @return            the generated Javadoc
161   * @throws Exception  in case the generation fails
162   */
163  protected String generateJavadoc(int index) throws Exception {
164    String              result;
165    OptionHandler       handler;
166    String              optionStr;
167   
168    result = "";
169   
170    if (index == 0) {
171      if (!canInstantiateClass())
172        return result;
173           
174      if (!ClassDiscovery.hasInterface(OptionHandler.class, getInstance().getClass()))
175        throw new Exception("Class '" + getClassname() + "' is not an OptionHandler!");
176     
177      // any options at all?
178      handler = (OptionHandler) getInstance();
179      Enumeration enm = handler.listOptions();
180      if (!enm.hasMoreElements())
181        return result;
182     
183      // prolog?
184      if (getProlog())
185        result = "Valid options are: <p/>\n\n";
186     
187      // options
188      enm = handler.listOptions();
189      while (enm.hasMoreElements()) {
190        Option option = (Option) enm.nextElement();
191        optionStr =   toHTML(option.synopsis()) 
192                    + "\n" 
193                    + toHTML(option.description().replaceAll("\\t", " "));
194        result += "<pre> " + optionStr.replaceAll("<br/>", "") + "</pre>\n\n";
195      }
196     
197      // stars?
198      if (getUseStars()) 
199        result = indent(result, 1, "* ");
200    }
201   
202    return result;
203  }
204 
205  /**
206   * Returns the revision string.
207   *
208   * @return            the revision
209   */
210  public String getRevision() {
211    return RevisionUtils.extract("$Revision: 5953 $");
212  }
213 
214  /**
215   * Parses the given commandline parameters and generates the Javadoc.
216   *
217   * @param args        the commandline parameters for the object
218   */
219  public static void main(String[] args) {
220    runJavadoc(new OptionHandlerJavadoc(), args);
221  }
222}
Note: See TracBrowser for help on using the repository browser.