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