aboutsummaryrefslogtreecommitdiffstats
path: root/tools/java/org/unicode/cldr/util/CoreCoverageInfo.java
blob: cbeb2f4fd670c94e95ca0cf6b86194e589a58eb0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
package org.unicode.cldr.util;

import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumMap;
import java.util.EnumSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;

import org.unicode.cldr.draft.ScriptMetadata;
import org.unicode.cldr.draft.ScriptMetadata.Info;
import org.unicode.cldr.draft.ScriptMetadata.Trinary;
import org.unicode.cldr.tool.LikelySubtags;
import org.unicode.cldr.util.SupplementalDataInfo.PluralType;

import com.ibm.icu.impl.Relation;
import com.ibm.icu.lang.UCharacter;
import com.ibm.icu.lang.UCharacterDirection;
import com.ibm.icu.lang.UScript;
import com.ibm.icu.text.UnicodeSet;
import com.ibm.icu.text.UnicodeSetIterator;

public class CoreCoverageInfo {

    private static final CLDRConfig config = CLDRConfig.getInstance();
    private static final String CLDR_BASE_DIRECTORY = config.getCldrBaseDirectory().toString();
    private static final SupplementalDataInfo sdi = SupplementalDataInfo.getInstance();
    private static final LikelySubtags ls = new LikelySubtags(sdi);

    public enum CoreItems {
        main_exemplar, auxiliary_exemplar, index_exemplar, orientation, plurals, ordinals, default_content, likely_subtags, country_data, romanization, casing, collation;
        public static Set<CoreItems> ONLY_RECOMMENDED = Collections.unmodifiableSet(
            EnumSet.of(romanization, ordinals));
        public static final int COUNT = CoreItems.values().length;
    }

    public static Set<CoreItems> getCoreCoverageInfo(CLDRFile file, Set<String> detailedErrors) {
        if (file.isResolved()) {
            file = file.getUnresolved();
        }
        String locale = file.getLocaleID();
        LanguageTagParser ltp = new LanguageTagParser();
        locale = ltp.set(locale).getLanguageScript();
        String baseLanguage = ltp.getLanguage();
        String script = ltp.getScript();

        Set<CoreItems> result = EnumSet.noneOf(CoreItems.class);

        //      (04) Exemplar sets: main, auxiliary, index, punctuation. [main/xxx.xml]
        //      These must reflect the Unicode model. For more information, see tr35-general.html#Character_Elements.
        boolean isRtl = false;
        for (CoreItems exemplar : EXEMPLARS) {
            String type = exemplar.toString();
            type = type.substring(0, type.indexOf('_'));

            String path = "//ldml/characters/exemplarCharacters";
            boolean isMain = type.equals("main");
            if (!isMain) {
                path += "[@type=\"" + type + "\"]";
            }
            String value = file.getStringValue(path);
            if (value != null) {
                String sourceLocale = file.getSourceLocaleID(path, null);
                if (locale.equals(sourceLocale)) {
                    result.add(exemplar);
                }
            }
            if (isMain && result.contains(exemplar)) {
                UnicodeSet main = new UnicodeSet(value);
                isRtl = isRtl(main);
            }
        }
        //      (02) Orientation (bidi writing systems only) [main/xxx.xml]
        String path = "//ldml/layout/orientation/characterOrder";
        String value = file.getStringValue(path);
        if ("right-to-left".equals(value) == isRtl) {
            result.add(CoreItems.orientation);
        }

        //      (01) Plural rules [supplemental/plurals.xml and ordinals.xml]
        //      For more information, see cldr-spec/plural-rules.
        if (sdi.getPluralLocales(PluralType.cardinal).contains(baseLanguage)) {
            result.add(CoreItems.plurals);
        }
        if (sdi.getPluralLocales(PluralType.ordinal).contains(baseLanguage)) {
            result.add(CoreItems.ordinals);
        }

        //      (01) Default content script and region (normally: normally country with largest population using that language, and normal script for that).  [supplemental/supplementalMetadata.xml]

        String defaultContent = sdi.getDefaultContentLocale(locale);
        if (defaultContent != null) {
            result.add(CoreItems.default_content);
        }
        // likely subtags
        String max = ls.maximize(locale);
        String maxLangScript = null;
        if (max != null) {
            ltp.set(max);
            maxLangScript = ltp.getLanguageScript();
            script = ltp.getScript();
            if (!script.isEmpty() && !ltp.getRegion().isEmpty()) {
                result.add(CoreItems.likely_subtags);
            }
        }
        //      (N) Verify the country data ( i.e. which territories in which the language is spoken enough to create a locale ) [supplemental/supplementalData.xml]
        // we verify that there is at least one region
        // we try 3 cases: language, locale, maxLangScript
        Set<String> territories = sdi.getTerritoriesForPopulationData(locale);
        if (territories == null) {
            territories = sdi.getTerritoriesForPopulationData(baseLanguage);
        }
        if (territories == null && maxLangScript != null) {
            territories = sdi.getTerritoriesForPopulationData(maxLangScript);
        }
        if (territories != null && territories.size() != 0) {
            result.add(CoreItems.country_data);
        } else {
            sdi.getTerritoriesForPopulationData(locale); // for debugging
        }
        //      *(N) Romanization table (non-Latin writing systems only) [spreadsheet, we'll translate into transforms/xxx-en.xml]
        //      If a spreadsheet, for each letter (or sequence) in the exemplars, what is the corresponding Latin letter (or sequence).
        //      More sophisticated users can do a better job, supplying a file of rules like transforms/Arabic-Latin-BGN.xml.

        if (script.equals("Latn")) {
            result.add(CoreItems.romanization);
        } else {
            boolean found = false;
            Set<String> scriptNames = getScriptNames(script);
            Set<String> tempErrors = new HashSet<>();
            for (String scriptName : scriptNames) {
                for (String[] pair : ROMANIZATION_PATHS) {
                    String filename = pair[0] + scriptName + pair[1];
                    if (hasFile(SpecialDir.transforms, filename)) {
                        result.add(CoreItems.romanization);
                        found = true;
                        break;
                    } else {
                        tempErrors.add("No romanization for " + script + " => " + scriptName);
                    }
                }
            }
            if (!found) {
                detailedErrors.addAll(tempErrors);
            }
        }

        //      (N) Casing information (cased scripts only, according to ScriptMetadata.txt)
        //      This will be in common/casing
        Info scriptData = ScriptMetadata.getInfo(script);
        if (scriptData.hasCase == Trinary.YES) {
            if (hasFile(SpecialDir.casing, baseLanguage)) {
                result.add(CoreItems.casing);
            }
        } else {
            result.add(CoreItems.casing);
        }
        //      (N) Collation rules [non-Survey Tool]
        //      For details, see cldr-spec/collation-guidelines.
        //      The result will be a file like: common/collation/ar.xml or common/collation/da.xml.
        //      Note that the "search" collators (which tend to be large) are not needed initially.

        // check for file cldr/collation/<language>.xml
        if (hasFile(SpecialDir.collation, baseLanguage)) {
            result.add(CoreItems.collation);
        }
        return Collections.unmodifiableSet(result);
    }

