Skip to content

Commit

Permalink
#1 (bbottema/outlook-message-parser/issues/17): Restored support for …
Browse files Browse the repository at this point in the history
…UTF-8's legacy name (cp)65001
  • Loading branch information
Benny Bottema authored and Benny Bottema committed Oct 22, 2019
1 parent 4d25630 commit 20a8f47
Show file tree
Hide file tree
Showing 8 changed files with 2,174 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;

import static java.nio.charset.StandardCharsets.UTF_8;

public class CharsetHelper {
private static String[] CHARSET_PREFIXES = {"", "cp", "iso-", "ibm", "x-windows-", "ms"};

public static final Charset WINDOWS_CHARSET = Charset.forName("CP1252");

public static Charset findCharset(String rtfCodePage) {
return rtfCodePage.equals("65001") || rtfCodePage.equalsIgnoreCase("cp65001") ? UTF_8 : detectCharset(rtfCodePage);
}

private static Charset detectCharset(String rtfCodePage) {
for (String prefix : CHARSET_PREFIXES) {
try {
return Charset.forName(prefix + rtfCodePage);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,20 @@ public void testComplexRtfConversion() {

assertThat(normalizeText(html)).isEqualTo(normalizeText(expectedHtml));
}

@Test
public void testChineseRtfConversion() {
public void testChineseRtfConversion() {
String html = RTF2HTMLConverterClassic.INSTANCE.rtf2html(classpathFileToString("test-messages/input/chinese-exotic-test.rtf"));
String expectedHtml = classpathFileToString("test-messages/output/classic/chinese-exotic-test.html");


assertThat(normalizeText(html)).isEqualTo(normalizeText(expectedHtml));
}

@Test
public void testUnicodeRtfConversion() {
String html = RTF2HTMLConverterClassic.INSTANCE.rtf2html(classpathFileToString("test-messages/input/unicode-test.rtf"));
String expectedHtml = classpathFileToString("test-messages/output/classic/unicode-test.html");

assertThat(normalizeText(html)).isEqualTo(normalizeText(expectedHtml));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,12 @@ public void testChineseRtfConversion() {

assertThat(normalizeText(html)).isEqualTo(normalizeText(expectedHtml));
}

@Test
public void testUnicodeRtfConversion() {
String html = RTF2HTMLConverterClassic.INSTANCE.rtf2html(classpathFileToString("test-messages/input/unicode-test.rtf"));
String expectedHtml = classpathFileToString("test-messages/output/swing/unicode-test.html");

assertThat(normalizeText(html)).isEqualTo(normalizeText(expectedHtml));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,12 @@ public void testChineseRtfConversion() {

assertThat(normalizeText(html)).isEqualTo(normalizeText(expectedHtml));
}

@Test
public void testUnicodeRtfConversion() {
String html = RTF2HTMLConverterClassic.INSTANCE.rtf2html(classpathFileToString("test-messages/input/unicode-test.rtf"));
String expectedHtml = classpathFileToString("test-messages/output/rfcompliant/unicode-test.html");

assertThat(normalizeText(html)).isEqualTo(normalizeText(expectedHtml));
}
}
129 changes: 129 additions & 0 deletions src/test/resources/test-messages/input/unicode-test.rtf

Large diffs are not rendered by default.

670 changes: 670 additions & 0 deletions src/test/resources/test-messages/output/classic/unicode-test.html

Large diffs are not rendered by default.

670 changes: 670 additions & 0 deletions src/test/resources/test-messages/output/rfcompliant/unicode-test.html

Large diffs are not rendered by default.

670 changes: 670 additions & 0 deletions src/test/resources/test-messages/output/swing/unicode-test.html

Large diffs are not rendered by default.

0 comments on commit 20a8f47

Please sign in to comment.