001: package de.java2html.converter;
002:
003: import java.io.BufferedWriter;
004: import java.io.IOException;
005:
006: import de.java2html.javasource.JavaSource;
007: import de.java2html.javasource.JavaSourceType;
008: import de.java2html.options.JavaSourceConversionOptions;
009: import de.java2html.options.JavaSourceStyleTable;
010:
011: /**
012: * @author Markus Gebhard
013: */
014: public class JavaSource2XhtmlConverter extends
015: AbstractJavaSourceToXmlConverter {
016:
017: private final static String XHTML_HEADER_END = "</head>\n<body>\n";
018: private final static String XHTML_FOOTER = "</body>\n</html>";
019:
020: public JavaSource2XhtmlConverter() {
021: super (new ConverterMetaData("xhtml",
022: "XHTML 1.0 transitional (with stylesheet)", "xhtml"));
023: }
024:
025: protected String getHeaderEnd() {
026: return XHTML_HEADER_END;
027: }
028:
029: protected String getFooter() {
030: return XHTML_FOOTER;
031: }
032:
033: protected String createHeader(JavaSourceStyleTable styleTable,
034: String title) {
035: return XML_HEADER + createHeaderStart(styleTable, title);
036: }
037:
038: private String createHeaderStart(JavaSourceStyleTable styleTable,
039: String title) {
040: if (title == null) {
041: title = ""; //$NON-NLS-1$
042: }
043: return "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">\n"
044: + "<html xmlns=\"http://www.w3.org/1999/xhtml\">\n"
045: + "<head>\n<style>\n"
046: + createStyleSheet(styleTable)
047: + "</style>\n"
048: + "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\" />\n"
049: + "<title>" + title + "</title>\n";
050: }
051:
052: public void convert(JavaSource source,
053: JavaSourceConversionOptions options, BufferedWriter writer)
054: throws IOException {
055: if (source == null) {
056: throw new IllegalStateException(
057: "Trying to write out converted code without having source set.");
058: }
059:
060: String sourceCode = source.getCode();
061: JavaSourceType[] sourceTypes = source.getClassification();
062:
063: if (lineNumbers) {
064: writer
065: .write("<table class=\"java\">\n<tr class=\"java\">\n<td valign=\"top\" class=\"java-ln\">");
066: writer.newLine();
067:
068: if (pre) {
069: writer.write("<pre class=\"java-ln\">");
070: } else {
071: writer.write("<tt class=\"java-ln\">");
072: }
073: for (int i = 1; i <= source.getLineCount(); i++) {
074: writer.write(String.valueOf(i) + lineEnd);
075: writer.newLine();
076: }
077: // sb.append(new Integer(source.getLineCount()).toString());
078:
079: if (pre) {
080: writer.write("</pre>\n");
081: } else {
082: writer.write("</tt>\n");
083: }
084: writer.write("</td>\n");
085: writer.write("<td valign=\"top\" class=\"java\">");
086: writer.newLine();
087: }
088:
089: else if (pre) {
090: writer.write("<pre class=\"java\">");
091: } else {
092: writer.write("<tt class=\"java\">");
093: }
094:
095: int start = 0;
096: int end = 0;
097:
098: while (start < sourceTypes.length) {
099: while (end < sourceTypes.length - 1
100: && (sourceTypes[end + 1] == sourceTypes[start] || sourceTypes[end + 1] == JavaSourceType.BACKGROUND)) {
101: ++end;
102: }
103: toXml(sourceCode, sourceTypes, start, end, writer);
104: start = end + 1;
105: end = start;
106: }
107:
108: if (pre) {
109: writer.write("</pre>");
110: } else {
111: writer.write("</tt>");
112: }
113:
114: if (lineNumbers) {
115: writer.write("</td>\n</tr>\n</table>");
116: writer.newLine();
117: }
118: }
119: }
|