Debian Codegraph @

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

raw symbols SCIP
1 /*
2 * Copyright (C) 2011.
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.util.Locale;
17
18 /**
19 * A sparse character-based transliterator that leaves most characters unchanged.
20 *
21 */
22 public class SparseTransliterator implements Transliterator {
23
24 private final boolean useNoMacron;
25 private boolean forceUppercase;
26
27 public SparseTransliterator(String targetCharset) {
28 useNoMacron = "nomacron".equals(targetCharset);
29 }
30
31 /**
32 * Convert a string into a string that uses only acceptable characters.
33 *
34 * @param s The original string. It can use any unicode character. Can be null in which case null will
35 * be returned.
36 * @return A string that uses only acceptable characters.
37 */
38 public String transliterate(String s) {
39 if (s == null)
40 return null;
41
42 StringBuilder sb = new StringBuilder(s.length() + 5);
43 for (char c : s.toCharArray()) {
44 if (useNoMacron) {
45 // Only macrons are modified, all other chars (including non-ascii) are left unchanged
46 if (c == 0x101) // Unicode Character 'LATIN SMALL LETTER A WITH MACRON' (U+0101)
47 c = 'a';
48 else if (c == 0x113) // Unicode Character 'LATIN SMALL LETTER E WITH MACRON' (U+0113)
49 c = 'e';
50 else if (c == 0x12b) // Unicode Character 'LATIN SMALL LETTER I WITH MACRON' (U+012B)
51 c = 'i';
52 else if (c == 0x14d) // Unicode Character 'LATIN SMALL LETTER O WITH MACRON' (U+014D)
53 c = 'o';
54 else if (c == 0x16b) // Unicode Character 'LATIN SMALL LETTER U WITH MACRON' (U+016B)
55 c = 'u';
56 }
57 sb.append(c);
58 }
59
60 String text = sb.toString();
61 if (forceUppercase)
62 text = text.toUpperCase(Locale.ENGLISH);
63 return text;
64 }
65
66 public void forceUppercase(boolean uc) {
67 forceUppercase = uc;
68 }
69 }