jdk/src/java.base/share/classes/sun/util/locale/LocaleMatcher.java
author chegar
Sun, 17 Aug 2014 15:54:13 +0100
changeset 25859 3317bb8137f4
parent 14009 jdk/src/share/classes/sun/util/locale/LocaleMatcher.java@21856a20cc1d
child 38950 89fe9dae591e
permissions -rw-r--r--
8054834: Modular Source Code Reviewed-by: alanb, chegar, ihse, mduigou Contributed-by: alan.bateman@oracle.com, alex.buckley@oracle.com, chris.hegarty@oracle.com, erik.joelsson@oracle.com, jonathan.gibbons@oracle.com, karen.kinnear@oracle.com, magnus.ihse.bursie@oracle.com, mandy.chung@oracle.com, mark.reinhold@oracle.com, paul.sandoz@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
14009
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
     1
/*
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
     2
 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
     4
 *
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    10
 *
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    15
 * accompanied this code).
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    16
 *
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    20
 *
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    23
 * questions.
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    24
 */
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    25
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    26
package sun.util.locale;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    27
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    28
import java.util.ArrayList;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    29
import java.util.Collection;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    30
import java.util.HashMap;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    31
import java.util.Iterator;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    32
import java.util.LinkedHashMap;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    33
import java.util.LinkedList;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    34
import java.util.List;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    35
import java.util.Locale;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    36
import java.util.Locale.*;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    37
import static java.util.Locale.FilteringMode.*;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    38
import static java.util.Locale.LanguageRange.*;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    39
import java.util.Map;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    40
import java.util.Set;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    41
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    42
/**
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    43
 * Implementation for BCP47 Locale matching
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    44
 *
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    45
 */
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    46
public final class LocaleMatcher {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    47
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    48
    public static List<Locale> filter(List<LanguageRange> priorityList,
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    49
                                      Collection<Locale> locales,
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    50
                                      FilteringMode mode) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    51
        if (priorityList.isEmpty() || locales.isEmpty()) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    52
            return new ArrayList<>(); // need to return a empty mutable List
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    53
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    54
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    55
        // Create a list of language tags to be matched.
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    56
        List<String> tags = new ArrayList<>();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    57
        for (Locale locale : locales) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    58
            tags.add(locale.toLanguageTag());
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    59
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    60
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    61
        // Filter language tags.
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    62
        List<String> filteredTags = filterTags(priorityList, tags, mode);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    63
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    64
        // Create a list of matching locales.
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    65
        List<Locale> filteredLocales = new ArrayList<>(filteredTags.size());
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    66
        for (String tag : filteredTags) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    67
              filteredLocales.add(Locale.forLanguageTag(tag));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    68
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    69
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    70
        return filteredLocales;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    71
    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    72
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    73
    public static List<String> filterTags(List<LanguageRange> priorityList,
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    74
                                          Collection<String> tags,
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    75
                                          FilteringMode mode) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    76
        if (priorityList.isEmpty() || tags.isEmpty()) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    77
            return new ArrayList<>(); // need to return a empty mutable List
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    78
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    79
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    80
        ArrayList<LanguageRange> list;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    81
        if (mode == EXTENDED_FILTERING) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    82
            return filterExtended(priorityList, tags);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    83
        } else {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    84
            list = new ArrayList<>();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    85
            for (LanguageRange lr : priorityList) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    86
                String range = lr.getRange();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    87
                if (range.startsWith("*-")
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    88
                    || range.indexOf("-*") != -1) { // Extended range
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    89
                    if (mode == AUTOSELECT_FILTERING) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    90
                        return filterExtended(priorityList, tags);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    91
                    } else if (mode == MAP_EXTENDED_RANGES) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    92
                        if (range.charAt(0) == '*') {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    93
                            range = "*";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    94
                        } else {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    95
                            range = range.replaceAll("-[*]", "");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    96
                        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    97
                        list.add(new LanguageRange(range, lr.getWeight()));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    98
                    } else if (mode == REJECT_EXTENDED_RANGES) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
    99
                        throw new IllegalArgumentException("An extended range \""
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   100
                                      + range
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   101
                                      + "\" found in REJECT_EXTENDED_RANGES mode.");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   102
                    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   103
                } else { // Basic range
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   104
                    list.add(lr);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   105
                }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   106
            }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   107
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   108
            return filterBasic(list, tags);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   109
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   110
    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   111
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   112
    private static List<String> filterBasic(List<LanguageRange> priorityList,
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   113
                                            Collection<String> tags) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   114
        List<String> list = new ArrayList<>();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   115
        for (LanguageRange lr : priorityList) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   116
            String range = lr.getRange();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   117
            if (range.equals("*")) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   118
                return new ArrayList<String>(tags);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   119
            } else {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   120
                for (String tag : tags) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   121
                    tag = tag.toLowerCase();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   122
                    if (tag.startsWith(range)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   123
                        int len = range.length();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   124
                        if ((tag.length() == len || tag.charAt(len) == '-')
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   125
                            && !list.contains(tag)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   126
                            list.add(tag);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   127
                        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   128
                    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   129
                }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   130
            }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   131
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   132
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   133
        return list;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   134
    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   135
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   136
    private static List<String> filterExtended(List<LanguageRange> priorityList,
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   137
                                               Collection<String> tags) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   138
        List<String> list = new ArrayList<>();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   139
        for (LanguageRange lr : priorityList) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   140
            String range = lr.getRange();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   141
            if (range.equals("*")) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   142
                return new ArrayList<String>(tags);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   143
            }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   144
            String[] rangeSubtags = range.split("-");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   145
            for (String tag : tags) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   146
                tag = tag.toLowerCase();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   147
                String[] tagSubtags = tag.split("-");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   148
                if (!rangeSubtags[0].equals(tagSubtags[0])
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   149
                    && !rangeSubtags[0].equals("*")) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   150
                    continue;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   151
                }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   152
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   153
                int rangeIndex = 1;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   154
                int tagIndex = 1;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   155
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   156
                while (rangeIndex < rangeSubtags.length
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   157
                       && tagIndex < tagSubtags.length) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   158
                   if (rangeSubtags[rangeIndex].equals("*")) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   159
                       rangeIndex++;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   160
                   } else if (rangeSubtags[rangeIndex].equals(tagSubtags[tagIndex])) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   161
                       rangeIndex++;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   162
                       tagIndex++;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   163
                   } else if (tagSubtags[tagIndex].length() == 1
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   164
                              && !tagSubtags[tagIndex].equals("*")) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   165
                       break;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   166
                   } else {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   167
                       tagIndex++;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   168
                   }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   169
               }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   170
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   171
               if (rangeSubtags.length == rangeIndex && !list.contains(tag)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   172
                   list.add(tag);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   173
               }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   174
            }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   175
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   176
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   177
        return list;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   178
    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   179
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   180
    public static Locale lookup(List<LanguageRange> priorityList,
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   181
                                Collection<Locale> locales) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   182
        if (priorityList.isEmpty() || locales.isEmpty()) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   183
            return null;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   184
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   185
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   186
        // Create a list of language tags to be matched.
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   187
        List<String> tags = new ArrayList<>();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   188
        for (Locale locale : locales) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   189
            tags.add(locale.toLanguageTag());
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   190
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   191
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   192
        // Look up a language tags.
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   193
        String lookedUpTag = lookupTag(priorityList, tags);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   194
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   195
        if (lookedUpTag == null) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   196
            return null;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   197
        } else {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   198
            return Locale.forLanguageTag(lookedUpTag);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   199
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   200
    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   201
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   202
    public static String lookupTag(List<LanguageRange> priorityList,
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   203
                                   Collection<String> tags) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   204
        if (priorityList.isEmpty() || tags.isEmpty()) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   205
            return null;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   206
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   207
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   208
        for (LanguageRange lr : priorityList) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   209
            String range = lr.getRange();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   210
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   211
            // Special language range ("*") is ignored in lookup.
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   212
            if (range.equals("*")) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   213
                continue;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   214
            }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   215
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   216
            String rangeForRegex = range.replaceAll("\\x2A", "\\\\p{Alnum}*");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   217
            while (rangeForRegex.length() > 0) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   218
                for (String tag : tags) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   219
                    tag = tag.toLowerCase();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   220
                    if (tag.matches(rangeForRegex)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   221
                        return tag;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   222
                    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   223
                }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   224
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   225
                // Truncate from the end....
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   226
                int index = rangeForRegex.lastIndexOf('-');
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   227
                if (index >= 0) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   228
                    rangeForRegex = rangeForRegex.substring(0, index);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   229
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   230
                    // if range ends with an extension key, truncate it.
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   231
                    if (rangeForRegex.lastIndexOf('-') == rangeForRegex.length()-2) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   232
                        rangeForRegex =
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   233
                            rangeForRegex.substring(0, rangeForRegex.length()-2);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   234
                    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   235
                } else {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   236
                    rangeForRegex = "";
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   237
                }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   238
            }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   239
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   240
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   241
        return null;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   242
    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   243
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   244
    public static List<LanguageRange> parse(String ranges) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   245
        ranges = ranges.replaceAll(" ", "").toLowerCase();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   246
        if (ranges.startsWith("accept-language:")) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   247
            ranges = ranges.substring(16); // delete unnecessary prefix
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   248
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   249
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   250
        String[] langRanges = ranges.split(",");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   251
        List<LanguageRange> list = new ArrayList<>(langRanges.length);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   252
        List<String> tempList = new ArrayList<>();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   253
        int numOfRanges = 0;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   254
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   255
        for (String range : langRanges) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   256
            int index;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   257
            String r;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   258
            double w;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   259
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   260
            if ((index = range.indexOf(";q=")) == -1) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   261
                r = range;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   262
                w = MAX_WEIGHT;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   263
            } else {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   264
                r = range.substring(0, index);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   265
                index += 3;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   266
                try {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   267
                    w = Double.parseDouble(range.substring(index));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   268
                }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   269
                catch (Exception e) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   270
                    throw new IllegalArgumentException("weight=\""
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   271
                                  + range.substring(index)
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   272
                                  + "\" for language range \"" + r + "\"");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   273
                }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   274
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   275
                if (w < MIN_WEIGHT || w > MAX_WEIGHT) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   276
                    throw new IllegalArgumentException("weight=" + w
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   277
                                  + " for language range \"" + r
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   278
                                  + "\". It must be between " + MIN_WEIGHT
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   279
                                  + " and " + MAX_WEIGHT + ".");
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   280
                }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   281
            }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   282
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   283
            if (!tempList.contains(r)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   284
                LanguageRange lr = new LanguageRange(r, w);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   285
                index = numOfRanges;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   286
                for (int j = 0; j < numOfRanges; j++) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   287
                    if (list.get(j).getWeight() < w) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   288
                        index = j;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   289
                        break;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   290
                    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   291
                }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   292
                list.add(index, lr);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   293
                numOfRanges++;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   294
                tempList.add(r);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   295
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   296
                // Check if the range has an equivalent using IANA LSR data.
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   297
                // If yes, add it to the User's Language Priority List as well.
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   298
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   299
                // aa-XX -> aa-YY
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   300
                String equivalent;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   301
                if ((equivalent = getEquivalentForRegionAndVariant(r)) != null
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   302
                    && !tempList.contains(equivalent)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   303
                    list.add(index+1, new LanguageRange(equivalent, w));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   304
                    numOfRanges++;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   305
                    tempList.add(equivalent);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   306
                }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   307
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   308
                String[] equivalents;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   309
                if ((equivalents = getEquivalentsForLanguage(r)) != null) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   310
                    for (String equiv: equivalents) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   311
                        // aa-XX -> bb-XX(, cc-XX)
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   312
                        if (!tempList.contains(equiv)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   313
                            list.add(index+1, new LanguageRange(equiv, w));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   314
                            numOfRanges++;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   315
                            tempList.add(equiv);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   316
                        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   317
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   318
                        // bb-XX -> bb-YY(, cc-YY)
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   319
                        equivalent = getEquivalentForRegionAndVariant(equiv);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   320
                        if (equivalent != null
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   321
                            && !tempList.contains(equivalent)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   322
                            list.add(index+1, new LanguageRange(equivalent, w));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   323
                            numOfRanges++;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   324
                            tempList.add(equivalent);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   325
                        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   326
                    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   327
                }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   328
            }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   329
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   330
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   331
        return list;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   332
    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   333
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   334
    private static String[] getEquivalentsForLanguage(String range) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   335
        String r = range;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   336
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   337
        while (r.length() > 0) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   338
            if (LocaleEquivalentMaps.singleEquivMap.containsKey(r)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   339
                String equiv = LocaleEquivalentMaps.singleEquivMap.get(r);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   340
                // Return immediately for performance if the first matching
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   341
                // subtag is found.
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   342
                return new String[] {range.replaceFirst(r, equiv)};
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   343
            } else if (LocaleEquivalentMaps.multiEquivsMap.containsKey(r)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   344
                String[] equivs = LocaleEquivalentMaps.multiEquivsMap.get(r);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   345
                for (int i = 0; i < equivs.length; i++) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   346
                    equivs[i] = range.replaceFirst(r, equivs[i]);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   347
                }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   348
                return equivs;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   349
            }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   350
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   351
            // Truncate the last subtag simply.
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   352
            int index = r.lastIndexOf('-');
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   353
            if (index == -1) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   354
                break;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   355
            }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   356
            r = r.substring(0, index);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   357
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   358
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   359
        return null;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   360
    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   361
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   362
    private static String getEquivalentForRegionAndVariant(String range) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   363
        int extensionKeyIndex = getExtentionKeyIndex(range);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   364
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   365
        for (String subtag : LocaleEquivalentMaps.regionVariantEquivMap.keySet()) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   366
            int index;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   367
            if ((index = range.indexOf(subtag)) != -1) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   368
                // Check if the matching text is a valid region or variant.
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   369
                if (extensionKeyIndex != Integer.MIN_VALUE
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   370
                    && index > extensionKeyIndex) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   371
                    continue;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   372
                }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   373
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   374
                int len = index + subtag.length();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   375
                if (range.length() == len || range.charAt(len) == '-') {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   376
                    return range.replaceFirst(subtag, LocaleEquivalentMaps.regionVariantEquivMap.get(subtag));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   377
                }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   378
            }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   379
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   380
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   381
        return null;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   382
    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   383
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   384
    private static int getExtentionKeyIndex(String s) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   385
        char[] c = s.toCharArray();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   386
        int index = Integer.MIN_VALUE;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   387
        for (int i = 1; i < c.length; i++) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   388
            if (c[i] == '-') {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   389
                if (i - index == 2) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   390
                    return index;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   391
                } else {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   392
                    index = i;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   393
                }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   394
            }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   395
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   396
        return Integer.MIN_VALUE;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   397
    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   398
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   399
    public static List<LanguageRange> mapEquivalents(
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   400
                                          List<LanguageRange>priorityList,
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   401
                                          Map<String, List<String>> map) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   402
        if (priorityList.isEmpty()) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   403
            return new ArrayList<>(); // need to return a empty mutable List
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   404
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   405
        if (map == null || map.isEmpty()) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   406
            return new ArrayList<LanguageRange>(priorityList);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   407
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   408
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   409
        // Create a map, key=originalKey.toLowerCaes(), value=originalKey
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   410
        Map<String, String> keyMap = new HashMap<>();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   411
        for (String key : map.keySet()) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   412
            keyMap.put(key.toLowerCase(), key);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   413
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   414
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   415
        List<LanguageRange> list = new ArrayList<>();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   416
        for (LanguageRange lr : priorityList) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   417
            String range = lr.getRange();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   418
            String r = range;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   419
            boolean hasEquivalent = false;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   420
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   421
            while (r.length() > 0) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   422
                if (keyMap.containsKey(r)) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   423
                    hasEquivalent = true;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   424
                    List<String> equivalents = map.get(keyMap.get(r));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   425
                    if (equivalents != null) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   426
                        int len = r.length();
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   427
                        for (String equivalent : equivalents) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   428
                            list.add(new LanguageRange(equivalent.toLowerCase()
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   429
                                     + range.substring(len),
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   430
                                     lr.getWeight()));
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   431
                        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   432
                    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   433
                    // Return immediately if the first matching subtag is found.
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   434
                    break;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   435
                }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   436
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   437
                // Truncate the last subtag simply.
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   438
                int index = r.lastIndexOf('-');
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   439
                if (index == -1) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   440
                    break;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   441
                }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   442
                r = r.substring(0, index);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   443
            }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   444
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   445
            if (!hasEquivalent) {
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   446
                list.add(lr);
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   447
            }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   448
        }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   449
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   450
        return list;
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   451
    }
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   452
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   453
    private LocaleMatcher() {}
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   454
21856a20cc1d 7069824: Support for BCP47 locale matching
peytoia
parents:
diff changeset
   455
}