Debian Codegraph @

src/uk/me/parabola/imgfmt/app/labelenc/CodeFunctions.java

raw symbols SCIP
1 /*
2 * Copyright (C) 2007 Steve Ratcliffe
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License version 2 as
6 * published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 *
14 * Author: Steve Ratcliffe
15 * Create date: Jan 1, 2008
16 */
17 package uk.me.parabola.imgfmt.app.labelenc;
18
19 import uk.me.parabola.imgfmt.ExitException;
20
21 /**
22 * @author Steve Ratcliffe
23 */
24 public class CodeFunctions {
25 // Label encoding length
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 * Create a CharacterEncoder for the given charset option. Note that this
69 * routine also writes to the lblHeader parameter to set the encoding type.
70 * @param charset The mkgmap command line option to be interpreted.
71 * @return The various character set parameters that will be needed.
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": // This is used for ascii but with the single byte format
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 * Sets encoding functions for a given format and code page. This is used
121 * when reading from an existing file.
122 *
123 * @param format The format from the lbl header.
124 * @param codePage The codepage found in the header.
125 * @return The various character set parameters that will be needed.
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 * Guess the code page from the given charset. Only works with things
141 * like cp1252, windows-1252 and some well known ones.
142 * @param charset The charset that was given.
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 // wasn't in the right form
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 // wasn't in the right form to guess
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 }