01 package de.java2html.converter;
02 
03 import java.io.BufferedWriter;
04 import java.io.IOException;
05 
06 import de.java2html.javasource.TypedSource;
07 import de.java2html.options.JavaSourceConversionOptions;
08 import de.java2html.options.JavaSourceStyleTable;
09 
10 /**
11  * Algorithm and stuff for converting a {@link de.java2html.javasource.TypedSource} object to to a 
12  * XML or XHTML representation (experimental!).
13  
14  @author <a href="mailto:Jan.Tisje@gmx.de">Jan Tisje</a>
15  @version 1.0
16  */
17 public class JavaSource2XmlConverter extends AbstractJavaSourceToXmlConverter {
18 
19   private final static String TAG_LINE_NUMBERS_START = "<lines>";
20   private final static String TAG_LINE_NUMBERS_END = "</lines>";
21 
22   private final static String TAG_STYLE_START = "<style>";
23   private final static String TAG_STYLE_END = "</style>";
24 
25   private final static String TAG_SOURCE_START = "<source>";
26   private final static String TAG_SOURCE_END = "</source>";
27 
28   private final static String TAG_JAVA_START = "<java>";
29   private final static String TAG_JAVA_END = "</java>";
30 
31   @Override
32   protected String createHeader(JavaSourceStyleTable styleTable, String title, String charset) {
33     return XML_HEADER + TAG_JAVA_START + TAG_STYLE_START + createStyleSheet(styleTable+ TAG_STYLE_END + "\n";
34   }
35 
36   public JavaSource2XmlConverter() {
37     super(new ConverterMetaData("xml""XML""xml"));
38   }
39 
40   public String getName() {
41     return "xml"//$NON-NLS-1$
42   }
43 
44   @Override
45   protected String getHeaderEnd() {
46     return "";
47   }
48 
49   @Override
50   protected String getFooter() {
51     return TAG_JAVA_END;
52   }
53 
54   @Override
55   public void convert(TypedSource source, JavaSourceConversionOptions options, BufferedWriter writer)
56       throws IOException {
57     if (source == null) {
58       throw new IllegalStateException("Trying to write out converted code without having source set.");
59     }
60 
61     if (options.isShowLineNumbers()) {
62       writer.write(TAG_LINE_NUMBERS_START);
63       for (int i = 1; i <= source.getLineCount(); i++) {
64         writer.write(String.valueOf(i+ getLineEnd());
65         writer.write("\n");
66       }
67       writer.write(TAG_LINE_NUMBERS_END);
68     }
69 
70     writer.write(TAG_SOURCE_START);
71     writeCodeToXml(writer, source, options);
72     writer.write(TAG_SOURCE_END);
73   }
74 }