1 package de.java2html.converter; 2 3 import java.io.BufferedWriter ; 4 import java.io.IOException ; 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 18 public class JavaSource2XmlConverter extends AbstractJavaSourceToXmlConverter { 19 20 protected String createHeader(JavaSourceStyleTable styleTable, String 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 BLOCK_LINE_NUMBERS = "lines>"; 33 private final static String BLOCK_STYLE = "style>"; 34 private final static String BLOCK_JAVA = "source>"; 35 private final static String BLOCK_ROOT = "java>"; 36 37 public JavaSource2XmlConverter() { 38 super(new ConverterMetaData("xml", "XML", "xml")); 39 } 40 41 public String getName() { 42 return "xml"; } 44 45 protected String getHeaderEnd() { 46 return ""; 47 } 48 49 protected String getFooter() { 50 return "</" + BLOCK_ROOT; 51 } 52 53 public void convert(JavaSource source, JavaSourceConversionOptions options, BufferedWriter writer) 54 throws IOException { 55 if (source == null) { 56 throw new IllegalStateException ("Trying to write out converted code without having source set."); 57 } 58 59 String 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 |