Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright (C) 2010.
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 3 or
6 * version 2 as published by the Free Software Foundation.
7 *
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 */
13
14 package uk.me.parabola.imgfmt.app.labelenc;
15
16 import java.io.BufferedReader;
17 import java.io.IOException;
18 import java.io.InputStream;
19 import java.io.InputStreamReader;
20 import java.nio.charset.StandardCharsets;
21 import java.util.Arrays;
22 import java.util.Locale;
23
24 import uk.me.parabola.log.Logger;
25
26 /**
27 * A simple transliterator that transliterates character by character based
28 * on pre-prepared tables. It is not context sensitive - the same input character
29 * always produces the same output character(s), so the results are
30 * not very good for languages where that is important.
31 *
32 * Tables are only read when needed, so for a typical map only a small
33 * number of files will actually be read.
34 */
35 public class TableTransliterator implements Transliterator {
36 private static final Logger log = Logger.getLogger(TableTransliterator.class);
37
38 private final String[][] rows = new String[256][];
39 private final boolean useLatin;
40 private boolean forceUppercase;
41
42 public TableTransliterator(String targetCharset) {
43 if ("latin1".equals(targetCharset) || "cp1252".equals(targetCharset))
44 useLatin = true;
45 else
46 useLatin = false;
47 }
48
49 /**
50 * Convert a string into a string that uses only ascii characters.
51 *
52 * @param s The original string. It can use any unicode character. Can be null in which case null will
53 * be returned.
54 * @return A string that uses only ascii characters that is a transcription or
55 * transliteration of the original string.
56 */
57 public String transliterate(String s) {
58 if (s == null)
59 return null;
60
61 StringBuilder sb = new StringBuilder(s.length() + 5);
62 for (char c : s.toCharArray()) {
63 if (c <= (useLatin? 0xff: 0x7f)) {
64 sb.append(c);
65 } else {
66 int row = c >>> 8;
67
68 String[] rowmap = rows[row];
69 if (rowmap == null)
70 rowmap = loadRow(row);
71 sb.append(rowmap[c & 0xff]);
72 }
73 }
74
75 String text = sb.toString();
76 if (forceUppercase)
77 text = text.toUpperCase(Locale.ENGLISH);
78 return text;
79 }
80
81 public void forceUppercase(boolean uc) {
82 forceUppercase = uc;
83 }
84
85 /**
86 * Load one row of characters. This means unicode characters that are of the
87 * form U+RRXX where RR is the row.
88 *
89 * @param row Row number 0-255.
90 * @return An array of strings, one for each character in the row. If there is
91 * no ascii representation then a '?' character will fill that
92 * position.
93 */
94 private String[] loadRow(int row) {
95 if (rows[row] != null)
96 return rows[row];
97
98 String[] newRow = new String[256];
99 rows[row] = newRow;
100
101 // Default all to a question mark
102 Arrays.fill(newRow, "?");
103
104 // If we are doing latin1, see if there is a specific file for latin
105 // characters first.
106 if (useLatin) {
107 String name = String.format("/chars/latin1/row%02x.trans", row);
108 readCharFile(name, newRow);
109 }
110
111 // Fill in any remaining characters from the ascii mappings.
112 String name = String.format("/chars/ascii/row%02x.trans", row);
113 readCharFile(name, newRow);
114
115 return newRow;
116 }
117
118 private void readCharFile(String name, String[] newRow) {
119 try (InputStream is = getClass().getResourceAsStream(name)) {
120 if (is == null)
121 return;
122 BufferedReader br = new BufferedReader(new InputStreamReader(is, StandardCharsets.UTF_8));
123
124 String line;
125 while ((line = br.readLine()) != null) {
126 line = line.trim();
127 if (line.isEmpty() || line.charAt(0) == '#')
128 continue;
129
130 String[] fields = line.split("\\s+");
131 if (fields.length < 2)
132 continue;
133
134 String upoint = fields[0];
135 String translation = fields[1];
136
137 if ("?".equals(translation)) continue;
138 if (upoint.length() != 6 || upoint.charAt(0) != 'U') continue;
139
140 // The first field must look like 'U+RRXX', we extract the XX part
141 int index = Integer.parseInt(upoint.substring(4), 16);
142 if ("?".equals(newRow[index])) {
143 if (forceUppercase)
144 newRow[index] = translation.toUpperCase(Locale.ENGLISH);
145 else
146 newRow[index] = translation;
147 }
148 }
149 } catch (IOException e) {
150 log.error("Could not read character translation table");
151 }
152 }
153
154 }