1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-27 19:52:17 -05:00

Enhancement for charset fallback.

Fallback rule is defined in CHARSET_FALLBACK_MAP.
Multi-level fallback is supported.
This commit is contained in:
eagan 2013-02-16 19:11:34 +09:00 committed by cketti
parent 337487e2ae
commit a5f4ddae91

View File

@ -905,6 +905,16 @@ public class MimeUtility {
{"image/pjpeg", "image/jpeg"}, // see issue 1712 {"image/pjpeg", "image/jpeg"}, // see issue 1712
{"application/x-zip-compressed", "application/zip"} // see issue 3791 {"application/x-zip-compressed", "application/zip"} // see issue 3791
}; };
/**
* Table for charset fallback.
*
* Table format: not supported charset (regex), fallback
*/
private static final String[][] CHARSET_FALLBACK_MAP = new String[][] {
{"iso-2022-jp-[\\d]+", "iso-2022-jp"},
{".*", "US-ASCII"}
};
public static String unfold(String s) { public static String unfold(String s) {
if (s == null) { if (s == null) {
@ -2291,7 +2301,7 @@ public class MimeUtility {
charset = "shift_jis"; charset = "shift_jis";
} }
/* /*
* See if there is conversion from the MIME charset to the Java one. * See if there is conversion from the MIME charset to the Java one.
* this function may also throw an exception if the charset name is not known * this function may also throw an exception if the charset name is not known
@ -2302,10 +2312,17 @@ public class MimeUtility {
} catch (IllegalCharsetNameException e) { } catch (IllegalCharsetNameException e) {
supported = false; supported = false;
} }
if (!supported) { for (int i = 0, len = CHARSET_FALLBACK_MAP.length; !supported && i < len; i++) {
Log.e(K9.LOG_TAG, "I don't know how to deal with the charset " + charset + if (charset.matches(CHARSET_FALLBACK_MAP[i][0])) {
". Falling back to US-ASCII"); Log.e(K9.LOG_TAG, "I don't know how to deal with the charset " + charset +
charset = "US-ASCII"; ". Falling back to " + CHARSET_FALLBACK_MAP[i][1]);
charset = CHARSET_FALLBACK_MAP[i][1];
try {
supported = Charset.isSupported(charset);
} catch (IllegalCharsetNameException e) {
supported = false;
}
}
} }
/* /*
* Convert and return as new String * Convert and return as new String