001: package de.java2html.converter;
002:
003: import java.io.BufferedWriter;
004: import java.io.IOException;
005:
006: import de.java2html.javasource.JavaSourceType;
007: import de.java2html.options.JavaSourceConversionOptions;
008: import de.java2html.options.JavaSourceStyleTable;
009: import de.java2html.util.HtmlUtilities;
010:
011: /**
012: * @author Markus Gebhard
013: */
014: public abstract class AbstractJavaSourceToXmlConverter extends
015: AbstractJavaSourceConverter {
016:
017: //original style sheet by Jan Tisje
018: // "td.java, td.java-ln {vertical-align:top;}\n" +
019: // "tt.java, tt.java-ln, pre.java, pre.java-ln {line-height:1em; margin-bottom:0em;}\n" +
020: // "td.java-ln { text-align:right; }\n"+
021: // "tt.java-ln, pre.java-ln { color:#888888 }\n"+
022: // "/* UNDEFINED */ span.java0 { color:black; }\n" +
023: // "/* CODE */ span.java1 { color:black; }\n" +
024: // "/* CODE_KEYWORD */ span.java2 { color:black; font-weight:bold; }\n" +
025: // "/* CODE_TYPE */ span.java3 { color:darkblue ;}\n" +
026: // "/* QUOTE */ span.java4 { color:darkgreen; }\n" +
027: // "/* COMMENT_LINE */ span.java5 { color:#888888; font-style:italic; }\n" +
028: // "/* COMMENT_BLOCK */ span.java6 { color:#888888; font-style:italic; }\n" +
029: // "/* COMMENT_JAVADOC */ span.java7 { color:green; font-style:italic; }\n" +
030: // "/* COMMENT_KEYWORD */ span.java8 { color:green; font-style:italic; font-weight:bold; }\n" +
031: // "/* EMPTY */ span.java9 {}\n" +
032: // "/* NUM_CONSTANT */ span.java10 { color:orange; }\n" +
033: // "/* CHAR_CONSTANT */ span.java11 { color:red; } ";
034:
035: protected final static String XML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n";
036:
037: protected boolean lineNumbers = true;
038: protected boolean pre = true;
039: protected String lineEnd = "";
040: private String space = " ";
041: private String headEnd = "";
042: private String foot = "";
043:
044: private final static String TAG_START = "<span class=\"java";
045: private final static String TAG_END = "\">";
046: private final static String TAG_CLOSE = "</span>";
047:
048: public AbstractJavaSourceToXmlConverter(ConverterMetaData metaData) {
049: super (metaData);
050: setOptions(false, false);
051: }
052:
053: /** @deprecated As of Sep 13, 2004 (Markus Gebhard) Only options in the
054: * Java2HtmlConversionOptions are available */
055: public void setOptions(boolean lineNumbers, boolean pre) {
056: this .lineNumbers = lineNumbers;
057: this .pre = pre;
058: foot = getFooter();
059: headEnd = getHeaderEnd();
060: if (pre) {
061: lineEnd = "";
062: space = " "; // normal space
063: } else {
064: lineEnd = "<br />";
065: space = " ";
066: }
067: }
068:
069: public String getDocumentHeader(
070: JavaSourceConversionOptions options, String title) {
071: return createHeader(options.getStyleTable(), title) + headEnd;
072: }
073:
074: public String getDocumentFooter(JavaSourceConversionOptions options) {
075: return foot;
076: }
077:
078: protected abstract String createHeader(
079: JavaSourceStyleTable styleTable, String title);
080:
081: protected abstract String getHeaderEnd();
082:
083: protected abstract String getFooter();
084:
085: protected static String createStyleSheet(
086: JavaSourceStyleTable styleTable) {
087: StringBuffer buffer = new StringBuffer();
088: buffer
089: .append("td.java, td.java-ln {vertical-align:top;}\n"
090: + "tt.java, tt.java-ln, pre.java, pre.java-ln {line-height:1em; margin-bottom:0em;}\n"
091: + "td.java-ln { text-align:right; }\n"
092: + "tt.java-ln, pre.java-ln { color:#888888 }\n");
093:
094: JavaSourceType[] types = JavaSourceType.getAll();
095: for (int i = 0; i < types.length; i++) {
096: appendStyle(buffer, types[i], styleTable);
097: }
098: return buffer.toString();
099: }
100:
101: private static void appendStyle(StringBuffer buffer,
102: JavaSourceType type, JavaSourceStyleTable styleTable) {
103: buffer.append("/* " + type.getName() + " */ ");
104: buffer.append("span.java" + type.getID() + " { ");
105: buffer.append("font-size: 10pt; ");
106: buffer.append("color:" + styleTable.get(type).getHtmlColor()
107: + "; ");
108: if (styleTable.get(type).isBold()) {
109: buffer.append("font-weight:bold; ");
110: }
111: if (styleTable.get(type).isItalic()) {
112: buffer.append("font-style:italic; ");
113: }
114: buffer.append("}\n");
115: }
116:
117: public String getBlockSeparator(JavaSourceConversionOptions options) {
118: return "";
119: }
120:
121: protected void toXml(String sourceCode,
122: JavaSourceType[] sourceTypes, int start, int end,
123: BufferedWriter writer) throws IOException {
124: writer.write(TAG_START + sourceTypes[start].getID() + TAG_END);
125:
126: String t = HtmlUtilities.encode(sourceCode, start, end + 1,
127: "\r\n ");
128:
129: for (int i = 0; i < t.length(); ++i) {
130: char ch = t.charAt(i);
131: if (ch == ' ')
132: if ((i < t.length() - 1) && (t.charAt(i + 1) == ' ')) {
133: writer.write(space);
134: } else {
135: writer.write(" ");
136: }
137: else if (ch == '\n') {
138: writer.write(lineEnd + "\n");
139: } else if (ch != '\r') {
140: writer.write(ch);
141: }
142: }
143:
144: writer.write(TAG_CLOSE);
145: }
146: }
|