1
2
3
4 package net.sf.tomp.highlight.docbook;
5
6 import java.io.IOException;
7 import java.io.StringReader;
8 import java.io.StringWriter;
9
10 import javax.xml.transform.Source;
11 import javax.xml.transform.stream.StreamSource;
12
13 import de.java2html.javasource.JavaSource;
14 import de.java2html.javasource.JavaSourceParser;
15 import de.java2html.options.Java2HtmlConversionOptions;
16 import de.java2html.options.JavaSourceStyleTable;
17
18 /***
19 * @author tomp
20 */
21 public class Java2HTML {
22
23 protected static int TAB_SIZE = 4;
24
25 protected static boolean SHOW_LINE_NUMBERS = true;
26
27 protected static boolean SHOW_TABLE_BORDER = true;
28
29 protected static boolean ADD_LINE_ANCHORS = true;
30
31 protected static String DEFAULT_STYLE_TABLE = "Kawa";
32
33 /***
34 * @param src the orig. source code
35 * @return Simplest version of java2html with all default options
36 */
37 public static Source java2html(String src) {
38 return java2html(src, SHOW_LINE_NUMBERS, null);
39 }
40
41 /***
42 * @param src the orig. source code
43 * @return Simplest version of java2html with all default options
44 */
45 public static Source java2html(String src, String filename) {
46 return java2html(src, SHOW_LINE_NUMBERS, filename);
47 }
48
49 /***
50 * @param src the orig. source code
51 * @param showLineNumbers whether to show line numbers
52 * @param showFileName whether to showFileName
53 * @return Simple version of java2html with some default options
54 */
55 public static Source java2html(String src, boolean showLineNumbers,
56 String fileName) {
57 return java2html(src, showLineNumbers, TAB_SIZE,
58 SHOW_TABLE_BORDER, ADD_LINE_ANCHORS, fileName);
59 }
60
61 /***
62 * Creates a StreamSource with the highlighted source code src
63 *
64 * @param src the orig. source code
65 * @param showLineNumbers whether to show line numbers
66 * @param tabSize which tab size
67 * @param showFileName whether to showFileName
68 * @param showTableBorder whether to showTableBorder
69 * @param addLineAnchors whether to addLineAnchors
70 * @return the StreamSource with the highlighted source code, in case of an
71 * error, the orig. src is returned.
72 */
73 public static Source java2html(String src, boolean showLineNumbers,
74 int tabSize, boolean showTableBorder,
75 boolean addLineAnchors, String fileName) {
76
77
78 Java2HtmlConversionOptions options = Java2HtmlConversionOptions
79 .getDefault();
80
81 options.setStyleTable(JavaSourceStyleTable.getPredefinedTable(DEFAULT_STYLE_TABLE));
82
83 options.setShowLineNumbers(showLineNumbers);
84 options.setShowFileName(fileName != null);
85 options.setTabSize(tabSize);
86 options.setShowTableBorder(showTableBorder);
87 options.setAddLineAnchors(addLineAnchors);
88
89
90
91 StringReader stringReader = new StringReader(src);
92
93
94 JavaSource javaSrc = null;
95 try {
96 javaSrc = new JavaSourceParser().parse(stringReader);
97 javaSrc.setFileName(fileName);
98 } catch (IOException e) {
99 e.printStackTrace();
100 }
101
102
103 JavaSource2HTMLConverter converter = new JavaSource2HTMLConverter(javaSrc);
104 converter.setConversionOptions(options);
105 StringWriter writer = new StringWriter();
106 try {
107 converter.convert(writer);
108
109 return asSource(writer.toString());
110 } catch (IOException e) {
111
112 return asSource(src);
113 }
114 }
115
116 protected static Source asSource(String src) {
117 return new StreamSource(new StringReader(src));
118 }
119 }