1 package de.java2html.converter; 2 3 import java.io.BufferedWriter ; 4 import java.io.IOException ; 5 6 import de.java2html.javasource.JavaSourceType; 7 import de.java2html.options.JavaSourceConversionOptions; 8 import de.java2html.options.JavaSourceStyleTable; 9 import de.java2html.util.HtmlUtilities; 10 11 14 public abstract class AbstractJavaSourceToXmlConverter extends AbstractJavaSourceConverter { 15 16 34 protected final static String XML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; 35 36 protected boolean lineNumbers = true; 37 protected boolean pre = true; 38 protected String lineEnd = ""; 39 private String space = " "; 40 private String headEnd = ""; 41 private String foot = ""; 42 43 private final static String TAG_START = "<span class=\"java"; 44 private final static String TAG_END = "\">"; 45 private final static String TAG_CLOSE = "</span>"; 46 47 public AbstractJavaSourceToXmlConverter(ConverterMetaData metaData) { 48 super(metaData); 49 setOptions(false, false); 50 } 51 52 54 public void setOptions(boolean lineNumbers, boolean pre) { 55 this.lineNumbers = lineNumbers; 56 this.pre = pre; 57 foot = getFooter(); 58 headEnd = getHeaderEnd(); 59 if (pre) { 60 lineEnd = ""; 61 space = " "; } 63 else { 64 lineEnd = "<br />"; 65 space = " "; 66 } 67 } 68 69 public String getDocumentHeader(JavaSourceConversionOptions options, String title) { 70 return createHeader(options.getStyleTable(), title) + headEnd; 71 } 72 73 public String getDocumentFooter(JavaSourceConversionOptions options) { 74 return foot; 75 } 76 77 protected abstract String createHeader(JavaSourceStyleTable styleTable, String title); 78 79 protected abstract String getHeaderEnd(); 80 81 protected abstract String getFooter(); 82 83 protected static String createStyleSheet(JavaSourceStyleTable styleTable) { 84 StringBuffer buffer = new StringBuffer (); 85 buffer.append("td.java, td.java-ln {vertical-align:top;}\n" 86 + "tt.java, tt.java-ln, pre.java, pre.java-ln {line-height:1em; margin-bottom:0em;}\n" 87 + "td.java-ln { text-align:right; }\n" 88 + "tt.java-ln, pre.java-ln { color:#888888 }\n"); 89 90 JavaSourceType[] types = JavaSourceType.getAll(); 91 for (int i = 0; i < types.length; i++) { 92 appendStyle(buffer, types[i], styleTable); 93 } 94 return buffer.toString(); 95 } 96 97 private static void appendStyle(StringBuffer buffer, JavaSourceType type, JavaSourceStyleTable styleTable) { 98 buffer.append("/* " + type.getName() + " */ "); 99 buffer.append("span.java" + type.getID() + " { "); 100 buffer.append("font-size: 10pt; "); 101 buffer.append("color:" + styleTable.get(type).getHtmlColor() + "; "); 102 if (styleTable.get(type).isBold()) { 103 buffer.append("font-weight:bold; "); 104 } 105 if (styleTable.get(type).isItalic()) { 106 buffer.append("font-style:italic; "); 107 } 108 buffer.append("}\n"); 109 } 110 111 public String getBlockSeparator(JavaSourceConversionOptions options) { 112 return ""; 113 } 114 115 protected void toXml(String sourceCode, JavaSourceType[] sourceTypes, int start, int end, BufferedWriter writer) 116 throws IOException { 117 writer.write(TAG_START + sourceTypes[start].getID() + TAG_END); 118 119 String t = HtmlUtilities.encode(sourceCode, start, end + 1, "\r\n "); 120 121 for (int i = 0; i < t.length(); ++i) { 122 char ch = t.charAt(i); 123 if (ch == ' ') 124 if ((i < t.length() - 1) && (t.charAt(i + 1) == ' ')) { 125 writer.write(space); 126 } 127 else { 128 writer.write(" "); 129 } 130 else if (ch == '\n') { 131 writer.write(lineEnd + "\n"); 132 } 133 else if (ch != '\r') { 134 writer.write(ch); 135 } 136 } 137 138 writer.write(TAG_CLOSE); 139 } 140 } | Popular Tags |