KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > java2html > Java2Html


1 package de.java2html;
2
3 import java.io.IOException JavaDoc;
4 import java.io.StringReader JavaDoc;
5 import java.io.StringWriter JavaDoc;
6
7 import de.java2html.commandline.IJava2HtmlConversion;
8 import de.java2html.commandline.IllegalCommandlineParametersException;
9 import de.java2html.commandline.Java2HtmlCommandline;
10 import de.java2html.converter.IJavaSourceConverter;
11 import de.java2html.javasource.JavaSource;
12 import de.java2html.javasource.JavaSourceParser;
13 import de.java2html.options.JavaSourceConversionOptions;
14
15 /**
16  * A convenience class providing methods to use the Java2Html converter. By using this class you
17  * will not have the ability to benefit from all the features of the Java2Html converter library.
18  * However for most standard use cases the methods here will fit your needs.
19  */

20
21 public class Java2Html {
22   private Java2Html() {
23     //nothing to do
24
}
25
26   /** Converts the given <code>String</code> containing Java source code to HTML
27    * by using the standard options of the converter.
28    * Will return <code>null</code> if an error occures.
29    * The result itself does not have &lt;html&gt; and &lt;/html&gt; tags and so
30    * can be embedded in any HTML page.
31    *
32    * @param javaSource The Java source code as plain text.
33    * @return A HTML representation of the Java source code or <code>null</code>
34    * if an error occures.
35    * @see #convertToHtml(String, JavaSourceConversionOptions)
36    * @see #convertToHtmlPage(String)
37    */

38   public static String JavaDoc convertToHtml(String JavaDoc javaSource) {
39     return convertToHtml(javaSource, (JavaSourceConversionSettings) null);
40   }
41
42   /** Converts the given <code>String</code> containing Java source code to HTML
43    * by using the given options for the converter.
44    * Will return <code>null</code> if an error occures.
45    * The result itself does not have &lt;html&gt; and &lt;/html&gt; tags and so
46    * can be embedded in any HTML page.
47    *
48    * @param javaSource The Java source code as plain text.
49    * @param settings conversion options or <code>null</code> to use the standard options
50    * @return A HTML representation of the Java source code or <code>null</code>
51    * if an error occures.
52    * @see #convertToHtml(String)
53    * @see #convertToHtmlPage(String, JavaSourceConversionSettings)
54    */

55   public static String JavaDoc convertToHtml(String JavaDoc javaSource, JavaSourceConversionSettings settings) {
56     if (javaSource == null) {
57       return null;
58     }
59     if (settings == null) {
60       settings = JavaSourceConversionSettings.getDefault();
61     }
62
63     StringReader JavaDoc stringReader = new StringReader JavaDoc(javaSource);
64     JavaSource source = null;
65     try {
66       source = new JavaSourceParser(settings.getConversionOptions()).parse(stringReader);
67     }
68     catch (IOException JavaDoc e) {
69       return null;
70     }
71
72     IJavaSourceConverter converter = settings.createConverter();
73     StringWriter JavaDoc writer = new StringWriter JavaDoc();
74     try {
75       converter.convert(source, settings.getConversionOptions(), writer);
76     }
77     catch (IOException JavaDoc e) {
78       return null;
79     }
80     return writer.toString();
81   }
82
83   /** Converts the given <code>String</code> containing Java source code to a complete
84    * HTML page by using the standard options of the converter.
85    * Will return <code>null</code> if an error occures.
86    *
87    * @param javaSource The Java source code as plain text.
88    * @return A HTML representation of the Java source code or <code>null</code>
89    * if an error occures.
90    * @see #convertToHtmlPage(String, JavaSourceConversionSettings)
91    * @see #convertToHtml(String)
92    */

93   public static String JavaDoc convertToHtmlPage(String JavaDoc javaSource) {
94     return convertToHtmlPage(javaSource, (JavaSourceConversionSettings) null);
95   }
96
97   /** Converts the given <code>String</code> containing Java source code to a complete
98    * HTML page by using the given options for the converter.
99    * Will return <code>null</code> if an error occures.
100    *
101    * @param javaSource The Java source code as plain text.
102    * @param settings conversion options or <code>null</code> to use the standard options
103    * @return A HTML representation of the Java source code or <code>null</code>
104    * if an error occures.
105    * @see #convertToHtmlPage(String)
106    * @see #convertToHtmlPage(String, JavaSourceConversionSettings)
107    */

108   public static String JavaDoc convertToHtmlPage(String JavaDoc javaSource, JavaSourceConversionSettings settings) {
109     if (settings == null) {
110       settings = JavaSourceConversionSettings.getDefault();
111     }
112     IJavaSourceConverter converter = settings.createConverter();
113     StringWriter JavaDoc writer = new StringWriter JavaDoc();
114     try {
115       converter.writeDocumentHeader(writer, settings.getConversionOptions(), ""); //$NON-NLS-1$
116
writer.write(convertToHtml(javaSource, settings));
117       converter.writeDocumentFooter(writer, settings.getConversionOptions());
118     }
119     catch (IOException JavaDoc e) {
120       return null;
121     }
122     return writer.toString();
123   }
124
125   /**
126    * The commandline conversion from {@link Java2HtmlCommandline}
127    * can be invoked by running this class.
128    */

129   public static void main(String JavaDoc[] args) {
130     IJava2HtmlConversion commandlineConversion = null;
131     try {
132       commandlineConversion = Java2HtmlCommandline.createCommandlineConversion(args);
133     }
134     catch (IllegalCommandlineParametersException exception) {
135       System.err.println("Illegal commandline parameters: " + exception.getMessage());
136       Java2HtmlCommandline.printUsage();
137       System.exit(-1);
138     }
139     commandlineConversion.execute();
140   }
141
142   public static String JavaDoc convertToHtml(String JavaDoc text, JavaSourceConversionOptions options) {
143     return convertToHtml(text, new JavaSourceConversionSettings(options));
144   }
145
146   public static String JavaDoc convertToHtmlPage(String JavaDoc text, JavaSourceConversionOptions options) {
147     return convertToHtmlPage(text, new JavaSourceConversionSettings(options));
148   }
149 }
Popular Tags