View Javadoc

1   /*
2    * Created on 28.7.2004
3    */
4   package net.sf.tomp.highlight.html;
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, DEFAULT_STYLE_TABLE);
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, DEFAULT_STYLE_TABLE);
47      }
48  
49      /***
50       * @param src the orig. source code
51       * @return Simplest version of java2html with all default options
52       */
53      public static Source java2html(String src, String filename, String styletable) {
54          return java2html(src, SHOW_LINE_NUMBERS, filename, styletable);
55      }
56  
57      /***
58       * @param src the orig. source code
59       * @param showLineNumbers whether to show line numbers
60       * @param showFileName whether to showFileName
61       * @return Simple version of java2html with some default options
62       */
63      public static Source java2html(String src, boolean showLineNumbers,
64              String fileName, String styletable) {
65          return java2html(src, showLineNumbers, TAB_SIZE,
66                  SHOW_TABLE_BORDER, ADD_LINE_ANCHORS, fileName, styletable);
67      }
68  
69      /***
70       * Creates a StreamSource with the highlighted source code src
71       * 
72       * @param src the orig. source code
73       * @param showLineNumbers whether to show line numbers
74       * @param tabSize which tab size
75       * @param showFileName whether to showFileName
76       * @param showTableBorder whether to showTableBorder
77       * @param addLineAnchors whether to addLineAnchors
78       * @return the StreamSource with the highlighted source code, in case of an
79       *         error, the orig. src is returned.
80       */
81      public static Source java2html(String src, boolean showLineNumbers,
82              int tabSize, boolean showTableBorder,
83              boolean addLineAnchors, String fileName, String styletable) {
84  
85          // set the options
86          Java2HtmlConversionOptions options = Java2HtmlConversionOptions
87                  .getDefault();
88          // use default style
89          options.setStyleTable(JavaSourceStyleTable.getPredefinedTable(styletable));
90          // ... and modify it
91          options.setShowLineNumbers(showLineNumbers);
92          options.setShowFileName(fileName != null);
93          options.setTabSize(tabSize);
94          options.setShowTableBorder(showTableBorder);
95          options.setAddLineAnchors(addLineAnchors);
96  
97          // generate Source
98          //      Create a reader of the raw input text
99          StringReader stringReader = new StringReader(src);
100 
101         //        Parse the raw text to a JavaSource object
102         JavaSource javaSrc = null;
103         try {
104             javaSrc = new JavaSourceParser().parse(stringReader);
105             javaSrc.setFileName(fileName);
106         } catch (IOException e) {
107             e.printStackTrace();
108         }
109 
110         //      Create a converter and write the JavaSource object as XML
111         JavaSource2HTMLConverter converter = new JavaSource2HTMLConverter(javaSrc);
112         converter.setConversionOptions(options);
113         StringWriter writer = new StringWriter();
114         try {
115             converter.convert(writer);
116             return asSource(writer.toString());
117         } catch (IOException e) {
118             return asSource(src);
119         }
120     }
121 
122     protected static Source asSource(String src) {
123         return new StreamSource(new StringReader(src));
124     }
125 }