9224
|
1 |
|
6501
|
2 |
/*
|
9224
|
3 |
* Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
|
6501
|
4 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
5 |
*
|
|
6 |
* This code is free software; you can redistribute it and/or modify it
|
|
7 |
* under the terms of the GNU General Public License version 2 only, as
|
|
8 |
* published by the Free Software Foundation. Oracle designates this
|
|
9 |
* particular file as subject to the "Classpath" exception as provided
|
|
10 |
* by Oracle in the LICENSE file that accompanied this code.
|
|
11 |
*
|
|
12 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
13 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
14 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
15 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
16 |
* accompanied this code).
|
|
17 |
*
|
|
18 |
* You should have received a copy of the GNU General Public License version
|
|
19 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
20 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
21 |
*
|
|
22 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
23 |
* or visit www.oracle.com if you need additional information or have any
|
|
24 |
* questions.
|
|
25 |
*/
|
|
26 |
|
|
27 |
/*
|
|
28 |
*******************************************************************************
|
|
29 |
* Copyright (C) 2009-2010, International Business Machines Corporation and *
|
|
30 |
* others. All Rights Reserved. *
|
|
31 |
*******************************************************************************
|
|
32 |
*/
|
|
33 |
package sun.util.locale;
|
|
34 |
|
|
35 |
import java.util.Collections;
|
9224
|
36 |
import java.util.Map;
|
6501
|
37 |
import java.util.Map.Entry;
|
|
38 |
import java.util.Set;
|
|
39 |
import java.util.SortedMap;
|
|
40 |
import java.util.SortedSet;
|
|
41 |
|
|
42 |
public class UnicodeLocaleExtension extends Extension {
|
|
43 |
public static final char SINGLETON = 'u';
|
|
44 |
|
9224
|
45 |
private final Set<String> attributes;
|
|
46 |
private final Map<String, String> keywords;
|
6501
|
47 |
|
9224
|
48 |
public static final UnicodeLocaleExtension CA_JAPANESE
|
|
49 |
= new UnicodeLocaleExtension("ca", "japanese");
|
|
50 |
public static final UnicodeLocaleExtension NU_THAI
|
|
51 |
= new UnicodeLocaleExtension("nu", "thai");
|
6501
|
52 |
|
9224
|
53 |
private UnicodeLocaleExtension(String key, String value) {
|
|
54 |
super(SINGLETON, key + "-" + value);
|
|
55 |
attributes = Collections.emptySet();
|
|
56 |
keywords = Collections.singletonMap(key, value);
|
6501
|
57 |
}
|
|
58 |
|
|
59 |
UnicodeLocaleExtension(SortedSet<String> attributes, SortedMap<String, String> keywords) {
|
9224
|
60 |
super(SINGLETON);
|
|
61 |
if (attributes != null) {
|
|
62 |
this.attributes = attributes;
|
|
63 |
} else {
|
|
64 |
this.attributes = Collections.emptySet();
|
6501
|
65 |
}
|
9224
|
66 |
if (keywords != null) {
|
|
67 |
this.keywords = keywords;
|
|
68 |
} else {
|
|
69 |
this.keywords = Collections.emptyMap();
|
6501
|
70 |
}
|
|
71 |
|
9224
|
72 |
if (!this.attributes.isEmpty() || !this.keywords.isEmpty()) {
|
6501
|
73 |
StringBuilder sb = new StringBuilder();
|
9224
|
74 |
for (String attribute : this.attributes) {
|
6501
|
75 |
sb.append(LanguageTag.SEP).append(attribute);
|
|
76 |
}
|
9224
|
77 |
for (Entry<String, String> keyword : this.keywords.entrySet()) {
|
6501
|
78 |
String key = keyword.getKey();
|
|
79 |
String value = keyword.getValue();
|
|
80 |
|
|
81 |
sb.append(LanguageTag.SEP).append(key);
|
|
82 |
if (value.length() > 0) {
|
|
83 |
sb.append(LanguageTag.SEP).append(value);
|
|
84 |
}
|
|
85 |
}
|
9224
|
86 |
setValue(sb.substring(1)); // skip leading '-'
|
6501
|
87 |
}
|
|
88 |
}
|
|
89 |
|
|
90 |
public Set<String> getUnicodeLocaleAttributes() {
|
9224
|
91 |
if (attributes == Collections.EMPTY_SET) {
|
|
92 |
return attributes;
|
|
93 |
}
|
|
94 |
return Collections.unmodifiableSet(attributes);
|
6501
|
95 |
}
|
|
96 |
|
|
97 |
public Set<String> getUnicodeLocaleKeys() {
|
9224
|
98 |
if (keywords == Collections.EMPTY_MAP) {
|
|
99 |
return Collections.emptySet();
|
|
100 |
}
|
|
101 |
return Collections.unmodifiableSet(keywords.keySet());
|
6501
|
102 |
}
|
|
103 |
|
|
104 |
public String getUnicodeLocaleType(String unicodeLocaleKey) {
|
9224
|
105 |
return keywords.get(unicodeLocaleKey);
|
6501
|
106 |
}
|
|
107 |
|
|
108 |
public static boolean isSingletonChar(char c) {
|
9224
|
109 |
return (SINGLETON == LocaleUtils.toLower(c));
|
6501
|
110 |
}
|
|
111 |
|
|
112 |
public static boolean isAttribute(String s) {
|
|
113 |
// 3*8alphanum
|
9224
|
114 |
int len = s.length();
|
|
115 |
return (len >= 3) && (len <= 8) && LocaleUtils.isAlphaNumericString(s);
|
6501
|
116 |
}
|
|
117 |
|
|
118 |
public static boolean isKey(String s) {
|
|
119 |
// 2alphanum
|
9224
|
120 |
return (s.length() == 2) && LocaleUtils.isAlphaNumericString(s);
|
6501
|
121 |
}
|
|
122 |
|
|
123 |
public static boolean isTypeSubtag(String s) {
|
|
124 |
// 3*8alphanum
|
9224
|
125 |
int len = s.length();
|
|
126 |
return (len >= 3) && (len <= 8) && LocaleUtils.isAlphaNumericString(s);
|
6501
|
127 |
}
|
|
128 |
}
|