KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > java2html > commandline > Java2HtmlCommandline


1 package de.java2html.commandline;
2
3 import java.io.File JavaDoc;
4
5 import de.java2html.converter.AbstractJavaSourceConverter;
6 import de.java2html.converter.IJavaSourceConverter;
7 import de.java2html.converter.JavaSourceConverterProvider;
8 import de.java2html.options.ConversionOptionsUtilities;
9 import de.java2html.options.JavaSourceConversionOptions;
10 import de.java2html.options.JavaSourceStyleTable;
11
12 /**
13  *
14  * Command line tool to invoke the {@link de.java2html.converter.AbstractJavaSourceConverter} without a GUI.
15  * Also allows handling of complete source trees.
16  *
17  * If the -copyToTargetUnprocessedFiles parameter is supplied all files that are NOT processed
18  * by this tool will just be copied to the target directory.
19  *
20  * Valid invocations are:
21  * <ul>
22  * <li><code>java -jar java2html.jar -srcdir d:/src [-targetdir d:/src/output] [-filemask *.java] [-converter HTML LaTeX XHtml ] [-tabs 4] [-copyToTargetUnprocessedFiles] [-style KAWA ECLIPSE]</code>
23  * <li><code>java -jar java2html.jar -srcfile d:/src/test.java [-targetfile d:/src/output/target.java] [-converter HTML LaTeX XHtml ] [-tabs 4] [-style KAWA ECLIPSE]</code>
24  * </ul>
25  *
26  * default converter is HTML and default filemask is <code>*.java</code>
27  * Other options are set to the default options defined by the <code>java2html.properties</code> file.
28  *
29  * @author Sanjay Madhavan
30  * @author Markus Gebhard
31  */

