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