001: package de.java2html.converter;
002:
003: import java.io.BufferedWriter;
004: import java.io.IOException;
005: import java.io.Writer;
006:
007: import de.java2html.javasource.JavaSource;
008: import de.java2html.javasource.JavaSourceIterator;
009: import de.java2html.javasource.JavaSourceRun;
010: import de.java2html.javasource.JavaSourceType;
011: import de.java2html.options.JavaSourceConversionOptions;
012: import de.java2html.options.JavaSourceStyleEntry;
013: import de.java2html.options.JavaSourceStyleTable;
014: import de.java2html.util.RGB;
015:
016: /**
017: * @author Markus Gebhard
018: */
019: public class JavaSource2RtfConverter extends
020: AbstractJavaSourceConverter {
021:
022: public JavaSource2RtfConverter() {
023: super (new ConverterMetaData("rtf", "RTF (Rich Text Format)",
024: "rtf"));
025: }
026:
027: private final static int FONT_SIZE = 9;
028: private final static String FONT_NAME = "Courier New";
029:
030: public String getDocumentHeader(
031: JavaSourceConversionOptions options, String title) {
032: return "";
033: }
034:
035: public String getDocumentFooter(JavaSourceConversionOptions options) {
036: return "";
037: }
038:
039: public String getBlockSeparator(JavaSourceConversionOptions options) {
040: return "";
041: }
042:
043: public void convert(JavaSource source,
044: JavaSourceConversionOptions options, BufferedWriter writer)
045: throws IOException {
046: writer.write("{\\rtf1\\ansi\\deff0{\\fonttbl{");
047: writer.write("\\f0\\fmodern ");
048: writer.write(FONT_NAME);
049: writer.write(";");
050: writer.write("}");
051: writer.write("}\n");
052:
053: writer.write("{\\colortbl");
054: JavaSourceType[] types = JavaSourceType.getAll();
055: for (int i = 0; i < types.length; ++i) {
056: JavaSourceStyleEntry style = options.getStyleTable().get(
057: types[i]);
058: writeColor(style.getColor(), writer);
059: }
060: writer.write("}\n");
061:
062: writer.write("\\deflang1031\\pard\\plain\\f0");
063: writer.write("\\fs" + FONT_SIZE * 2);
064: writer.write("\\cb" + JavaSourceType.BACKGROUND.getID());
065:
066: int lineCifferCount = String.valueOf(source.getLineCount())
067: .length();
068: JavaSourceIterator iterator = source.getIterator();
069: int lineNumber = 1;
070: while (iterator.hasNext()) {
071: JavaSourceRun run = iterator.getNext();
072:
073: if (run.isAtStartOfLine()) {
074: if (lineNumber > 1) {
075: writer.newLine();
076: writer.write("\\par ");
077: }
078:
079: if (options.isShowLineNumbers()) {
080: writeLineNumber(writer, lineCifferCount,
081: lineNumber, options);
082: }
083: ++lineNumber;
084: }
085: writeText(options, run, writer);
086: }
087:
088: writer.write("}");
089: }
090:
091: private void writeText(JavaSourceConversionOptions options,
092: JavaSourceRun run, BufferedWriter writer)
093: throws IOException {
094: writeText(options.getStyleTable(), writer, run.getCode(), run
095: .getType());
096: }
097:
098: private void writeText(JavaSourceStyleTable styleTable,
099: BufferedWriter writer, String text, JavaSourceType type)
100: throws IOException {
101: JavaSourceStyleEntry style = styleTable.get(type);
102:
103: writer.write("{\\f0");
104: if (style.isBold()) {
105: writer.write("\\b");
106: }
107: if (style.isItalic()) {
108: writer.write("\\i");
109: }
110:
111: writer.write("\\cf" + type.getID() + " ");
112:
113: for (int i = 0; i < text.length(); ++i) {
114: char ch = text.charAt(i);
115: if (ch == '\\') {
116: writer.write("\\\\");
117: } else if (ch == '{') {
118: writer.write("\\{");
119: } else if (ch == '}') {
120: writer.write("\\}");
121: } else {
122: writer.write(ch);
123: }
124: }
125:
126: writer.write("}");
127: }
128:
129: private void writeLineNumber(BufferedWriter writer,
130: int lineCifferCount, int lineNumber,
131: JavaSourceConversionOptions options) throws IOException {
132:
133: String text = String.valueOf(lineNumber);
134: int cifferCount = lineCifferCount - text.length();
135: while (cifferCount > 0) {
136: text = '0' + text;
137: --cifferCount;
138: }
139: text = text + " ";
140:
141: writeText(options.getStyleTable(), writer, text,
142: JavaSourceType.LINE_NUMBERS);
143: }
144:
145: private static void writeColor(RGB color, Writer writer)
146: throws IOException {
147: writer.write("\\red" + color.getRed()); //$NON-NLS-1$
148: writer.write("\\green" + color.getGreen()); //$NON-NLS-1$
149: writer.write("\\blue" + color.getBlue()); //$NON-NLS-1$
150: writer.write(";"); //$NON-NLS-1$
151: }
152: }
|