32 public class Java2HtmlCommandline {
33   private static final String JavaDoc PARAM_TARGETFILE = "targetfile";
34   private static final String JavaDoc PARAM_SRCFILE = "srcfile";
35   private static final String JavaDoc PARAM_STYLE = "style";
36   private static final String JavaDoc FLAG_COPYTOTARGETUNPROCESSEDFILES = "copytotargetunprocessedfiles";
37   private static final String JavaDoc PARAM_TABS = "tabs";
38   private static final String JavaDoc PARAM_CONVERTER = "converter";
39   private static final String JavaDoc PARAM_TARGETDIR = "targetdir";
40   private static final String JavaDoc PARAM_SRCDIR = "srcdir";
41   private static final String JavaDoc PARAM_FILEMASK = "filemask";
42
43   private static final String JavaDoc DEFAULT_CONVERTER_TYPE_NAME = "HTML";
44   private final static String JavaDoc DEFAULT_FILEMASK = "*.java";
45
46   /**
47    * Launch this tool with the supplied parameter. See the class comment for valid parameters.
48    *
49    * @param args
50    */

51   public static IJava2HtmlConversion createCommandlineConversion(String JavaDoc[] args)
52       throws IllegalCommandlineParametersException {
53     CommandlineArguments arguments = new CommandlineArguments(args);
54     if (arguments.isParameterSet(PARAM_SRCDIR)) {
55       return createDirectoryConversion(arguments);
56     }
57     else if (arguments.isParameterSet(PARAM_SRCFILE)) {
58       return createFileConversion(arguments);
59     }
60     else {
61       throw new IllegalCommandlineParametersException("No parameter -srcdir or -srcfile specified.");
62     }
63   }
64
65   private static IJava2HtmlConversion createFileConversion(CommandlineArguments arguments)
66       throws IllegalCommandlineParametersException {
67     arguments.assertContainsNoUnsupportedFlags(new String JavaDoc[]{});
68     arguments.assertContainsNoUnsupportedParameters(new String JavaDoc[]{
69         PARAM_SRCFILE,
70         PARAM_TARGETFILE,
71         PARAM_CONVERTER,
72         PARAM_TABS,
73         PARAM_STYLE });
74
75     String JavaDoc srcFileName = arguments.getRequiredParameterStringValue(PARAM_SRCFILE);
76     String JavaDoc targetFileName = arguments.getParameterStringValue(PARAM_TARGETFILE, null);
77     String JavaDoc converterType = arguments.getParameterStringValue(PARAM_CONVERTER, DEFAULT_CONVERTER_TYPE_NAME);
78     int tabSize = arguments.getParameterPositiveIntValue(PARAM_TABS, JavaSourceConversionOptions
79         .getDefault().getTabSize());
80     String JavaDoc styleName = arguments
81         .getParameterStringValue(PARAM_STYLE, JavaSourceStyleTable.getDefault().getName());
82
83     IJavaSourceConverter converter = getConverter(converterType);
84     if (converter == null) {
85       System.err.println("ERROR: Unknown converter type: " + converterType);
86       printUsage();
87       System.exit(1);
88     }
89
90     JavaSourceConversionOptions options = JavaSourceConversionOptions.getDefault();
91     applyStyle(styleName, options);
92
93     options.setTabSize(tabSize);
94
95     return new Java2HtmlFileConversion(new File JavaDoc(srcFileName), targetFileName == null ? null : new File JavaDoc(
96         targetFileName), converter, options);
97   }
98
99   private static IJava2HtmlConversion createDirectoryConversion(CommandlineArguments arguments)
100       throws IllegalCommandlineParametersException {
101     arguments.assertContainsNoUnsupportedFlags(new String JavaDoc[]{ FLAG_COPYTOTARGETUNPROCESSEDFILES });
102     arguments.assertContainsNoUnsupportedParameters(new String JavaDoc[]{
103         PARAM_SRCDIR,
104         PARAM_TARGETDIR,
105         PARAM_CONVERTER,
106         PARAM_TABS,
107         PARAM_STYLE,
108         PARAM_FILEMASK });
109
110     String JavaDoc srcDirectoryName = arguments.getRequiredParameterStringValue(PARAM_SRCDIR);
111     String JavaDoc targetDirectoryName = arguments.getParameterStringValue(PARAM_TARGETDIR, null);
112     String JavaDoc converterType = arguments.getParameterStringValue(PARAM_CONVERTER, DEFAULT_CONVERTER_TYPE_NAME);
113     int tabSize = arguments.getParameterPositiveIntValue(PARAM_TABS, JavaSourceConversionOptions
114         .getDefault().getTabSize());
115     boolean copyUnprocessedFiles = arguments.isFlagSet(FLAG_COPYTOTARGETUNPROCESSEDFILES);
116     String JavaDoc styleName = arguments
117         .getParameterStringValue(PARAM_STYLE, JavaSourceStyleTable.getDefault().getName());
118
119     String JavaDoc fileMask = arguments.getParameterStringValue(PARAM_FILEMASK, DEFAULT_FILEMASK);
120
121     IJavaSourceConverter converter = getConverter(converterType);
122     if (converter == null) {
123       System.err.println("ERROR: Unknown converter type: " + converterType);
124       printUsage();
125       System.exit(1);
126     }
127
128     JavaSourceConversionOptions options = JavaSourceConversionOptions.getDefault();
129     applyStyle(styleName, options);
130     options.setTabSize(tabSize);
131
132     return new Java2HtmlDirectoryConversion(new File JavaDoc(srcDirectoryName), converter, targetDirectoryName == null
133         ? null
134         : new File JavaDoc(targetDirectoryName), fileMask, copyUnprocessedFiles, options);
135   }
136
137   /**
138    * Prints the valid usage of this tool.
139    *
140    */

141   public static void printUsage() {
142     System.out.println("\nValid usage: \n");
143     System.out.println("1. To convert a directory including subdirectories: \n");
144
145     System.out.println("\tjava -jar java2html.jar -"
146         + PARAM_SRCDIR
147         + " d:/src [-"
148         + PARAM_TARGETDIR
149         + " d:/src/output] [-"
150         + PARAM_FILEMASK
151         + " *.java] [-"
152         + PARAM_CONVERTER
153         + " "
154         + getValidConverterNames()
155         + "] [-"
156         + PARAM_TABS
157         + " 4] [-"
158         + FLAG_COPYTOTARGETUNPROCESSEDFILES
159         + "] -"
160         + PARAM_STYLE
161         + " [ "
162         + getValidStylesAsString()
163         + " ]\n");
164
165     System.out.println("2. To convert a single file: \n");
166     System.out.println("\tjava -jar java2html.jar -"
167         + PARAM_SRCFILE
168         + " d:/src/test.java [-"
169         + PARAM_TARGETFILE
170         + " d:/src/output/target.java] [-"
171         + PARAM_CONVERTER
172         + " "
173         + getValidConverterNames()
174         + "] [-"
175         + PARAM_TABS
176         + " 8]"
177         + " -"
178         + PARAM_STYLE
179         + " [ "
180         + getValidStylesAsString()
181         + " ]\n");
182   }
183
184   /**
185    * set the style of generation, If the specified style is not a valid style then use the first defined style.
186    * @param options
187    * @param styleName
188    */

189   private static void applyStyle(String JavaDoc styleName, JavaSourceConversionOptions options)
190       throws IllegalCommandlineParametersException {
191     JavaSourceStyleTable styleTable = JavaSourceStyleTable.getPredefinedTable(styleName);
192     if (styleTable == null) {
193       throw new IllegalCommandlineParametersException("Unknown style '" + styleName + "'");
194     }
195     options.setStyleTable(styleTable);
196   }
197
198   /**
199    * Return a list of all defined styles as a string
200    *
201    * @return String
202    */

203   private static String JavaDoc getValidStylesAsString() {
204     return ConversionOptionsUtilities.getSeparatedString(ConversionOptionsUtilities
205         .getPredefinedStyleTableNames(), " | ");
206   }
207
208   private static String JavaDoc getValidConverterNames() {
209     return ConversionOptionsUtilities.getSeparatedString(
210         JavaSourceConverterProvider.getAllConverterNames(),
211         " | ");
212   }
213
214   /**
215    * Return a converter instance for the specified converterType
216    * or null if no matching converter was found.
217    *
218    * @param converterType
219    * @return JavaSourceConverter
220    */

221   private static IJavaSourceConverter getConverter(String JavaDoc converterType)
222       throws IllegalCommandlineParametersException {
223     IJavaSourceConverter converter = JavaSourceConverterProvider.getJavaSourceConverterByName(converterType);
224     if (converter == null) {
225       throw new IllegalCommandlineParametersException("Unsupported converter type '" + converterType + "'");
226     }
227     return converter;
228   }
229 }
Popular Tags