| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package uk.me.parabola.imgfmt.app.labelenc; |
| 18 |
|
| 19 |
import uk.me.parabola.imgfmt.ExitException; |
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
public class CodeFunctions { |
| 25 |
|
| 26 |
public static final int ENCODING_FORMAT6 = 6; |
| 27 |
private static final int ENCODING_FORMAT9 = 9; |
| 28 |
private static final int ENCODING_FORMAT10 = 10; |
| 29 |
|
| 30 |
private int codepage; |
| 31 |
private int encodingType; |
| 32 |
private CharacterEncoder encoder; |
| 33 |
private CharacterDecoder decoder; |
| 34 |
|
| 35 |
protected void setEncoder(CharacterEncoder encoder) { |
| 36 |
this.encoder = encoder; |
| 37 |
} |
| 38 |
|
| 39 |
public CharacterEncoder getEncoder() { |
| 40 |
return encoder; |
| 41 |
} |
| 42 |
|
| 43 |
private void setDecoder(CharacterDecoder decoder) { |
| 44 |
this.decoder = decoder; |
| 45 |
} |
| 46 |
|
| 47 |
public CharacterDecoder getDecoder() { |
| 48 |
return decoder; |
| 49 |
} |
| 50 |
|
| 51 |
public int getEncodingType() { |
| 52 |
return encodingType; |
| 53 |
} |
| 54 |
|
| 55 |
private void setEncodingType(int encodingType) { |
| 56 |
this.encodingType = encodingType; |
| 57 |
} |
| 58 |
|
| 59 |
public int getCodepage() { |
| 60 |
return codepage; |
| 61 |
} |
| 62 |
|
| 63 |
protected void setCodepage(int codepage) { |
| 64 |
this.codepage = codepage; |
| 65 |
} |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public static CodeFunctions createEncoderForLBL(String charset) { |
| 74 |
CodeFunctions funcs = new CodeFunctions(); |
| 75 |
switch (charset) { |
| 76 |
case "ascii": |
| 77 |
funcs.setEncodingType(ENCODING_FORMAT6); |
| 78 |
funcs.setEncoder(new Format6Encoder()); |
| 79 |
funcs.setDecoder(new Format6Decoder()); |
| 80 |
break; |
| 81 |
case "cp0": |
| 82 |
funcs.setEncodingType(ENCODING_FORMAT9); |
| 83 |
funcs.setEncoder(new AnyCharsetEncoder("ascii", new TableTransliterator("ascii"))); |
| 84 |
funcs.setDecoder(new AnyCharsetDecoder("ascii")); |
| 85 |
funcs.setCodepage(0); |
| 86 |
break; |
| 87 |
case "cp1252": |
| 88 |
case "latin1": |
| 89 |
funcs.setEncodingType(ENCODING_FORMAT9); |
| 90 |
funcs.setEncoder(new AnyCharsetEncoder("cp1252", new TableTransliterator("latin1"))); |
| 91 |
funcs.setDecoder(new AnyCharsetDecoder("cp1252")); |
| 92 |
funcs.setCodepage(1252); |
| 93 |
break; |
| 94 |
case "cp65001": |
| 95 |
case "unicode": |
| 96 |
funcs.setEncodingType(ENCODING_FORMAT10); |
| 97 |
funcs.setEncoder(new Utf8Encoder()); |
| 98 |
funcs.setDecoder(new Utf8Decoder()); |
| 99 |
funcs.setCodepage(65001); |
| 100 |
break; |
| 101 |
case "cp932": |
| 102 |
case "ms932": |
| 103 |
funcs.setEncodingType(ENCODING_FORMAT10); |
| 104 |
funcs.setEncoder(new AnyCharsetEncoder("ms932", new SparseTransliterator("nomacron"))); |
| 105 |
funcs.setDecoder(new AnyCharsetDecoder("ms932")); |
| 106 |
funcs.setCodepage(932); |
| 107 |
break; |
| 108 |
default: |
| 109 |
funcs.setEncodingType(ENCODING_FORMAT9); |
| 110 |
funcs.setDecoder(new AnyCharsetDecoder(charset)); |
| 111 |
funcs.setEncoder(new AnyCharsetEncoder(charset, new TableTransliterator("ascii"))); |
| 112 |
funcs.setCodepage(guessCodepage(charset)); |
| 113 |
break; |
| 114 |
} |
| 115 |
|
| 116 |
return funcs; |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public static CodeFunctions createEncoderForLBL(int format, int codePage) { |
| 128 |
CodeFunctions funcs; |
| 129 |
|
| 130 |
if (format == ENCODING_FORMAT6) { |
| 131 |
funcs = createEncoderForLBL("ascii"); |
| 132 |
} else { |
| 133 |
funcs = createEncoderForLBL("cp" + codePage); |
| 134 |
} |
| 135 |
|
| 136 |
return funcs; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
private static int guessCodepage(String charset) { |
| 145 |
String cs = charset.toLowerCase(); |
| 146 |
if (cs.startsWith("cp")) { |
| 147 |
try { |
| 148 |
return Integer.parseInt(charset.substring(2)); |
| 149 |
} catch (NumberFormatException e) { |
| 150 |
|
| 151 |
throw new ExitException("Invalid character set: " + cs); |
| 152 |
} |
| 153 |
} else if (cs.startsWith("windows-")) { |
| 154 |
try { |
| 155 |
return Integer.parseInt(charset.substring(8)); |
| 156 |
} catch (NumberFormatException e) { |
| 157 |
|
| 158 |
throw new ExitException("Invalid character set: " + cs); |
| 159 |
} |
| 160 |
} else if ("latin1".equals(cs)) { |
| 161 |
return 1252; |
| 162 |
} |
| 163 |
return 0; |
| 164 |
} |
| 165 |
|
| 166 |
public static CharacterEncoder getDefaultEncoder() { |
| 167 |
return new Format6Encoder(); |
| 168 |
} |
| 169 |
|
| 170 |
public static CharacterDecoder getDefaultDecoder() { |
| 171 |
return new Format6Decoder(); |
| 172 |
} |
| 173 |
} |