KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > java2html > converter > JavaSource2XmlConverter


1 package de.java2html.converter;
2
3 import java.io.BufferedWriter JavaDoc;
4 import java.io.IOException JavaDoc;
5
6 import de.java2html.javasource.JavaSource;
7 import de.java2html.javasource.JavaSourceType;
8 import de.java2html.options.JavaSourceConversionOptions;
9 import de.java2html.options.JavaSourceStyleTable;
10
11 /**
12  * Algorithm and stuff for converting a {@link de.java2html.javasource.JavaSource} object to to a
13  * XML or XHTML representation (experimental!).
14  *
15  * @author <a HREF="mailto:Jan.Tisje@gmx.de">Jan Tisje</a>
16  * @version 1.0
17  */

18 public class JavaSource2XmlConverter extends AbstractJavaSourceToXmlConverter {
19
20   protected String JavaDoc createHeader(JavaSourceStyleTable styleTable, String JavaDoc title) {
21     return XML_HEADER
22         + "<"
23         + BLOCK_ROOT
24         + "<"
25         + BLOCK_STYLE
26         + createStyleSheet(styleTable)
27         + "</"
28         + BLOCK_STYLE
29         + "\n";
30   }
31
32   private final static String JavaDoc BLOCK_LINE_NUMBERS = "lines>";
33   private final static String JavaDoc BLOCK_STYLE = "style>";
34   private final static String JavaDoc BLOCK_JAVA = "source>";
35   private final static String JavaDoc BLOCK_ROOT = "java>";
36
37   public JavaSource2XmlConverter() {
38     super(new ConverterMetaData("xml", "XML", "xml"));
39   }
40
41   public String JavaDoc getName() {
42     return "xml"; //$NON-NLS-1$
43
}
44
45   protected String JavaDoc getHeaderEnd() {
46     return "";
47   }
48
49   protected String JavaDoc getFooter() {
50     return "</" + BLOCK_ROOT;
51   }
52
53   public void convert(JavaSource source, JavaSourceConversionOptions options, BufferedWriter JavaDoc writer)
54       throws IOException JavaDoc {
55     if (source == null) {
56       throw new IllegalStateException JavaDoc("Trying to write out converted code without having source set.");
57     }
58
59     String JavaDoc sourceCode = source.getCode();
60     JavaSourceType[] sourceTypes = source.getClassification();
61
62     if (lineNumbers) {
63       writer.write("<" + BLOCK_LINE_NUMBERS);
64       for (int i = 1; i <= source.getLineCount(); i++) {
65         writer.write(String.valueOf(i) + lineEnd);
66         writer.newLine();
67       }
68       writer.write("</" + BLOCK_LINE_NUMBERS);
69     }
70
71     writer.write("<" + BLOCK_JAVA);
72
73     int start = 0;
74     int end = 0;
75     while (start < sourceTypes.length) {
76       while (end < sourceTypes.length - 1
77           && (sourceTypes[end + 1] == sourceTypes[start] || sourceTypes[end + 1] == JavaSourceType.BACKGROUND)) {
78         ++end;
79       }
80       toXml(sourceCode, sourceTypes, start, end, writer);
81       start = end + 1;
82       end = start;
83     }
84
85     writer.write("</" + BLOCK_JAVA);
86   }
87 }
Popular Tags