1 package de.java2html.converter; 2 3 import java.io.BufferedWriter ; 4 import java.io.IOException ; 5 import java.io.Writer ; 6 7 import de.java2html.javasource.JavaSource; 8 import de.java2html.javasource.JavaSourceIterator; 9 import de.java2html.javasource.JavaSourceRun; 10 import de.java2html.javasource.JavaSourceType; 11 import de.java2html.options.JavaSourceConversionOptions; 12 import de.java2html.options.JavaSourceStyleEntry; 13 import de.java2html.options.JavaSourceStyleTable; 14 import de.java2html.util.RGB; 15 16 19 public class JavaSource2RtfConverter extends AbstractJavaSourceConverter { 20 21 public JavaSource2RtfConverter() { 22 super(new ConverterMetaData("rtf", "RTF (Rich Text Format)", "rtf")); 23 } 24 25 private final static int FONT_SIZE = 9; 26 private final static String FONT_NAME = "Courier New"; 27 28 public String getDocumentHeader(JavaSourceConversionOptions options, String title) { 29 return ""; 30 } 31 32 public String getDocumentFooter(JavaSourceConversionOptions options) { 33 return ""; 34 } 35 36 public String getBlockSeparator(JavaSourceConversionOptions options) { 37 return ""; 38 } 39 40 public void convert(JavaSource source, JavaSourceConversionOptions options, BufferedWriter writer) 41 throws IOException { 42 writer.write("{\\rtf1\\ansi\\deff0{\\fonttbl{"); 43 writer.write("\\f0\\fmodern "); 44 writer.write(FONT_NAME); 45 writer.write(";"); 46 writer.write("}"); 47 writer.write("}\n"); 48 49 writer.write("{\\colortbl"); 50 JavaSourceType[] types = JavaSourceType.getAll(); 51 for (int i = 0; i < types.length; ++i) { 52 JavaSourceStyleEntry style = options.getStyleTable().get(types[i]); 53 writeColor(style.getColor(), writer); 54 } 55 writer.write("}\n"); 56 57 writer.write("\\deflang1031\\pard\\plain\\f0"); 58 writer.write("\\fs" + FONT_SIZE * 2); 59 writer.write("\\cb" + JavaSourceType.BACKGROUND.getID()); 60 61 int lineCifferCount = String.valueOf(source.getLineCount()).length(); 62 JavaSourceIterator iterator = source.getIterator(); 63 int lineNumber = 1; 64 while (iterator.hasNext()) { 65 JavaSourceRun run = iterator.getNext(); 66 67 if (run.isAtStartOfLine()) { 68 if (lineNumber > 1) { 69 writer.newLine(); 70 writer.write("\\par "); 71 } 72 73 if (options.isShowLineNumbers()) { 74 writeLineNumber(writer, lineCifferCount, lineNumber, options); 75 } 76 ++lineNumber; 77 } 78 writeText(options, run, writer); 79 } 80 81 writer.write("}"); 82 } 83 84 private void writeText(JavaSourceConversionOptions options, JavaSourceRun run, BufferedWriter writer) 85 throws IOException { 86 writeText(options.getStyleTable(), writer, run.getCode(), run.getType()); 87 } 88 89 private void writeText(JavaSourceStyleTable styleTable, BufferedWriter writer, String text, JavaSourceType type) 90 throws IOException { 91 JavaSourceStyleEntry style = styleTable.get(type); 92 93 writer.write("{\\f0"); 94 if (style.isBold()) { 95 writer.write("\\b"); 96 } 97 if (style.isItalic()) { 98 writer.write("\\i"); 99 } 100 101 writer.write("\\cf" + type.getID() + " "); 102 103 for (int i = 0; i < text.length(); ++i) { 104 char ch = text.charAt(i); 105 if (ch == '\\') { 106 writer.write("\\\\"); 107 } 108 else if (ch == '{') { 109 writer.write("\\{"); 110 } 111 else if (ch == '}') { 112 writer.write("\\}"); 113 } 114 else { 115 writer.write(ch); 116 } 117 } 118 119 writer.write("}"); 120 } 121 122 private void writeLineNumber( 123 BufferedWriter writer, 124 int lineCifferCount, 125 int lineNumber, 126 JavaSourceConversionOptions options) throws IOException { 127 128 String text = String.valueOf(lineNumber); 129 int cifferCount = lineCifferCount - text.length(); 130 while (cifferCount > 0) { 131 text = '0' + text; 132 --cifferCount; 133 } 134 text = text + " "; 135 136 writeText(options.getStyleTable(), writer, text, JavaSourceType.LINE_NUMBERS); 137 } 138 139 private static void writeColor(RGB color, Writer writer) throws IOException { 140 writer.write("\\red" + color.getRed()); writer.write("\\green" + color.getGreen()); writer.write("\\blue" + color.getBlue()); writer.write(";"); } 145 } | Popular Tags |