001: package de.java2html;
002:
003: import java.io.IOException;
004: import java.io.StringReader;
005: import java.io.StringWriter;
006:
007: import de.java2html.commandline.IJava2HtmlConversion;
008: import de.java2html.commandline.IllegalCommandlineParametersException;
009: import de.java2html.commandline.Java2HtmlCommandline;
010: import de.java2html.converter.IJavaSourceConverter;
011: import de.java2html.javasource.JavaSource;
012: import de.java2html.javasource.JavaSourceParser;
013: import de.java2html.options.JavaSourceConversionOptions;
014:
015: /**
016: * A convenience class providing methods to use the Java2Html converter. By using this class you
017: * will not have the ability to benefit from all the features of the Java2Html converter library.
018: * However for most standard use cases the methods here will fit your needs.
019: */
020:
021: public class Java2Html {
022: private Java2Html() {
023: //nothing to do
024: }
025:
026: /** Converts the given <code>String</code> containing Java source code to HTML
027: * by using the standard options of the converter.
028: * Will return <code>null</code> if an error occures.
029: * The result itself does not have <html> and </html> tags and so
030: * can be embedded in any HTML page.
031: *
032: * @param javaSource The Java source code as plain text.
033: * @return A HTML representation of the Java source code or <code>null</code>
034: * if an error occures.
035: * @see #convertToHtml(String, JavaSourceConversionOptions)
036: * @see #convertToHtmlPage(String)
037: */
038: public static String convertToHtml(String javaSource) {
039: return convertToHtml(javaSource,
040: (JavaSourceConversionSettings) null);
041: }
042:
043: /** Converts the given <code>String</code> containing Java source code to HTML
044: * by using the given options for the converter.
045: * Will return <code>null</code> if an error occures.
046: * The result itself does not have <html> and </html> tags and so
047: * can be embedded in any HTML page.
048: *
049: * @param javaSource The Java source code as plain text.
050: * @param settings conversion options or <code>null</code> to use the standard options
051: * @return A HTML representation of the Java source code or <code>null</code>
052: * if an error occures.
053: * @see #convertToHtml(String)
054: * @see #convertToHtmlPage(String, JavaSourceConversionSettings)
055: */
056: public static String convertToHtml(String javaSource,
057: JavaSourceConversionSettings settings) {
058: if (javaSource == null) {
059: return null;
060: }
061: if (settings == null) {
062: settings = JavaSourceConversionSettings.getDefault();
063: }
064:
065: StringReader stringReader = new StringReader(javaSource);
066: JavaSource source = null;
067: try {
068: source = new JavaSourceParser(settings
069: .getConversionOptions()).parse(stringReader);
070: } catch (IOException e) {
071: return null;
072: }
073:
074: IJavaSourceConverter converter = settings.createConverter();
075: StringWriter writer = new StringWriter();
076: try {
077: converter.convert(source, settings.getConversionOptions(),
078: writer);
079: } catch (IOException e) {
080: return null;
081: }
082: return writer.toString();
083: }
084:
085: /** Converts the given <code>String</code> containing Java source code to a complete
086: * HTML page by using the standard options of the converter.
087: * Will return <code>null</code> if an error occures.
088: *
089: * @param javaSource The Java source code as plain text.
090: * @return A HTML representation of the Java source code or <code>null</code>
091: * if an error occures.
092: * @see #convertToHtmlPage(String, JavaSourceConversionSettings)
093: * @see #convertToHtml(String)
094: */
095: public static String convertToHtmlPage(String javaSource) {
096: return convertToHtmlPage(javaSource,
097: (JavaSourceConversionSettings) null);
098: }
099:
100: /** Converts the given <code>String</code> containing Java source code to a complete
101: * HTML page by using the given options for the converter.
102: * Will return <code>null</code> if an error occures.
103: *
104: * @param javaSource The Java source code as plain text.
105: * @param settings conversion options or <code>null</code> to use the standard options
106: * @return A HTML representation of the Java source code or <code>null</code>
107: * if an error occures.
108: * @see #convertToHtmlPage(String)
109: * @see #convertToHtmlPage(String, JavaSourceConversionSettings)
110: */
111: public static String convertToHtmlPage(String javaSource,
112: JavaSourceConversionSettings settings) {
113: if (settings == null) {
114: settings = JavaSourceConversionSettings.getDefault();
115: }
116: IJavaSourceConverter converter = settings.createConverter();
117: StringWriter writer = new StringWriter();
118: try {
119: converter.writeDocumentHeader(writer, settings
120: .getConversionOptions(), ""); //$NON-NLS-1$
121: writer.write(convertToHtml(javaSource, settings));
122: converter.writeDocumentFooter(writer, settings
123: .getConversionOptions());
124: } catch (IOException e) {
125: return null;
126: }
127: return writer.toString();
128: }
129:
130: /**
131: * The commandline conversion from {@link Java2HtmlCommandline}
132: * can be invoked by running this class.
133: */
134: public static void main(String[] args) {
135: IJava2HtmlConversion commandlineConversion = null;
136: try {
137: commandlineConversion = Java2HtmlCommandline
138: .createCommandlineConversion(args);
139: } catch (IllegalCommandlineParametersException exception) {
140: System.err.println("Illegal commandline parameters: "
141: + exception.getMessage());
142: Java2HtmlCommandline.printUsage();
143: System.exit(-1);
144: }
145: commandlineConversion.execute();
146: }
147:
148: public static String convertToHtml(String text,
149: JavaSourceConversionOptions options) {
150: return convertToHtml(text, new JavaSourceConversionSettings(
151: options));
152: }
153:
154: public static String convertToHtmlPage(String text,
155: JavaSourceConversionOptions options) {
156: return convertToHtmlPage(text,
157: new JavaSourceConversionSettings(options));
158: }
159: }
|