    private static final Set<CoreItems> EXEMPLARS = EnumSet.of(
        CoreItems.main_exemplar,
        CoreItems.auxiliary_exemplar,
        CoreItems.index_exemplar);

    private static final String[][] ROMANIZATION_PATHS = {
        { "", "-Latin" },
        { "", "-Latin-BGN" },
        { "Latin-", "" },
    };

    private static final Relation SCRIPT_NAMES = Relation.of(new HashMap(), HashSet.class);
    static {
        SCRIPT_NAMES.putAll("Arab", Arrays.asList("Arabic", "Arab"));
        SCRIPT_NAMES.putAll("Jpan", Arrays.asList("Jpan", "Han"));
        SCRIPT_NAMES.putAll("Hant", Arrays.asList("Hant", "Han"));
        SCRIPT_NAMES.putAll("Hans", Arrays.asList("Hans", "Han"));
        SCRIPT_NAMES.putAll("Kore", Arrays.asList("Hang", "Hangul"));
        SCRIPT_NAMES.freeze();
    }

    private static Set<String> getScriptNames(String script) {
        Set<String> result = SCRIPT_NAMES.get(script);
        if (result != null) {
            return result;
        }
        result = new HashSet();
        String name = UScript.getName(UScript.getCodeFromName(script));
        result.add(name);
        result.add(script);
        return result;
    }

    private enum SpecialDir {
        transforms, collation, casing
    }

    private static final Relation<SpecialDir, String> SPECIAL_FILES = Relation.of(new EnumMap(SpecialDir.class), HashSet.class);
    static {
        for (SpecialDir dir : SpecialDir.values()) {
            File realDir = new File(CLDR_BASE_DIRECTORY + "/common/" + dir);
            for (String s : realDir.list()) {
                if (s.endsWith(".xml")) {
                    s = s.substring(0, s.length() - 4);
                }
                SPECIAL_FILES.put(dir, s);
            }
        }
    }

    private static boolean hasFile(SpecialDir type, String filename) {
        return SPECIAL_FILES.get(type).contains(filename);
    }

    public static boolean isRtl(UnicodeSet main) {
        for (UnicodeSetIterator it = new UnicodeSetIterator(main); it.nextRange();) {
            for (int i = it.codepoint; i <= it.codepointEnd; ++i) {
                int bidiClass = UCharacter.getDirection(i);
                switch (bidiClass) {
                case UCharacterDirection.RIGHT_TO_LEFT:
                case UCharacterDirection.RIGHT_TO_LEFT_ARABIC:
                    return true;
                case UCharacterDirection.LEFT_TO_RIGHT:
                    return false;
                }
            }
        }
        return false;
    }

}