001: package de.java2html.commandline;
002:
003: import java.io.File;
004:
005: import de.java2html.converter.AbstractJavaSourceConverter;
006: import de.java2html.converter.IJavaSourceConverter;
007: import de.java2html.converter.JavaSourceConverterProvider;
008: import de.java2html.options.ConversionOptionsUtilities;
009: import de.java2html.options.JavaSourceConversionOptions;
010: import de.java2html.options.JavaSourceStyleTable;
011:
012: /**
013: *
014: * Command line tool to invoke the {@link de.java2html.converter.AbstractJavaSourceConverter} without a GUI.
015: * Also allows handling of complete source trees.
016: *
017: * If the -copyToTargetUnprocessedFiles parameter is supplied all files that are NOT processed
018: * by this tool will just be copied to the target directory.
019: *
020: * Valid invocations are:
021: * <ul>
022: * <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>
023: * <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>
024: * </ul>
025: *
026: * default converter is HTML and default filemask is <code>*.java</code>
027: * Other options are set to the default options defined by the <code>java2html.properties</code> file.
028: *
029: * @author Sanjay Madhavan
030: * @author Markus Gebhard
031: */
032: public class Java2HtmlCommandline {
033: private static final String PARAM_TARGETFILE = "targetfile";
034: private static final String PARAM_SRCFILE = "srcfile";
035: private static final String PARAM_STYLE = "style";
036: private static final String FLAG_COPYTOTARGETUNPROCESSEDFILES = "copytotargetunprocessedfiles";
037: private static final String PARAM_TABS = "tabs";
038: private static final String PARAM_CONVERTER = "converter";
039: private static final String PARAM_TARGETDIR = "targetdir";
040: private static final String PARAM_SRCDIR = "srcdir";
041: private static final String PARAM_FILEMASK = "filemask";
042:
043: private static final String DEFAULT_CONVERTER_TYPE_NAME = "HTML";
044: private final static String DEFAULT_FILEMASK = "*.java";
045:
046: /**
047: * Launch this tool with the supplied parameter. See the class comment for valid parameters.
048: *
049: * @param args
050: */
051: public static IJava2HtmlConversion createCommandlineConversion(
052: String[] args) throws IllegalCommandlineParametersException {
053: CommandlineArguments arguments = new CommandlineArguments(args);
054: if (arguments.isParameterSet(PARAM_SRCDIR)) {
055: return createDirectoryConversion(arguments);
056: } else if (arguments.isParameterSet(PARAM_SRCFILE)) {
057: return createFileConversion(arguments);
058: } else {
059: throw new IllegalCommandlineParametersException(
060: "No parameter -srcdir or -srcfile specified.");
061: }
062: }
063:
064: private static IJava2HtmlConversion createFileConversion(
065: CommandlineArguments arguments)
066: throws IllegalCommandlineParametersException {
067: arguments.assertContainsNoUnsupportedFlags(new String[] {});
068: arguments.assertContainsNoUnsupportedParameters(new String[] {
069: PARAM_SRCFILE, PARAM_TARGETFILE, PARAM_CONVERTER,
070: PARAM_TABS, PARAM_STYLE });
071:
072: String srcFileName = arguments
073: .getRequiredParameterStringValue(PARAM_SRCFILE);
074: String targetFileName = arguments.getParameterStringValue(
075: PARAM_TARGETFILE, null);
076: String converterType = arguments.getParameterStringValue(
077: PARAM_CONVERTER, DEFAULT_CONVERTER_TYPE_NAME);
078: int tabSize = arguments.getParameterPositiveIntValue(
079: PARAM_TABS, JavaSourceConversionOptions.getDefault()
080: .getTabSize());
081: String styleName = arguments.getParameterStringValue(
082: PARAM_STYLE, JavaSourceStyleTable.getDefault()
083: .getName());
084:
085: IJavaSourceConverter converter = getConverter(converterType);
086: if (converter == null) {
087: System.err.println("ERROR: Unknown converter type: "
088: + converterType);
089: printUsage();
090: System.exit(1);
091: }
092:
093: JavaSourceConversionOptions options = JavaSourceConversionOptions
094: .getDefault();
095: applyStyle(styleName, options);
096:
097: options.setTabSize(tabSize);
098:
099: return new Java2HtmlFileConversion(new File(srcFileName),
100: targetFileName == null ? null
101: : new File(targetFileName), converter, options);
102: }
103:
104: private static IJava2HtmlConversion createDirectoryConversion(
105: CommandlineArguments arguments)
106: throws IllegalCommandlineParametersException {
107: arguments
108: .assertContainsNoUnsupportedFlags(new String[] { FLAG_COPYTOTARGETUNPROCESSEDFILES });
109: arguments.assertContainsNoUnsupportedParameters(new String[] {
110: PARAM_SRCDIR, PARAM_TARGETDIR, PARAM_CONVERTER,
111: PARAM_TABS, PARAM_STYLE, PARAM_FILEMASK });
112:
113: String srcDirectoryName = arguments
114: .getRequiredParameterStringValue(PARAM_SRCDIR);
115: String targetDirectoryName = arguments.getParameterStringValue(
116: PARAM_TARGETDIR, null);
117: String converterType = arguments.getParameterStringValue(
118: PARAM_CONVERTER, DEFAULT_CONVERTER_TYPE_NAME);
119: int tabSize = arguments.getParameterPositiveIntValue(
120: PARAM_TABS, JavaSourceConversionOptions.getDefault()
121: .getTabSize());
122: boolean copyUnprocessedFiles = arguments
123: .isFlagSet(FLAG_COPYTOTARGETUNPROCESSEDFILES);
124: String styleName = arguments.getParameterStringValue(
125: PARAM_STYLE, JavaSourceStyleTable.getDefault()
126: .getName());
127:
128: String fileMask = arguments.getParameterStringValue(
129: PARAM_FILEMASK, DEFAULT_FILEMASK);
130:
131: IJavaSourceConverter converter = getConverter(converterType);
132: if (converter == null) {
133: System.err.println("ERROR: Unknown converter type: "
134: + converterType);
135: printUsage();
136: System.exit(1);
137: }
138:
139: JavaSourceConversionOptions options = JavaSourceConversionOptions
140: .getDefault();
141: applyStyle(styleName, options);
142: options.setTabSize(tabSize);
143:
144: return new Java2HtmlDirectoryConversion(new File(
145: srcDirectoryName), converter,
146: targetDirectoryName == null ? null : new File(
147: targetDirectoryName), fileMask,
148: copyUnprocessedFiles, options);
149: }
150:
151: /**
152: * Prints the valid usage of this tool.
153: *
154: */
155: public static void printUsage() {
156: System.out.println("\nValid usage: \n");
157: System.out
158: .println("1. To convert a directory including subdirectories: \n");
159:
160: System.out.println("\tjava -jar java2html.jar -" + PARAM_SRCDIR
161: + " d:/src [-" + PARAM_TARGETDIR + " d:/src/output] [-"
162: + PARAM_FILEMASK + " *.java] [-" + PARAM_CONVERTER
163: + " " + getValidConverterNames() + "] [-" + PARAM_TABS
164: + " 4] [-" + FLAG_COPYTOTARGETUNPROCESSEDFILES + "] -"
165: + PARAM_STYLE + " [ " + getValidStylesAsString()
166: + " ]\n");
167:
168: System.out.println("2. To convert a single file: \n");
169: System.out.println("\tjava -jar java2html.jar -"
170: + PARAM_SRCFILE + " d:/src/test.java [-"
171: + PARAM_TARGETFILE + " d:/src/output/target.java] [-"
172: + PARAM_CONVERTER + " " + getValidConverterNames()
173: + "] [-" + PARAM_TABS + " 8]" + " -" + PARAM_STYLE
174: + " [ " + getValidStylesAsString() + " ]\n");
175: }
176:
177: /**
178: * set the style of generation, If the specified style is not a valid style then use the first defined style.
179: * @param options
180: * @param styleName
181: */
182: private static void applyStyle(String styleName,
183: JavaSourceConversionOptions options)
184: throws IllegalCommandlineParametersException {
185: JavaSourceStyleTable styleTable = JavaSourceStyleTable
186: .getPredefinedTable(styleName);
187: if (styleTable == null) {
188: throw new IllegalCommandlineParametersException(
189: "Unknown style '" + styleName + "'");
190: }
191: options.setStyleTable(styleTable);
192: }
193:
194: /**
195: * Return a list of all defined styles as a string
196: *
197: * @return String
198: */
199: private static String getValidStylesAsString() {
200: return ConversionOptionsUtilities.getSeparatedString(
201: ConversionOptionsUtilities
202: .getPredefinedStyleTableNames(), " | ");
203: }
204:
205: private static String getValidConverterNames() {
206: return ConversionOptionsUtilities.getSeparatedString(
207: JavaSourceConverterProvider.getAllConverterNames(),
208: " | ");
209: }
210:
211: /**
212: * Return a converter instance for the specified converterType
213: * or null if no matching converter was found.
214: *
215: * @param converterType
216: * @return JavaSourceConverter
217: */
218: private static IJavaSourceConverter getConverter(
219: String converterType)
220: throws IllegalCommandlineParametersException {
221: IJavaSourceConverter converter = JavaSourceConverterProvider
222: .getJavaSourceConverterByName(converterType);
223: if (converter == null) {
224: throw new IllegalCommandlineParametersException(
225: "Unsupported converter type '" + converterType
226: + "'");
227: }
228: return converter;
229: }
230: }
|